Search in sources :

Example 1 with AttributeModifier

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

the class AttributeModifierParser method parsePrimitive.

@Override
protected ParserResult<AttributeModifier> parsePrimitive(Node node, String name, String value) throws ParserException {
    AttributeModifier.Operation operation = this.operationParser.parse(node.property("operation")).orDefault(AttributeModifier.Operation.ADD_NUMBER);
    double amount = this.amountParser.parseWithDefinition(node, name, value).orFail();
    UUID uniqueId = this.fastUUID.next();
    return ParserResult.fine(node, name, value, new AttributeModifier(uniqueId, this.computeName(uniqueId, operation, amount), amount, operation));
}
Also used : AttributeModifier(org.bukkit.attribute.AttributeModifier) UUID(java.util.UUID) FastUUID(pl.themolka.arcade.util.FastUUID)

Example 2 with AttributeModifier

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

the class BoundedModifierParser method parseNode.

@Override
protected ParserResult<BoundedModifier> parseNode(Node node, String name, String value) throws ParserException {
    AttributeKey key = this.keyParser.parse(node.property("attribute", "attribute-key", "attributekey", "attr", "key")).orFail();
    AttributeModifier modifier = this.modifierParser.parseWithDefinition(node, name, value).orFail();
    return ParserResult.fine(node, name, value, new BoundedModifier(key, modifier));
}
Also used : AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 3 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project Glowstone by GlowstoneMC.

the class EntityPropertyCodec method encode.

@Override
public ByteBuf encode(ByteBuf buf, EntityPropertyMessage message) throws IOException {
    ByteBufUtils.writeVarInt(buf, message.getId());
    Map<String, Property> props = message.getProperties();
    buf.writeInt(props.size());
    for (Entry<String, Property> property : props.entrySet()) {
        ByteBufUtils.writeUTF8(buf, property.getKey());
        buf.writeDouble(property.getValue().getValue());
        Collection<AttributeModifier> modifiers = property.getValue().getModifiers();
        if (modifiers == null) {
            ByteBufUtils.writeVarInt(buf, 0);
        } else {
            ByteBufUtils.writeVarInt(buf, modifiers.size());
            for (AttributeModifier modifier : modifiers) {
                GlowBufUtils.writeUuid(buf, modifier.getUniqueId());
                buf.writeDouble(modifier.getAmount());
                buf.writeByte(modifier.getOperation().ordinal());
            }
        }
    }
    return buf;
}
Also used : AttributeModifier(org.bukkit.attribute.AttributeModifier) Property(net.glowstone.entity.AttributeManager.Property)

Example 4 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier 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 5 with AttributeModifier

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

the class EntityAttributeModifiers method modiferForMap.

public static AttributeModifier modiferForMap(Attribute attr, MapTag map) {
    ObjectTag name = map.getObject("name");
    ObjectTag amount = map.getObject("amount");
    ObjectTag operation = map.getObject("operation");
    ObjectTag slot = map.getObject("slot");
    ObjectTag id = map.getObject("id");
    AttributeModifier.Operation operationValue;
    EquipmentSlot slotValue;
    UUID idValue;
    double amountValue;
    try {
        operationValue = AttributeModifier.Operation.valueOf(operation.toString().toUpperCase());
    } catch (IllegalArgumentException ex) {
        Debug.echoError("Attribute modifier operation '" + operation + "' does not exist.");
        return null;
    }
    try {
        idValue = id == null ? UUID.randomUUID() : UUID.fromString(id.toString());
    } catch (IllegalArgumentException ex) {
        Debug.echoError("Attribute modifier ID '" + id + "' is not a valid UUID.");
        return null;
    }
    try {
        slotValue = slot == null || CoreUtilities.equalsIgnoreCase(slot.toString(), "any") ? null : EquipmentSlot.valueOf(slot.toString().toUpperCase());
    } catch (IllegalArgumentException ex) {
        Debug.echoError("Attribute modifier slot '" + slot + "' is not a valid slot.");
        return null;
    }
    try {
        amountValue = Double.parseDouble(amount.toString());
    } catch (NumberFormatException ex) {
        Debug.echoError("Attribute modifier amount '" + amount + "' is not a valid decimal number.");
        return null;
    }
    return new AttributeModifier(idValue, name == null ? attr.name() : name.toString(), amountValue, operationValue, slotValue);
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) AttributeModifier(org.bukkit.attribute.AttributeModifier) UUID(java.util.UUID)

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