Search in sources :

Example 1 with SpawnableHelper

use of com.denizenscript.denizen.utilities.blocks.SpawnableHelper 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)

Aggregations

BukkitScriptEvent (com.denizenscript.denizen.events.BukkitScriptEvent)1 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)1 SpawnableHelper (com.denizenscript.denizen.utilities.blocks.SpawnableHelper)1 Depends (com.denizenscript.denizen.utilities.depends.Depends)1 LocationFlagSearchHelper (com.denizenscript.denizen.utilities.flags.LocationFlagSearchHelper)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 Attribute (com.denizenscript.denizencore.tags.Attribute)1 ObjectTagProcessor (com.denizenscript.denizencore.tags.ObjectTagProcessor)1 CoreUtilities (com.denizenscript.denizencore.utilities.CoreUtilities)1 Predicate (java.util.function.Predicate)1 CitizensAPI (net.citizensnpcs.api.CitizensAPI)1 NPC (net.citizensnpcs.api.npc.NPC)1 Bukkit (org.bukkit.Bukkit)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1