Solved Remove sword when right clicked.

Discussion in 'Plugin Development' started by qonyin, Nov 16, 2012.

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

    qonyin

    Okay, I'm working on a plugin that will allow you to uncraft an item when you right click a gold block.

    My problem is that
    Code:
    inventory.removeItem(player.getItemInHand());
    
    works for any item except a sword. and
    Code:
    player.setItemInHand(newitemstack1);
    will change the item held into the itemstack i want to give but again it doesn't work for swords.

    In both scenarios the sword stays in your hand and the itemStack is never given to you.

    I hope there is a simple fix or work around for this all i can think of is that it has something to do with the blocking effect.
     
  2. Offline

    Jogy34

    Try setting the item in the player's hand to null
     
  3. Offline

    qonyin

    I have tried that using
    player.setItemInHand(null);
    and i still get the same result.
     
  4. Offline

    wristdirect

    have you tried this?

    Code:
    player.getItemInHand().setType(Material.AIR);
     
  5. Offline

    qonyin

    Thanks for the suggestion, I tried that and am still unable to remove the sword on a right click event.
    the strange thing is that it works for all other items I have coded just not the sword. infact i still get my debugging messages saying its working. However nothing i do seems to remove the sword.
     
  6. Offline

    Jogy34

    Try setting the durability to 0 then removing it
     
  7. Offline

    qonyin

    Sorry tried that it also didn't work. is there perhaps a way to damage the item once i set it to 0 so it breaks naturally?
     
  8. Offline

    Jogy34

    Setting the damage value to 0 makes it like a brand new sword.
     
  9. Offline

    qonyin

    Correct but i also tried using
    player.getItemInHand().setDurability(player.getItemInHand().getType().getMaxDurability());

    Witch makes it so it breaks next use. I then tried using the same code and adding 1 to the max durability. again same scenario it breaks next use but doesn't remove the sword from inventory or allow it to be removed.
     
  10. Offline

    Jogy34

    try adding a player.updateInventory(); after that.
     
  11. Offline

    qonyin

    Now the sword Flickers away for just a moment but reappears right back in the inventory.Still fully damaged as if it should be broken.
     
  12. Offline

    Jogy34

    Huh. That's really weird. And it does work with other items just not sword right?
     
  13. Offline

    qonyin

    Yes all other tools and all armor works just fine but for some reason swords will not be removed and it will not give me my materials back.
    I have coded messages in to tell me what items i should be getting back and there is even one in the function call to remove the sword that posts but the sword doesn't go away.
     
  14. Offline

    Jogy34

    Can you post your code as you have it right now?
     
  15. Offline

    qonyin

    Absolutely
    Here is the function called from the event listener

    Code:java
    1.  
    2. private void RecycleItem(final Material basematerial, final int basequantity, final int sticks,
    3. final Player player, final double dmg) {
    4. int newamnt = getamnt(sticks, basequantity, dmg);
    5. player.sendMessage("Item is at " + (int) dmg + "% Durability");
    6. player.sendMessage("You would get back " + newamnt + " Blocks!");
    7. final ItemStack newite1 = new ItemStack(basematerial, newamnt);
    8. player.getItemInHand().setDurability((short) (player.getItemInHand().getType().getMaxDurability()+1));
    9. player.updateInventory();
    10. player.setItemInHand(newite1);
    11. }
    12.  
     
  16. Offline

    Jogy34

    I got nothing. This looks like it should work. Only thing I can think of is adding another player.updateInventory(); at the very end.
     
    qonyin likes this.
  17. Offline

    qonyin

    Thank you that was giving me a weird glitch where it would only work the second time I right clicked. i changed it to the following and it appears to work now I really appreciate the help.

    Code:java
    1.  
    2. private void RecycleItem(final Material basematerial, final int basequantity, final int sticks,
    3. final Player player, final double dmg) {
    4. int newamnt = getamnt(sticks, basequantity, dmg);
    5. player.sendMessage("Item is at " + (int) dmg + "% Durability");
    6. player.sendMessage("You would get back " + newamnt + " Blocks!");
    7. final ItemStack newite1 = new ItemStack(basematerial, newamnt);
    8. //player.getItemInHand().setDurability((short) (player.getItemInHand().getType().getMaxDurability()+1));
    9. //player.updateInventory();
    10. player.setItemInHand(new ItemStack(Material.AIR));
    11. player.updateInventory();
    12. player.setItemInHand(newite1);
    13. player.updateInventory();
    14. }
    15.  
     
Thread Status:
Not open for further replies.

Share This Page