Durability carftbukkit 1.7.10

Discussion in 'Plugin Development' started by maliimaloo, Jan 9, 2019.

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

    maliimaloo

    Hello,

    I have a little problem, I custom all my items etc., and now I would like to change the durability of the item, I tried with the Item Stack.Set durability(20), but the durability is reduced (Ex: Helmet in diamond 343/363), I then tried to add the (-20), but the amount does not exceed the maximum durability of the item.

    Code:
    package com.maliimaloo.Commands;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.maliimaloo.Main;
    import com.maliimaloo.Utils.SettingsManagerArmure;
    import com.maliimaloo.Utils.SettingsManagerMessage;
    
    import net.minecraft.server.v1_7_R4.NBTTagCompound;
    import net.minecraft.server.v1_7_R4.NBTTagDouble;
    import net.minecraft.server.v1_7_R4.NBTTagInt;
    import net.minecraft.server.v1_7_R4.NBTTagList;
    import net.minecraft.server.v1_7_R4.NBTTagString;
    
    public class GiveCommande implements CommandExecutor {
        SettingsManagerArmure settingsArmure = SettingsManagerArmure.getInstance();
        SettingsManagerMessage settingsMessage = SettingsManagerMessage.getInstance();
    
        Main main = Main.getInstance();
        public int idItemConfig;
    
        @SuppressWarnings({ "deprecation" })
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
    
                if (args.length == 0 || args.length == 1) {
                    player.sendMessage(settingsMessage.getMessageConfig().getString("Commande_GV.Invalide_Commande")
                            .replace("&", "§"));
                    return false;
                } else if (args.length >= 2) {
                    if (!(player.isOp())) {
                        player.sendMessage(settingsMessage.getMessageConfig().getString("Commande_GV.Op_Commande_Invalide")
                                .replace("&", "§"));
                        return false;
                    }
    
                    int idItem = Integer.valueOf(args[0]);
                    int quantiter = Integer.valueOf(args[1]);
    
                    List<String> getArmure = new ArrayList<String>();
                    for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure").getKeys(false)) {
                        getArmure.add(str);
                    }
    
                    List<String> getArmure1 = new ArrayList<String>();
                    for (int a = 0; a < getArmure.size(); a++)
                    {
                        for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure." + getArmure.get(a)).getKeys(false))
                        {
                            getArmure1.add(str);
                        }
                    }
    
                    for (int a = 0; a < getArmure.size(); a++)
                    {
                        for (int b = 0; b < getArmure1.size(); b++)
                        {
                            idItemConfig = settingsArmure.getArmureConfig().getInt("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".ID");
    
                            if (idItem == idItemConfig)
                            {
                                List<String> getGeneric = new ArrayList<String>();
                                for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Generic").getKeys(false))
                                {
                                    if (str != null)
                                    {
                                        getGeneric.add(str);
                                    }
                                }
    
                                int idItemGeneric = idItemConfig;
                                ItemStack item = new ItemStack(idItemGeneric, quantiter);
                               
                                item.setDurability(Short.valueOf(settingsArmure.getArmureConfig().getString("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Durabiliter")));
                                ItemMeta itemMeta = item.getItemMeta();
                                itemMeta.setDisplayName(settingsArmure.getArmureConfig().getString("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".ItemName"));
                                itemMeta.setLore(settingsArmure.getArmureConfig().getStringList("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Description"));
                                item.setItemMeta(itemMeta);
    
                                net.minecraft.server.v1_7_R4.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
                                NBTTagCompound compound = nmsStack.hasTag() ? nmsStack.getTag() : new NBTTagCompound();
                                NBTTagList modifiers = new NBTTagList();
    
                                for (int c = 0; c < getGeneric.size(); c++)
                                {
                                    String generic = settingsArmure.getArmureConfig().getString("Armure." + getArmure.get(a)+ "." + getArmure1.get(b) + ".Generic." + getGeneric.get(c) + ".GenericType");
                                    Double mount = settingsArmure.getArmureConfig().getDouble("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Generic."+ getGeneric.get(c) + ".GenericMontant");
    
                                    NBTTagCompound genericCreate = new NBTTagCompound();
                                    genericCreate.set("AttributeName", new NBTTagString("generic." + generic));
                                    genericCreate.set("Name", new NBTTagString("generic." + generic));
                                    genericCreate.set("Amount", new NBTTagDouble(mount));
                                    genericCreate.set("Operation", new NBTTagInt(0));
                                    genericCreate.set("UUIDLeast", new NBTTagInt(894654));
                                    genericCreate.set("UUIDMost", new NBTTagInt(2872));
    
                                    modifiers.add(genericCreate);
                                }
    
                                compound.set("AttributeModifiers", modifiers);
                                nmsStack.setTag(compound);
                                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                                player.getInventory().addItem(item);
                                return false;
                            }
                        }
                    }
    
                    player.sendMessage(settingsMessage.getMessageConfig().getString("Commande_GV.ItemId_Invalide").replace("&", "§").replace("%idItem%", String.valueOf(idItem)));
                    return false;
                }
            }
            return false;
        }
    }
    
     
  2. Offline

    DerDonut

    actually this works in reverse as you just found out. So setting the durability to 0 fully repairs the item. It's more the damage than the durability
     
  3. Offline

    maliimaloo

    Would you have an example?, or could you correct it?
     
  4. @maliimaloo
    Instead of taking durability from the item, you should be adding it - it's like reverse. Think of replacing the word durability with damage.

    Durability works as a metadata tag on the original item, the more damaged it is, the higher the metadata tag.
     
  5. Offline

    maliimaloo

    Yes, good i have success ^^

    My new Code:

    Code:
    package com.maliimaloo.eventListener;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftCreature;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitScheduler;
    
    import com.maliimaloo.Main;
    import com.maliimaloo.Utils.SettingsManagerArmure;
    
    import net.minecraft.server.v1_7_R4.Material;
    
    public class DamagePlayer implements Listener
    {
    
        SettingsManagerArmure settingsArmure = SettingsManagerArmure.getInstance();
       
        public double armorBonus;
        String itemHelmet;
        String itemPlastron;
        String itemJambiere;
        String itemBotte;
        String itemHand;
       
        ArrayList<String> itemLore;
       
        @EventHandler
        public void playerDamage(EntityDamageByEntityEvent e)
        {       
            if(e.getEntity() instanceof Player && e.getDamager() instanceof CraftCreature)
            {
                armorBonus = 0.0;
                CraftPlayer player = (CraftPlayer) e.getEntity();
                CraftCreature damager = (CraftCreature) e.getDamager();
               
                this.playerDamageFonction(player);
                damager.damage(damager.getLastDamage() - armorBonus);
    
                double currentLife = player.getHealth();
                BukkitScheduler  scheduler  =  Main.getPlugin(Main.class).getServer().getScheduler();
                scheduler.scheduleSyncDelayedTask(Main.getPlugin(Main.class), new Runnable()
                {
    
                    @SuppressWarnings("deprecation")
                    @Override
                    public void run()
                    {       
                        playerDurabiliter(player, player.getInventory().getHelmet(), currentLife);
                        playerDurabiliter(player, player.getInventory().getChestplate(), currentLife);
                        playerDurabiliter(player, player.getInventory().getLeggings(), currentLife);
                        playerDurabiliter(player, player.getInventory().getBoots(), currentLife);
                       
                        player.updateInventory();
                    }
                   
                }, 20L);
            }
        }
       
        @SuppressWarnings("deprecation")
        public void playerDurabiliter(CraftPlayer player, ItemStack playerInventory, double currentLife)
        {
            itemLore = new ArrayList<String>();
            if(playerInventory != null)
            {
                playerInventory.setDurability((short) 0);
                player.updateInventory();
               
                itemLore = new ArrayList<String>();
                itemLore.addAll(playerInventory.getItemMeta().getLore());
               
                for(int c = 0; c < itemLore.size(); c++)
                {
                    if(itemLore.get(c).contains("Durabiliter") || itemLore.get(c).contains("durabiliter"))
                    {
                        String[] parts;
                       
                        String durabiliter = itemLore.get(c).replace(" ", "");
                        parts = durabiliter.split(":");
                        String parts_1 = parts[1];
                       
                        String nbDurabiliter = parts_1;
                        parts = nbDurabiliter.split("/");
                       
                        String durabiliterCurrent = parts[0];
                        if(Integer.valueOf(durabiliterCurrent) <= 0)
                        {
                            player.getInventory().setHelmet(new ItemStack(Material.AIR));
                        }
                        else
                        {
                            double _currentLife = player.getHealth();
                            int damage = (int) (_currentLife - currentLife);
                            int durabiliterResult = Integer.valueOf(durabiliterCurrent) + damage;
    
                            int idItem = playerInventory.getTypeId();
                           
                            List<String> getArmure = new ArrayList<String>();
                            for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure").getKeys(false)) {
                                getArmure.add(str);
                            }
    
                            List<String> getArmure1 = new ArrayList<String>();
                            for (int a = 0; a < getArmure.size(); a++)
                            {
                                for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure." + getArmure.get(a)).getKeys(false))
                                {
                                    getArmure1.add(str);
                                }
                            }
    
                            for (int a = 0; a < getArmure.size(); a++)
                            {
                                for (int b = 0; b < getArmure1.size(); b++)
                                {
                                    int idItemConfig = settingsArmure.getArmureConfig().getInt("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".ID");
                                   
                                    if(idItemConfig == idItem)
                                    {
                                        ItemStack item = playerInventory;
                                        ItemMeta itemM = item.getItemMeta();
                                       
                                        ArrayList<String> description = new ArrayList<>();
                                        for(String str : settingsArmure.getArmureConfig().getStringList("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Description"))
                                        {
                                            description.add((str)
                                                    .replace("%durabiliter%", String.valueOf(durabiliterResult))
                                                    .replace("%durabiliterMax%", String.valueOf(settingsArmure.getArmureConfig().getInt("Armure." + getArmure.get(a) + "." + getArmure1.get(b) + ".Durabiliter"))));
                                        }
                                        itemM.setLore(description);
                                        item.setItemMeta(itemM);
                                    }
                                }
                            }
                        }
                    }
                }
            }   
        }
    
        private void playerDamageFonction(CraftPlayer player)
        {   
            armorBonus = 0.0;
           
            if(player.getInventory().getHelmet() != null)
            {
                itemHelmet = player.getInventory().getHelmet().getItemMeta().getDisplayName();
            }
            if(player.getInventory().getChestplate() != null)
            {
                itemPlastron = player.getInventory().getChestplate().getItemMeta().getDisplayName();
            }
            if(player.getInventory().getLeggings() != null)
            {
                itemJambiere = player.getInventory().getLeggings().getItemMeta().getDisplayName();
            }
            if(player.getInventory().getBoots() != null)
            {
                itemBotte = player.getInventory().getBoots().getItemMeta().getDisplayName();
            }
            if(player.getInventory().getItemInHand() != null && player.getInventory().getItemInHand().hasItemMeta())
            {
                itemHand = player.getItemInHand().getItemMeta().getDisplayName();
            }
           
            List<String> getArmure = new ArrayList<String>();
            for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure").getKeys(false))
            {
                getArmure.add(str);
            }
           
            List<String> getArmure1 = new ArrayList<String>();
            for (int a = 0; a < getArmure.size(); a++)
            {
                for (String str : settingsArmure.getArmureConfig().getConfigurationSection("Armure."+getArmure.get(a)).getKeys(false))
                {
                    getArmure1.add(str);
                }
            }
           
            for (int a = 0; a < getArmure.size(); a++)
            {
                for(int b = 0; b < getArmure1.size(); b++)
                {
                    String itemArmorName = settingsArmure.getArmureConfig().getString("Armure." + getArmure.get(a) +"."+ getArmure1.get(b) + ".ItemName");
               
                    if(itemArmorName.equalsIgnoreCase(itemHelmet) || itemArmorName.equalsIgnoreCase(itemPlastron) ||itemArmorName.equalsIgnoreCase(itemJambiere) ||itemArmorName.equalsIgnoreCase(itemBotte) ||itemArmorName.equalsIgnoreCase(itemHand))
                    {
                        double configArmorBonus = settingsArmure.getArmureConfig().getDouble("Armure."+ getArmure.get(a) +"."+ getArmure1.get(b) +".ArmureBonus");
                        armorBonus = armorBonus + configArmorBonus;
                       
                    }
                }
            }
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page