Solved How to detect when enyity has entered in region?

Discussion in 'Spigot Plugin Development' started by Lemar98, Jan 23, 2020.

Thread Status:
Not open for further replies.
  1. 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 ?
     
  2. 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
     
  3. I want to detect every entity if it has type agressive mob and remove it. (zombie, skeleton, creeper etc.)
     
  4. Oh I missed the WorldGuard part.. I thought he wants his own region
     
  5. 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
     
  6. TeamBergerhealer

    Supporter

    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.
     
    #11 TeamBergerhealer, Jan 23, 2020
    Last edited: Jan 23, 2020
  7. Ok, i want try this.
    But i have a new trouble... I don't know how i can get location of my region
     
  8. TeamBergerhealer

    Supporter

  9. 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?
     
  10. TeamBergerhealer

    Supporter

    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.
     
  11. How i can check if entity is in region?..
     
  12. TeamBergerhealer

    Supporter

    ScreenShot_2020-01-23 22-00-14.png

    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
        }
    }
     
    • Like Like x 1
  13. Thank you very much!
     
    • Like Like x 1
Thread Status:
Not open for further replies.