Skip to content

Commit

Permalink
Prevent home teleport when already home teleporting
Browse files Browse the repository at this point in the history
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.

#1637
  • Loading branch information
tastybento committed Sep 3, 2021
1 parent 2385750 commit 5bb12d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void setup() {

@Override
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
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
if (island == null) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/world/bentobox/bentobox/managers/IslandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public class IslandsManager {

private boolean isSaveTaskRunning;

private final Set<UUID> goingHome;

/**
* Islands Manager
* @param plugin - plugin
Expand All @@ -119,6 +121,8 @@ public IslandsManager(@NonNull BentoBox plugin){
// This list should always be empty unless database deletion failed
// In that case a purge utility may be required in the future
deletedIslands = new ArrayList<>();
// Mid-teleport players going home
goingHome = new HashSet<>();
}

/**
Expand Down Expand Up @@ -1057,6 +1061,7 @@ private CompletableFuture<Boolean> homeTeleportAsync(@NonNull World world, @NonN
CompletableFuture<Boolean> result = new CompletableFuture<>();
User user = User.getInstance(player);
user.sendMessage("commands.island.go.teleport");
goingHome.add(user.getUniqueId());
// Stop any gliding
player.setGliding(false);
// Check if the player is a passenger in a boat
Expand Down Expand Up @@ -1156,6 +1161,8 @@ private void teleported(World world, User user, String name, boolean newIsland,

// Set the game mode
user.setGameMode(plugin.getIWM().getDefaultGameMode(world));
// Remove from mid-teleport set
goingHome.remove(user.getUniqueId());
}
}

Expand Down Expand Up @@ -1882,4 +1889,12 @@ public CompletableFuture<Boolean> checkTeams(User user, World world) {
return r;
}

/**
* Is user mid home teleport?
* @return true or false
*/
public boolean isGoingHome(User user) {
return goingHome.contains(user.getUniqueId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public void tearDown() {
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)}
*/
Expand Down

0 comments on commit 5bb12d5

Please sign in to comment.