Durability

Discussion in 'Plugin Development' started by Hellborn5456, Mar 12, 2014.

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

    Hellborn5456

    Trying to make a gun with durability and when it hit 0 durability it keeps going...

    Code:java
    1. @EventHandler
    2. public void playerInteractEvent(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)
    5. && p.getItemInHand().getItemMeta().equals(ItemUtil.pack.getItemMeta())){
    6. short damage = ItemUtil.pack.getDurability();
    7. if(damage >= 1){
    8. p.playEffect(p.getLocation(), Effect.SMOKE, 10);
    9. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1, 5));
    10. short dur = p.getItemInHand().getDurability();
    11. p.getItemInHand().setDurability((short) (dur + 9));
    12. }else{
    13. p.sendMessage("You need to reloaded!");
    14. }
    15. }
    16. }
     
  2. Offline

    97WaterPolo

    Code:text
    1. if (p.getItemInHand().getDurability() == 0 &&
    2. p.getItemInHand().getItemMeta().equals(ItemUtil.pack.getItemMeta()))
    3. )
    4. {
    5. //remove it, not sure if there is a method to play the breaking effect, but you can always do
    6. // player.getInventory().remove(p.getItemInHand());
    7. }
    8.  


    Did this without IDE, so might not work.
     
  3. Offline

    GameplayJDK

  4. Offline

    Hellborn5456

    97waterpolo well that ok but that not what i trying to do i want it so when it hits 0 it will say you need to reloaded
    but not break

    GameplayJDK & #97waterpolo im trying to get it so when the item hit 0 on durability it will say "you need to reloaded" but it not working like that.

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

    97WaterPolo

    Hellborn5456
    Ah, so try this.
    Code:java
    1. @EventHandler
    2. public void playerInteractEvent(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)
    5. && p.getItemInHand().getItemMeta().equals(ItemUtil.pack.getItemMeta())){
    6. short damage = ItemUtil.pack.getDurability();
    7. if (p.getItemInHand().getDurability() == 0 && p.getItemInHand().getItemMeta().equals(ItemUtil.pack.getItemMeta())
    8. {//Adding the check for the Item Meta data so that only this item will execute this.
    9. p.sendMessage("You need to reloaded!");
    10. }else
    11. {
    12. p.playEffect(p.getLocation(), Effect.SMOKE, 10);
    13. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1, 5));
    14. short dur = p.getItemInHand().getDurability();
    15. p.getItemInHand().setDurability((short) (dur + 9));
    16. }
    17.  


    Like I said, no IDE so play around with this. I am thinking its not actually updating the item, if this doesn't work, try updating the player's inventory.
     
  6. Offline

    GameplayJDK

  7. Offline

    97WaterPolo

    GameplayJDK
    Thanks, I know, was just contemplating that the inventory wasn't being updated which is why it didn't check if the durability was correct. Just ignore it, lol.
     
  8. Offline

    Hellborn5456

    97waterpolo
    thats not working.. i also updated the inv but i look up some thing and a guy said to invert 0
    "Its inverted. 0 is no damage etc.
    Thats why its called a damage value. its a little weird i know."

    i dont know what he means
     
  9. Offline

    97WaterPolo

    Hellborn5456
    Just re looked at your original code, what is the damage variable for? It seems to be retrieving a number, if that is the case, then your statement damage >= 1 will not return an else statement on fire. Basically what that is saying, if damage is >= 1 run that, but its a set value, doesn't change, thats why you need to use item's durability which returns an int.

    Hellborn5456
    Will help you when I get home to my IDE if no one else steps in, sorry.

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

    GameplayJDK

    97waterpolo
    I just wanted to help the people Who don't know how to update the players inventory. :)
     
    97waterpolo likes this.
  11. Offline

    97WaterPolo

    GameplayJDK
    Thanks! ^_^

    Hellborn5456

    This should work, didn't test it in game, but IDE accepted it. Stil not sure what damage variable does.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void playerInteractEvent(PlayerInteractEvent e){
    4. Player p = e.getPlayer(); //Player variable
    5. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) && p.getItemInHand().getItemMeta().equals(ItemUtil.pack.getItemMeta()))
    6. {//makes sure the item that is right clicked is the defined item. MIGHT want to add RIGHT_CLICK_BLOCK as an action
    7. short damage = ItemUtil.pack.getDurability(); //Gets the damage
    8. if (p.getItemInHand().getDurability() == 0)
    9. //Checks to see if the durability is 0
    10. {
    11. p.sendMessage("You need to reloaded!");
    12. }else
    13. {
    14. if (damage >= 1) //To make sure the damage subtraceted is at least 1
    15. {
    16. p.playEffect(p.getLocation(), Effect.SMOKE, 10);
    17. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1, 5));
    18. short dur = p.getItemInHand().getDurability();
    19. p.getItemInHand().setDurability((short) (dur + 9));
    20. }
    21. }
    22. }
    23. }
     
Thread Status:
Not open for further replies.

Share This Page