From 7fbd041be96da41de7539854b9e1ea25d9f20e74 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 7 Oct 2023 11:15:23 -0700 Subject: [PATCH] Check passengers of jockeys when spawned. #2195 --- .../worldsettings/LimitMobsListener.java | 11 +- .../worldsettings/LimitMobsListenerTest.java | 126 ++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListenerTest.java diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListener.java index 5dde9dbae..f8a131280 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListener.java @@ -1,8 +1,10 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; +import org.bukkit.entity.EntityType; 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; @@ -19,7 +21,14 @@ public class LimitMobsListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onMobSpawn(CreatureSpawnEvent e) { - if (getIWM().inWorld(e.getLocation()) && getIWM().getMobLimitSettings(e.getLocation().getWorld()).contains(e.getEntityType().name())) { + check(e, e.getEntityType()); + if (e.getSpawnReason().equals(SpawnReason.JOCKEY) ) { + e.getEntity().getPassengers().forEach(pass -> check(e, pass.getType())); + } + } + + private void check(CreatureSpawnEvent e, EntityType type) { + if (getIWM().inWorld(e.getLocation()) && getIWM().getMobLimitSettings(e.getLocation().getWorld()).contains(type.name())) { e.setCancelled(true); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListenerTest.java new file mode 100644 index 000000000..177ebb441 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LimitMobsListenerTest.java @@ -0,0 +1,126 @@ +package world.bentobox.bentobox.listeners.flags.worldsettings; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.eclipse.jdt.annotation.NonNull; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.managers.IslandWorldManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +public class LimitMobsListenerTest { + + @Mock + private IslandWorldManager iwm; + @Mock + private @NonNull World world; + private List list = new ArrayList<>(); + private LimitMobsListener lml; + @Mock + private LivingEntity zombie; + @Mock + private LivingEntity skelly; + @Mock + private LivingEntity jockey; + @Mock + private Location location; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + when(plugin.getIWM()).thenReturn(iwm); + list.add("SKELETON"); + when(iwm.getMobLimitSettings(world)).thenReturn(list); + when(iwm.inWorld(world)).thenReturn(true); + when(iwm.inWorld(location)).thenReturn(true); + when(location.getWorld()).thenReturn(world); + when(zombie.getType()).thenReturn(EntityType.ZOMBIE); + when(zombie.getLocation()).thenReturn(location); + when(skelly.getType()).thenReturn(EntityType.SKELETON); + when(skelly.getLocation()).thenReturn(location); + when(jockey.getType()).thenReturn(EntityType.SPIDER); + when(jockey.getLocation()).thenReturn(location); + when(jockey.getPassengers()).thenReturn(List.of(skelly)); + + lml = new LimitMobsListener(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + Mockito.framework().clearInlineMocks(); + } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.LimitMobsListener#onMobSpawn(org.bukkit.event.entity.CreatureSpawnEvent)}. + */ + @Test + public void testOnMobSpawn() { + CreatureSpawnEvent e = new CreatureSpawnEvent(skelly, SpawnReason.NATURAL); + lml.onMobSpawn(e); + assertTrue(e.isCancelled()); + } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.LimitMobsListener#onMobSpawn(org.bukkit.event.entity.CreatureSpawnEvent)}. + */ + @Test + public void testOnMobSpawnNotInWorld() { + when(location.getWorld()).thenReturn(mock(World.class)); + CreatureSpawnEvent e = new CreatureSpawnEvent(skelly, SpawnReason.NATURAL); + lml.onMobSpawn(e); + assertFalse(e.isCancelled()); + } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.LimitMobsListener#onMobSpawn(org.bukkit.event.entity.CreatureSpawnEvent)}. + */ + @Test + public void testOnMobSpawnOkayToSpawn() { + CreatureSpawnEvent e = new CreatureSpawnEvent(zombie, SpawnReason.NATURAL); + lml.onMobSpawn(e); + assertFalse(e.isCancelled()); + } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.LimitMobsListener#onMobSpawn(org.bukkit.event.entity.CreatureSpawnEvent)}. + */ + @Test + public void testOnMobSpawnJockey() { + CreatureSpawnEvent e = new CreatureSpawnEvent(jockey, SpawnReason.JOCKEY); + lml.onMobSpawn(e); + assertTrue(e.isCancelled()); + } + +}