Search in sources :

Example 1 with Attribute

use of org.bukkit.attribute.Attribute in project Denizen-For-Bukkit by DenizenScript.

the class EntityAttributeModifiers method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        try {
            MapTag input = mechanism.valueAsType(MapTag.class);
            Attributable ent = getAttributable();
            for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
                Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (AttributeModifier modifier : instance.getModifiers()) {
                    instance.removeModifier(modifier);
                }
                for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
                    instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
                }
            }
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
    }
    // -->
    if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        try {
            MapTag input = mechanism.valueAsType(MapTag.class);
            Attributable ent = getAttributable();
            for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
                Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
                    instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
                }
            }
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
    }
    // -->
    if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
        ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
        Attributable ent = getAttributable();
        for (String toRemove : new ArrayList<>(inputList)) {
            if (new ElementTag(toRemove).matchesEnum(Attribute.class)) {
                inputList.remove(toRemove);
                Attribute attr = Attribute.valueOf(toRemove.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (AttributeModifier modifier : instance.getModifiers()) {
                    instance.removeModifier(modifier);
                }
            }
        }
        for (String toRemove : inputList) {
            UUID id = UUID.fromString(toRemove);
            for (Attribute attr : Attribute.values()) {
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    continue;
                }
                for (AttributeModifier modifer : instance.getModifiers()) {
                    if (modifer.getUniqueId().equals(id)) {
                        instance.removeModifier(modifer);
                        break;
                    }
                }
            }
        }
    }
    if (mechanism.matches("attributes") && mechanism.hasValue()) {
        Deprecations.legacyAttributeProperties.warn(mechanism.context);
        Attributable ent = getAttributable();
        ListTag list = mechanism.valueAsType(ListTag.class);
        for (String str : list) {
            List<String> subList = CoreUtilities.split(str, '/');
            Attribute attr = Attribute.valueOf(EscapeTagBase.unEscape(subList.get(0)).toUpperCase());
            AttributeInstance instance = ent.getAttribute(attr);
            if (instance == null) {
                mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                continue;
            }
            instance.setBaseValue(Double.parseDouble(subList.get(1)));
            for (AttributeModifier modifier : instance.getModifiers()) {
                instance.removeModifier(modifier);
            }
            for (int x = 2; x < subList.size(); x += 4) {
                String slot = subList.get(x + 3).toUpperCase();
                AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), EscapeTagBase.unEscape(subList.get(x)), Double.parseDouble(subList.get(x + 1)), AttributeModifier.Operation.valueOf(subList.get(x + 2).toUpperCase()), slot.equals("ANY") ? null : EquipmentSlot.valueOf(slot));
                instance.addModifier(modifier);
            }
        }
    }
}
Also used : Attribute(org.bukkit.attribute.Attribute) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) Attributable(org.bukkit.attribute.Attributable) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) AttributeInstance(org.bukkit.attribute.AttributeInstance) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Map(java.util.Map)

Example 2 with Attribute

use of org.bukkit.attribute.Attribute in project Denizen-For-Bukkit by DenizenScript.

the class EntityAttributeBaseValues method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <EntityTag.has_attribute[<attribute>]>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the entity has the named attribute.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "has_attribute", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.has_attribute[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        return new ElementTag(object.getAttributable().getAttribute(attr) != null);
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the final calculated value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getValue());
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_base_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the base value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_base_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_base_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getBaseValue());
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_default_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the default value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_default_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_default_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getDefaultValue());
    });
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeInstance(org.bukkit.attribute.AttributeInstance) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 3 with Attribute

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

the class InventoryUtils method applyAttributes.

public static void applyAttributes(ItemStack item, ConfigurationSection attributeConfig, String slot) {
    if (item == null || attributeConfig == null)
        return;
    Collection<String> attributeKeys = attributeConfig.getKeys(false);
    for (String attributeKey : attributeKeys) {
        try {
            Attribute attribute = Attribute.valueOf(attributeKey.toUpperCase());
            double value = attributeConfig.getDouble(attributeKey);
            if (!CompatibilityUtils.setItemAttribute(item, attribute, value, slot)) {
                Bukkit.getLogger().warning("Failed to set attribute: " + attributeKey);
            }
        } catch (Exception ex) {
            Bukkit.getLogger().warning("Invalid attribute: " + attributeKey);
        }
    }
}
Also used : Attribute(org.bukkit.attribute.Attribute) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with Attribute

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

the class MagicItemCommandExecutor method onItemAddAttribute.

public boolean onItemAddAttribute(Player player, ItemStack item, String attributeName, String attributeValue, String attributeSlot) {
    Attribute attribute = null;
    if (attributeName == null)
        return false;
    try {
        attribute = Attribute.valueOf(attributeName.toUpperCase());
    } catch (Exception ex) {
        player.sendMessage(ChatColor.RED + "Invalid attribute: " + ChatColor.WHITE + attributeName);
        return true;
    }
    double value = 0;
    try {
        value = Double.parseDouble(attributeValue);
    } catch (Exception ex) {
        player.sendMessage(ChatColor.RED + "Invalid attribute value: " + ChatColor.WHITE + attributeValue);
        return true;
    }
    if (CompatibilityUtils.setItemAttribute(item, attribute, value, attributeSlot)) {
        if (attributeSlot == null) {
            attributeSlot = "(All Slots)";
        }
        player.sendMessage(api.getMessages().get("item.attribute_added").replace("$attribute", attribute.name()).replace("$value", Double.toString(value)).replace("$slot", attributeSlot));
    } else {
        player.sendMessage(api.getMessages().get("item.attribute_not_added").replace("$attribute", attribute.name()));
    }
    return true;
}
Also used : Attribute(org.bukkit.attribute.Attribute) IOException(java.io.IOException)

Example 5 with Attribute

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

the class MagicItemCommandExecutor method onTabComplete.

@Override
public Collection<String> onTabComplete(CommandSender sender, String commandName, String[] args) {
    List<String> options = new ArrayList<>();
    if (args.length == 1) {
        addIfPermissible(sender, options, "Magic.commands.mitem.", "add");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "remove");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "name");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "configure");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "describe");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "duplicate");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "save");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "delete");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "destroy");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "clean");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "worth");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "type");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "damage");
        addIfPermissible(sender, options, "Magic.commands.mitem.", "skull");
    }
    if (args.length == 2) {
        String subCommand = args[0];
        String subCommandPNode = "Magic.commands.mitem." + subCommand;
        if (!api.hasPermission(sender, subCommandPNode)) {
            return options;
        }
        if (subCommand.equalsIgnoreCase("add")) {
            options.add("enchant");
            options.add("attribute");
            options.add("lore");
            options.add("flag");
            options.add("unbreakable");
            options.add("unplaceable");
        }
        if (subCommand.equalsIgnoreCase("remove")) {
            options.add("enchant");
            options.add("attribute");
            options.add("lore");
            options.add("flag");
            options.add("unbreakable");
            options.add("unplaceable");
        }
        if (subCommand.equalsIgnoreCase("type")) {
            for (Material material : Material.values()) {
                options.add(material.name().toLowerCase());
            }
        }
        if (subCommand.equalsIgnoreCase("damage")) {
            options.add("0");
            options.add("100");
        }
        if (subCommand.equalsIgnoreCase("delete")) {
            File itemFolder = new File(api.getController().getConfigFolder(), "items");
            if (itemFolder.exists()) {
                File[] files = itemFolder.listFiles();
                for (File file : files) {
                    if (file.getName().endsWith(".yml")) {
                        options.add(file.getName().replace(".yml", ""));
                    }
                }
            }
        }
    }
    if (args.length == 3) {
        String subCommand = args[0];
        String subCommand2 = args[1];
        String commandPNode = "Magic.commands.mitem." + subCommand;
        if (!api.hasPermission(sender, commandPNode)) {
            return options;
        }
        boolean isAddRemove = subCommand.equalsIgnoreCase("remove") || subCommand.equalsIgnoreCase("add");
        if (isAddRemove && subCommand2.equalsIgnoreCase("enchant")) {
            for (Enchantment enchantment : Enchantment.values()) {
                options.add(enchantment.getName().toLowerCase());
            }
        }
        if (isAddRemove && subCommand2.equalsIgnoreCase("attribute")) {
            for (Attribute attribute : Attribute.values()) {
                options.add(attribute.name().toLowerCase());
            }
        }
        if (isAddRemove && subCommand2.equalsIgnoreCase("flag")) {
            for (ItemFlag flag : ItemFlag.values()) {
                options.add(flag.name().toLowerCase());
            }
        }
        if (subCommand.equalsIgnoreCase("remove") && subCommand2.equalsIgnoreCase("lore")) {
            options.add("1");
            options.add("2");
            options.add("3");
        }
    }
    if (args.length == 5) {
        String subCommand = args[0];
        String subCommand2 = args[1];
        if (subCommand.equalsIgnoreCase("add") && subCommand2.equalsIgnoreCase("attribute")) {
            options.add("mainhand");
            options.add("offhand");
            options.add("feet");
            options.add("legs");
            options.add("chest");
            options.add("head");
        }
    }
    return options;
}
Also used : Attribute(org.bukkit.attribute.Attribute) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) Enchantment(org.bukkit.enchantments.Enchantment) ItemFlag(org.bukkit.inventory.ItemFlag) File(java.io.File)

Aggregations

Attribute (org.bukkit.attribute.Attribute)9 AttributeInstance (org.bukkit.attribute.AttributeInstance)5 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 Attributable (org.bukkit.attribute.Attributable)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AttributeModifier (org.bukkit.attribute.AttributeModifier)2 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 UUID (java.util.UUID)1 Material (org.bukkit.Material)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 Enchantment (org.bukkit.enchantments.Enchantment)1