Search in sources :

Example 6 with AttributeModifier

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

the class ItemAttributeModifiers method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = LinkedHashMultimap.create();
        MapTag map = mechanism.valueAsType(MapTag.class);
        for (Map.Entry<StringHolder, ObjectTag> mapEntry : map.map.entrySet()) {
            org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(mapEntry.getKey().str.toUpperCase());
            for (ObjectTag listValue : CoreUtilities.objectToList(mapEntry.getValue(), mechanism.context)) {
                metaMap.put(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
            }
        }
        ItemMeta meta = item.getItemMeta();
        meta.setAttributeModifiers(metaMap);
        item.setItemMeta(meta);
    }
    // -->
    if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        ItemMeta meta = item.getItemMeta();
        MapTag input = mechanism.valueAsType(MapTag.class);
        for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
            org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(subValue.getKey().str.toUpperCase());
            for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
                meta.addAttributeModifier(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
            }
        }
        item.setItemMeta(meta);
    }
    // -->
    if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
        ItemMeta meta = item.getItemMeta();
        ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
        for (String toRemove : new ArrayList<>(inputList)) {
            if (new ElementTag(toRemove).matchesEnum(org.bukkit.attribute.Attribute.class)) {
                inputList.remove(toRemove);
                org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(toRemove.toUpperCase());
                meta.removeAttributeModifier(attr);
            }
        }
        for (String toRemove : inputList) {
            UUID id = UUID.fromString(toRemove);
            Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = meta.getAttributeModifiers();
            for (org.bukkit.attribute.Attribute attribute : metaMap.keys()) {
                for (AttributeModifier modifer : metaMap.get(attribute)) {
                    if (modifer.getUniqueId().equals(id)) {
                        meta.removeAttributeModifier(attribute, modifer);
                        break;
                    }
                }
            }
        }
        item.setItemMeta(meta);
    }
}
Also used : Attribute(com.denizenscript.denizencore.tags.Attribute) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 7 with AttributeModifier

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

the class ItemAttributeModifiers method getAttributeModifiers.

public MapTag getAttributeModifiers() {
    ItemMeta meta = item.getItemMeta();
    if (meta == null) {
        return null;
    }
    MapTag map = new MapTag();
    Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = meta.getAttributeModifiers();
    if (metaMap == null) {
        return map;
    }
    for (org.bukkit.attribute.Attribute attribute : metaMap.keys()) {
        Collection<AttributeModifier> modifiers = metaMap.get(attribute);
        if (modifiers.isEmpty()) {
            continue;
        }
        ListTag subList = new ListTag();
        for (AttributeModifier modifier : modifiers) {
            subList.addObject(EntityAttributeModifiers.mapify(modifier));
        }
        map.putObject(attribute.name(), subList);
    }
    return map;
}
Also used : Attribute(com.denizenscript.denizencore.tags.Attribute) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 8 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project Arcade2 by ShootGame.

the class ItemAttributeModifierParser method parseNode.

@Override
protected ParserResult<ItemAttributeModifier> parseNode(Node node, String name, String value) throws ParserException {
    EquipmentSlot slot = this.slotParser.parse(node.property("slot", "equipment-slot", "equipmentslot")).orDefaultNull();
    AttributeModifier modifier = this.modifierParser.parseWithDefinition(node, name, value).orFail();
    return ParserResult.fine(node, name, value, new ItemAttributeModifier(slot, modifier));
}
Also used : EquipmentSlot(org.bukkit.inventory.EquipmentSlot) ItemAttributeModifier(org.bukkit.attribute.ItemAttributeModifier) AttributeModifier(org.bukkit.attribute.AttributeModifier) ItemAttributeModifier(org.bukkit.attribute.ItemAttributeModifier)

Example 9 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project NoCheatPlus by NoCheatPlus.

the class BukkitAttributeAccess method getSprintAttributeMultiplier.

@Override
public double getSprintAttributeMultiplier(final Player player) {
    final AttributeInstance attrInst = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
    final AttributeModifier mod = getModifier(attrInst, AttribUtil.ID_SPRINT_BOOST);
    return mod == null ? 1.0 : getMultiplier(mod);
}
Also used : AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 10 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project NoCheatPlus by NoCheatPlus.

the class BukkitAttributeAccess method getSpeedAttributeMultiplier.

@Override
public double getSpeedAttributeMultiplier(final Player player) {
    final AttributeInstance attrInst = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
    final double val = attrInst.getValue() / attrInst.getBaseValue();
    final AttributeModifier mod = getModifier(attrInst, AttribUtil.ID_SPRINT_BOOST);
    return mod == null ? val : (val / getMultiplier(mod));
}
Also used : AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Aggregations

AttributeModifier (org.bukkit.attribute.AttributeModifier)13 UUID (java.util.UUID)6 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ArrayList (java.util.ArrayList)4 AttributeInstance (org.bukkit.attribute.AttributeInstance)4 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)3 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 Map (java.util.Map)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 Attribute (com.denizenscript.denizencore.tags.Attribute)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 AttributeManager (net.glowstone.entity.AttributeManager)2 Property (net.glowstone.entity.AttributeManager.Property)2 GlowLeashHitch (net.glowstone.entity.objects.GlowLeashHitch)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Location (org.bukkit.Location)2 Attribute (org.bukkit.attribute.Attribute)2 LeashHitch (org.bukkit.entity.LeashHitch)2 EntityEquipment (org.bukkit.inventory.EntityEquipment)2 EquipmentSlot (org.bukkit.inventory.EquipmentSlot)2