Hi everyone! I want to remove every mob when it has enetered in specific worldguard region. I have problems with detecting entity interact with region. How i can do it ?
Do you want to detect only players or every type of entity? PS do you want to detect if a entity enters the region or do just want to detect every entity in this region and then remove it
I want to detect every entity if it has type agressive mob and remove it. (zombie, skeleton, creeper etc.)
This is the way to go: https://hub.spigotmc.org/javadocs/s...til.BoundingBox-java.util.function.Predicate-
No, sorry for the misinformation. I don't think there's anyway to detect entities entering a worldguard zone since there's no event to detect entities movement
There is no entity move event in Bukkit, your next best option is using a tick task and polling the positions of all entities of a world in realtime. You would then have to check if it intersects one of the worldguard regions. If performance is a concern, there are ways to do this checking more efficiently. For example, by using an Octree to store and update the positions of all entities so you can do a highly efficient "get all entities inside this cuboid" check. I use an Octree for a similar purpose, checking whether a particle is visible to a player or not. Since there can be millions of particles on a world this was the best solution for that problem. Benchmark performance before you take this route, octrees have overhead, especially on Java. There are free Octree implementations available online, you don't have to write your own.
https://worldguard.enginehub.org/en/latest/developer/regions/protected-region/ Your ProtectedRegion can be an instance of ProtectedCuboidRegion or ProtectedPolygonalRegion depending on what type it is (2D infinite y or a cuboid). Check using instanceof, then cast to the appropriate type and use the properties to find the points inside/outside the region.
Code (Java): public class CheckEntitiesTask implements Runnable { private JavaPlugin plugin; public CheckEntitiesTask(JavaPlugin plugin) { this.plugin = plugin; } @Override public void run() { RegionManager regionManager = WorldGuardPlugin.inst().getRegionManager(Bukkit.getWorld(plugin.getConfig().getString("world"))); for(String regionName : plugin.getConfig().getStringList("Regions")) { ProtectedRegion region = regionManager.getRegion(regionName); if(region == null) continue; for(BlockVector2D blockVector2D : region.getPoints()) { Location location = new Location(Bukkit.getWorld(plugin.getConfig().getString("world")), blockVector2D.toVector().getX(), blockVector2D.toVector().getY(), blockVector2D.toVector().getZ()); for(Entity entity : location.getNearbyEntities(location.getX(), location.getY(), location.getZ())) { if(!(entity instanceof Player)) { entity.remove(); } } } } } } It's does't work. Where trouble?
I believe WorldGuard regions have a contains(Location) method that would be much easier. Just loop by all entities on the server (world.getEntities()), call .getLocation() on them. Loop by all your regions, and check if that location is inside the region. Do not use getNearbyEntities, it is extremely slow. Looping by the points also isnt correct, right now youre checking whether entities are on the edge of a region rather than inside one.
Do read the api documentation I linked, itll help Code (Text): Vector entity_loc = entity.getLocation().toVector(); for (region : regions) { // use your own code here if (region.contains(entity_loc)) { // Remove the entity } }