Skip to content

Commit

Permalink
Add natural spawn protection listener.
Browse files Browse the repository at this point in the history
Fixes #84
  • Loading branch information
BONNe committed Feb 27, 2022
1 parent d64be1c commit 0a016c2
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;

import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.data.Greenhouse;
Expand Down Expand Up @@ -78,6 +79,23 @@ public void onPistonPull(BlockPistonRetractEvent e) {
.anyMatch(this::inGreenhouse));
}


/**
* Guard Greenhouse from natural entity spawning.
*
* @param spawnEvent the spawn event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent spawnEvent)
{
if (CreatureSpawnEvent.SpawnReason.NATURAL == spawnEvent.getSpawnReason())
{
// Natural spawn events should be cancelled. Greenhouse spawns its own mobs.
spawnEvent.setCancelled(this.inGreenhouse(spawnEvent.getLocation()));
}
}


private boolean inGreenhouse(Location l) {
return addon.getManager().getMap().getGreenhouse(l).map(g -> g.isRoofOrWallBlock(l)).orElse(false);
}
Expand Down

0 comments on commit 0a016c2

Please sign in to comment.