Search in sources :

Example 1 with ChunkSnapshot

use of org.bukkit.ChunkSnapshot 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

DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 Chunk (org.bukkit.Chunk)1 ChunkSnapshot (org.bukkit.ChunkSnapshot)1 World (org.bukkit.World)1 BlockState (org.bukkit.block.BlockState)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 Plugin (org.bukkit.plugin.Plugin)1