}
@EventHandler(priority=EventPriority.HIGHEST)
public void creatureSpawn(CreatureSpawnEvent event)
{
AutoRefMatch match = plugin.getMatch(event.getEntity().getWorld());
if (match == null || match.getCurrentState() == MatchStatus.NONE) return;
if (event.getSpawnReason() == SpawnReason.SPAWNER_EGG)
{
Player spawner = null;
double distance = Double.POSITIVE_INFINITY;
// get the player who spawned this entity
Location loc = event.getEntity().getLocation();
for (Player pl : event.getEntity().getWorld().getPlayers())
{
double d = loc.distanceSquared(pl.getLocation());
if (d < distance && pl.getItemInHand() != null &&
pl.getItemInHand().getType() == Material.MONSTER_EGG)
{ spawner = pl; distance = d; }
}
// if the player who spawned this creature can configure...
if (spawner != null && spawner.hasPermission("autoreferee.configure")
&& spawner.getGameMode() == GameMode.CREATIVE) return;
}
if (event.getEntityType() == EntityType.SLIME &&
event.getSpawnReason() == SpawnReason.NATURAL)
{ event.setCancelled(true); return; }
// if the match hasn't started, cancel
if (!match.getCurrentState().inProgress())
{ event.setCancelled(true); return; }
// if this is a spawners-only region and its a non-spawner spawn, cancel
// note: spawning rules for silverfish are a bit of a special case, so allow them
if (match.hasFlag(event.getLocation(), AutoRefRegion.Flag.SPAWNERS_ONLY) &&
event.getSpawnReason() != SpawnReason.SPAWNER && event.getEntityType() != EntityType.SILVERFISH)
{ event.setCancelled(true); return; }
// if this is a safe zone, cancel
if (match.hasFlag(event.getLocation(), AutoRefRegion.Flag.SAFE))
{ event.setCancelled(true); return; }
}