Solved setDamage Working

Discussion in 'Spigot Plugin Development' started by Th3Wh1teG0d, Jan 27, 2020.

Thread Status:
Not open for further replies.
  1. I have created a jetpack but the setDamage not working.

    Code:

    Code (Java):

    public class Jetpack implements Listener {
       
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            final Player p = e.getPlayer();
           
            if (p.isSneaking()) {
                if(p.getInventory().getChestplate() != null) {
                    if (p.getInventory().getChestplate().getType() == Material.GOLDEN_CHESTPLATE) {
                        if(p.getInventory().getChestplate().hasItemMeta()) {
                            if (p.getInventory().getChestplate().getItemMeta().getDisplayName().equalsIgnoreCase("§c§lJetpack")) {
                                org.bukkit.util.Vector v = p.getLocation().getDirection().multiply(1.01D).setY(0.51D);
                                p.setVelocity(v);
                                p.getWorld().spawnParticle(Particle.FLAME, p.getLocation().add(0, 1, 0), 0, 0, 0, 0, 0);
                                p.getWorld().spawnParticle(Particle.SMOKE_LARGE, p.getLocation().add(0, 1, 0), 0, 0, 0, 0, 0);
                                p.playSound(p.getLocation(), Sound.ENTITY_ARROW_SHOOT, 10, (float) 0.5);
                                ItemStack jpi = p.getInventory().getChestplate();
                                ItemMeta jpmta = jpi.getItemMeta();
                                if (jpi instanceof Damageable) {
                                    ((Damageable) jpi).setDamage(+1);
                                }
                                jpi.setItemMeta(jpmta);
                            }                    
                        }
                    }                  
                }
            }
        }
    }
     
     
  2. drives_a_ford

    Moderator

    You need to get the value and then add 1 to it and then set it. Your code is always setting the damage value of the item to 1.
     
  3. How do i do that ?
     
  4. setDamage( getDamage + 1);

    It is in pseudo intentionally, so you try to figure it out yourself
     
  5. getDamage cannot be resolved to a variable

    Code (Java):
                                    ((Damageable) jpi).setDamage(getDamage + 1);
     
  6. drives_a_ford

    Moderator

  7. If it's solved, please mark as solved
     
  8. What was given you ia psuedo code.. You can't just copy and paste that. Please learn some Java before making plugins, and while making plugins, take a look at the javadocs
     
  9. I have do this but i didnt work:

    Code (Java):
     ((Damageable) jpi).setDamage(((Damageable) jpi).getDamage() + 1);
     
  10. You should do this to the meta, not the item. In fact, this should throw ClassCastException, shouldn't it?
     
  11. It won't work like that.
    You are using the wrong variable as Damagable.
    You should try like this:

    Code (Java):

    ItemStack jpi = p.getInventory().getChestplate();
    ItemMeta jpmta = jpi.getItemMeta();
    if (jpmta instanceof Damageable) {
        Damageable damageable = ((Damageable) jpmta);
        damageable.setDamage(damageable.getDamage() +1);
    }
    jpi.setItemMeta(jpmta);
     
    If it doesn't work you may try with -1
     
  12. Nope it dosent work with +1 and -1

    Current Code:
    Code (Java):

    public class Jetpack implements Listener {
       
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            final Player p = e.getPlayer();
           
            if (p.isSneaking()) {
                if(p.getInventory().getChestplate() != null) {
                    if (p.getInventory().getChestplate().getType() == Material.GOLDEN_CHESTPLATE) {
                        if(p.getInventory().getChestplate().hasItemMeta()) {
                            if (p.getInventory().getChestplate().getItemMeta().getDisplayName().equalsIgnoreCase("§c§lJetpack")) {
                                org.bukkit.util.Vector v = p.getLocation().getDirection().multiply(1.01D).setY(0.51D);
                                p.setVelocity(v);
                                p.getWorld().spawnParticle(Particle.FLAME, p.getLocation().add(0, 1, 0), 0, 0, 0, 0, 0);
                                p.getWorld().spawnParticle(Particle.SMOKE_LARGE, p.getLocation().add(0, 1, 0), 0, 0, 0, 0, 0);
                                p.playSound(p.getLocation(), Sound.ENTITY_ARROW_SHOOT, 10, (float) 0.5);
                                ItemStack jpi = p.getInventory().getChestplate();
                                ItemMeta jpmta = jpi.getItemMeta();
                                if (jpi instanceof Damageable) {
                                    Damageable damageable = ((Damageable) jpmta);
                                    damageable.setDamage(damageable.getDamage() -1);
                                }
                                jpi.setItemMeta(jpmta);
                            }                    
                        }
                    }                  
                }
            }
        }
    }
     
     
  13. Did you see the last edit I sent you?
    The if is wrong as well. Itemstack can't be Damageable, only the meta.
     
  14. drives_a_ford

    Moderator

    You need to check if jpmta is instance of Damageable and cast that. Right now you're checking if the ItemStack is an instance of Damageable, which will never be the case.
     
  15. How do i remove the Chestplate from the players inventory or How do i set the Chestplate slot to Air ?
     
  16. p.getInventory().setChestplate(null)

    or


    p.getInventory().setChestplate(new Itemstack(Material.AIR))
     
Thread Status:
Not open for further replies.