Search in sources :

Example 21 with ElementTag

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

the class PlayerReceivesMessageScriptEvent method applyDetermination.

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
    if (determinationObj instanceof ElementTag) {
        String determination = determinationObj.toString();
        String lower = CoreUtilities.toLowerCase(determination);
        if (lower.startsWith("message:")) {
            message = new ElementTag(determination.substring("message:".length()));
            modifyMessage.accept(message.asString());
            return true;
        }
        if (lower.startsWith("raw_json:")) {
            rawJson = new ElementTag(determination.substring("raw_json:".length()));
            modifyRawJson.accept(rawJson.asString());
            return true;
        }
    }
    return super.applyDetermination(path, determinationObj);
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 22 with ElementTag

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

the class PlayerFishesScriptEvent method onPlayerFishes.

@EventHandler
public void onPlayerFishes(PlayerFishEvent event) {
    if (EntityTag.isNPC(event.getPlayer())) {
        return;
    }
    Entity hookEntity = NMSHandler.getEntityHelper().getFishHook(event);
    EntityTag.rememberEntity(hookEntity);
    hook = new EntityTag(hookEntity);
    state = new ElementTag(event.getState().toString());
    item = null;
    entity = null;
    Entity caughtEntity = event.getCaught();
    if (caughtEntity != null) {
        EntityTag.rememberEntity(caughtEntity);
        entity = new EntityTag(caughtEntity);
        if (caughtEntity instanceof Item) {
            item = new ItemTag(((Item) caughtEntity).getItemStack());
        }
    }
    this.event = event;
    fire(event);
    EntityTag.forgetEntity(hookEntity);
    EntityTag.forgetEntity(caughtEntity);
}
Also used : Entity(org.bukkit.entity.Entity) Item(org.bukkit.entity.Item) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) EventHandler(org.bukkit.event.EventHandler)

Example 23 with ElementTag

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

the class LootGenerateScriptEvent method applyDetermination.

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
    if (determinationObj instanceof ElementTag) {
        String determination = determinationObj.toString();
        String determinationLower = CoreUtilities.toLowerCase(determination);
        if (determinationLower.startsWith("loot:")) {
            ListTag list = ListTag.valueOf(determination.substring("loot:".length()), getTagContext(path));
            ArrayList<ItemStack> newLoot = new ArrayList<>(list.size());
            for (ItemTag item : list.filter(ItemTag.class, getTagContext(path))) {
                newLoot.add(item.getItemStack());
            }
            event.setLoot(newLoot);
            return true;
        }
    }
    return super.applyDetermination(path, determinationObj);
}
Also used : ArrayList(java.util.ArrayList) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 24 with ElementTag

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

the class AreaContainmentObject method registerTags.

static <T extends AreaContainmentObject> void registerTags(Class<T> type, ObjectTagProcessor<T> processor) {
    // <--[tag]
    // @attribute <AreaObject.bounding_box>
    // @returns CuboidTag
    // @description
    // Returns a cuboid approximately representing the maximal bounding box of the area (anything this cuboid does not contain, is also not contained by the area, but not vice versa).
    // For single-member CuboidTags, this tag returns a copy of the cuboid.
    // -->
    processor.registerTag(CuboidTag.class, "bounding_box", (attribute, area) -> {
        return area.getCuboidBoundary();
    });
    // <--[tag]
    // @attribute <AreaObject.world>
    // @returns WorldTag
    // @description
    // Returns the area's world.
    // -->
    processor.registerTag(WorldTag.class, "world", (attribute, area) -> {
        return area.getWorld();
    });
    // <--[tag]
    // @attribute <AreaObject.players>
    // @returns ListTag(PlayerTag)
    // @description
    // Gets a list of all players currently within the area.
    // -->
    processor.registerTag(ListTag.class, "players", (attribute, area) -> {
        ListTag result = new ListTag();
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (area.doesContainLocation(player.getLocation())) {
                result.addObject(PlayerTag.mirrorBukkitPlayer(player));
            }
        }
        return result;
    });
    // -->
    if (Depends.citizens != null) {
        processor.registerTag(ListTag.class, "npcs", (attribute, area) -> {
            ListTag result = new ListTag();
            for (NPC npc : CitizensAPI.getNPCRegistry()) {
                NPCTag dnpc = new NPCTag(npc);
                if (area.doesContainLocation(dnpc.getLocation())) {
                    result.addObject(dnpc);
                }
            }
            return result;
        });
    }
    // <--[tag]
    // @attribute <AreaObject.entities[(<matcher>)]>
    // @returns ListTag(EntityTag)
    // @description
    // Gets a list of all entities currently within the area, with an optional search parameter for the entity.
    // -->
    processor.registerTag(ListTag.class, "entities", (attribute, area) -> {
        String matcher = attribute.hasParam() ? attribute.getParam() : null;
        ListTag entities = new ListTag();
        for (Entity ent : area.getCuboidBoundary().getEntitiesPossiblyWithinForTag()) {
            if (area.doesContainLocation(ent.getLocation())) {
                EntityTag current = new EntityTag(ent);
                if (matcher == null || BukkitScriptEvent.tryEntity(current, matcher)) {
                    entities.addObject(current.getDenizenObject());
                }
            }
        }
        return entities;
    });
    // <--[tag]
    // @attribute <AreaObject.living_entities>
    // @returns ListTag(EntityTag)
    // @description
    // Gets a list of all living entities currently within the area.
    // This includes Players, mobs, NPCs, etc., but excludes dropped items, experience orbs, etc.
    // -->
    processor.registerTag(ListTag.class, "living_entities", (attribute, area) -> {
        ListTag result = new ListTag();
        for (Entity ent : area.getCuboidBoundary().getEntitiesPossiblyWithinForTag()) {
            if (ent instanceof LivingEntity && area.doesContainLocation(ent.getLocation()) && !EntityTag.isCitizensNPC(ent)) {
                result.addObject(new EntityTag(ent).getDenizenObject());
            }
        }
        return result;
    });
    // <--[tag]
    // @attribute <AreaObject.contains[<location>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns a boolean indicating whether the specified location is inside this area.
    // -->
    processor.registerTag(ElementTag.class, "contains", (attribute, area) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        LocationTag loc = attribute.paramAsType(LocationTag.class);
        if (loc == null) {
            return null;
        }
        return new ElementTag(area.doesContainLocation(loc));
    }, "contains_location");
    // <--[tag]
    // @attribute <AreaObject.blocks[(<matcher>)]>
    // @returns ListTag(LocationTag)
    // @description
    // Returns each block location within the area.
    // Optionally, specify a material match to only return locations with that block type.
    // -->
    processor.registerTag(ListTag.class, "blocks", (attribute, area) -> {
        if (attribute.hasParam()) {
            NMSHandler.getChunkHelper().changeChunkServerThread(area.getWorld().getWorld());
            try {
                String matcher = attribute.getParam();
                Predicate<Location> predicate = (l) -> BukkitScriptEvent.tryMaterial(l.getBlock().getType(), matcher);
                return area.getBlocks(predicate);
            } finally {
                NMSHandler.getChunkHelper().restoreServerThread(area.getWorld().getWorld());
            }
        }
        return area.getBlocks(null);
    }, "get_blocks");
    // <--[tag]
    // @attribute <AreaObject.spawnable_blocks[(<matcher>)]>
    // @returns ListTag(LocationTag)
    // @description
    // Returns each LocationTag within the area that is safe for players or similar entities to spawn in.
    // Optionally, specify a material matcher to only return locations with that block type.
    // Uses the same spawnable check as <@link tag LocationTag.is_spawnable>
    // -->
    processor.registerTag(ListTag.class, "spawnable_blocks", (attribute, area) -> {
        NMSHandler.getChunkHelper().changeChunkServerThread(area.getWorld().getWorld());
        try {
            if (attribute.hasParam()) {
                String matcher = attribute.getParam();
                Predicate<Location> predicate = (l) -> SpawnableHelper.isSpawnable(l) && BukkitScriptEvent.tryMaterial(l.getBlock().getRelative(0, -1, 0).getType(), matcher);
                return area.getBlocks(predicate);
            }
            return area.getBlocks(SpawnableHelper::isSpawnable);
        } finally {
            NMSHandler.getChunkHelper().restoreServerThread(area.getWorld().getWorld());
        }
    }, "get_spawnable_blocks");
    // <--[tag]
    // @attribute <AreaObject.blocks_flagged[<flag_name>]>
    // @returns ListTag(LocationTag)
    // @description
    // Gets a list of all block locations with a specified flag within the area.
    // Searches the internal flag lists, rather than through all possible blocks.
    // -->
    processor.registerTag(ListTag.class, "blocks_flagged", (attribute, area) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        return area.getBlocksFlagged(CoreUtilities.toLowerCase(attribute.getParam()), attribute);
    });
    // <--[tag]
    // @attribute <AreaObject.shell>
    // @returns ListTag(LocationTag)
    // @description
    // Returns each block location on the 3D outer shell of the area.
    // This tag is useful for displaying particles or blocks to mark the boundary of the area.
    // -->
    processor.registerTag(ListTag.class, "shell", (attribute, area) -> {
        return area.getShell();
    });
    // <--[tag]
    // @attribute <AreaObject.is_within[<cuboid>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether this area is fully inside another cuboid.
    // -->
    processor.registerTag(ElementTag.class, "is_within", (attribute, area) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        CuboidTag cub2 = attribute.paramAsType(CuboidTag.class);
        if (cub2 == null) {
            return null;
        }
        CuboidTag cuboid = area instanceof CuboidTag ? (CuboidTag) area : area.getCuboidBoundary();
        if (cub2 != null) {
            boolean contains = true;
            for (CuboidTag.LocationPair pair2 : cuboid.pairs) {
                boolean contained = false;
                for (CuboidTag.LocationPair pair : cub2.pairs) {
                    if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
                        return new ElementTag(false);
                    }
                    if (pair2.low.getX() >= pair.low.getX() && pair2.low.getY() >= pair.low.getY() && pair2.low.getZ() >= pair.low.getZ() && pair2.high.getX() <= pair.high.getX() && pair2.high.getY() <= pair.high.getY() && pair2.high.getZ() <= pair.high.getZ()) {
                        contained = true;
                        break;
                    }
                }
                if (!contained) {
                    contains = false;
                    break;
                }
            }
            return new ElementTag(contains);
        }
        return null;
    });
    // <--[tag]
    // @attribute <AreaObject.with_world[<world>]>
    // @returns AreaObject
    // @description
    // Returns a copy of the area, with the specified world.
    // -->
    processor.registerTag(type, "with_world", (attribute, area) -> {
        if (!attribute.hasParam()) {
            return null;
        }
        WorldTag world = attribute.paramAsType(WorldTag.class);
        if (world == null) {
            return null;
        }
        return (T) area.withWorld(world);
    });
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTagProcessor(com.denizenscript.denizencore.tags.ObjectTagProcessor) Entity(org.bukkit.entity.Entity) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) Predicate(java.util.function.Predicate) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) SpawnableHelper(com.denizenscript.denizen.utilities.blocks.SpawnableHelper) Depends(com.denizenscript.denizen.utilities.depends.Depends) LocationFlagSearchHelper(com.denizenscript.denizen.utilities.flags.LocationFlagSearchHelper) Player(org.bukkit.entity.Player) LivingEntity(org.bukkit.entity.LivingEntity) BukkitScriptEvent(com.denizenscript.denizen.events.BukkitScriptEvent) CitizensAPI(net.citizensnpcs.api.CitizensAPI) NPC(net.citizensnpcs.api.npc.NPC) Location(org.bukkit.Location) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) Attribute(com.denizenscript.denizencore.tags.Attribute) Bukkit(org.bukkit.Bukkit) SpawnableHelper(com.denizenscript.denizen.utilities.blocks.SpawnableHelper) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ListTag(com.denizenscript.denizencore.objects.core.ListTag) LivingEntity(org.bukkit.entity.LivingEntity) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Location(org.bukkit.Location)

Example 25 with ElementTag

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

the class ChunkTag method registerTags.

public static void registerTags() {
    AbstractFlagTracker.registerFlagHandlers(tagProcessor);
    // <--[tag]
    // @attribute <ChunkTag.add[<#>,<#>]>
    // @returns ChunkTag
    // @description
    // Returns the chunk with the specified coordinates added to it.
    // -->
    tagProcessor.registerTag(ChunkTag.class, "add", (attribute, object) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("The tag ChunkTag.add[<#>,<#>] must have a value.");
            return null;
        }
        List<String> coords = CoreUtilities.split(attribute.getParam(), ',');
        if (coords.size() < 2) {
            attribute.echoError("The tag ChunkTag.add[<#>,<#>] requires two values!");
            return null;
        }
        if (!ArgumentHelper.matchesInteger(coords.get(0)) || !ArgumentHelper.matchesInteger(coords.get(1))) {
            attribute.echoError("Input to ChunkTag.add[x,z] is not a valid integer pair!");
            return null;
        }
        int x = Integer.parseInt(coords.get(0));
        int z = Integer.parseInt(coords.get(1));
        return new ChunkTag(object.world, object.chunkX + x, object.chunkZ + z);
    });
    // <--[tag]
    // @attribute <ChunkTag.sub[<#>,<#>]>
    // @returns ChunkTag
    // @description
    // Returns the chunk with the specified coordinates subtracted from it.
    // -->
    tagProcessor.registerTag(ChunkTag.class, "sub", (attribute, object) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("The tag ChunkTag.add[<#>,<#>] must have a value.");
            return null;
        }
        List<String> coords = CoreUtilities.split(attribute.getParam(), ',');
        if (coords.size() < 2) {
            attribute.echoError("The tag ChunkTag.sub[<#>,<#>] requires two values!");
            return null;
        }
        if (!ArgumentHelper.matchesInteger(coords.get(0)) || !ArgumentHelper.matchesInteger(coords.get(1))) {
            attribute.echoError("Input to ChunkTag.sub[x,z] is not a valid integer pair!");
            return null;
        }
        int x = Integer.parseInt(coords.get(0));
        int z = Integer.parseInt(coords.get(1));
        return new ChunkTag(object.world, object.chunkX - x, object.chunkZ - z);
    });
    // <--[tag]
    // @attribute <ChunkTag.is_generated>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the chunk has already been generated.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_generated", (attribute, object) -> {
        return new ElementTag(object.getBukkitWorld().isChunkGenerated(object.chunkX, object.chunkZ));
    });
    // <--[tag]
    // @attribute <ChunkTag.is_loaded>
    // @returns ElementTag(Boolean)
    // @description
    // Returns true if the chunk is currently loaded into memory.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_loaded", (attribute, object) -> {
        return new ElementTag(object.isLoadedSafe());
    });
    // <--[tag]
    // @attribute <ChunkTag.force_loaded>
    // @returns ElementTag(Boolean)
    // @mechanism ChunkTag.force_loaded
    // @description
    // Returns whether the chunk is forced to stay loaded at all times.
    // -->
    tagProcessor.registerTag(ElementTag.class, "force_loaded", (attribute, object) -> {
        if (!object.isLoadedSafe()) {
            return new ElementTag(false);
        }
        Chunk chunk = object.getChunkForTag(attribute);
        return new ElementTag(chunk != null && chunk.isForceLoaded());
    });
    // <--[tag]
    // @attribute <ChunkTag.plugin_tickets>
    // @returns ListTag(PluginTag)
    // @mechanism ChunkTag.clear_plugin_tickets
    // @description
    // Returns a list of plugins that are keeping this chunk loaded.
    // This is related to the <@link command chunkload> command.
    // -->
    tagProcessor.registerTag(ListTag.class, "plugin_tickets", (attribute, object) -> {
        if (!object.isLoadedSafe()) {
            return new ListTag();
        }
        Chunk chunk = object.getChunkForTag(attribute);
        ListTag result = new ListTag();
        for (Plugin plugin : chunk.getPluginChunkTickets()) {
            result.addObject(new PluginTag(plugin));
        }
        return result;
    });
    // <--[tag]
    // @attribute <ChunkTag.x>
    // @returns ElementTag(Number)
    // @description
    // Returns the x coordinate of the chunk.
    // -->
    tagProcessor.registerTag(ElementTag.class, "x", (attribute, object) -> {
        return new ElementTag(object.chunkX);
    });
    // <--[tag]
    // @attribute <ChunkTag.z>
    // @returns ElementTag(Number)
    // @description
    // Returns the z coordinate of the chunk.
    // -->
    tagProcessor.registerTag(ElementTag.class, "z", (attribute, object) -> {
        return new ElementTag(object.chunkZ);
    });
    // <--[tag]
    // @attribute <ChunkTag.world>
    // @returns WorldTag
    // @description
    // Returns the world associated with the chunk.
    // -->
    tagProcessor.registerTag(WorldTag.class, "world", (attribute, object) -> {
        return object.world;
    });
    // <--[tag]
    // @attribute <ChunkTag.cuboid>
    // @returns CuboidTag
    // @description
    // Returns a cuboid of this chunk.
    // -->
    tagProcessor.registerTag(CuboidTag.class, "cuboid", (attribute, object) -> {
        int yMin = 0, yMax = 255;
        if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
            World world = object.getBukkitWorld();
            if (world != null) {
                yMin = world.getMinHeight();
                yMax = world.getMaxHeight();
            }
        }
        return new CuboidTag(new LocationTag(object.getWorldName(), object.getX() * 16, yMin, object.getZ() * 16, 0, 0), new LocationTag(object.getWorldName(), object.getX() * 16 + 15, yMax, object.getZ() * 16 + 15, 0, 0));
    });
    // <--[tag]
    // @attribute <ChunkTag.tile_entities>
    // @returns ListTag(LocationTag)
    // @description
    // Returns a list of tile entity locations in the chunk.
    // -->
    tagProcessor.registerTag(ListTag.class, "tile_entities", (attribute, object) -> {
        ListTag tiles = new ListTag();
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        try {
            NMSHandler.getChunkHelper().changeChunkServerThread(object.getBukkitWorld());
            for (BlockState block : chunk.getTileEntities()) {
                tiles.addObject(new LocationTag(block.getLocation()));
            }
        } finally {
            NMSHandler.getChunkHelper().restoreServerThread(object.getBukkitWorld());
        }
        return tiles;
    });
    // <--[tag]
    // @attribute <ChunkTag.entities[(<entity>|...)]>
    // @returns ListTag(EntityTag)
    // @description
    // Returns a list of entities in the chunk.
    // Optionally specify entity types to filter down to.
    // -->
    tagProcessor.registerTag(ListTag.class, "entities", (attribute, object) -> {
        ListTag entities = new ListTag();
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        ListTag typeFilter = attribute.hasParam() ? attribute.paramAsType(ListTag.class) : null;
        try {
            NMSHandler.getChunkHelper().changeChunkServerThread(object.getBukkitWorld());
            for (Entity entity : chunk.getEntities()) {
                EntityTag current = new EntityTag(entity);
                if (typeFilter != null) {
                    for (String type : typeFilter) {
                        if (current.comparedTo(type)) {
                            entities.addObject(current.getDenizenObject());
                            break;
                        }
                    }
                } else {
                    entities.addObject(current.getDenizenObject());
                }
            }
        } finally {
            NMSHandler.getChunkHelper().restoreServerThread(object.getBukkitWorld());
        }
        return entities;
    });
    // <--[tag]
    // @attribute <ChunkTag.living_entities>
    // @returns ListTag(EntityTag)
    // @description
    // Returns a list of living entities in the chunk.
    // This includes Players, mobs, NPCs, etc., but excludes dropped items, experience orbs, etc.
    // -->
    tagProcessor.registerTag(ListTag.class, "living_entities", (attribute, object) -> {
        ListTag entities = new ListTag();
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        try {
            NMSHandler.getChunkHelper().changeChunkServerThread(object.getBukkitWorld());
            for (Entity ent : chunk.getEntities()) {
                if (ent instanceof LivingEntity) {
                    entities.addObject(new EntityTag(ent).getDenizenObject());
                }
            }
        } finally {
            NMSHandler.getChunkHelper().restoreServerThread(object.getBukkitWorld());
        }
        return entities;
    });
    // <--[tag]
    // @attribute <ChunkTag.players>
    // @returns ListTag(PlayerTag)
    // @description
    // Returns a list of players in the chunk.
    // -->
    tagProcessor.registerTag(ListTag.class, "players", (attribute, object) -> {
        ListTag entities = new ListTag();
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        for (Entity ent : chunk.getEntities()) {
            if (EntityTag.isPlayer(ent)) {
                entities.addObject(new PlayerTag((Player) ent));
            }
        }
        return entities;
    });
    // <--[tag]
    // @attribute <ChunkTag.height_map>
    // @returns ListTag
    // @description
    // Returns a list of the height of each block in the chunk.
    // -->
    tagProcessor.registerTag(ListTag.class, "height_map", (attribute, object) -> {
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        int[] heightMap = NMSHandler.getChunkHelper().getHeightMap(chunk);
        List<String> height_map = new ArrayList<>(heightMap.length);
        for (int i : heightMap) {
            height_map.add(String.valueOf(i));
        }
        return new ListTag(height_map);
    });
    // <--[tag]
    // @attribute <ChunkTag.average_height>
    // @returns ElementTag(Decimal)
    // @description
    // Returns the average height of the blocks in the chunk.
    // -->
    tagProcessor.registerTag(ElementTag.class, "average_height", (attribute, object) -> {
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        int[] heightMap = NMSHandler.getChunkHelper().getHeightMap(chunk);
        int sum = 0;
        for (int i : heightMap) {
            sum += i;
        }
        return new ElementTag(((double) sum) / heightMap.length);
    });
    // <--[tag]
    // @attribute <ChunkTag.is_flat[(<#>)]>
    // @returns ElementTag(Boolean)
    // @description
    // Scans the heights of the blocks to check variance between them.
    // If no number is supplied, is_flat will return true if all the blocks are less than 2 blocks apart in height.
    // Specifying a number will modify the number criteria for determining if it is flat.
    // -->
    tagProcessor.registerTag(ElementTag.class, "is_flat", (attribute, object) -> {
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        int[] heightMap = NMSHandler.getChunkHelper().getHeightMap(chunk);
        int tolerance = 2;
        if (attribute.hasParam() && ArgumentHelper.matchesInteger(attribute.getParam())) {
            tolerance = attribute.getIntParam();
        }
        int x = heightMap[0];
        for (int i : heightMap) {
            if (Math.abs(x - i) > tolerance) {
                return new ElementTag(false);
            }
        }
        return new ElementTag(true);
    });
    // <--[tag]
    // @attribute <ChunkTag.surface_blocks>
    // @returns ListTag(LocationTag)
    // @description
    // Returns a list of the highest non-air surface blocks in the chunk.
    // -->
    tagProcessor.registerTag(ListTag.class, "surface_blocks", (attribute, object) -> {
        ListTag surface_blocks = new ListTag();
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        ChunkSnapshot snapshot = chunk.getChunkSnapshot();
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                surface_blocks.addObject(new LocationTag(chunk.getWorld(), chunk.getX() << 4 | x, snapshot.getHighestBlockYAt(x, z) - 1, chunk.getZ() << 4 | z));
            }
        }
        return surface_blocks;
    });
    // <--[tag]
    // @attribute <ChunkTag.blocks_flagged[<flag_name>]>
    // @returns ListTag(LocationTag)
    // @description
    // Gets a list of all block locations with a specified flag within the CuboidTag.
    // Searches the internal flag lists, rather than through all possible blocks.
    // -->
    tagProcessor.registerTag(ListTag.class, "blocks_flagged", (attribute, object) -> {
        if (!attribute.hasParam()) {
            attribute.echoError("ChunkTag.blocks_flagged[...] must have an input value.");
            return null;
        }
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        String flagName = CoreUtilities.toLowerCase(attribute.getParam());
        ListTag blocks = new ListTag();
        LocationFlagSearchHelper.getFlaggedLocations(chunk, flagName, (loc) -> {
            blocks.addObject(new LocationTag(loc));
        });
        return blocks;
    });
    // <--[tag]
    // @attribute <ChunkTag.spawn_slimes>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the chunk is a specially located 'slime spawner' chunk.
    // -->
    tagProcessor.registerTag(ElementTag.class, "spawn_slimes", (attribute, object) -> {
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        return new ElementTag(chunk.isSlimeChunk());
    });
    // <--[tag]
    // @attribute <ChunkTag.inhabited_time>
    // @returns DurationTag
    // @mechanism ChunkTag.inhabited_time
    // @description
    // Returns the total time the chunk has been inhabited for.
    // This is a primary deciding factor in the "local difficulty" setting.
    // -->
    tagProcessor.registerTag(DurationTag.class, "inhabited_time", (attribute, object) -> {
        Chunk chunk = object.getChunkForTag(attribute);
        if (chunk == null) {
            return null;
        }
        return new DurationTag(chunk.getInhabitedTime());
    });
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ChunkSnapshot(org.bukkit.ChunkSnapshot) Chunk(org.bukkit.Chunk) World(org.bukkit.World) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) LivingEntity(org.bukkit.entity.LivingEntity) BlockState(org.bukkit.block.BlockState) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Plugin(org.bukkit.plugin.Plugin)

Aggregations

ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)237 ListTag (com.denizenscript.denizencore.objects.core.ListTag)86 EntityTag (com.denizenscript.denizen.objects.EntityTag)66 LocationTag (com.denizenscript.denizen.objects.LocationTag)49 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)48 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)43 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)40 List (java.util.List)40 Argument (com.denizenscript.denizencore.objects.Argument)28 Player (org.bukkit.entity.Player)28 MapTag (com.denizenscript.denizencore.objects.core.MapTag)27 EventHandler (org.bukkit.event.EventHandler)26 NPCTag (com.denizenscript.denizen.objects.NPCTag)24 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)19 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)18 ArrayList (java.util.ArrayList)16 Entity (org.bukkit.entity.Entity)16 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)12 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)12