From 0a86ddd0ab96c77f97441becc8ccc36710be6f2e Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 10 Mar 2020 18:30:50 -0700 Subject: [PATCH] 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. --- .../bentobox/listeners/JoinLeaveListener.java | 68 +++++++++++-------- .../listeners/JoinLeaveListenerTest.java | 4 +- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 61f82f45a..23b8bd2ed 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -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. diff --git a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java index 9e6b49efa..3c67df954 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java @@ -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")); }