[SPOUT] Setting durability and damage of an item

Discussion in 'Plugin Development' started by thehutch, Nov 13, 2011.

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

    thehutch

    Ok so i'm having problems with setting the durability and damage of an item(Golden stuff) although there are no errors with the code and no errors in-game, I don't understand why it wouldn't work :(
    Here's my code@:
    Code:
    package me.thehutch.improvedgold;
    
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.getspout.spoutapi.event.inventory.InventoryCraftEvent;
    import org.getspout.spoutapi.event.inventory.InventoryListener;
    
    public class ImprovedGold extends JavaPlugin {
    
        private FileConfiguration config;
    
        @Override
        public void onDisable() {
            System.out.println("[ImprovedGold] has been disabled");
        }
        @Override
        public void onEnable() {
    
            setupConfig();
     
            getServer().getPluginManager().registerEvent(Event.Type.ENTITY_DAMAGE, new EntityListener() {
    
                public void onEntityDamage(EntityDamageEvent ev) {
                    if (ev.getEntity() instanceof Player) {
    
                        Player p = (Player)ev.getEntity();
    
                        if (p.getItemInHand().equals(Material.GOLD_AXE)) {
                            ev.setDamage(getConfig().getInt("Damage.Gold_AXE"));
                        }
                        else if (p.getItemInHand().equals(Material.GOLD_HOE)) {
                            ev.setDamage(getConfig().getInt("Damage.Gold_HOE"));
                        }
                        else if (p.getItemInHand().equals(Material.GOLD_SWORD)) {
                            ev.setDamage(getConfig().getInt("Damage.Gold_SWORD"));
                        }
                        else if (p.getItemInHand().equals(Material.GOLD_SPADE)) {
                            ev.setDamage(getConfig().getInt("Damage.Gold_SPADE"));
                        }
                        else if (p.getItemInHand().equals(Material.GOLD_PICKAXE)) {
                            ev.setDamage(getConfig().getInt("Damage.Gold_PICKAXE"));
                        }
                        else {
    
                        }
                    }
                }
            }, Event.Priority.Normal, this);
    
            getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, new InventoryListener() {
    
                public void onInventoryCraft(InventoryCraftEvent ev) {
    
                    // TOOLS
                    if (ev.getResult().equals(Material.GOLD_AXE)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_AXE"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_SWORD)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_SWORD"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_HOE)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_HOE"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_PICKAXE)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_PICKAXE"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_SPADE)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_SPADE"));
                    }
    
                    // ARMOUR
                    else if (ev.getResult().equals(Material.GOLD_HELMET)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_HELMET"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_CHESTPLATE)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_CHESTPLATE"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_LEGGINGS)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_LEGGINGS"));
                    }
                    else if (ev.getResult().equals(Material.GOLD_BOOTS)) {
                        ev.getResult().setDurability((short)config.getInt("Durability.GOLD_BOOTS"));
                    }
                    else {
    
                    }
                }
            }, Event.Priority.Normal, this);
            System.out.println("[ImprovedGold] version : " + getDescription().getVersion() + " has been enabled");
        }
     
        public void setupConfig() {
            config = getConfig();
            // Damage
            config.addDefault("Damage.Gold_AXE", 3);
            config.addDefault("Damage.Gold_SWORD", 5);
            config.addDefault("Damage.Gold_SPADE", 3);
            config.addDefault("Damage.Gold_PICKAXE", 3);
            config.addDefault("Damage.Gold_HOE", 2);
    
            // Durability of tools
            config.addDefault("Durability.GOLD_AXE", 500);
            config.addDefault("Durability.GOLD_SWORD", 500);
            config.addDefault("Durability.GOLD_SPADE", 500);
            config.addDefault("Durability.GOLD_PICKAXE", 500);
            config.addDefault("Durability.GOLD_HOE", 500);
    
            // Durability pf armour
            config.addDefault("Durability.GOLD_HELMET", 500);
            config.addDefault("Durability.GOLD_CHESTPLATE", 500);
            config.addDefault("Durability.GOLD_LEGGINGS", 500);
            config.addDefault("Durability.GOLD_BOOTS", 500);
    
            config.options().copyDefaults(true);
            saveConfig();
        }
    }
     
  2. Offline

    DDoS

    If I can remember correctly, damage is a byte, not a short.

    Edit: you're not setting the right thing. You're setting the damage the player caused to another one, not the damage of the item. Also, "p.getItemInHand()" returns an ItemStack, "Material.GOLD_AXE" is a Material, they can't be equal.

    Replace this:

    PHP:
    if (p.getItemInHand().equals(Material.GOLD_AXE)) {
        
    ev.setDamage((short)getConfig().getInt("Damage.Gold_AXE"));
    }
    with this:

    PHP:
    ItemStack item p.getItemInHand();
    if (
    item.getType().equals(Material.GOLD_AXE)) {
        
    item.setDurability((shortgetConfig().getInt("Damage.Gold_AXE"));
    }
     
  3. Offline

    thehutch

    Ok and also it isnt a short or byte :D
    "void org.bukkit.event.entity.EntityDamageEvent.setDamage(int damage)"

    @DDoS any ideas on how to then?

    Although the setDurability is a short :D
    void org.bukkit.inventory.ItemStack.setDurability(short durability)

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

    DDoS

    Ya, sorry, corrected that.
     
  5. Offline

    thehutch

    Ok I updated the OP with the changes? Is there anything else you sport @DDoS ? Oh yeah except for the checks :p sorry i'm doing homework and forgot :D
     
  6. Offline

    DDoS

    I don't see any changes...
     
  7. Offline

    thehutch

    Well the only changes were the castings to short but I removed them anyway let change what you said about not being the item's damage
     
  8. Offline

    DDoS

    ???

    Look at the code I put in my first post. This is a possible fix (possible, because I haven't tested it) for your item check and setting the durability. Replace it as I said, and test it, to see if it works.
     
  9. Offline

    thehutch

    Erm well one of them is damage and the other is durability which one am I changing??? I want one for both setting the damage done the the Golden tools and one for increasing its durability which atm is when it is crafted

    Updated code:
    PHP:
    public class ImprovedGold extends JavaPlugin {

        private 
    FileConfiguration config;

        @
    Override
        
    public void onDisable() {
            
    System.out.println("[ImprovedGold] has been disabled");
        }

        public 
    void onEnable() {

            
    setupConfig();
     
            
    getServer().getPluginManager().registerEvent(Event.Type.ENTITY_DAMAGE, new EntityListener() {

                public 
    void onEntityDamage(EntityDamageEvent ev) {

                    if (
    ev.getEntity() instanceof Player) {
     
                        
    Player p = (Player)ev.getEntity();

                        if (
    p.getItemInHand().getType() == Material.GOLD_AXE) {
                            
    p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
                            
    ev.setDamage(getConfig().getInt("Damage.Gold_AXE"));
                        }
                        else if (
    p.getItemInHand().getType() == Material.GOLD_HOE) {
                            
    p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
                            
    ev.setDamage(getConfig().getInt("Damage.Gold_HOE"));
                        }
                        else if (
    p.getItemInHand().getType() == Material.GOLD_SWORD) {
                            
    p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
                            
    ev.setDamage(getConfig().getInt("Damage.Gold_SWORD"));
                        }
                        else if (
    p.getItemInHand().getType() == Material.GOLD_SPADE) {
                            
    p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
                            
    ev.setDamage(getConfig().getInt("Damage.Gold_SPADE"));
                        }
                        else if (
    p.getItemInHand().getType() == Material.GOLD_PICKAXE) {
                            
    p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
                            
    ev.setDamage(getConfig().getInt("Damage.Gold_PICKAXE"));
                        }
                        else {

                        }
                    }
                }
            }, 
    Event.Priority.Normalthis);

            
    getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, new InventoryListener() {

                public 
    void onInventoryCraft(InventoryCraftEvent ev) {

                    
    // TOOLS
                    
    if (ev.getResult().getType() == Material.GOLD_AXE) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_AXE"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_SWORD) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_SWORD"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_HOE) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_HOE"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_PICKAXE) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_PICKAXE"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_SPADE) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_SPADE"));
                    }

                    
    // ARMOUR
                    
    else if (ev.getResult().getType() == Material.GOLD_HELMET) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_HELMET"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_CHESTPLATE) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_CHESTPLATE"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_LEGGINGS) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_LEGGINGS"));
                    }
                    else if (
    ev.getResult().getType() == Material.GOLD_BOOTS) {
                        
    ev.getResult().setDurability((short)config.getInt("Durability.GOLD_BOOTS"));
                    }
                    else {

                    }
                }
            }, 
    Event.Priority.Normalthis);
            
    System.out.println("[ImprovedGold] version : " getDescription().getVersion() + " has been enabled");
        }
     
        public 
    void setupConfig() {
            
    config getConfig();
            
    // Damage
            
    config.addDefault("Damage.GOLD_AXE"3);
            
    config.addDefault("Damage.GOLD_SWORD"5);
            
    config.addDefault("Damage.GOLD_SPADE"3);
            
    config.addDefault("Damage.GOLD_PICKAXE"3);
            
    config.addDefault("Damage.GOLD_HOE"2);

            
    // Durability of tools
            
    config.addDefault("Durability.GOLD_AXE"500);
            
    config.addDefault("Durability.GOLD_SWORD"500);
            
    config.addDefault("Durability.GOLD_SPADE"500);
            
    config.addDefault("Durability.GOLD_PICKAXE"500);
            
    config.addDefault("Durability.GOLD_HOE"500);

            
    // Durability pf armour
            
    config.addDefault("Durability.GOLD_HELMET"500);
            
    config.addDefault("Durability.GOLD_CHESTPLATE"500);
            
    config.addDefault("Durability.GOLD_LEGGINGS"500);
            
    config.addDefault("Durability.GOLD_BOOTS"500);

            
    config.options().copyDefaults(true);
            
    saveConfig();
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  10. Offline

    DDoS

    That looks better, but instead of using "p.getItemInHand()" 3 times, on every case, why not just set the value to a variable, and use it? That should clean up the code. Well, give it a try now.
     
  11. Offline

    thehutch

    @DDoS Well I get a better result but this time the durability is set to 1 at creation and then only has 1 use :(
     
  12. Offline

    DDoS

    Well, revise your code, there's something wrong somewhere, shouldn't be hard to find.
     
  13. Offline

    thehutch

    Well apparently the InventoryCraftEvent extends InventoryEvent not listener but it required 3 args and I don't know what to put in them
    http://jddev.getspout.org/
     
  14. Offline

    DDoS

    ???

    If you want to make an inventory listener, extend the InventoryListener, and use the "public void onInventoryCraft(InventoryCraftEvent event) {}" method to listen to the event. You don't have to build the event yourself!
     
    thehutch likes this.
  15. Err, make sure you extend the Spout one, also...
     
    thehutch likes this.
  16. Offline

    DDoS

    They have the same name. You just have to make sure you're using the right package.
     
    tips48 and thehutch like this.
  17. Thats what I meant ;)
     
  18. Offline

    DDoS

    ah ok!
     
  19. good thanks for the hard working i see you doing it hutch :D
     
Thread Status:
Not open for further replies.

Share This Page