1.15.2 Stop monsters from spawning over sealevel

Discussion in 'Spigot Plugin Development' started by MiniGutt, Mar 4, 2020.

  1. Hey, I cant figure out how to stop hostile mobs from spawning over sealevel. I've searched for an answer, but cant seem to find an answer. This is what I thought it was, but mobs seems to spawn way over sea level anyways:
    Code (Text):
        @EventHandler
        public void onEntitySpawn(EntitySpawnEvent event) {
            Location loc = event.getLocation();
            Entity entity = event.getEntity();
            if(entity instanceof Monster && entity.getWorld().getSeaLevel() > loc.getY()) {
                event.setCancelled(true);
            }
        }
    Any tips?
     
  2. Tried debugging the sea level and the monster location? Check if the numbers are right?
     
  3. drives_a_ford

    Moderator

    You're checking if the sea level is higher than the location of the event. And then cancelling if it is.

    Sounds like you want the opposite?
     
    • Agree Agree x 1
  4. FrostedSnowman

    Resource Staff

    Your logic is backwards. Check if the location’s Y is greater than the sea level
     
  5. Both solutions are correct, but make sure it's greater than or equal so it's not equal to the sea level (if you don't want mobs spawning halfway above the ocean and halfway below) it's a small chance but still worth covering it.