Expand remove mobs world setting to include respawning protection

Addresses #2058

We already have a option to protect against hostile mobs when logging in
or teleporting, but not respawn. This adds protection to that. Admin can
turn off the option if they want it more hard core.
This commit is contained in:
tastybento 2022-12-04 21:06:14 -08:00
parent 69fa951907
commit e2b5545f3f
2 changed files with 49 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package world.bentobox.bentobox.listeners.flags.worldsettings;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@ -33,4 +34,13 @@ public class RemoveMobsListener extends FlagListener {
Bukkit.getScheduler().runTask(getPlugin(), () -> getIslands().clearArea(e.getTo()));
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onUserRespawn(PlayerRespawnEvent e) {
// Only process if flag is active
if (getIslands().locationIsOnIsland(e.getPlayer(), e.getRespawnLocation()) && Flags.REMOVE_MOBS.isSetForWorld(e.getRespawnLocation().getWorld())) {
Bukkit.getScheduler().runTask(getPlugin(), () -> getIslands().clearArea(e.getRespawnLocation()));
}
}
}

View File

@ -15,6 +15,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.After;
@ -81,7 +82,7 @@ public class RemoveMobsListenerTest {
when(island.getOwner()).thenReturn(uuid1);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island);
// Location
when(inside.getWorld()).thenReturn(world);
@ -92,17 +93,17 @@ public class RemoveMobsListenerTest {
Optional<Island> opIsland = Optional.ofNullable(island);
when(im.getProtectedIslandAt(Mockito.eq(inside))).thenReturn(opIsland);
// On island
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
when(im.locationIsOnIsland(any(), any())).thenReturn(true);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
when(Util.getWorld(any())).thenReturn(world);
// World Settings
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getAddon(any())).thenReturn(Optional.empty());
when(plugin.getIWM()).thenReturn(iwm);
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
Flags.REMOVE_MOBS.setSetting(world, true);
@ -171,6 +172,7 @@ public class RemoveMobsListenerTest {
new RemoveMobsListener().onUserTeleport(e);
verify(scheduler, never()).runTask(any(), any(Runnable.class));
}
/**
* Test method for {@link RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
*/
@ -188,10 +190,42 @@ public class RemoveMobsListenerTest {
@Test
public void testOnUserTeleportToNotIsland() {
// Not on island
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
when(im.locationIsOnIsland(any(), any())).thenReturn(false);
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
new RemoveMobsListener().onUserTeleport(e);
verify(scheduler, never()).runTask(any(), any(Runnable.class));
}
/**
* Test method for {@link RemoveMobsListener#onUserRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnUserRespawn() {
PlayerRespawnEvent e = new PlayerRespawnEvent(player, inside, false, false);
new RemoveMobsListener().onUserRespawn(e);
verify(scheduler).runTask(any(), any(Runnable.class));
}
/**
* Test method for {@link RemoveMobsListener#onUserRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnUserRespawnDoNotRemove() {
Flags.REMOVE_MOBS.setSetting(world, false);
PlayerRespawnEvent e = new PlayerRespawnEvent(player, inside, false, false);
new RemoveMobsListener().onUserRespawn(e);
verify(scheduler, never()).runTask(any(), any(Runnable.class));
}
/**
* Test method for {@link RemoveMobsListener#onUserRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnUserRespawnNotIsland() {
// Not on island
when(im.locationIsOnIsland(any(), any())).thenReturn(false);
PlayerRespawnEvent e = new PlayerRespawnEvent(player, inside, false, false);
new RemoveMobsListener().onUserRespawn(e);
verify(scheduler, never()).runTask(any(), any(Runnable.class));
}
}