Search in sources :

Example 6 with Attribute

use of org.bukkit.attribute.Attribute in project MagicPlugin by elBukkit.

the class CompatibilityUtils method removeItemAttributes.

@Override
public boolean removeItemAttributes(ItemStack item) {
    if (item == null)
        return false;
    ItemMeta meta = item.getItemMeta();
    if (meta == null)
        return false;
    Multimap<Attribute, AttributeModifier> modifiers = meta.getAttributeModifiers();
    if (modifiers == null || modifiers.isEmpty()) {
        return false;
    }
    for (Attribute attribute : modifiers.keySet()) {
        meta.removeAttributeModifier(attribute);
    }
    item.setItemMeta(meta);
    return true;
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeModifier(org.bukkit.attribute.AttributeModifier) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 7 with Attribute

use of org.bukkit.attribute.Attribute in project MagicPlugin by elBukkit.

the class BaseMageModifier method getAttributeModifiers.

@Nullable
public Collection<EntityAttributeModifier> getAttributeModifiers() {
    if (attributeModifiers != null) {
        return attributeModifiers;
    }
    ConfigurationSection config = getConfigurationSection("entity_attributes");
    if (config == null)
        return null;
    Set<String> keys = config.getKeys(false);
    if (keys.isEmpty())
        return null;
    attributeModifiers = new ArrayList<>();
    for (String key : keys) {
        String name = "mage_" + getKey() + "_" + key;
        double value;
        Double base = null;
        String attributeKey = key;
        AttributeModifier.Operation operation = AttributeModifier.Operation.ADD_NUMBER;
        if (config.isConfigurationSection(key)) {
            ConfigurationSection modifierConfig = config.getConfigurationSection(key);
            name = modifierConfig.getString("name", name);
            attributeKey = modifierConfig.getString("attribute", attributeKey);
            value = modifierConfig.getDouble("value");
            String operationType = modifierConfig.getString("operation");
            if (operationType != null && operationType.equalsIgnoreCase("base")) {
                base = value;
            } else if (operationType != null && !operationType.isEmpty()) {
                try {
                    operation = AttributeModifier.Operation.valueOf(operationType.toUpperCase());
                } catch (Exception ex) {
                    controller.getLogger().warning("Invalid operation " + operationType + " on entity_attributes." + key + " in mage class " + getKey());
                }
            }
        } else {
            value = config.getDouble(key);
        }
        Attribute attribute = null;
        try {
            attribute = Attribute.valueOf(attributeKey.toUpperCase());
        } catch (Exception ex) {
            controller.getLogger().warning("Invalid attribute " + attributeKey + " on entity_attributes." + key + " in mage class " + getKey());
        }
        if (attribute != null) {
            if (base != null) {
                attributeModifiers.add(new EntityAttributeModifier(attribute, base));
            } else {
                AttributeModifier modifier = new AttributeModifier(name, value, operation);
                attributeModifiers.add(new EntityAttributeModifier(attribute, modifier));
            }
        }
    }
    return attributeModifiers;
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeModifier(org.bukkit.attribute.AttributeModifier) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Nullable(javax.annotation.Nullable)

Example 8 with Attribute

use of org.bukkit.attribute.Attribute in project SwissKnife by EgirlsNationDev.

the class ItemUtil method removeAttributes.

public static void removeAttributes(ItemStack item) {
    ItemMeta itemMeta = item.getItemMeta();
    if (item.getItemMeta() == null)
        return;
    if (item.getItemMeta().getAttributeModifiers() == null)
        return;
    for (Attribute attribute : item.getItemMeta().getAttributeModifiers().keys()) {
        itemMeta.removeAttributeModifier(attribute);
    }
    item.setItemMeta(itemMeta);
}
Also used : Attribute(org.bukkit.attribute.Attribute) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 9 with Attribute

use of org.bukkit.attribute.Attribute in project SwissKnife by EgirlsNationDev.

the class EgirlsAttributeCorrector method getReducedDraconiteTotemMeta.

private ItemMeta getReducedDraconiteTotemMeta(ItemMeta meta) {
    Multimap<Attribute, AttributeModifier> modifierMap = meta.getAttributeModifiers();
    if (modifierMap == null)
        return null;
    if (modifierMap.size() == 2) {
        Multimap<Attribute, AttributeModifier> offHandAttributes = meta.getAttributeModifiers(EquipmentSlot.OFF_HAND);
        Multimap<Attribute, AttributeModifier> mainHandAttributes = meta.getAttributeModifiers(EquipmentSlot.HAND);
        if (offHandAttributes == null || mainHandAttributes == null) {
            return getCleanDraconiteTotemMeta(meta, modifierMap);
        }
        if (!offHandAttributes.containsKey(GENERIC_MAX_HEALTH) || !mainHandAttributes.containsKey(GENERIC_MAX_HEALTH)) {
            return getCleanDraconiteTotemMeta(meta, modifierMap);
        }
        if (offHandAttributes.keys().size() > 1 || mainHandAttributes.keys().size() > 1) {
            return getCleanDraconiteTotemMeta(meta, modifierMap);
        } else {
            meta.removeAttributeModifier(GENERIC_MAX_HEALTH);
            meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), "max_health", Modules.get().get(DraconiteItems.class).additionalTotemHp.get(), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND));
            meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), "max_health", Modules.get().get(DraconiteItems.class).additionalTotemHp.get(), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.OFF_HAND));
            return meta;
        }
    } else {
        return getCleanDraconiteSwordMeta(meta, modifierMap);
    }
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 10 with Attribute

use of org.bukkit.attribute.Attribute in project SwissKnife by EgirlsNationDev.

the class IllegalArmorAttributes method isArmorWithIllegalAttributes.

private boolean isArmorWithIllegalAttributes(ItemStack item) {
    if (item == null)
        return false;
    if (!item.hasItemMeta())
        return false;
    Multimap<Attribute, AttributeModifier> attributeMultiMap = item.getItemMeta().getAttributeModifiers();
    if (attributeMultiMap == null)
        return false;
    if (attributeMultiMap.isEmpty())
        return false;
    AtomicBoolean illegallyAttributed = new AtomicBoolean(false);
    if (customAttributes.get()) {
        Collection<AttributeModifier> armorAttribute = attributeMultiMap.get(Attribute.GENERIC_ARMOR);
        Collection<AttributeModifier> armorToughnessAttribute = attributeMultiMap.get(Attribute.GENERIC_ARMOR_TOUGHNESS);
        Collection<AttributeModifier> knockbackResistanceAttribute = attributeMultiMap.get(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
        Collection<AttributeModifier> maxHealthAttribute = attributeMultiMap.get(Attribute.GENERIC_MAX_HEALTH);
        armorAttribute.forEach(attributeModifier -> {
            if (attributeModifier.getAmount() > armor.get())
                illegallyAttributed.set(true);
        });
        armorToughnessAttribute.forEach(attributeModifier -> {
            if (attributeModifier.getAmount() > armorToughness.get())
                illegallyAttributed.set(true);
        });
        knockbackResistanceAttribute.forEach(attributeModifier -> {
            if (attributeModifier.getAmount() > knockbackResistance.get())
                illegallyAttributed.set(true);
        });
        maxHealthAttribute.forEach(attributeModifier -> {
            if (attributeModifier.getAmount() > maxHealth.get())
                illegallyAttributed.set(true);
        });
        return illegallyAttributed.get();
    }
    return true;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Attribute(org.bukkit.attribute.Attribute) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Aggregations

Attribute (org.bukkit.attribute.Attribute)70 AttributeModifier (org.bukkit.attribute.AttributeModifier)45 ItemMeta (org.bukkit.inventory.meta.ItemMeta)21 Map (java.util.Map)18 AttributeInstance (org.bukkit.attribute.AttributeInstance)17 ItemStack (org.bukkit.inventory.ItemStack)15 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 List (java.util.List)11 UUID (java.util.UUID)11 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)11 PotionEffect (org.bukkit.potion.PotionEffect)10 Attributable (org.bukkit.attribute.Attributable)9 Player (org.bukkit.entity.Player)9 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)8 Material (org.bukkit.Material)8 Pattern (org.bukkit.block.banner.Pattern)8 EntityType (org.bukkit.entity.EntityType)8 BannerMeta (org.bukkit.inventory.meta.BannerMeta)8 GameProfile (com.mojang.authlib.GameProfile)7