Solved Can't find a fix for a bug

Discussion in 'Plugin Development' started by DjTamo, Jun 25, 2013.

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

    DjTamo

    So I'm making this new mini-game and its almost ready to be released. I was going through it to test for any bugs I may have missed and I found a really big one. When a tool breaks, it disappears but when you open your inventory and move that slot it appears with full durability and can be used again. I have no idea whats causing this to happen and I'm completely stumped. The only part of my plugin that uses it is my Listener for block breaking:

    Code:java
    1. public void onBreakBlock(BlockBreakEvent evt) {
    2. Block brokenBlock = evt.getBlock();
    3. Player player = evt.getPlayer();
    4. if (brokenBlock.getType() == Material.WOOL){
    5.  
    6. ItemStack clay2 = new ItemStack(Material.FLINT, 1);
    7. ItemStack clay3 = new ItemStack(Material.IRON_INGOT, 1);
    8. ItemStack clay4 = new ItemStack(Material.GOLD_INGOT, 1);
    9. ItemStack clay5 = new ItemStack(Material.DIAMOND, 1);
    10.  
    11. Material tool = player.getItemInHand().getType();
    12. World world = player.getWorld();
    13.  
    14. boolean woodAxe = false;
    15. boolean stoneAxe = false;
    16. boolean ironAxe = false;
    17. boolean diamondAxe = false;
    18. boolean woodPickaxe = false;
    19. boolean stonePickaxe = false;
    20. boolean ironPickaxe = false;
    21. boolean diamondPickaxe = false;
    22. if (tool == Material.WOOD_AXE) {woodAxe = true;}
    23. if (tool == Material.STONE_AXE) {woodAxe=true;stoneAxe=true;}
    24. if (tool == Material.IRON_AXE) {woodAxe=true;stoneAxe=true;ironAxe=true;}
    25. if (tool == Material.DIAMOND_AXE) {woodAxe=true;stoneAxe=true;ironAxe=true;diamondAxe=true;}
    26. if (tool == Material.WOOD_PICKAXE) {woodPickaxe = true;}
    27. if (tool == Material.STONE_PICKAXE) {woodPickaxe=true;stonePickaxe=true;}
    28. if (tool == Material.IRON_PICKAXE) {woodPickaxe=true;stonePickaxe=true;ironPickaxe=true;}
    29. if (tool == Material.DIAMOND_PICKAXE) {woodPickaxe=true;stonePickaxe=true;ironPickaxe=true;diamondPickaxe=true;}
    30.  
    31. int blockData = brokenBlock.getData();
    32.  
    33. if (blockData == 0 && woodAxe) {world.dropItem(player.getLocation(), clay2);}
    34. //White wool - T1 Tree
    35. if (blockData == 1 && stoneAxe) {world.dropItem(player.getLocation(), clay3);}
    36. //Orange wool - T2 Tree
    37. if (blockData == 2 && ironAxe) {world.dropItem(player.getLocation(), clay4);}
    38. //Magenta wool - T3 Tree
    39. if (blockData == 3 && diamondAxe) {world.dropItem(player.getLocation(), clay5);}
    40. //Light Blue wool - T4 Tree
    41. if (blockData == 4 && woodPickaxe) {world.dropItem(player.getLocation(), clay2);}
    42. //Yellow wool - T1 Rock
    43. if (blockData == 5 && stonePickaxe) {world.dropItem(player.getLocation(), clay3);}
    44. //Light Green wool - T2 Rock
    45. if (blockData == 6 && ironPickaxe) {world.dropItem(player.getLocation(), clay4);}
    46. //Pink wool - T3 Rock
    47. if (blockData == 7 && diamondPickaxe) {world.dropItem(player.getLocation(), clay5);}
    48. //Gray wool - T4 Rock
    49. }
    50. if (brokenBlock.getType() == Material.CLAY) {
    51.  
    52. ItemStack clay1 = new ItemStack(Material.CLAY_BALL, 1);
    53.  
    54. World world = player.getWorld();
    55. int blockData = brokenBlock.getData();
    56.  
    57. world.dropItem(player.getLocation(), clay1);
    58. //Clay Block - Fragments
    59. }
    60. evt.setCancelled(true);
    61. }


    This is the only code that uses the tools but I don't see where it would affect the durability.

    Anyone know what might be causing this? It may not be the code above but that was the only thing I could think of.

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

    1SmallVille1

    DjTamo
    ooohh this is a good one. If not for past experience I would have been lost. When you do "evt.setCancelled" what happens is you completely cancel every part of the event. That includes the damage done to your tool. The reason that you see damage done to it is that it's a client side glitch that has no fix. If you still need your tool to be damaged, you'll need to do:

    Code:Java
    1. player.getItemInHand().setDamage(player.getItemInHand().getDamage() + 1);


    The only problem with this is that it will cancel out all unbreaking enchants. In order to fix this, you'll have to research the probability of a tool with x unbreaking will take damage, then do a Random with the same probability and so on...
     
  3. Offline

    DjTamo

    Ahhhhh! Thanks so much I was so lost on this!

    Btw it's getDurability and setDurability not Damage
     
  4. Make sure you mark the thread as solved :p, also i'm also trying to make a minigame but i'm a little stuck on how i would go about it.
     
Thread Status:
Not open for further replies.

Share This Page