Fixes island creation on joining.

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1221

The original code could actually never work because the check for
whether a player had played before or not was occuring immediately after
addPlayer, which added the player to the server. Also, the code to run
was running in a thread and not on the main thread, so if it had run
could have caused errors.
This commit is contained in:
tastybento 2020-03-10 18:30:50 -07:00
parent f111bb9244
commit 0a86ddd0ab
2 changed files with 40 additions and 32 deletions

View File

@ -50,40 +50,14 @@ public class JoinLeaveListener implements Listener {
}
UUID playerUUID = user.getUniqueId();
// Make sure the player is loaded into the cache or create the player if they don't exist
players.addPlayer(playerUUID);
// Check if player hasn't joined before
if (!players.isKnown(playerUUID)) {
plugin.getIWM().getOverWorlds().stream()
.filter(w -> plugin.getIWM().isCreateIslandOnFirstLoginEnabled(w))
.forEach(w -> {
// Even if that'd be extremely unlikely, it's better to check if the player doesn't have an island already.
if (!(plugin.getIslands().hasIsland(w, user) || plugin.getIslands().inTeam(w, user.getUniqueId()))) {
int delay = plugin.getIWM().getCreateIslandOnFirstLoginDelay(w);
user.sendMessage("commands.island.create.on-first-login",
TextVariables.NUMBER, String.valueOf(delay));
Runnable createIsland = () -> {
// should only execute if:
// - abort on logout is false
// - abort on logout is true && user is online
if (!plugin.getIWM().isCreateIslandOnFirstLoginAbortOnLogout(w) || user.isOnline()){
plugin.getIWM().getAddon(w).ifPresent(addon -> addon.getPlayerCommand()
.map(command -> command.getSubCommand("create").orElse(null))
.ifPresent(command -> command.execute(user, "create", Collections.singletonList(BlueprintsManager.DEFAULT_BUNDLE_NAME))));
}
};
if (delay <= 0) {
createIsland.run();
} else {
Bukkit.getScheduler().runTaskLater(plugin, createIsland, delay * 20L);
}
}
});
firstTime(user);
}
// Make sure the player is loaded into the cache or create the player if they don't exist
players.addPlayer(playerUUID);
// Reset island resets if required
plugin.getIWM().getOverWorlds().stream()
@ -116,6 +90,40 @@ public class JoinLeaveListener implements Listener {
}
private void firstTime(User user) {
// Make sure the player is loaded into the cache or create the player if they don't exist
players.addPlayer(user.getUniqueId());
plugin.getIWM().getOverWorlds().stream()
.filter(w -> plugin.getIWM().isCreateIslandOnFirstLoginEnabled(w))
.forEach(w -> {
// Even if that'd be extremely unlikely, it's better to check if the player doesn't have an island already.
if (!(plugin.getIslands().hasIsland(w, user) || plugin.getIslands().inTeam(w, user.getUniqueId()))) {
int delay = plugin.getIWM().getCreateIslandOnFirstLoginDelay(w);
user.sendMessage("commands.island.create.on-first-login",
TextVariables.NUMBER, String.valueOf(delay));
Runnable createIsland = () -> {
// should only execute if:
// - abort on logout is false
// - abort on logout is true && user is online
if (!plugin.getIWM().isCreateIslandOnFirstLoginAbortOnLogout(w) || user.isOnline()){
plugin.getIWM().getAddon(w).ifPresent(addon -> addon.getPlayerCommand()
.map(command -> command.getSubCommand("create").orElse(null))
.ifPresent(command -> command.execute(user, "create", Collections.singletonList(BlueprintsManager.DEFAULT_BUNDLE_NAME))));
}
};
if (delay <= 0) {
Bukkit.getScheduler().runTask(plugin, createIsland);
} else {
Bukkit.getScheduler().runTaskLater(plugin, createIsland, delay * 20L);
}
}
});
}
/**
* This event will clean players inventor
* @param event SwitchWorld event.

View File

@ -224,7 +224,7 @@ public class JoinLeaveListenerTest {
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
jll.onPlayerJoin(event);
// Verify
verify(pm).addPlayer(any());
verify(pm, times(2)).addPlayer(any());
verify(pm, times(2)).save(any());
verify(player, never()).sendMessage(anyString());
// Verify resets
@ -312,7 +312,7 @@ public class JoinLeaveListenerTest {
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
jll.onPlayerJoin(event);
// Verify
verify(pm).addPlayer(any());
verify(pm, times(2)).addPlayer(any());
verify(pm, times(2)).save(any());
verify(player).sendMessage(eq("commands.island.create.on-first-login"));
}