Armor error

Discussion in 'Plugin Development' started by B0ich1K | UA minecraft, Apr 15, 2023.

Thread Status:
Not open for further replies.
  1. Hi Bukkit! We need your help. The fact is that when putting on armor, a certain error occurs.

    The characteristics work when I put on the first item (Characteristics of Armor), but when I put on another item of armor, the characteristics of the previous Armor are canceled and changed to another characteristic of the other item.

    P.S. I immediately apologize for the mistakes, because my English is probably very bad)


    Code:
    private static void addAttackDamage(ItemMeta meta, Integer damage, List<String> lore){
            AttributeModifier t1 = new AttributeModifier(UUID.randomUUID(), "generic.attack_damage", damage, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
            meta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, t1);
            if(damage > 0){
                lore.add(ChatColor.AQUA + "Шкода: " + ChatColor.RED + damage.toString());
                meta.setLore(lore);
            }
        }
        private static void addMaxHealth(ItemMeta meta, List<String> lore, Integer maxhealth , EquipmentSlot slot){
            AttributeModifier max = new AttributeModifier(UUID.randomUUID(), "generic.max_health", maxhealth, AttributeModifier.Operation.ADD_NUMBER, slot);
            meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, max);
            if (maxhealth > 0) {
                lore.add(ChatColor.AQUA + "Додаткові серця: " + ChatColor.RED + maxhealth.toString());
                meta.setLore(lore);
            }
        }
        private static void addArmour(ItemMeta meta, List<String> lore, Integer armour, EquipmentSlot slot){
            AttributeModifier max = new AttributeModifier(UUID.fromString("7E0292F2-9434-48D5-A29F-9583AF7DF27F"), "generic.armor", armour, AttributeModifier.Operation.ADD_NUMBER, slot);
            meta.addAttributeModifier(Attribute.GENERIC_ARMOR, max);
            if (armour > 0) {
                lore.add(ChatColor.AQUA + "Додаткова броня: " + ChatColor.RED + armour.toString());
                meta.setLore(lore);
            }
        }
        private static void addArmStr(ItemMeta meta, List<String> lore, Integer armstr, EquipmentSlot slot){
            AttributeModifier armourhand = new AttributeModifier(UUID.randomUUID(), "generic.armorToughness", armstr, AttributeModifier.Operation.ADD_NUMBER, slot);
            meta.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS, armourhand);
            if (armstr > 0) {
                lore.add(ChatColor.AQUA + "Сила Броні: " + ChatColor.RED + armstr.toString());
                meta.setLore(lore);
            }
        }
        private static void addSpeedPlayer(ItemMeta meta, List<String> lore, Double speedplayer, EquipmentSlot slot){
            AttributeModifier speed = new AttributeModifier(UUID.randomUUID(), "generic.movement_speed", speedplayer, AttributeModifier.Operation.ADD_NUMBER, slot);
            meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, speed);
            if (speedplayer > 0) {
                lore.add(ChatColor.AQUA + "Додаткова швидкість: +" + ChatColor.RED + speedplayer.toString());
                meta.setLore(lore);
            }else if(speedplayer < 0){
                lore.add(ChatColor.AQUA + "Зменьшеня швидкості: -" + ChatColor.RED + speedplayer.toString());
                meta.setLore(lore);
            }else{
                return;
            }
        }
        public ItemsC(Gothic plugin){
            this.plugin = plugin;
        }
        public static void create(Material material,String displayname, String l, Integer dam, Player p){
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.WHITE + displayname);
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GRAY + l);
            meta.setLore(lore);
            addAttackDamage(meta, dam, lore);
            item.setItemMeta(meta);
            p.getInventory().addItem(item);
        }
        public static void create(Material material, String displayname, String l, Integer armor, Integer maxhp, Integer armstr, Double playerspeed,String slot, Player p){
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.WHITE + displayname);
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GRAY + l);
            meta.setLore(lore);
            addMaxHealth(meta, lore, maxhp, EquipmentSlot.valueOf(slot));
            addArmour(meta, lore, armor, EquipmentSlot.valueOf(slot));
            addArmStr(meta, lore, armstr, EquipmentSlot.valueOf(slot));
            addSpeedPlayer(meta , lore , playerspeed, EquipmentSlot.valueOf(slot));
            item.setItemMeta(meta);
            p.getInventory().addItem(item);
        }
    P.S. I understand that my legs are not worn there, but there really is a problem)
     

    Attached Files:

  2. Offline

    lst96

    In your code, you are creating a new List<String> called lore for each attribute modifier, and then you are setting this lore list as the lore of the ItemMeta object using the meta.setLore(lore) method. However, each time you call setLore(lore), it overrides the previous lore that was set for the item, so you end up losing the previous lore entries.

    To fix this issue, you can create a single List<String> for the entire item's lore, and then add the lore entries for each attribute modifier to this list using the lore.add() method. Then, after adding all the attribute modifiers, you can set the final lore list to the ItemMeta object using meta.setLore(lore).
     
Thread Status:
Not open for further replies.

Share This Page