[Solved] How to add damage to tools!

Discussion in 'Plugin Development' started by Milkywayz, Mar 10, 2012.

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

    Milkywayz

    Hey. i want to allow players to break iron, lapis, and gold ore with a gold pick, and i want this to happen instantly. That works. What doesn't is the durability doesn't go down. Ideally i would like double damage since its instant mine, and with this particular plugin you can convert wood tools into gold tools. Here the code for the Interact listener (super pick):
    Code:java
    1. public class MyGoldListener implements Listener{
    2. @EventHandler(priority = EventPriority.HIGH)
    3. public void onLeftClick(PlayerInteractEvent e) {
    4. Player p = (Player)e.getPlayer();
    5. if(e.getPlayer().getItemInHand().getType() == Material.GOLD_PICKAXE) {
    6. if(e.getAction() == Action.LEFT_CLICK_BLOCK) {
    7. if(e.getClickedBlock().getType() == Material.IRON_ORE) {
    8. e.getClickedBlock().breakNaturally();
    9. p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 2));
    10. return;
    11. }
    12. if(e.getClickedBlock().getType() == Material.GOLD_ORE) {
    13. e.getClickedBlock().breakNaturally();
    14. p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 2));
    15. return;
    16. }
    17. if(e.getClickedBlock().getType() == Material.LAPIS_ORE) {
    18. e.getClickedBlock().breakNaturally();
    19. p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 2));
    20. return;
    21. }
    22. }
    23. }
    24. }}
    25.  

    The Block Listener:
    Code:java
    1. public class MyBlockListener implements Listener{
    2. public void onBlockBreak(BlockBreakEvent ev) {
    3. Block block = ev.getBlock();
    4. World world = block.getWorld();
    5. Location loc = block.getLocation();
    6. BlockState bs = block.getState();
    7. ItemStack goldpick = new ItemStack(285);
    8. if(ev.getPlayer().getItemInHand() == goldpick) {
    9. if(block.getType() == Material.IRON_ORE) {
    10. block.setType(Material.AIR);
    11. world.dropItemNaturally(loc, new ItemStack(bs.getType(), 1, bs.getRawData(), bs.getRawData()));
    12. return;
    13. }
    14. if(block.getType() == Material.GOLD_ORE) {
    15. block.setType(Material.AIR);
    16. world.dropItemNaturally(loc, new ItemStack(bs.getType(), 1, bs.getRawData(), bs.getRawData()));
    17. return;
    18. }
    19. if(block.getType() == Material.LAPIS_ORE) {
    20. block.setType(Material.AIR);
    21. world.dropItemNaturally(loc, new ItemStack(bs.getType(), 1, bs.getRawData(), bs.getRawData()));
    22. return;
    23. }
    24. }
    25. }
    26. }
     
  2. Offline

    Cirno

    ItemStack.setDurability(int_here)
     
  3. Offline

    Milkywayz

    That will just set the durability to the same thing every time someone breaks a block with the super pick. I need to damage the tool once every time they "left click" or destroy a block.
     
  4. Offline

    Cirno

    Really...
    ItemStack.setDurability(ItemStack.getDurability() - 1)
     
  5. Offline

    Milkywayz

    That does not work. Put it in eclipse or netbeans, you will see. It will tell you to cast -1 to short, then its the same thing as what i had.

    Could anyone help please?

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

    zachoooo

    Why do you cast the entire value to short? Why dont you only cast the 2 to short? I doubt that will fix it, but just saying.
     
  7. Offline

    nisovin

    You need to add durability, not subtract durability.
     
  8. Offline

    Milkywayz

    This is what I'm trying now, what happens is ,it damages 1, but the data++ doesn't add more durability.
    PHP:
    public class MyGoldListener implements Listener{
        @
    EventHandler(priority EventPriority.HIGH)
    public 
    void onLeftClick(PlayerInteractEvent e) {
            
    short data 1;
            if(
    e.getPlayer().getItemInHand().getType() == Material.GOLD_PICKAXE) {
                if(
    e.getAction() == Action.LEFT_CLICK_BLOCK) {
                    if(
    e.getClickedBlock().getType() == Material.IRON_ORE) {               
                        
    e.getClickedBlock().breakNaturally();               
                            
    e.getPlayer().getInventory().getItemInHand().setDurability(data) ;
                            
    data++;
                            return;
                        }                   
                        }           
                else if(
    e.getClickedBlock().getType() == Material.GOLD_ORE) {
                            
    e.getClickedBlock().breakNaturally();
                                
    e.getPlayer().getInventory().getItemInHand().setDurability(data) ;
                                
    data++;
                                return;
                            }   
                else if(
    e.getClickedBlock().getType() == Material.LAPIS_ORE) {
                                
    e.getClickedBlock().breakNaturally();
                                    
    e.getPlayer().getInventory().getItemInHand().setDurability(data) ;
                                    
    data++;
                                    return;
                            }
            } else {
                
    e.setCancelled(false);
            }
        }
    }
     
  9. Offline

    nisovin

    You need to increment the data before setting it to the item. Primitives do not use references.

    Edit: Just use the code you had originally, but change the minuses to pluses.
     
    Milkywayz likes this.
  10. Offline

    Milkywayz

    EDIT: As simple as it was, your epic. Thanks so much man.
     
Thread Status:
Not open for further replies.

Share This Page