mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-08 09:27:38 +01:00
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:
parent
f111bb9244
commit
0a86ddd0ab
@ -50,40 +50,14 @@ public class JoinLeaveListener implements Listener {
|
|||||||
}
|
}
|
||||||
UUID playerUUID = user.getUniqueId();
|
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
|
// Check if player hasn't joined before
|
||||||
if (!players.isKnown(playerUUID)) {
|
if (!players.isKnown(playerUUID)) {
|
||||||
plugin.getIWM().getOverWorlds().stream()
|
firstTime(user);
|
||||||
.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Reset island resets if required
|
||||||
plugin.getIWM().getOverWorlds().stream()
|
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
|
* This event will clean players inventor
|
||||||
* @param event SwitchWorld event.
|
* @param event SwitchWorld event.
|
||||||
|
@ -224,7 +224,7 @@ public class JoinLeaveListenerTest {
|
|||||||
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
|
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
|
||||||
jll.onPlayerJoin(event);
|
jll.onPlayerJoin(event);
|
||||||
// Verify
|
// Verify
|
||||||
verify(pm).addPlayer(any());
|
verify(pm, times(2)).addPlayer(any());
|
||||||
verify(pm, times(2)).save(any());
|
verify(pm, times(2)).save(any());
|
||||||
verify(player, never()).sendMessage(anyString());
|
verify(player, never()).sendMessage(anyString());
|
||||||
// Verify resets
|
// Verify resets
|
||||||
@ -312,7 +312,7 @@ public class JoinLeaveListenerTest {
|
|||||||
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
|
PlayerJoinEvent event = new PlayerJoinEvent(player, "");
|
||||||
jll.onPlayerJoin(event);
|
jll.onPlayerJoin(event);
|
||||||
// Verify
|
// Verify
|
||||||
verify(pm).addPlayer(any());
|
verify(pm, times(2)).addPlayer(any());
|
||||||
verify(pm, times(2)).save(any());
|
verify(pm, times(2)).save(any());
|
||||||
verify(player).sendMessage(eq("commands.island.create.on-first-login"));
|
verify(player).sendMessage(eq("commands.island.create.on-first-login"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user