How to disable dropping a specific item (need help as quick as possible)

Discussion in 'Plugin Development' started by alexlyn1, May 9, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    alexlyn1

    Okay, so i have my plugin where i need to make people thats in a arraylist not able to drop an item IF it has the displayname i want (sorry for bad explain and bad english)

    EX.

    I do /test i get added to the arraylist "test" and i get a diamond sword with the display name "§1Special Sword"

    Then under an DropItemEvent i want it to do

    if(test.contains(p.getName()) {
    DISABLE THE PLAYER FROM DROPPING THE DIAMOND SWORD IF ITS NAMED "§3Test Sword"
    }

    If you could help me with this it would be appreciated =)

    NOTE: What i want to get out of this is so the items that the player get from typing /test hes not able to drop. Also while im already doing a thread Would it work to remove an player from an arraylist and clearing hes inventory on QuitEvent? Cause i dont want people to just logout and login and then be removed of the arraylist, still having their items from the command.

    Code so far:

    Code:java
    1. package me.alex.test;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerDropItemEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Main extends JavaPlugin implements Listener {
    20.  
    21. public ArrayList<String> test = new ArrayList<String>();
    22.  
    23. public void onEnable() {
    24. Bukkit.getServer().getLogger().info("Test Plugin Enabled!");
    25. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    26. }
    27.  
    28. public void onDisable() {
    29. Bukkit.getServer().getLogger().info("Test Plugin Disabled!");
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    33.  
    34. Player p = (Player) sender;
    35. PlayerInventory pi = p.getInventory();
    36.  
    37. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
    38. ItemMeta swordmeta = sword.getItemMeta();
    39. ArrayList<String> sa = new ArrayList<String>();
    40. swordmeta.setDisplayName("§3Test Sword");
    41. sa.add(ChatColor.DARK_PURPLE + "Property of " + ChatColor.DARK_PURPLE + p.getName());
    42. swordmeta.setLore(sa);
    43. sword.setItemMeta(swordmeta);
    44.  
    45. if(cmd.getName().equalsIgnoreCase("test")) {
    46. if(!test.contains(p.getName())) {
    47. test.add(p.getName());
    48. p.sendMessage("You were added to the ArrayList test!");
    49. pi.addItem(sword);
    50. return true;
    51. }
    52. if(test.contains(p.getName())) {
    53. test.remove(p.getName());
    54. pi.clear();
    55. p.sendMessage("You were removed from the ArrayList test!");
    56. return true;
    57. }
    58. }
    59. return true;
    60. }
    61.  
    62. @EventHandler
    63. public void onDrop(PlayerDropItemEvent e) {
    64. Player p = e.getPlayer();
    65. if(test.contains(p.getName())) {
    66. // CODE TO DISABLE THE DROPPING OF THE DIAMOND SWORD IF THE PLAYER IS IN THE ARRAYLIST TEST AND THE ITEM TRYING TO DROP IS NAMED "§3Test Sword"
    67. }
    68. }
    69.  
    70. }
    71.  
     
  2. Offline

    Glumpz

    alexlyn1 Use the method e.getItem().getItemMeta().getDisplayName() and check if that is equal to whatever you don't want them to drop.
     
    alexlyn1 likes this.
  3. Offline

    alexlyn1

    Hello =) Thanks for the reply, but i don't see how i would use the e.getItem()?

    can you write some example code for me? i tried to type

    if(p.getItemInHand() == Material.DIAMOND_SWORD && e.getItem() but i can only seem to get e.getItemDrop() not the e.getItem()?

    Please write some example code thanks.

    Anyways while i'm waiting i gotta ask you another question can i make the player not able to drop it if it has a lore instead of the display name?

    like if(p.getItemInHand.hasLore("Property of " + p.getName()); this was written in hand btw so will not work :3

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    Epixpenguin

    alexlyn1
    You can create a player item drop event and check if the dropped item is equal to the item you don't want dropped with @Glumpz's method.
     
  5. Offline

    alexlyn1

    I get an error:

    CODE:
    Code:java
    1. package me.alex.test;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerDropItemEvent;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. public ArrayList<String> test = new ArrayList<String>();
    23.  
    24. public void onEnable() {
    25. Bukkit.getServer().getLogger().info("Test Plugin Enabled!");
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. }
    28.  
    29. public void onDisable() {
    30. Bukkit.getServer().getLogger().info("Test Plugin Disabled!");
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    34.  
    35. Player p = (Player) sender;
    36. PlayerInventory pi = p.getInventory();
    37.  
    38. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
    39. ItemMeta swordmeta = sword.getItemMeta();
    40. ArrayList<String> sa = new ArrayList<String>();
    41. swordmeta.setDisplayName("§3Test Sword");
    42. sa.add(ChatColor.DARK_PURPLE + "Property of " + ChatColor.DARK_PURPLE + p.getName());
    43. swordmeta.setLore(sa);
    44. sword.setItemMeta(swordmeta);
    45.  
    46. if(cmd.getName().equalsIgnoreCase("test")) {
    47. if(!test.contains(p.getName())) {
    48. test.add(p.getName());
    49. p.sendMessage("You were added to the ArrayList test!");
    50. pi.addItem(sword);
    51. return true;
    52. }
    53. if(test.contains(p.getName())) {
    54. test.remove(p.getName());
    55. pi.clear();
    56. p.sendMessage("You were removed from the ArrayList test!");
    57. return true;
    58. }
    59. }
    60. return true;
    61. }
    62.  
    63. @EventHandler
    64. public void onDrop(PlayerDropItemEvent e) {
    65. Player p = e.getPlayer();
    66. if(test.contains(p.getName()) && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§3Test Sword")) {
    67. e.setCancelled(true);
    68. p.sendMessage(ChatColor.DARK_RED + "You cannot drop your test item!");
    69. }
    70. }
    71.  
    72. @EventHandler
    73. public void onQuit(PlayerQuitEvent e) {
    74. Player p = e.getPlayer();
    75. if(test.contains(p.getName())) {
    76. test.remove(p.getName());
    77. p.getInventory().clear();
    78. p.getInventory().setArmorContents(null);
    79. }
    80. }
    81.  
    82. }
    83.  


    ERROR:

    HTML:
    [13:52:16 ERROR]: Could not pass event PlayerDropItemEvent to FirstPlugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.EntityHuman.a(EntityHuman.java:565) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.EntityHuman.a(EntityHuman.java:517) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :495) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.a(SourceFile:53) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.handle(SourceFile:8
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.NullPointerException
            at me.alex.test.Main.onDrop(Main.java:66) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _03]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _03]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_03]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_03]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            ... 15 more
    >
     
  6. Offline

    TGRHavoc

    alexlyn1
    In this line of code, you're assuming that the player is dropping the modified item that has metadata. Do a check to see if the item actually has metadata before checking what it's metadata is.
    Code:java
    1. if(test.contains(p.getName()) && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§3Test Sword")) {67. e.setCancelled(true);68. p.sendMessage(ChatColor.DARK_RED + "You cannot drop your test item!");69. }


    E.g.:
    Code:
     if (!p.getItemInHand().hasItemMeta(){ //If the item doesn't have item meta (is normal sword)
    return; //Get out of the method
    }
     
    alexlyn1 likes this.
  7. Offline

    alexlyn1

    TGRHavoc

    Okay i changed it to check but its still not working i dont get any console errors its just letting me drop the item
    CODE:

    Code:java
    1. @EventHandler
    2. public void onDrop(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4. if(!p.getItemInHand().hasItemMeta()) return;
    5. if(test.contains(p.getName()) && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§3Test Sword")) {
    6. e.setCancelled(true);
    7. p.sendMessage(ChatColor.DARK_RED + "You cannot drop your test item!");
    8. }
    9. }


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  8. Offline

    xMrPoi

    I made a method to check if an item had a specific name the other day. When I get on my computer, I can post it. If someone sees this message and I haven't posted it yet, please tell me and I will.
     
  9. Offline

    coasterman10

    You also need to check if the item's meta has a display name.
     
  10. Offline

    Glumpz

    alexlyn1 Bump once per 24 hours max.
     
  11. Offline

    alexlyn1

    Havn't posted yet :3

    How? if(!p.getItemInHand().hasItemMeta().hasDisplayName()) return; will not work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  12. Offline

    Konkz

    PHP:
        @EventHandler
        
    public void onDropItem(PlayerDropItemEvent e) {
            
    Player p e.getPlayer();
            if (
    e.getItemDrop().getItemStack().hasItemMeta()) {
                if(
    e.getItemDrop().getItemStack().getItemMeta().getDisplayName() == "§3Test Sword") {
                    
    e.setCancelled(true);
                    
    p.sendMessage("You cant't drop this item.");
                }
            }
        }
    You were checking for item in hand and not item dropped.
     
    alexlyn1 likes this.
  13. Offline

    xMrPoi

    Thanks for reminding me!
    Code:java
    1. public boolean isItemWithName(ItemStack item, Material material, String name){
    2. if(item.getType() == material && name == null) return true;
    3. if(item.getType() != material) return false;
    4. if(!(item.hasItemMeta())) return false;
    5. ItemMeta meta = item.getItemMeta();
    6. if(!(meta.hasDisplayName())) return false;
    7. if(!((meta.getDisplayName()).equals(name))) return false;
    8. return true;
    9. }
     
  14. Offline

    alexlyn1

    Code:java
    1.  
    2.  
    3. No problem! =)
    4.  
    5. [quote="Konkz, post: 2477638, member: 90676881"][PHP] @EventHandler
    6. public void onDropItem(PlayerDropItemEvent e) {
    7. Player p = e.getPlayer();
    8. if (e.getItemDrop().getItemStack().hasItemMeta()) {
    9. if(e.getItemDrop().getItemStack().getItemMeta().getDisplayName() == "§3Test Sword") {
    10. e.setCancelled(true);
    11. p.sendMessage("You cant't drop this item.");
    12. }
    13. }
    14. }[/PHP]
    15.  
    16. You were checking for item in hand and not item dropped.[/quote]
    17.  
    18. Okay it works not, EXCEPT if i click the Test Sword and put it back in to my inventory im allowed to drop it? And i can also drop it by taking it and clicking outside my inventory
    19.  
    20. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  15. Offline

    BeastCraft3

    Have you tried everything to find it out?

    Ik how to do it but I wont say it if you havent tried hard

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  16. Offline

    alexlyn1

    I have tried the most i could help me if you want.
     
  17. Offline

    xMrPoi

  18. Offline

    alexlyn1

    No, thats why i ask for help
     
  19. Offline

    xMrPoi

    My method didn't work? What exactly is happening when you try to drop an item? Put in debug messages to see what is getting triggered and what isn't.
     
  20. Offline

    alexlyn1

    Oh, i never tried yours cause i don't understand it
    could you try to add the code so if the name was §3Test Sword it couldn't be dropped?
     
  21. Offline

    xMrPoi

    My method's parameters are an ItemStack, a material, and a name. The ItemStack is the item you're checking. The material is the item it must be, and the name is, well, the name. It's a Boolean so it will return true or false.
     
Thread Status:
Not open for further replies.

Share This Page