Search in sources :

Example 1 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemArmorStand method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("armor_stand_data") && mechanism.requireObject(MapTag.class)) {
        MapTag map = mechanism.valueAsType(MapTag.class);
        ArmorStandMeta meta = (ArmorStandMeta) item.getItemMeta();
        ObjectTag base_plate = map.getObject("base_plate");
        ObjectTag visible = map.getObject("visible");
        ObjectTag marker = map.getObject("marker");
        ObjectTag is_small = map.getObject("is_small");
        ObjectTag arms = map.getObject("arms");
        if (base_plate != null) {
            meta.setNoBasePlate(!((ElementTag) base_plate).asBoolean());
        }
        if (visible != null) {
            meta.setInvisible(!((ElementTag) visible).asBoolean());
        }
        if (marker != null) {
            meta.setMarker(((ElementTag) marker).asBoolean());
        }
        if (is_small != null) {
            meta.setSmall(((ElementTag) is_small).asBoolean());
        }
        if (arms != null) {
            meta.setShowArms(((ElementTag) arms).asBoolean());
        }
        item.setItemMeta(meta);
    }
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ArmorStandMeta(com.destroystokyo.paper.inventory.meta.ArmorStandMeta) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 2 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class PlayerPreparesEnchantScriptEvent method applyDetermination.

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
    if (determinationObj instanceof ElementTag) {
        String determineLow = CoreUtilities.toLowerCase(determinationObj.toString());
        if (determineLow.startsWith("offers:")) {
            ListTag offers = ListTag.valueOf(determineLow.substring("offers:".length()), getTagContext(path));
            if (offers.size() != event.getOffers().length) {
                Debug.echoError("Offer list size incorrect.");
                return false;
            }
            for (int i = 0; i < offers.size(); i++) {
                MapTag map = MapTag.getMapFor(offers.getObject(i), getTagContext(path));
                event.getOffers()[i].setCost(map.getObject("cost").asElement().asInt());
                ObjectTag enchantment = map.getObject("enchantment_type");
                if (enchantment == null) {
                    enchantment = map.getObject("enchantment");
                }
                event.getOffers()[i].setEnchantment(enchantment.asType(EnchantmentTag.class, getTagContext(path)).enchantment);
                event.getOffers()[i].setEnchantmentLevel(map.getObject("level").asElement().asInt());
            }
            return true;
        }
    }
    return super.applyDetermination(path, determinationObj);
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 3 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag 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 4 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag 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)

Example 5 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class BukkitListProperties method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <ListTag.formatted>
    // @returns ElementTag
    // @description
    // Returns the list in a human-readable format.
    // Note that this will parse the values within the list to be human-readable as well when possible.
    // EG, a list of "<npc>|<player>|potato" will return "GuardNPC, bob, and potato".
    // The exact formatting rules that will be followed are not guaranteed, other than that it will be a semi-clean human-readable format.
    // -->
    PropertyParser.<BukkitListProperties, ElementTag>registerTag(ElementTag.class, "formatted", (attribute, listObj) -> {
        ListTag list = listObj.list;
        if (list.isEmpty()) {
            return new ElementTag("");
        }
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            ObjectTag object = list.getObject(i);
            String val = object.toString();
            boolean handled = false;
            if (val.startsWith("p@")) {
                PlayerTag gotten = object.asType(PlayerTag.class, attribute.context);
                if (gotten != null) {
                    output.append(gotten.getName());
                    handled = true;
                }
            }
            if (val.startsWith("e@") || val.startsWith("n@")) {
                EntityTag gotten = object.asType(EntityTag.class, attribute.context);
                if (gotten != null) {
                    if (gotten.isValid()) {
                        output.append(gotten.getName());
                    } else {
                        output.append(gotten.getEntityType().getName());
                    }
                    handled = true;
                }
            }
            if (val.startsWith("i@")) {
                ItemTag item = object.asType(ItemTag.class, attribute.context);
                if (item != null) {
                    output.append(item.formattedName());
                    handled = true;
                }
            }
            if (val.startsWith("m@")) {
                MaterialTag material = object.asType(MaterialTag.class, attribute.context);
                if (material != null) {
                    output.append(material.name());
                    handled = true;
                }
            }
            if (!handled) {
                if (object instanceof ElementTag) {
                    output.append(val.replaceAll("\\w+@", ""));
                } else {
                    output.append(ChatColor.stripColor(Debug.cleanTextForDebugOutput(object.debuggable())));
                }
            }
            if (i == list.size() - 2) {
                output.append(i == 0 ? " and " : ", and ");
            } else {
                output.append(", ");
            }
        }
        return new ElementTag(output.toString().substring(0, output.length() - 2));
    });
    // <--[tag]
    // @attribute <ListTag.to_polygon>
    // @returns PolygonTag
    // @description
    // Converts a list of locations to a PolygonTag.
    // The Y-Min and Y-Max values will be assigned based the range of Y values in the locations given.
    // -->
    PropertyParser.<BukkitListProperties, PolygonTag>registerTag(PolygonTag.class, "to_polygon", (attribute, listObj) -> {
        List<LocationTag> locations = listObj.list.filter(LocationTag.class, attribute.context);
        if (locations == null || locations.isEmpty()) {
            return null;
        }
        if (locations.size() > Settings.blockTagsMaxBlocks()) {
            return null;
        }
        PolygonTag polygon = new PolygonTag(new WorldTag(locations.get(0).getWorldName()));
        polygon.yMin = locations.get(0).getY();
        polygon.yMax = polygon.yMin;
        for (LocationTag location : locations) {
            polygon.yMin = Math.min(polygon.yMin, location.getY());
            polygon.yMax = Math.max(polygon.yMax, location.getY());
            polygon.corners.add(new PolygonTag.Corner(location.getX(), location.getZ()));
        }
        polygon.recalculateBox();
        return polygon;
    });
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Aggregations

ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)38 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)21 MapTag (com.denizenscript.denizencore.objects.core.MapTag)19 ListTag (com.denizenscript.denizencore.objects.core.ListTag)16 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)7 ItemTag (com.denizenscript.denizen.objects.ItemTag)6 ArrayList (java.util.ArrayList)6 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 UUID (java.util.UUID)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)3 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)3 AttributeModifier (org.bukkit.attribute.AttributeModifier)3 Player (org.bukkit.entity.Player)3 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2