Skip to content

Commit

Permalink
Check passengers of jockeys when spawned. #2195
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 7, 2023
1 parent 6d2f798 commit 7fbd041
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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());
}

}

0 comments on commit 7fbd041

Please sign in to comment.