Implemented remove mobs on teleport

I simplified this to just remove mobs on login and island teleport. The
mobs to keep are currently hard coded. They could be taken from settings
in the future.
This commit is contained in:
tastybento 2018-06-09 23:06:59 -07:00
parent 90563dac35
commit 1240cd266e
7 changed files with 235 additions and 71 deletions

View File

@ -302,19 +302,6 @@ public class Settings implements DataObject, WorldSettings {
@ConfigEntry(path = "island.reset.kicked-keep-inventory")
private boolean kickedKeepInventory = false;
// Remove mobs
@ConfigComment("Removing mobs - this kills all monsters in the vicinity. Benefit is that it helps")
@ConfigComment("players return to their island if the island has been overrun by monsters.")
@ConfigComment("Con is that it kills any mob grinders.")
@ConfigEntry(path = "island.remove-mobs.on-login")
private boolean removeMobsOnLogin = false;
@ConfigComment("Remove mobs when /island.")
@ConfigEntry(path = "island.remove-mobs.on-island")
private boolean removeMobsOnIsland = false;
@ConfigComment("Mob white list - these mobs will NOT be removed when logging in or doing /island")
@ConfigEntry(path = "island.remove-mobs.whitelist")
private List<String> removeMobsWhitelist = new ArrayList<>();
@ConfigComment("Make island if player teleports to the island world and does not have one")
@ConfigEntry(path = "island.make-island-if-none")
private boolean makeIslandIfNone = false;
@ConfigComment("Immediately teleport player to their island (home 1 if it exists) when entering the world")
@ -713,12 +700,6 @@ public class Settings implements DataObject, WorldSettings {
public int getPurgeMaxIslandLevel() {
return purgeMaxIslandLevel;
}
/**
* @return the removeMobsWhitelist
*/
public List<String> getRemoveMobsWhitelist() {
return removeMobsWhitelist;
}
/**
* @return the resetLimit
*/
@ -921,18 +902,6 @@ public class Settings implements DataObject, WorldSettings {
public boolean isPurgeRemoveUserData() {
return purgeRemoveUserData;
}
/**
* @return the removeMobsOnIsland
*/
public boolean isRemoveMobsOnIsland() {
return removeMobsOnIsland;
}
/**
* @return the removeMobsOnLogin
*/
public boolean isRemoveMobsOnLogin() {
return removeMobsOnLogin;
}
/**
* @return the resetConfirmation
*/
@ -1347,25 +1316,6 @@ public class Settings implements DataObject, WorldSettings {
public void setPurgeRemoveUserData(boolean purgeRemoveUserData) {
this.purgeRemoveUserData = purgeRemoveUserData;
}
/**
* @param removeMobsOnIsland the removeMobsOnIsland to set
*/
public void setRemoveMobsOnIsland(boolean removeMobsOnIsland) {
this.removeMobsOnIsland = removeMobsOnIsland;
}
/**
* @param removeMobsOnLogin the removeMobsOnLogin to set
*/
public void setRemoveMobsOnLogin(boolean removeMobsOnLogin) {
this.removeMobsOnLogin = removeMobsOnLogin;
}
/**
* @param removeMobsWhitelist the removeMobsWhitelist to set
*/
public void setRemoveMobsWhitelist(List<String> removeMobsWhitelist) {
this.removeMobsWhitelist = removeMobsWhitelist;
}
/**
* @param resetConfirmation the resetConfirmation to set
*/

View File

@ -10,6 +10,8 @@ import org.bukkit.event.player.PlayerQuitEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.listeners.flags.RemoveMobsListener;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.PlayersManager;
public class JoinLeaveListener implements Listener {
@ -50,8 +52,8 @@ public class JoinLeaveListener implements Listener {
} else {
plugin.logWarning("Player that just logged in has no name! " + playerUUID.toString());
}
if (plugin.getSettings().isRemoveMobsOnLogin()) {
plugin.getIslands().removeMobs(user.getLocation());
if (Flags.REMOVE_MOBS.isSetForWorld(user.getWorld())) {
RemoveMobsListener.clearArea(user.getLocation());
}
}
}

View File

@ -3,10 +3,47 @@
*/
package us.tastybento.bskyblock.listeners.flags;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Monster;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerTeleportEvent;
import us.tastybento.bskyblock.lists.Flags;
/**
* Removes mobs when teleporting to an island
* @author tastybento
*
*/
public class RemoveMobsListener extends AbstractFlagListener {
private static Set<EntityType> keepers = new HashSet<>();
{
keepers.add(EntityType.ZOMBIE_VILLAGER);
keepers.add(EntityType.PIG_ZOMBIE);
keepers.add(EntityType.WITHER);
keepers.add(EntityType.ENDERMAN);
keepers.add(EntityType.GHAST);
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onUserTeleport(PlayerTeleportEvent e) {
// Only process if flag is active
if (Flags.REMOVE_MOBS.isSetForWorld(e.getTo().getWorld()) && getIslands().locationIsOnIsland(e.getPlayer(), e.getTo())) {
clearArea(e.getTo());
}
}
public static void clearArea(Location loc) {
loc.getWorld().getNearbyEntities(loc, 5D, 5D, 5D).stream()
.filter(en -> (en instanceof Monster))
.filter(en -> !keepers.contains(en.getType()))
.forEach(Entity::remove);
}
}

View File

@ -16,6 +16,7 @@ import org.bukkit.entity.EntityType;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.configuration.WorldSettings;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.generators.ChunkGeneratorWorld;
import us.tastybento.bskyblock.util.Util;
@ -465,4 +466,14 @@ public class IslandWorldManager {
return worldSettings.get(Util.getWorld(world)).getIvSettings();
}
/**
* Returns whether a world flag is set or not
* @param world - world
* @param flag - world setting flag
* @return true or false
*/
public boolean isWorldFlag(World world, Flag flag) {
return worldSettings.get(Util.getWorld(world)).getWorldFlags().get(flag.getID());
}
}

View File

@ -609,7 +609,7 @@ public class IslandsManager {
/**
* Checks if a specific location is within the protected range of an island
* owned by the player
* that the player is a member of (owner or member)
*
* @param player - the player
* @param loc - location
@ -646,15 +646,6 @@ public class IslandsManager {
return Optional.ofNullable(getIsland(world, user)).map(i -> i.onIsland(user.getLocation())).orElse(false);
}
/**
* @param location - the location
*/
public void removeMobs(Location location) {
// TODO Auto-generated method stub
}
/**
* Removes this player from any and all islands in world
* @param world - world

View File

@ -0,0 +1,182 @@
/**
*
*/
package us.tastybento.bskyblock.listeners.flags;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Wither;
import org.bukkit.entity.Zombie;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.configuration.WorldSettings;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({BSkyBlock.class, Util.class })
public class RemoveMobsListenerTest {
private Island island;
private IslandsManager im;
private World world;
private Location inside;
private UUID uuid;
private Zombie zombie;
private Slime slime;
private Cow cow;
private Player player;
private Wither wither;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BSkyBlock plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// World
world = mock(World.class);
// Owner
uuid = UUID.randomUUID();
// Island initialization
island = mock(Island.class);
when(island.getOwner()).thenReturn(uuid);
im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
inside = mock(Location.class);
when(inside.getWorld()).thenReturn(world);
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);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
// World Settings
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Monsters and animals
zombie = mock(Zombie.class);
when(zombie.getLocation()).thenReturn(inside);
when(zombie.getType()).thenReturn(EntityType.ZOMBIE);
slime = mock(Slime.class);
when(slime.getLocation()).thenReturn(inside);
when(slime.getType()).thenReturn(EntityType.SLIME);
cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(inside);
when(cow.getType()).thenReturn(EntityType.COW);
wither = mock(Wither.class);
when(wither.getType()).thenReturn(EntityType.WITHER);
Collection<Entity> collection = new ArrayList<>();
collection.add(player);
collection.add(zombie);
collection.add(cow);
collection.add(slime);
collection.add(wither);
when(world
.getNearbyEntities(Mockito.any(Location.class), Mockito.anyDouble(), Mockito.anyDouble(), Mockito.anyDouble()))
.thenReturn(collection);
Flags.REMOVE_MOBS.setSetting(world, true);
// Sometimes use Mockito.withSettings().verboseLogging()
player = mock(Player.class);
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
*/
@Test
public void testOnUserTeleport() {
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
new RemoveMobsListener().onUserTeleport(e);
Mockito.verify(zombie).remove();
Mockito.verify(player, Mockito.never()).remove();
Mockito.verify(cow, Mockito.never()).remove();
Mockito.verify(slime, Mockito.never()).remove();
Mockito.verify(wither, Mockito.never()).remove();
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
*/
@Test
public void testOnUserTeleportDoNotRemove() {
Flags.REMOVE_MOBS.setSetting(world, false);
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
new RemoveMobsListener().onUserTeleport(e);
Mockito.verify(zombie, Mockito.never()).remove();
Mockito.verify(player, Mockito.never()).remove();
Mockito.verify(cow, Mockito.never()).remove();
Mockito.verify(slime, Mockito.never()).remove();
Mockito.verify(wither, Mockito.never()).remove();
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
*/
@Test
public void testOnUserTeleportToNotIsland() {
// Not on island
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
new RemoveMobsListener().onUserTeleport(e);
Mockito.verify(zombie, Mockito.never()).remove();
Mockito.verify(player, Mockito.never()).remove();
Mockito.verify(cow, Mockito.never()).remove();
Mockito.verify(slime, Mockito.never()).remove();
Mockito.verify(wither, Mockito.never()).remove();
}
}

View File

@ -820,15 +820,6 @@ public class IslandsManagerTest {
assertTrue(im.userIsOnIsland(world, user));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.IslandsManager#removeMobs(org.bukkit.Location)}.
*/
@Test
public void testRemoveMobs() {
IslandsManager im = new IslandsManager(plugin);
im.removeMobs(location);
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.IslandsManager#removePlayer(java.util.UUID)}.
*/