Addresses new types of creature spawning for 1.14.x servers

https://github.com/BentoBoxWorld/BentoBox/issues/1020
This commit is contained in:
tastybento 2019-10-30 14:16:47 -07:00
parent 44a7f26c04
commit de2f2a22ce
2 changed files with 64 additions and 18 deletions

View File

@ -6,7 +6,6 @@ import org.bukkit.entity.PufferFish;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.database.objects.Island;
@ -29,17 +28,29 @@ public class MobSpawnListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNaturalMobSpawn(CreatureSpawnEvent e) {
// If not in the right world, return
// If not in the right world, or spawning is not natural return
if (!getIWM().inWorld(e.getEntity().getLocation())) {
return false;
}
// Deal with natural spawning
if (e.getSpawnReason().equals(SpawnReason.NATURAL)
|| e.getSpawnReason().equals(SpawnReason.JOCKEY)
|| e.getSpawnReason().equals(SpawnReason.DEFAULT)
|| e.getSpawnReason().equals(SpawnReason.MOUNT)
|| e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) {
switch (e.getSpawnReason()) {
// Natural
case DEFAULT:
case DROWNED:
case JOCKEY:
case LIGHTNING:
case MOUNT:
case NATURAL:
case NETHER_PORTAL:
case OCELOT_BABY:
case PATROL:
case RAID:
case REINFORCEMENTS:
case SILVERFISH_BLOCK:
case SLIME_SPLIT:
case TRAP:
case VILLAGE_DEFENSE:
case VILLAGE_INVASION:
// Deal with natural spawning
Optional<Island> island = getIslands().getIslandAt(e.getLocation());
// Cancel the event if these are true
if (Util.isHostileEntity(e.getEntity()) && !(e.getEntity() instanceof PufferFish)) {
@ -51,8 +62,9 @@ public class MobSpawnListener extends FlagListener {
e.setCancelled(cancel);
return cancel;
}
default:
return false;
}
return false;
}
}

View File

@ -198,14 +198,48 @@ public class MobSpawnListenerTest {
private void checkBlocked(CreatureSpawnEvent e, MobSpawnListener l) {
for (SpawnReason reason: SpawnReason.values()) {
when(e.getSpawnReason()).thenReturn(reason);
if (reason.equals(SpawnReason.NATURAL)
|| reason.equals(SpawnReason.JOCKEY)
|| reason.equals(SpawnReason.DEFAULT)
|| reason.equals(SpawnReason.MOUNT)
|| reason.equals(SpawnReason.NETHER_PORTAL)) {
assertTrue(l.onNaturalMobSpawn(e));
} else {
assertFalse(l.onNaturalMobSpawn(e));
switch (reason) {
// Natural
case DEFAULT:
case DROWNED:
case JOCKEY:
case LIGHTNING:
case MOUNT:
case NATURAL:
case NETHER_PORTAL:
case OCELOT_BABY:
case PATROL:
case RAID:
case REINFORCEMENTS:
case SILVERFISH_BLOCK:
case SLIME_SPLIT:
case TRAP:
case VILLAGE_DEFENSE:
case VILLAGE_INVASION:
// These should be blocked
assertTrue("Should be blocked: " + reason.toString(), l.onNaturalMobSpawn(e));
break;
// Unnatural - player involved
case BREEDING:
case BUILD_IRONGOLEM:
case BUILD_SNOWMAN:
case BUILD_WITHER:
case CURED:
case CUSTOM:
case DISPENSE_EGG:
case EGG:
case ENDER_PEARL:
case EXPLOSION:
case INFECTION:
case SHEARED:
case SHOULDER_ENTITY:
case SPAWNER:
case SPAWNER_EGG:
assertFalse("Should be not blocked: " + reason.toString(), l.onNaturalMobSpawn(e));
break;
default:
break;
}
}