Solved Attributes aren't working

Discussion in 'Plugin Development' started by voltywolty, Jan 15, 2024.

  1. Offline

    voltywolty

    So I have an item manager that creates custom items, with attributes, enchants, etc. However, the attributes don't really seem to work. I have white dye set to do 8 damage with the custom item, but it isn't doing that.

    Here is my applyAttributes
    Code:
    private void applyAttributes(ItemMeta meta, ConfigurationSection config) {
            if (config.contains("attributes")) {
                List<String> attributesList = config.getStringList("attributes");
    
                for (String attributeString : attributesList) {
                    String[] parts = attributeString.split(":");
                    if (parts.length == 2) {
                        String attributeName = parts[0].toUpperCase().trim();
                        double attributeValue;
    
                        try {
                            attributeValue = Double.parseDouble(parts[1].trim());
                        }
                        catch (NumberFormatException e) {
                            plugin.getLogger().warning("Invalid attribute value format in config: " + attributeString);
                            continue;
                        }
    
                        Attribute attribute = getAttributeByName(attributeName);
    
                        if (attribute != null) {
                            AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), attributeName, attributeValue, AttributeModifier.Operation.ADD_NUMBER);
                            meta.addAttributeModifier(attribute, modifier);
                        }
                        else
                            plugin.getLogger().warning("Invalid attribute name in config: " + attributeName);
                    }
                    else
                        plugin.getLogger().warning("Invalid attribute format in config: " + attributeString);
                }
            }
        }
    
        private Attribute getAttributeByName(String attributeName) {
            for (Attribute attr : Attribute.values()) {
                if (attr.name().equals(attributeName)) {
                    return attr;
                }
            }
            return null;
        }
    Here is my ItemStack that applies the attribute.
    Code:
    public ItemStack createItem(String itemName, int amount, FileConfiguration config) {
            if (config.contains("custom-items." + itemName)) {
                ConfigurationSection itemConfig = config.getConfigurationSection("custom-items." + itemName);
    
                Material type = Material.matchMaterial(itemConfig.getString("type", "").toUpperCase());
                if (type == null) {
                    plugin.getLogger().warning("Invalid material type for item: " + itemName);
                    return null;
                }
    
                ItemStack customItem;
                ItemMeta meta;
    
                if (type == Material.POTION) {
                    customItem = new ItemStack(type, amount);
                    meta = customItem.getItemMeta();
    
                    if (meta instanceof PotionMeta) {
                        PotionMeta potionMeta = (PotionMeta) meta;
    
                        // NOTE - Sets potion data
                        PotionData potionData = new PotionData(PotionType.valueOf(itemConfig.getString("potion-type", "WATER").toUpperCase()));
                        potionMeta.setBasePotionData(potionData);
    
                        // NOTE - Set custom color
                        if (itemConfig.contains("potion-color")) {
                            String hexColor = itemConfig.getString("potion-color", "").replace("#", "").toUpperCase(Locale.ROOT);
                            try {
                                int colorValue = Integer.parseInt(hexColor, 16);
                                Color potionColor = Color.fromRGB(colorValue);
                                potionMeta.setColor(potionColor);
                            }
                            catch (NumberFormatException e) {
                                plugin.getLogger().warning("Invalid hex color format for potion in config.");
                            }
                        }
    
                        meta = potionMeta;
                    }
                }
                else {
                    customItem = new ItemStack(type, amount);
                    meta = customItem.getItemMeta();
                }
    
                if (meta != null) {
                    String name = ChatColor.translateAlternateColorCodes('&', itemConfig.getString("name", ""));
                    List<String> lore = itemConfig.getStringList("lore");
                    List<String> translatedLore = lore.stream()
                            .map(line -> ChatColor.translateAlternateColorCodes('&', line))
                            .collect(Collectors.toList());
    
                    boolean unbreakable = itemConfig.getBoolean("unbreakable", false);
    
                    meta.setDisplayName(name);
                    meta.setLore(translatedLore);
                    meta.setUnbreakable(unbreakable);
    
                    applyAttributes(meta, itemConfig);
    
                    if (itemConfig.getBoolean("hide-flags", false))
                        meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_ITEM_SPECIFICS);
    
                    if (itemConfig.getBoolean("fake-glint", false))
                        meta.addEnchant(Enchantment.DURABILITY, 1, true);
    
                    if (itemConfig.contains("enchantments")) {
                        List<String> enchantments = itemConfig.getStringList("enchantments");
                        applyEnchantments(customItem, enchantments);
                    }
    
                    customItem.setItemMeta(meta);
                    return customItem;
                }
            }
            return null;
        }
    Not too sure what can be causing it, but in the config it should be:
    Code:
      zombie_sword:
        type: white_dye
        name: "&4Makeshift Blade"
        hide-flags: true
        attributes:
          - GENERIC_ATTACK_DAMAGE: 2.0
        lore:
          - "&3Deals 2 damage to dwarves."
    The white dye is not applying two damage.
     

Share This Page