Prevent home teleport when already home teleporting

Home teleporting is async so it's possible to issue the command multiple
times. This puts a flag in so that if a playeer is mid-teleport, then
issuing the go command will just repeat the text that the player is
teleporting home.

https://github.com/BentoBoxWorld/BentoBox/issues/1637
This commit is contained in:
tastybento 2021-09-03 15:39:08 -07:00
parent 23857501f5
commit 5bb12d53bd
3 changed files with 31 additions and 0 deletions

View File

@ -32,6 +32,12 @@ public class IslandGoCommand extends DelayedTeleportCommand {
@Override @Override
public boolean canExecute(User user, String label, List<String> args) { public boolean canExecute(User user, String label, List<String> args) {
// Check if mid-teleport
if (getIslands().isGoingHome(user)) {
// Tell them again that it's in progress
user.sendMessage("commands.island.go.teleport");
return false;
}
// Check if the island is reserved // Check if the island is reserved
Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
if (island == null) { if (island == null) {

View File

@ -104,6 +104,8 @@ public class IslandsManager {
private boolean isSaveTaskRunning; private boolean isSaveTaskRunning;
private final Set<UUID> goingHome;
/** /**
* Islands Manager * Islands Manager
* @param plugin - plugin * @param plugin - plugin
@ -119,6 +121,8 @@ public class IslandsManager {
// This list should always be empty unless database deletion failed // This list should always be empty unless database deletion failed
// In that case a purge utility may be required in the future // In that case a purge utility may be required in the future
deletedIslands = new ArrayList<>(); deletedIslands = new ArrayList<>();
// Mid-teleport players going home
goingHome = new HashSet<>();
} }
/** /**
@ -1057,6 +1061,7 @@ public class IslandsManager {
CompletableFuture<Boolean> result = new CompletableFuture<>(); CompletableFuture<Boolean> result = new CompletableFuture<>();
User user = User.getInstance(player); User user = User.getInstance(player);
user.sendMessage("commands.island.go.teleport"); user.sendMessage("commands.island.go.teleport");
goingHome.add(user.getUniqueId());
// Stop any gliding // Stop any gliding
player.setGliding(false); player.setGliding(false);
// Check if the player is a passenger in a boat // Check if the player is a passenger in a boat
@ -1156,6 +1161,8 @@ public class IslandsManager {
// Set the game mode // Set the game mode
user.setGameMode(plugin.getIWM().getDefaultGameMode(world)); user.setGameMode(plugin.getIWM().getDefaultGameMode(world));
// Remove from mid-teleport set
goingHome.remove(user.getUniqueId());
} }
} }
@ -1882,4 +1889,12 @@ public class IslandsManager {
return r; return r;
} }
/**
* Is user mid home teleport?
* @return true or false
*/
public boolean isGoingHome(User user) {
return goingHome.contains(user.getUniqueId());
}
} }

View File

@ -186,6 +186,16 @@ public class IslandGoCommandTest {
Mockito.framework().clearInlineMocks(); Mockito.framework().clearInlineMocks();
} }
/**
* Test method for {@link IslandGoCommand#canExecute(User, String, List)}
*/
@Test
public void testExecuteMidTeleport() {
when(im.isGoingHome(user)).thenReturn(true);
assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList()));
verify(player).sendMessage("commands.island.go.teleport");
}
/** /**
* Test method for {@link IslandGoCommand#canExecute(User, String, List)} * Test method for {@link IslandGoCommand#canExecute(User, String, List)}
*/ */