More voiding of event listeners

https://github.com/BentoBoxWorld/BentoBox/issues/1918
This commit is contained in:
tastybento 2022-01-22 18:04:00 -08:00
parent d14ad977ab
commit 2f0869fd6e
5 changed files with 56 additions and 83 deletions

View File

@ -98,7 +98,7 @@ public class PortalTeleportationListener implements Listener {
* @param e - event
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public boolean onEntityPortal(EntityPortalEvent e) {
public void onEntityPortal(EntityPortalEvent e) {
if (plugin.getIWM().inWorld(e.getFrom())) {
Optional<Material> mat = Arrays.stream(BlockFace.values())
.map(bf -> e.getFrom().getBlock().getRelative(bf).getType())
@ -108,14 +108,12 @@ public class PortalTeleportationListener implements Listener {
.findFirst();
if (mat.isEmpty()) {
e.setCancelled(true);
return false;
} else if (mat.get().equals(Material.NETHER_PORTAL)){
return processPortal(new PlayerEntityPortalEvent(e), Environment.NETHER);
processPortal(new PlayerEntityPortalEvent(e), Environment.NETHER);
} else {
return processPortal(new PlayerEntityPortalEvent(e), Environment.THE_END);
processPortal(new PlayerEntityPortalEvent(e), Environment.THE_END);
}
}
return false;
}
/**
@ -138,11 +136,14 @@ public class PortalTeleportationListener implements Listener {
* @param e - event
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public boolean onIslandPortal(PlayerPortalEvent e) {
return switch (e.getCause()) {
public void onIslandPortal(PlayerPortalEvent e) {
switch (e.getCause()) {
case END_GATEWAY, END_PORTAL -> processPortal(new PlayerEntityPortalEvent(e), Environment.THE_END);
case NETHER_PORTAL -> processPortal(new PlayerEntityPortalEvent(e), Environment.NETHER);
default -> false;
default -> {
// Do nothing
}
};
}

View File

@ -74,15 +74,14 @@ public class StandardSpawnProtectionListener implements Listener {
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onExplosion(EntityExplodeEvent e) {
public void onExplosion(EntityExplodeEvent e) {
if (!plugin.getIWM().inWorld(Util.getWorld(e.getLocation().getWorld()))
|| plugin.getIWM().isIslandNether(e.getLocation().getWorld())
|| plugin.getIWM().isIslandEnd(e.getLocation().getWorld())) {
// Not used in island worlds
return false;
return;
}
e.blockList().removeIf(b -> atSpawn(b.getLocation()));
return true;
}
/**

View File

@ -37,10 +37,10 @@ public class MobSpawnListener extends FlagListener {
* @return true if cancelled
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
boolean onMobSpawn(CreatureSpawnEvent e) {
void onMobSpawn(CreatureSpawnEvent e) {
// If not in the right world, or spawning is not natural return
if (!getIWM().inWorld(e.getEntity().getLocation())) {
return false;
return;
}
switch (e.getSpawnReason()) {
// Natural
@ -62,14 +62,14 @@ public class MobSpawnListener extends FlagListener {
case VILLAGE_INVASION:
boolean cancelNatural = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_NATURAL_SPAWN, Flags.MONSTER_NATURAL_SPAWN);
e.setCancelled(cancelNatural);
return cancelNatural;
return;
// Spawners
case SPAWNER:
boolean cancelSpawners = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_SPAWNERS_SPAWN, Flags.MONSTER_SPAWNERS_SPAWN);
e.setCancelled(cancelSpawners);
return cancelSpawners;
return;
default:
return false;
return;
}
}

View File

@ -262,7 +262,7 @@ public class PortalTeleportationListenerTest {
Location loc = mock(Location.class);
when(loc.getWorld()).thenReturn(null);
PlayerPortalEvent e = new PlayerPortalEvent(player, loc, null, TeleportCause.END_PORTAL);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
assertFalse(e.isCancelled());
}
@ -305,7 +305,7 @@ public class PortalTeleportationListenerTest {
// Teleport from nether to world
when(from.getWorld()).thenReturn(mock(World.class));
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, TeleportCause.NETHER_PORTAL);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertFalse(e.isCancelled());
verify(iwm, never()).isEndGenerate(any());
@ -346,7 +346,7 @@ public class PortalTeleportationListenerTest {
public void testonIslandPortalNotPortal() {
PortalTeleportationListener np = new PortalTeleportationListener(plugin);
PlayerPortalEvent e = new PlayerPortalEvent(player, null, null, TeleportCause.COMMAND);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
}
/**
@ -359,7 +359,7 @@ public class PortalTeleportationListenerTest {
when(from.getWorld()).thenReturn(mock(World.class));
wrongWorld();
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, TeleportCause.NETHER_PORTAL);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
}
/**
@ -376,7 +376,7 @@ public class PortalTeleportationListenerTest {
// Nether islands active
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Event is canceled
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
@ -412,7 +412,7 @@ public class PortalTeleportationListenerTest {
when(im.getIslandAt(any())).thenReturn(optionalIsland);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
@ -443,7 +443,7 @@ public class PortalTeleportationListenerTest {
when(im.getIslandAt(any())).thenReturn(optionalIsland);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
@ -466,7 +466,7 @@ public class PortalTeleportationListenerTest {
// Nether islands inactive
when(iwm.isNetherIslands(any())).thenReturn(false);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertFalse(e.isCancelled());
// We are not going to 1,2,3
@ -488,7 +488,7 @@ public class PortalTeleportationListenerTest {
// Nether islands inactive
when(iwm.isNetherIslands(any())).thenReturn(false);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertFalse(e.isCancelled());
assertTrue(e.getTo().toString().contains("x=1.0,y=2.0,z=3.0"));
@ -514,7 +514,7 @@ public class PortalTeleportationListenerTest {
when(iwm.isNetherGenerate(any())).thenReturn(true);
// Player should be teleported to their island
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertTrue(e.isCancelled());
}
@ -533,7 +533,7 @@ public class PortalTeleportationListenerTest {
// Nether islands active
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertTrue(e.isCancelled());
// If regular nether, then to = island location
@ -551,7 +551,7 @@ public class PortalTeleportationListenerTest {
// Teleport from nether to world
when(from.getWorld()).thenReturn(null);
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, TeleportCause.NETHER_PORTAL);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertFalse(e.isCancelled());
}
@ -567,7 +567,7 @@ public class PortalTeleportationListenerTest {
// Teleport from nether to world
when(from.getWorld()).thenReturn(mock(World.class));
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, TeleportCause.NETHER_PORTAL);
assertFalse(np.onIslandPortal(e));
np.onIslandPortal(e);
// Verify
assertFalse(e.isCancelled());
verify(iwm, never()).isNetherGenerate(any());

View File

@ -147,7 +147,7 @@ public class MobSpawnListenerTest {
when(plugin.isLoaded()).thenReturn(false);
CreatureSpawnEvent e = new CreatureSpawnEvent(livingEntity, SpawnReason.NATURAL);
MobSpawnListener l = new MobSpawnListener();
assertFalse(l.onMobSpawn(e));
l.onMobSpawn(e);
assertFalse(e.isCancelled());
}
@ -164,18 +164,14 @@ public class MobSpawnListenerTest {
when(entity.getLocation()).thenReturn(null);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
CreatureSpawnEvent e = new CreatureSpawnEvent(entity, SpawnReason.NATURAL);
// Setup the listener
MobSpawnListener l = new MobSpawnListener();
l.setPlugin(plugin);
// Check monsters
when(e.getEntity()).thenReturn(entity);
// Should not be canceled
assertFalse(l.onMobSpawn(e));
l.onMobSpawn(e);
}
@Test
@ -188,28 +184,21 @@ public class MobSpawnListenerTest {
// Block mobs
when(island.isAllowed(Mockito.any())).thenReturn(false);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
// Setup the listener
MobSpawnListener l = new MobSpawnListener();
l.setPlugin(plugin);
// Check monsters
when(e.getEntity()).thenReturn(zombie);
checkBlocked(e,l);
when(e.getEntity()).thenReturn(slime);
checkBlocked(e,l);
checkBlocked(zombie,l);
checkBlocked(slime,l);
// Check animal
when(e.getEntity()).thenReturn(cow);
checkBlocked(e,l);
checkBlocked(cow,l);
}
private void checkBlocked(CreatureSpawnEvent e, MobSpawnListener l) {
private void checkBlocked(LivingEntity le, MobSpawnListener l) {
for (SpawnReason reason: SpawnReason.values()) {
when(e.getSpawnReason()).thenReturn(reason);
CreatureSpawnEvent e = new CreatureSpawnEvent(le, reason);
switch (reason) {
// Natural
case DEFAULT:
@ -228,11 +217,13 @@ public class MobSpawnListenerTest {
case VILLAGE_DEFENSE:
case VILLAGE_INVASION:
// These should be blocked
assertTrue("Natural spawn should be blocked: " + reason.toString(), l.onMobSpawn(e));
l.onMobSpawn(e);
assertTrue("Natural spawn should be blocked: " + reason.toString(), e.isCancelled());
break;
// Spawners
case SPAWNER:
assertTrue("Spawners spawn should be blocked: " + reason.toString(), l.onMobSpawn(e));
l.onMobSpawn(e);
assertTrue("Spawners spawn should be blocked: " + reason.toString(), e.isCancelled());
break;
// Unnatural - player involved or allowed
case BREEDING:
@ -250,7 +241,8 @@ public class MobSpawnListenerTest {
case SHOULDER_ENTITY:
case SPAWNER_EGG:
case SLIME_SPLIT:
assertFalse("Should be not blocked: " + reason.toString(), l.onMobSpawn(e));
l.onMobSpawn(e);
assertFalse("Should be not blocked: " + reason.toString(), e.isCancelled());
break;
default:
break;
@ -270,29 +262,23 @@ public class MobSpawnListenerTest {
// Allow mobs
when(island.isAllowed(Mockito.any())).thenReturn(true);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
// Setup the listener
MobSpawnListener l = new MobSpawnListener();
l.setPlugin(plugin);
// Check monsters
when(e.getEntity()).thenReturn(zombie);
checkUnBlocked(e,l);
when(e.getEntity()).thenReturn(slime);
checkUnBlocked(e,l);
checkUnBlocked(zombie,l);
checkUnBlocked(slime,l);
// Check animal
when(e.getEntity()).thenReturn(cow);
checkUnBlocked(e,l);
checkUnBlocked(cow,l);
}
private void checkUnBlocked(CreatureSpawnEvent e, MobSpawnListener l) {
private void checkUnBlocked(LivingEntity le, MobSpawnListener l) {
for (SpawnReason reason: SpawnReason.values()) {
when(e.getSpawnReason()).thenReturn(reason);
assertFalse(l.onMobSpawn(e));
CreatureSpawnEvent e = new CreatureSpawnEvent(le, reason);
l.onMobSpawn(e);
assertFalse(e.isCancelled());
}
}
@ -307,22 +293,16 @@ public class MobSpawnListenerTest {
Flags.ANIMAL_NATURAL_SPAWN.setDefaultSetting(false);
Flags.MONSTER_SPAWNERS_SPAWN.setDefaultSetting(false);
Flags.ANIMAL_SPAWNERS_SPAWN.setDefaultSetting(false);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
// Setup the listener
MobSpawnListener l = new MobSpawnListener();
l.setPlugin(plugin);
// Check monsters
when(e.getEntity()).thenReturn(zombie);
checkBlocked(e,l);
when(e.getEntity()).thenReturn(slime);
checkBlocked(e,l);
checkBlocked(zombie,l);
checkBlocked(slime,l);
// Check animal
when(e.getEntity()).thenReturn(cow);
checkBlocked(e,l);
checkBlocked(cow,l);
}
@ -338,22 +318,15 @@ public class MobSpawnListenerTest {
Flags.MONSTER_SPAWNERS_SPAWN.setDefaultSetting(true);
Flags.ANIMAL_SPAWNERS_SPAWN.setDefaultSetting(true);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
// Setup the listener
MobSpawnListener l = new MobSpawnListener();
l.setPlugin(plugin);
// Check monsters
when(e.getEntity()).thenReturn(zombie);
checkUnBlocked(e,l);
when(e.getEntity()).thenReturn(slime);
checkUnBlocked(e,l);
checkUnBlocked(zombie,l);
checkUnBlocked(slime,l);
// Check animal
when(e.getEntity()).thenReturn(cow);
checkUnBlocked(e,l);
checkUnBlocked(cow,l);
}
}