Fixed admin register of spawn island.

https://github.com/BentoBoxWorld/BentoBox/issues/991
This commit is contained in:
tastybento 2019-10-18 19:32:54 -05:00
parent bee960d823
commit 02e8243bd0
5 changed files with 85 additions and 64 deletions

View File

@ -66,10 +66,23 @@ public class AdminRegisterCommand extends ConfirmableCommand {
user.sendMessage("commands.admin.register.already-owned"); user.sendMessage("commands.admin.register.already-owned");
return false; return false;
} }
// Check if island is spawn
if (island.map(i -> i.isSpawn()).orElse(false)) {
askConfirmation(user, user.getTranslation("commands.admin.register.island-is-spawn"), () -> register(user, targetUUID, island, closestIsland));
return false;
}
return register(user, targetUUID, island, closestIsland);
}
private boolean register(User user, UUID targetUUID, Optional<Island> island, Location closestIsland) {
// Register island if it exists // Register island if it exists
if (!island.map(i -> { if (!island.map(i -> {
// Island exists // Island exists
getIslands().setOwner(user, targetUUID, i); getIslands().setOwner(user, targetUUID, i);
if (i.isSpawn()) {
getIslands().clearSpawn(i.getWorld());
}
user.sendMessage("commands.admin.register.registered-island", "[xyz]", Util.xyz(i.getCenter().toVector())); user.sendMessage("commands.admin.register.registered-island", "[xyz]", Util.xyz(i.getCenter().toVector()));
user.sendMessage("general.success"); user.sendMessage("general.success");
IslandBaseEvent event = IslandEvent.builder() IslandBaseEvent event = IslandEvent.builder()
@ -79,7 +92,7 @@ public class AdminRegisterCommand extends ConfirmableCommand {
.involvedPlayer(targetUUID) .involvedPlayer(targetUUID)
.admin(true) .admin(true)
.build(); .build();
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return true; return true;
}).orElse(false)) { }).orElse(false)) {
// Island does not exist - this is a reservation // Island does not exist - this is a reservation
@ -102,11 +115,12 @@ public class AdminRegisterCommand extends ConfirmableCommand {
.involvedPlayer(targetUUID) .involvedPlayer(targetUUID)
.admin(true) .admin(true)
.build(); .build();
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
}); });
return false; return false;
} }
return true; return true;
} }
@Override @Override

View File

@ -789,6 +789,19 @@ public class IslandsManager {
this.spawn.put(spawn.getWorld(), spawn); this.spawn.put(spawn.getWorld(), spawn);
spawn.setSpawn(true); spawn.setSpawn(true);
} }
/**
* Clears the spawn island for this world
* @param world - world
* @since 1.8.0
*/
public void clearSpawn(World world) {
Island spawnIsland = spawn.get(Util.getWorld(world));
if (spawnIsland != null) {
spawnIsland.setSpawn(false);
}
this.spawn.remove(world);
}
/** /**
* @param uniqueId - unique ID * @param uniqueId - unique ID

View File

@ -150,6 +150,7 @@ commands:
no-island-here: "&cThere is no island here. Confirm to make one." no-island-here: "&cThere is no island here. Confirm to make one."
in-deletion: "&cThis island space is currently being deleted. Try later." in-deletion: "&cThis island space is currently being deleted. Try later."
cannot-make-island: "&cAn island cannot be placed here, sorry. See console for possible errors." cannot-make-island: "&cAn island cannot be placed here, sorry. See console for possible errors."
island-is-spawn: "&6Island is spawn. Are you sure? Enter command again to confirm."
unregister: unregister:
parameters: "<owner>" parameters: "<owner>"
description: "unregister owner from island, but keep island blocks" description: "unregister owner from island, but keep island blocks"

View File

@ -2,18 +2,20 @@ package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -21,7 +23,7 @@ import org.bukkit.util.Vector;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
@ -47,12 +49,18 @@ import world.bentobox.bentobox.managers.PlayersManager;
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AdminRegisterCommandTest { public class AdminRegisterCommandTest {
@Mock
private CompositeCommand ac; private CompositeCommand ac;
private UUID uuid; private UUID uuid;
@Mock
private User user; private User user;
@Mock
private IslandsManager im; private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
private UUID notUUID; private UUID notUUID;
private IslandDeletionManager idm; private IslandDeletionManager idm;
/** /**
@ -71,7 +79,6 @@ public class AdminRegisterCommandTest {
// Player // Player
Player p = mock(Player.class); Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging() // Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
@ -84,7 +91,6 @@ public class AdminRegisterCommandTest {
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases // Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager // Island World Manager
@ -93,16 +99,14 @@ public class AdminRegisterCommandTest {
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.isOwner(any(),any())).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); when(im.getOwner(any(),any())).thenReturn(uuid);
when(im.getOwner(Mockito.any(),Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
@ -113,19 +117,17 @@ public class AdminRegisterCommandTest {
// Locales // Locales
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// Deletion Manager // Deletion Manager
idm = mock(IslandDeletionManager.class); idm = mock(IslandDeletionManager.class);
when(idm.inDeletion(Mockito.any())).thenReturn(false); when(idm.inDeletion(any())).thenReturn(false);
when(plugin.getIslandDeletionManager()).thenReturn(idm); when(plugin.getIslandDeletionManager()).thenReturn(idm);
// Plugin Manager // Plugin Manager
Server server = mock(Server.class);
PluginManager pim = mock(PluginManager.class); PluginManager pim = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pim); when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getServer()).thenReturn(server);
} }
@ -146,10 +148,9 @@ public class AdminRegisterCommandTest {
@Test @Test
public void testExecuteUnknownPlayer() { public void testExecuteUnknownPlayer() {
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(null);
when(pm.getUUID(Mockito.any())).thenReturn(null); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); verify(user).sendMessage(eq("general.errors.unknown-player"), eq("[name]"), eq("tastybento"));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
} }
/** /**
@ -158,12 +159,11 @@ public class AdminRegisterCommandTest {
@Test @Test
public void testExecutePlayerHasIsland() { public void testExecutePlayerHasIsland() {
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any())).thenReturn(false);
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); verify(user).sendMessage(eq("general.errors.player-has-island"));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-island"));
} }
/** /**
@ -171,13 +171,12 @@ public class AdminRegisterCommandTest {
*/ */
@Test @Test
public void testExecuteInTeam() { public void testExecuteInTeam() {
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); when(im.inTeam(any(), any())).thenReturn(true);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
Mockito.verify(user).sendMessage("commands.admin.register.cannot-register-team-player"); verify(user).sendMessage("commands.admin.register.cannot-register-team-player");
} }
/** /**
@ -185,21 +184,20 @@ public class AdminRegisterCommandTest {
*/ */
@Test @Test
public void testExecuteAlreadyOwnedIsland() { public void testExecuteAlreadyOwnedIsland() {
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); when(im.inTeam(any(), any())).thenReturn(false);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Location loc = mock(Location.class); Location loc = mock(Location.class);
// Island has owner // Island has owner
Island is = mock(Island.class); Island is = mock(Island.class);
when(is.getOwner()).thenReturn(uuid); when(is.getOwner()).thenReturn(uuid);
Optional<Island> opi = Optional.of(is); Optional<Island> opi = Optional.of(is);
when(im.getIslandAt(Mockito.any())).thenReturn(opi); when(im.getIslandAt(any())).thenReturn(opi);
when(user.getLocation()).thenReturn(loc); when(user.getLocation()).thenReturn(loc);
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
Mockito.verify(user).sendMessage("commands.admin.register.already-owned"); verify(user).sendMessage("commands.admin.register.already-owned");
} }
/** /**
@ -207,22 +205,21 @@ public class AdminRegisterCommandTest {
*/ */
@Test @Test
public void testExecuteInDeletionIsland() { public void testExecuteInDeletionIsland() {
when(idm.inDeletion(Mockito.any())).thenReturn(true); when(idm.inDeletion(any())).thenReturn(true);
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); when(im.inTeam(any(), any())).thenReturn(false);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Location loc = mock(Location.class); Location loc = mock(Location.class);
// Island has owner // Island has owner
Island is = mock(Island.class); Island is = mock(Island.class);
when(is.getOwner()).thenReturn(uuid); when(is.getOwner()).thenReturn(uuid);
Optional<Island> opi = Optional.of(is); Optional<Island> opi = Optional.of(is);
when(im.getIslandAt(Mockito.any())).thenReturn(opi); when(im.getIslandAt(any())).thenReturn(opi);
when(user.getLocation()).thenReturn(loc); when(user.getLocation()).thenReturn(loc);
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
Mockito.verify(user).sendMessage("commands.admin.register.in-deletion"); verify(user).sendMessage("commands.admin.register.in-deletion");
} }
/** /**
@ -230,24 +227,23 @@ public class AdminRegisterCommandTest {
*/ */
@Test @Test
public void testExecuteSuccess() { public void testExecuteSuccess() {
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); when(im.inTeam(any(), any())).thenReturn(false);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
Island is = mock(Island.class); Island is = mock(Island.class);
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432)); when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc); when(is.getCenter()).thenReturn(loc);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is); when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
Optional<Island> opi = Optional.of(is); Optional<Island> opi = Optional.of(is);
when(im.getIslandAt(Mockito.any())).thenReturn(opi); when(im.getIslandAt(any())).thenReturn(opi);
when(user.getLocation()).thenReturn(loc); when(user.getLocation()).thenReturn(loc);
String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
AdminRegisterCommand itl = new AdminRegisterCommand(ac); AdminRegisterCommand itl = new AdminRegisterCommand(ac);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
// Add other verifications // Add other verifications
Mockito.verify(user).sendMessage("commands.admin.register.registered-island", "[xyz]", "123,123,432"); verify(user).sendMessage(eq("commands.admin.register.registered-island"), eq("[xyz]"), eq("123,123,432"));
Mockito.verify(user).sendMessage(Mockito.eq("general.success")); verify(user).sendMessage(eq("general.success"));
} }
} }

View File

@ -33,7 +33,6 @@ import org.bukkit.Chunk;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@ -230,10 +229,8 @@ public class IslandsManagerTest {
// User location // User location
when(user.getLocation()).thenReturn(location); when(user.getLocation()).thenReturn(location);
// Server for events // Plugin Manager for events
Server server = mock(Server.class); when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getServer()).thenReturn(server);
when(server.getPluginManager()).thenReturn(pim);
// Addon // Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());