Added Respawn On Island world flag.

Renamed getIslandWorldManaget to getIWM() in AbstractFlagListener.
This commit is contained in:
tastybento 2018-06-19 22:30:23 -07:00
parent f32c661133
commit 12a1700848
10 changed files with 262 additions and 7 deletions

View File

@ -374,6 +374,11 @@ protection:
&asettings. &asettings.
name: "Invincible Visitors" name: "Invincible Visitors"
visitors-protected: "&cVisitors protected" visitors-protected: "&cVisitors protected"
ISLAND_RESPAWN:
description: |
&aPlayers respawn
&aon island
name: "Island respawn"
ITEM_DROP: ITEM_DROP:
description: "Toggle dropping" description: "Toggle dropping"
name: "Item drop" name: "Item drop"

View File

@ -192,7 +192,7 @@ public abstract class AbstractFlagListener implements Listener {
* Get the island world manager * Get the island world manager
* @return Island World Manager * @return Island World Manager
*/ */
protected IslandWorldManager getIslandWorldManager() { protected IslandWorldManager getIWM() {
return plugin.getIWM(); return plugin.getIWM();
} }
} }

View File

@ -93,7 +93,7 @@ public class BreakBlocksListener extends AbstractFlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onVehicleDamageEvent(VehicleDamageEvent e) { public void onVehicleDamageEvent(VehicleDamageEvent e) {
if (getIslandWorldManager().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) { if (getIWM().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) {
User user = User.getInstance((Player) e.getAttacker()); User user = User.getInstance((Player) e.getAttacker());
// Get the island and if present, check the flag, react if required and return // Get the island and if present, check the flag, react if required and return
getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> { getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> {

View File

@ -45,7 +45,7 @@ public class EnderChestListener extends AbstractFlagListener {
private boolean checkEnderChest(Player player, Material type) { private boolean checkEnderChest(Player player, Material type) {
if (type.equals(Material.ENDER_CHEST) if (type.equals(Material.ENDER_CHEST)
&& getIslandWorldManager().inWorld(player.getLocation()) && getIWM().inWorld(player.getLocation())
&& !player.isOp() && !player.isOp()
&& !player.hasPermission(getPlugin().getIWM().getPermissionPrefix(player.getWorld()) + ".craft.enderchest") && !player.hasPermission(getPlugin().getIWM().getPermissionPrefix(player.getWorld()) + ".craft.enderchest")
&& !Flags.ENDER_CHEST.isSetForWorld(player.getWorld())) { && !Flags.ENDER_CHEST.isSetForWorld(player.getWorld())) {

View File

@ -26,7 +26,7 @@ public class EnterExitListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
// Only process if Enter Exit flags are active, we are in the right world and there is a change in X or Z coords // Only process if Enter Exit flags are active, we are in the right world and there is a change in X or Z coords
if (!getIslandWorldManager().inWorld(e.getFrom()) if (!getIWM().inWorld(e.getFrom())
|| e.getFrom().toVector().multiply(XZ).equals(e.getTo().toVector().multiply(XZ)) || e.getFrom().toVector().multiply(XZ).equals(e.getTo().toVector().multiply(XZ))
|| !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(e.getFrom().getWorld())) { || !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(e.getFrom().getWorld())) {
return; return;

View File

@ -36,7 +36,7 @@ public class FireListener extends AbstractFlagListener {
*/ */
public boolean checkFire(Cancellable e, Location l, Flag flag) { public boolean checkFire(Cancellable e, Location l, Flag flag) {
// Check world // Check world
if (!getIslandWorldManager().inWorld(l)) { if (!getIWM().inWorld(l)) {
return false; return false;
} }
// Check if the island exists and if fire is allowed // Check if the island exists and if fire is allowed
@ -119,7 +119,7 @@ public class FireListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public boolean onTNTDamage(EntityChangeBlockEvent e) { public boolean onTNTDamage(EntityChangeBlockEvent e) {
// Check world // Check world
if (!e.getBlock().getType().equals(Material.TNT) || !getIslandWorldManager().inWorld(e.getBlock().getLocation())) { if (!e.getBlock().getType().equals(Material.TNT) || !getIWM().inWorld(e.getBlock().getLocation())) {
return false; return false;
} }
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow // Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow

View File

@ -0,0 +1,56 @@
/**
*
*/
package us.tastybento.bskyblock.listeners.flags;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.lists.Flags;
/**
* Handles respawning back on island
* @author tastybento
*
*/
public class IslandRespawnListener extends AbstractFlagListener {
Map<UUID, World> respawn = new HashMap<>();
/**
* Tag players who die in island space and have an island
* @param e
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent e) {
if (getIWM().inWorld(e.getEntity().getLocation()) && Flags.ISLAND_RESPAWN.isSetForWorld(e.getEntity().getWorld())
&& getIslands().hasIsland(e.getEntity().getWorld(), e.getEntity().getUniqueId())) {
respawn.put(e.getEntity().getUniqueId(), e.getEntity().getWorld());
}
}
/**
* Place players back on their island if respawn on island is true and active
* @param e
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerRespawn(PlayerRespawnEvent e) {
if (Flags.ISLAND_RESPAWN.isSetForWorld(e.getPlayer().getWorld()) && respawn.containsKey(e.getPlayer().getUniqueId())) {
World world = respawn.get(e.getPlayer().getUniqueId());
respawn.remove(e.getPlayer().getUniqueId());
Location respawnLocation = getIslands().getSafeHomeLocation(world, User.getInstance(e.getPlayer().getUniqueId()), 1);
if (respawnLocation != null) {
e.setRespawnLocation(respawnLocation);
}
}
}
}

View File

@ -29,7 +29,7 @@ public class MobSpawnListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNaturalMobSpawn(CreatureSpawnEvent e) { public boolean onNaturalMobSpawn(CreatureSpawnEvent e) {
// If not in the right world, return // If not in the right world, return
if (!getIslandWorldManager().inWorld(e.getEntity().getLocation())) { if (!getIWM().inWorld(e.getEntity().getLocation())) {
return false; return false;
} }
// Deal with natural spawning // Deal with natural spawning

View File

@ -22,6 +22,7 @@ import us.tastybento.bskyblock.listeners.flags.FireListener;
import us.tastybento.bskyblock.listeners.flags.HurtingListener; import us.tastybento.bskyblock.listeners.flags.HurtingListener;
import us.tastybento.bskyblock.listeners.flags.InventoryListener; import us.tastybento.bskyblock.listeners.flags.InventoryListener;
import us.tastybento.bskyblock.listeners.flags.InvincibleVisitorsListener; import us.tastybento.bskyblock.listeners.flags.InvincibleVisitorsListener;
import us.tastybento.bskyblock.listeners.flags.IslandRespawnListener;
import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener; import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener;
import us.tastybento.bskyblock.listeners.flags.LeashListener; import us.tastybento.bskyblock.listeners.flags.LeashListener;
import us.tastybento.bskyblock.listeners.flags.LockAndBanListener; import us.tastybento.bskyblock.listeners.flags.LockAndBanListener;
@ -167,6 +168,8 @@ public class Flags {
.listener(ilv).onClick(ilv).subPanel(true).build(); .listener(ilv).onClick(ilv).subPanel(true).build();
public static final Flag REMOVE_MOBS = new FlagBuilder().id("REMOVE_MOBS").icon(Material.GLOWSTONE_DUST).type(Type.WORLD_SETTING) public static final Flag REMOVE_MOBS = new FlagBuilder().id("REMOVE_MOBS").icon(Material.GLOWSTONE_DUST).type(Type.WORLD_SETTING)
.listener(new RemoveMobsListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("REMOVE_MOBS")).build(); .listener(new RemoveMobsListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("REMOVE_MOBS")).build();
public static final Flag ISLAND_RESPAWN = new FlagBuilder().id("ISLAND_RESPAWN").icon(Material.TORCH).type(Type.WORLD_SETTING)
.listener(new IslandRespawnListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("ISLAND_RESPAWN")).build();
/** /**
* @return List of all the flags in this class * @return List of all the flags in this class

View File

@ -0,0 +1,191 @@
/**
*
*/
package us.tastybento.bskyblock.listeners.flags;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
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.Settings;
import us.tastybento.bskyblock.api.configuration.WorldSettings;
import us.tastybento.bskyblock.api.user.User;
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, Flags.class, Util.class })
public class IslandRespawnListenerTest {
private World world;
private Player player;
private IslandsManager im;
private IslandWorldManager iwm;
private Location safeLocation;
/**
* @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);
// Settings
Settings s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s);
// Player
player = mock(Player.class);
when(player.getWorld()).thenReturn(world);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getLocation()).thenReturn(mock(Location.class));
// Island World Manager
iwm = mock(IslandWorldManager.class);
// All locations are in world by default
when(iwm.inWorld(Mockito.any())).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(plugin.getIslands()).thenReturn(im);
safeLocation = mock(Location.class);
when(safeLocation.getWorld()).thenReturn(world);
when(im.getSafeHomeLocation(Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn(safeLocation);
// Sometimes use Mockito.withSettings().verboseLogging()
User.setPlugin(plugin);
User.getInstance(player);
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeath() {
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.IslandRespawnListener#onPlayerRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnPlayerRespawn() {
// Die
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
IslandRespawnListener l = new IslandRespawnListener();
l.onPlayerDeath(e);
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
assertEquals(safeLocation, ev.getRespawnLocation());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.IslandRespawnListener#onPlayerRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnPlayerRespawnWithoutDeath() {
IslandRespawnListener l = new IslandRespawnListener();
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
assertEquals(location, ev.getRespawnLocation());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.IslandRespawnListener#onPlayerRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnPlayerRespawnWrongWorld() {
when(iwm.inWorld(Mockito.any())).thenReturn(false);
// Die
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
IslandRespawnListener l = new IslandRespawnListener();
l.onPlayerDeath(e);
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
assertEquals(location, ev.getRespawnLocation());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.IslandRespawnListener#onPlayerRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Test
public void testOnPlayerRespawnFlagNotSet() {
Flags.ISLAND_RESPAWN.setSetting(world, false);
// Die
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
IslandRespawnListener l = new IslandRespawnListener();
l.onPlayerDeath(e);
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
assertEquals(location, ev.getRespawnLocation());
}
}