[Unsolved] Comparing itemstacks

Discussion in 'Plugin Development' started by Slamakans, Dec 9, 2011.

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

    Slamakans

    Code:java
    1. public void onItemHeldChange(PlayerItemHeldEvent event){
    2.  
    3. Player player = event.getPlayer();
    4. String playerName = player.getName();
    5. PlayerInventory inv = player.getInventory();
    6.  
    7. int prevSlotId = event.getPreviousSlot();
    8. ItemStack newItem = inv.getItem(event.getNewSlot());
    9.  
    10. ItemStack dSword = new ItemStack(Material.DIAMOND_SWORD, 1);
    11. ItemStack iSword = new ItemStack(Material.IRON_SWORD, 1);
    12.  
    13. ItemStack bow = new ItemStack(Material.BOW, 1);
    14.  
    15. player.sendMessage("new == "+newItem);
    16. player.sendMessage("dSword == "+dSword);
    17. if(plugin.getClass(playerName).equals("tank")){
    18.  
    19. if(newItem == dSword || newItem == bow){
    20.  
    21. inv.setItemInHand(inv.getItem(prevSlotId));
    22. player.sendMessage("You are not allowed to use this weapon.");
    23.  
    24. }
    25.  
    26. }else if(plugin.getClass(playerName).equals("archer")){
    27.  
    28. if(newItem == iSword || newItem == dSword){
    29.  
    30. inv.setItemInHand(inv.getItem(prevSlotId));
    31. player.sendMessage("You are not allowed to use this weapon.");
    32.  
    33. }
    34.  
    35. }else if(plugin.getClass(playerName).equals("bruiser")){
    36.  
    37. if(newItem == bow){
    38.  
    39. inv.setItemInHand(inv.getItem(prevSlotId));
    40. player.sendMessage("You are not allowed to use this weapon.");
    41.  
    42. }
    43.  
    44. }else if(plugin.getClass(playerName).equals("assassin")){
    45.  
    46. if(newItem == iSword || newItem == dSword){
    47.  
    48. inv.setItemInHand(inv.getItem(prevSlotId));
    49. player.sendMessage("You are not allowed to use this weapon.");
    50.  
    51. }
    52.  
    53. }
    54.  
    55. }


    No stacktrace, the player.sendMessage("You are not allowed to use this weapon.") is not sending.
    I have also tried using .equals(dSword) and .equals(Material.DIAMOND_SWORD, 1)
    Also, is my method for setting the selected hot bar slot back to the previous one correct? I believe not :(

    Grateful for any help ^^
     
  2. Have you registered the event correctly? And is @Override above that method?
    I'm saying this not because I assume you do not know what your doing, but because it is easy to forget :)
    Also please explain what you expect this line to do?
    if(plugin.getClass(playerName).equals("tank")){

    Also you may want to try:
    Code:
    if(newItem.getType() == Material.DIAMOND_SWORD || newItem.getType() == Material.IRON_SWORD){
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  3. Offline

    nisovin

    You shouldn't compare ItemStacks. Instead, use getType() on those ItemStacks and compare the Materials.
     
  4. Offline

    Slamakans

    Oh yes sorry, I will post the methods associated to this one :)

    onEnable:
    Code:java
    1. @Override
    2. public void onEnable() {
    3. PluginManager pm = this.getServer().getPluginManager();
    4.  
    5. pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Event.Priority.Lowest, this);
    6. pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Event.Priority.Lowest, this);
    7. pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Lowest, this);
    8. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Lowest, this);
    9. pm.registerEvent(Event.Type.PLAYER_ITEM_HELD, playerListener, Event.Priority.Highest, this);
    10.  
    11. }


    getClass:
    Code:java
    1. public String getClass(String playerName){
    2.  
    3. return getDBString("SELECT `class` FROM `classes` WHERE `playerName` = '" + playerName + "'");
    4.  
    5. }


    plugin.getClass(playerName) is getting the class that the player has chosen, it is stored in a MySQL database.

    I have confirmed that getClass() is working, and also that the event is registered correctly though :)

    Edit: I will try that, thanks Nisovin :)
    Edit2: I tried changing it to newItem.getType() == Material.DIAMOND_SWORD, but the code still wouldn't execute.
    Edit3: I mean randomer679 :p
     
  5. :O wheres my thanks? I posted that first :p
     
  6. Offline

    Slamakans

    Sorry didn't see your post o.0
    I am not used to getting so fast replies ;)

    inv.setItemHand is sending a NullPointerException stackTrace now, which means it works with .getType() == (materialwhatevs). Now I'd like help with that issue :S Line 91 is line 21 in my post.

    Code:
    02:03:13 [ALLVARLIG] Could not pass event PLAYER_ITEM_HELD to Zombie Apocalyps
    java.lang.NullPointerException
            at org.bukkit.craftbukkit.inventory.CraftItemStack.getEnchantments(Cra
    ItemStack.java:171)
            at org.bukkit.craftbukkit.inventory.CraftItemStack.<init>(CraftItemSta
    .java:29)
            at org.bukkit.craftbukkit.inventory.CraftItemStack.createNMSItemStack(
    aftItemStack.java:212)
            at org.bukkit.craftbukkit.inventory.CraftInventory.setItem(CraftInvent
    y.java:62)
            at org.bukkit.craftbukkit.inventory.CraftInventoryPlayer.setItemInHand
    raftInventoryPlayer.java:26)
            at me.slamakans.zombieapocalypse.ZAPlayerListener.onItemHeldChange(ZAP
    yerListener.java:91)
            at org.bukkit.plugin.java.JavaPluginLoader$18.execute(JavaPluginLoader
    ava:379)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.j
    a:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager
    ava:339)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:689)
            at net.minecraft.server.Packet16BlockItemSwitch.a(SourceFile:24)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    I am going to bed now, thanks for any helpful answers when I wake up ^^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
    randomer679 likes this.
  7. You should just cancel the event and it will automatically switch back :) I do not know why the NPE is happening but just cancel the event and the slot will switch back :)
     
  8. Offline

    Slamakans

    @nisovin @randomer679
    I am unable to cancel the event, I tried using event.setCancelled(true);
    Then it wants to cast to ((Cancellable) event).setCancelled(true);
    And I receive this stack trace since it isn't a cancellable event :s
    Code:
    11:18:02 [ALLVARLIG] Could not pass event PLAYER_ITEM_HELD to Zombie Apocalypse
    java.lang.ClassCastException: org.bukkit.event.player.PlayerItemHeldEvent cannot
     be cast to org.bukkit.event.Cancellable
            at me.slamakans.zombieapocalypse.ZAPlayerListener.onItemHeldChange(ZAPla
    yerListener.java:92)
            at org.bukkit.plugin.java.JavaPluginLoader$18.execute(JavaPluginLoader.j
    ava:379)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:339)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:689)
            at net.minecraft.server.Packet16BlockItemSwitch.a(SourceFile:24)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
     
  9. Oh I assumed all events were cancellable :/ I cannot find a way of switching the active slot back :/
     
  10. Offline

    nisovin

    You can't change the active hotbar slot.
     
  11. Offline

    Slamakans

    Is there a different way to achieve wanted results? And also, how would I go about restricting armor?
     
  12. Offline

    wwsean08

    you could move the sword to a different slot. As for restricting armor I'm not quite sure on that one.
     
  13. Offline

    Slamakans

  14. Offline

    CptSausage

    I guess you need Spout for the "restricting armor" - thing.
     
  15. I was wondering about restricting armor too as I need that for a plugin I'm working on. I don't think it can be done with spout/spoutcraft.
     
  16. Offline

    CptSausage

    You could listen for the onPlayerClickEvent and cancel it, if someone tries to place a wrong helmet in the armor slot.

    Or did you want to write "without", than ignore this post.
     
  17. Nope I meant with :) I don't get how you could check using that event though? Do you mean through the use of an addon made for spoutcraft or a plugin that utilizes spoutcraft? :)

    EDIT: Wait I may have found what you mean if you got the wrong event. Did you mean the InventoryClickEvent in spout? :)
     
  18. Offline

    CptSausage

    Yes right <.< looks like skyping and reading the forum at the same time doesn't work.
     
    randomer679 likes this.
  19. Offline

    coldandtired

    It won't work the way you want it to (the OP) because whatever you do to the held item, as soon as the event finishes it'll try to set it the the newslot (as it's not cancellable).

    The only way I can think of to achieve the result is:
    When players join (or at whatever interval you want) scan their inventory and remove any items they aren't allowed to use.

    Plus, listen for the PlayerPickUpItem event and cancel it (this one is cancellable :) ) if it's an item they aren't allowed to use.
     
  20. Offline

    Slamakans

    Thanks.
     
Thread Status:
Not open for further replies.

Share This Page