Fixes island respawn bug.

Not all players were respawning on their island. Also, deaths in nether
or end did not teleport back to overworld island.
This commit is contained in:
tastybento 2019-09-20 18:39:50 -07:00
parent 86da94182c
commit 55544ada0a
2 changed files with 99 additions and 20 deletions

View File

@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerRespawnEvent;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
/**
* Handles respawning back on island
@ -30,17 +31,18 @@ public class IslandRespawnListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW)
public void onPlayerDeath(PlayerDeathEvent e) {
if (!getIWM().inWorld(e.getEntity().getLocation())) {
World world = Util.getWorld(e.getEntity().getWorld());
if (!getIWM().inWorld(world)) {
return; // not in the island world
}
if (!Flags.ISLAND_RESPAWN.isSetForWorld(e.getEntity().getWorld())) {
if (!Flags.ISLAND_RESPAWN.isSetForWorld(world)) {
return; // world doesn't have the island respawn flag
}
if (!getIslands().hasIsland(e.getEntity().getWorld(), e.getEntity().getUniqueId()) || !getIslands().inTeam(e.getEntity().getWorld(), e.getEntity().getUniqueId())) {
if (!getIslands().hasIsland(world, e.getEntity().getUniqueId()) && !getIslands().inTeam(world, e.getEntity().getUniqueId())) {
return; // doesn't have an island in this world
}
respawn.put(e.getEntity().getUniqueId(), e.getEntity().getWorld().getUID());
respawn.put(e.getEntity().getUniqueId(), world.getUID());
}
/**
@ -59,7 +61,7 @@ public class IslandRespawnListener extends FlagListener {
return; // world no longer available
}
final Location respawnLocation = getIslands().getSafeHomeLocation(world, User.getInstance(e.getPlayer().getUniqueId()), 1);
final Location respawnLocation = getIslands().getSafeHomeLocation(Util.getWorld(world), User.getInstance(e.getPlayer().getUniqueId()), 1);
if (respawnLocation != null) {
e.setRespawnLocation(respawnLocation);
}

View File

@ -1,8 +1,10 @@
package world.bentobox.bentobox.listeners.flags.worldsettings;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
@ -13,15 +15,17 @@ import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Server;
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.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -46,11 +50,18 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest( {BentoBox.class, Flags.class, Util.class })
public class IslandRespawnListenerTest {
@Mock
private World world;
@Mock
private Player player;
@Mock
private IslandsManager im;
@Mock
private IslandWorldManager iwm;
@Mock
private Location safeLocation;
@Mock
private Server server;
/**
* @throws java.lang.Exception
@ -62,50 +73,116 @@ public class IslandRespawnListenerTest {
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// World
world = mock(World.class);
when(world.getUID()).thenReturn(UUID.randomUUID());
when(server.getWorld(any(UUID.class))).thenReturn(world);
// 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));
when(player.getServer()).thenReturn(server);
// Island World Manager
iwm = mock(IslandWorldManager.class);
// All locations are in world by default
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
when(Util.getWorld(any())).thenReturn(world);
// World Settings
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);
GameModeAddon gma = mock(GameModeAddon.class);
Optional<GameModeAddon> opGma = Optional.of(gma );
when(iwm.getAddon(any())).thenReturn(opGma);
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), 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);
when(im.getSafeHomeLocation(any(), any(), Mockito.anyInt())).thenReturn(safeLocation);
// Sometimes use Mockito.withSettings().verboseLogging()
User.setPlugin(plugin);
User.getInstance(player);
}
@After
public void tearDown() {
User.clearUsers();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeathNotIslandWorld() {
when(iwm.inWorld(any(World.class))).thenReturn(false);
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world, never()).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeathNoFlag() {
Flags.ISLAND_RESPAWN.setSetting(world, false);
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world, never()).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeathNotOwnerNotTeam() {
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
when(im.inTeam(any(), any(UUID.class))).thenReturn(false);
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world, never()).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeathNotOwnerInTeam() {
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnPlayerDeathOwnerNoTeam() {
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.inTeam(any(), any(UUID.class))).thenReturn(false);
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@ -114,12 +191,12 @@ public class IslandRespawnListenerTest {
List<ItemStack> drops = new ArrayList<>();
PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, "");
new IslandRespawnListener().onPlayerDeath(e);
verify(world).getUID();
}
/**
* Test method for {@link IslandRespawnListener#onPlayerRespawn(org.bukkit.event.player.PlayerRespawnEvent)}.
*/
@Ignore("mock of the server needed")
@Test
public void testOnPlayerRespawn() {
// Die
@ -130,7 +207,7 @@ public class IslandRespawnListenerTest {
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
@ -146,7 +223,7 @@ public class IslandRespawnListenerTest {
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
@ -169,7 +246,7 @@ public class IslandRespawnListenerTest {
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);
@ -190,7 +267,7 @@ public class IslandRespawnListenerTest {
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
// Respawn
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
l.onPlayerRespawn(ev);