Search in sources :

Example 16 with ListTag

use of com.denizenscript.denizencore.objects.core.ListTag 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 17 with ListTag

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

the class PluginTag method registerTags.

public static void registerTags() {
    AbstractFlagTracker.registerFlagHandlers(tagProcessor);
    // <--[tag]
    // @attribute <PluginTag.name>
    // @returns ElementTag
    // @description
    // Gets the name of this plugin.
    // -->
    tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
        return new ElementTag(object.plugin.getName());
    });
    // <--[tag]
    // @attribute <PluginTag.version>
    // @returns ElementTag
    // @description
    // Gets the version for the plugin specified.
    // -->
    tagProcessor.registerTag(ElementTag.class, "version", (attribute, object) -> {
        return new ElementTag(object.plugin.getDescription().getVersion());
    });
    // <--[tag]
    // @attribute <PluginTag.description>
    // @returns ElementTag
    // @description
    // Gets the description for the plugin specified.
    // -->
    tagProcessor.registerTag(ElementTag.class, "description", (attribute, object) -> {
        return new ElementTag(object.plugin.getDescription().getDescription());
    });
    // <--[tag]
    // @attribute <PluginTag.authors>
    // @returns ListTag
    // @description
    // Gets the list of authors for the plugin specified.
    // -->
    tagProcessor.registerTag(ListTag.class, "authors", (attribute, object) -> {
        return new ListTag(object.plugin.getDescription().getAuthors());
    });
    // <--[tag]
    // @attribute <PluginTag.depends>
    // @returns ListTag
    // @description
    // Gets the list of hard dependencies for the plugin specified.
    // -->
    tagProcessor.registerTag(ListTag.class, "depends", (attribute, object) -> {
        return new ListTag(object.plugin.getDescription().getDepend());
    });
    // <--[tag]
    // @attribute <PluginTag.soft_depends>
    // @returns ListTag
    // @description
    // Gets the list of soft dependencies for the plugin specified.
    // -->
    tagProcessor.registerTag(ListTag.class, "soft_depends", (attribute, object) -> {
        return new ListTag(object.plugin.getDescription().getSoftDepend());
    });
    // <--[tag]
    // @attribute <PluginTag.commands>
    // @returns MapTag(MapTag)
    // @description
    // Gets a map of commands registered this plugin registers by default.
    // Note that dynamically registered commands won't show up (for example, command scripts won't be listed under Denizen).
    // Map key is command name, map value is a sub-mapping with keys:
    // description (ElementTag), usage (ElementTag), permission (ElementTag), aliases (ListTag)
    // Not all keys will be present.
    // For example, <plugin[denizen].commands.get[ex]> will return a MapTag with:
    // [description=Executes a Denizen script command.;usage=/ex (-q) <Denizen script command> (arguments);permission=denizen.ex]
    // -->
    tagProcessor.registerTag(MapTag.class, "commands", (attribute, object) -> {
        Map<String, Map<String, Object>> commands = object.plugin.getDescription().getCommands();
        MapTag output = new MapTag();
        if (commands == null || commands.isEmpty()) {
            return output;
        }
        for (Map.Entry<String, Map<String, Object>> command : commands.entrySet()) {
            MapTag dataMap = new MapTag();
            if (command.getValue().containsKey("description")) {
                dataMap.putObject("description", new ElementTag(command.getValue().get("description").toString(), true));
            }
            if (command.getValue().containsKey("usage")) {
                dataMap.putObject("usage", new ElementTag(command.getValue().get("usage").toString(), true));
            }
            if (command.getValue().containsKey("permission")) {
                dataMap.putObject("permission", new ElementTag(command.getValue().get("permission").toString(), true));
            }
            if (command.getValue().containsKey("aliases")) {
                Object obj = command.getValue().get("aliases");
                if (obj instanceof List) {
                    ListTag aliases = new ListTag();
                    for (Object entry : (List) obj) {
                        aliases.addObject(new ElementTag(String.valueOf(entry), true));
                    }
                    dataMap.putObject("aliases", aliases);
                }
            }
            output.putObject(command.getKey(), dataMap);
        }
        return output;
    });
}
Also used : FlaggableObject(com.denizenscript.denizencore.flags.FlaggableObject) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Map(java.util.Map) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 18 with ListTag

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

Example 19 with ListTag

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

the class PolygonTag method registerTags.

public static void registerTags() {
    AbstractFlagTracker.registerFlagHandlers(tagProcessor);
    AreaContainmentObject.registerTags(PolygonTag.class, tagProcessor);
    // <--[tag]
    // @attribute <PolygonTag.max_y>
    // @returns ElementTag(Decimal)
    // @description
    // Returns the maximum Y level for this polygon.
    // -->
    tagProcessor.registerTag(ElementTag.class, "max_y", (attribute, polygon) -> {
        return new ElementTag(polygon.yMax);
    });
    // <--[tag]
    // @attribute <PolygonTag.min_y>
    // @returns ElementTag(Decimal)
    // @description
    // Returns the minimum Y level for this polygon.
    // -->
    tagProcessor.registerTag(ElementTag.class, "min_y", (attribute, polygon) -> {
        return new ElementTag(polygon.yMin);
    });
    // <--[tag]
    // @attribute <PolygonTag.note_name>
    // @returns ElementTag
    // @description
    // Gets the name of a noted PolygonTag. If the polygon isn't noted, this is null.
    // -->
    tagProcessor.registerTag(ElementTag.class, "note_name", (attribute, polygon) -> {
        String noteName = NoteManager.getSavedId(polygon);
        if (noteName == null) {
            return null;
        }
        return new ElementTag(noteName);
    });
    // <--[tag]
    // @attribute <PolygonTag.corners>
    // @returns ListTag(LocationTag)
    // @description
    // Returns a list of the polygon's corners, as locations with Y coordinate set to the y-min value.
    // -->
    tagProcessor.registerTag(ListTag.class, "corners", (attribute, polygon) -> {
        ListTag list = new ListTag();
        for (Corner corner : polygon.corners) {
            list.addObject(new LocationTag(corner.x, polygon.yMin, corner.z, polygon.world.getName()));
        }
        return list;
    });
    // <--[tag]
    // @attribute <PolygonTag.shift[<vector>]>
    // @returns PolygonTag
    // @description
    // Returns a copy of the polygon, with all coordinates shifted by the given location-vector.
    // -->
    tagProcessor.registerTag(PolygonTag.class, "shift", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.shift[...] tag must have an input.");
            return null;
        }
        LocationTag shift = attribute.paramAsType(LocationTag.class);
        PolygonTag toReturn = polygon.clone();
        toReturn.yMin += shift.getY();
        toReturn.yMax += shift.getY();
        for (Corner corner : toReturn.corners) {
            corner.x += shift.getX();
            corner.z += shift.getZ();
        }
        toReturn.boxMin.x += shift.getX();
        toReturn.boxMin.z += shift.getZ();
        toReturn.boxMax.x += shift.getX();
        toReturn.boxMax.z += shift.getZ();
        return toReturn;
    });
    // <--[tag]
    // @attribute <PolygonTag.with_corner[<location>]>
    // @returns PolygonTag
    // @mechanism PolygonTag.add_corner
    // @description
    // Returns a copy of the polygon, with the specified corner added to the end of the corner list.
    // -->
    tagProcessor.registerTag(PolygonTag.class, "with_corner", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.with_corner[...] tag must have an input.");
            return null;
        }
        LocationTag corner = attribute.paramAsType(LocationTag.class);
        PolygonTag toReturn = polygon.clone();
        Corner added = new Corner(corner.getX(), corner.getZ());
        toReturn.corners.add(added);
        toReturn.recalculateToFit(added);
        return toReturn;
    });
    // <--[tag]
    // @attribute <PolygonTag.with_y_min[<#.#>]>
    // @returns PolygonTag
    // @description
    // Returns a copy of the polygon, with the specified minimum-Y value.
    // -->
    tagProcessor.registerTag(PolygonTag.class, "with_y_min", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.with_y_min[...] tag must have an input.");
            return null;
        }
        PolygonTag toReturn = polygon.clone();
        toReturn.yMin = attribute.getDoubleParam();
        return toReturn;
    });
    // <--[tag]
    // @attribute <PolygonTag.with_y_max[<#.#>]>
    // @returns PolygonTag
    // @description
    // Returns a copy of the polygon, with the specified maximum-Y value.
    // -->
    tagProcessor.registerTag(PolygonTag.class, "with_y_max", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.with_y_max[...] tag must have an input.");
            return null;
        }
        PolygonTag toReturn = polygon.clone();
        toReturn.yMax = attribute.getDoubleParam();
        return toReturn;
    });
    // <--[tag]
    // @attribute <PolygonTag.include_y[<#.#>]>
    // @returns PolygonTag
    // @description
    // Returns a copy of the polygon, with the specified Y value included as part of the Y range (expanding the Y-min or Y-max as needed).
    // -->
    tagProcessor.registerTag(PolygonTag.class, "include_y", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.include_y[...] tag must have an input.");
            return null;
        }
        PolygonTag toReturn = polygon.clone();
        double y = attribute.getDoubleParam();
        toReturn.yMin = Math.min(y, toReturn.yMin);
        toReturn.yMax = Math.max(y, toReturn.yMax);
        return toReturn;
    });
    // <--[tag]
    // @attribute <PolygonTag.outline_2d[<#.#>]>
    // @returns ListTag(LocationTag)
    // @description
    // Returns a list of locations along the 2D outline of this polygon, at the specified Y level (roughly a block-width of separation between each).
    // -->
    tagProcessor.registerTag(ListTag.class, "outline_2d", (attribute, polygon) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("PolygonTag.outline_2d[...] tag must have an input.");
            return null;
        }
        double y = attribute.getDoubleParam();
        ListTag output = new ListTag();
        polygon.addOutline2D(y, output);
        return output;
    });
    // <--[tag]
    // @attribute <PolygonTag.outline>
    // @returns ListTag(LocationTag)
    // @description
    // Returns a list of locations along the 3D outline of this polygon (roughly a block-width of separation between each).
    // -->
    tagProcessor.registerTag(ListTag.class, "outline", (attribute, polygon) -> {
        return polygon.getOutline();
    });
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 20 with ListTag

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

the class PolygonTag method getShell.

@Override
public ListTag getShell() {
    int max = Settings.blockTagsMaxBlocks();
    ListTag addTo = new ListTag();
    List<LocationTag> flatShell = generateFlatBlockShell(yMin);
    for (LocationTag loc : flatShell) {
        addTo.addObject(loc.clone());
    }
    max -= flatShell.size();
    if (max <= 0) {
        return addTo;
    }
    for (LocationTag loc : flatShell) {
        LocationTag newLoc = loc.clone();
        newLoc.setY(yMax);
        addTo.addObject(newLoc);
    }
    max -= flatShell.size();
    if (max <= 0) {
        return addTo;
    }
    int per = (int) (yMax - yMin);
    if (per == 0) {
        return addTo;
    }
    for (int i = 0; i < corners.size(); i++) {
        Corner start = corners.get(i);
        Corner end = (i + 1 == corners.size() ? corners.get(0) : corners.get(i + 1));
        double xMove = (end.x - start.x);
        double zMove = (end.z - start.z);
        double len = Math.sqrt(xMove * xMove + zMove * zMove);
        if (len < 0.1) {
            continue;
        }
        xMove /= len;
        zMove /= len;
        double xSpot = start.x;
        double zSpot = start.z;
        max -= (int) (len + 1);
        if (max <= 0) {
            return addTo;
        }
        for (int j = 0; j < len; j++) {
            for (double y = yMin + 1; y < yMax; y++) {
                addTo.addObject(new LocationTag(xSpot, y, zSpot, world.getName()));
            }
            max -= per;
            if (max <= 0) {
                return addTo;
            }
            xSpot += xMove;
            zSpot += zMove;
        }
    }
    return addTo;
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

ListTag (com.denizenscript.denizencore.objects.core.ListTag)122 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)71 MapTag (com.denizenscript.denizencore.objects.core.MapTag)21 ItemStack (org.bukkit.inventory.ItemStack)18 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)14 List (java.util.List)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)13 Player (org.bukkit.entity.Player)13 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)12 LocationTag (com.denizenscript.denizen.objects.LocationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)9 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)9 ArrayList (java.util.ArrayList)9 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)7 NPC (net.citizensnpcs.api.npc.NPC)7 Entity (org.bukkit.entity.Entity)7