Adding Durability Use on a Tool

Discussion in 'Plugin Development' started by diamonddigger8, Jul 10, 2014.

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

    diamonddigger8

    My problem...

    I'm making a gun plugin and I'm going to add an if statement that will check if the player has ammo for the specific gun the player is holding in hand. The ammo will be diamond tools with 1561 durability usage, so I can configure how many bullets are in each magazine. The only problem is that I have no clue how to remove 65 durability on a diamond pick that is not in the player's hand when he/she shoots the gun on right clicking (with a bone in hand). I will also need to check when the tool is at 1 durability, remove 1 pickaxe from the stack, and set the stack of pickaxes back to 1561 durability.

    Is there a way to take away durability on a tool that is not in the player's hand?

    If so, explain to me in the comments how to do so.
     
  2. Offline

    tommycake50

    As long as you have the item's instance in a variable then you just do item.setDurability(Or whatever the method is, I honestly don't remember).
     
  3. Offline

    diamonddigger8

    Can someone show me some code showing me how to remove 65 durability from a diamond pickaxe?
     
  4. Offline

    ResultStatic

    diamonddigger8 durability starts at (short)0); meaning it has full use when the durability is at 0. you can use item.getType().getMaxDurability(); which will return a short. iron armor is around 800. so if you wanted to remove it you could get the current durability and set the new one to current - 65;
     
  5. Offline

    diamonddigger8

    No no no, I don't want to set the durability of the diamond pick so it will be unbreakable. I want to add 65 durability to the pickaxe so it will eventually hit 1 out of the total 1561 units of durability on a diamond pick.

    So far I have...
    Code:java
    1. ItemStack ammo = new ItemStack (Material.DIAMOND_PICKAXE);
    2. ammo.getDurability();

    I just don't know how to add 65 durability usage on the diamond pick.

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

    Necrodoom

    Best way I can think of is using lore to store ammo amount, and set the durability based on percentage, this means you must track breaking and such but the system makes it easier to use any ammo amount on a pickaxe.
     
  7. Offline

    diamonddigger8

    Code:java
    1. ItemStack ammo = new ItemStack (Material.DIAMOND_PICKAXE);
    2. ammo.setDurability((short) +65);

    Can someone fix this code to take 65 durability from a diamond pickaxe's total of 1561 each time the code is ran?
     
  8. Offline

    izarooni

    Is there no getDurability method?
     
  9. Offline

    diamonddigger8

    There is a getDurability method, the only problem is I have no idea how to use it. If someone could give me some source code to explain to me how to put durability onto a diamond pickaxe I would be on my way to finishing my gun plugin.
     
  10. Offline

    maved145

    diamonddigger8
    Set durability for some reason doesn’t work.

    Code:java
    1. ItemStack ammo = new ItemStack(Material.DIAMOND_PICKAXE, 1, (short) amo.getDurability() - 64);


    Try that. I’m not sure if it will work but i know if i do this:

    Code:java
    1. ItemStack flintandsteel = new ItemStack(Material.FLINT_AND_STEEL, 1, (short) 64);


    it will set the Flint and steel so it has one use left.
     
  11. Offline

    ResultStatic

    maved145 thats not durability the short is extra id information. like setting types of potions stained clay etc
     
  12. Offline

    maved145

    ResultStatic
    It is. Try it for your self with the flint and steel code i provided.
     
  13. Offline

    diamonddigger8

    maved145
    I'm not trying to give the player a diamond sword with durability use already on it, but instead I am trying to edit the diamond pick (named ammo) to gain durability when I fire my gun (which is a renamed bone with resourcepack and all). I need to take off 65 durability for every shot fired to make a total of 24 bullets per clip of ammo.

    Is it even possible?

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

    izarooni

    diamonddigger8
    Code:java
    1. itemstack.setDurability((short) (itemstack.getDurability() + number));

    Where number is the amount you want the item's durability to decrease. When it reaches 0 or below it doesn't break so you're going to have to check if it's less than or equal to 0. If it is then you can just make it air/null and play the break sound
     
  15. Offline

    diamonddigger8


    Would I have to use Material.DIAMOND_PICKAXE to replace itemstack? or would I have to create a variable that equals a diamond pick like
    Code:java
    1. ItemStack ammo = new ItemStack (Material.DIAMOND_PICKAXE);
    ?
     
  16. Offline

    izarooni

    diamonddigger8
    I don't get what you mean. I'm assuming you're using the PlayerInteractEvent because of the gun and ammo.
    So just create an ItemStack variable that equals to the item in the player's hand and check if it's the item type is the one you want to use for the gun.
     
  17. Offline

    diamonddigger8

    Yes, I am using the PlayerInteractEvent, but the thing is the item in the player's hand is not the ammo it is the gun. The gun is a bone and the ammo is a diamond pick. I am trying to make it so when the player right clicks to fire the gun it will add durability to the diamond pick, and once the diamond pick reaches a durability of 1 the plugin will remove one diamond pick from a stack of 12 of them and set the durability of the stack back to 1561. Here is the code relevant to this thread.
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. final Player p = (Player) e.getPlayer();
    5. ItemStack ammo = new ItemStack (Material.DIAMOND_PICKAXE);
    6. if (e.getPlayer().getItemInHand().getType() == Material.BONE) {
    7. if (p.getInventory().contains(Material.DIAMOND_PICKAXE)) {
    8. Snowball s = p.launchProjectile(Snowball.class);
    9. ammo.setDurability((short) (ammo.getDurability() + 65));
    10. }
    11. }
    12. }
    13. }
     
Thread Status:
Not open for further replies.

Share This Page