Added WorldSettings options to create island on first login

Implements https://github.com/BentoBoxWorld/BentoBox/issues/889
This commit is contained in:
Florian CUNY 2019-11-09 17:50:27 +01:00
parent e5646c72cd
commit a1b7df19c7
9 changed files with 156 additions and 4 deletions

View File

@ -391,4 +391,26 @@ public interface WorldSettings extends ConfigObject {
*/
boolean isKickedKeepInventory();
/* Create island on first login */
/**
*
* @return
* @since 1.9.0
*/
boolean isCreateIslandOnFirstLoginEnabled();
/**
*
* @return
* @since 1.9.0
*/
int getCreateIslandOnFirstLoginDelay();
/**
*
* @return
* @since 1.9.0
*/
boolean isCreateIslandOnFirstLoginAbortOnLogout();
}

View File

@ -1,7 +1,9 @@
package world.bentobox.bentobox.listeners;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -13,6 +15,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
@ -22,6 +25,7 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.BlueprintsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
@ -42,12 +46,48 @@ public class JoinLeaveListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerJoin(final PlayerJoinEvent event) {
User user = User.getInstance(event.getPlayer());
if (user.getUniqueId() == null) {
if (user == null || user.getUniqueId() == null) {
return;
}
UUID playerUUID = user.getUniqueId();
// Load player
// Check if player hasn't joined before
if (!players.isKnown(playerUUID)) {
// Create a database entry for that player
players.addPlayer(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 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 {
plugin.getServer().getScheduler().runTaskLater(plugin, createIsland, delay * 20L);
}
}
});
}
// Make sure the player is loaded into the cache (doesn't impact performance)
players.addPlayer(playerUUID);
// Reset island resets if required
plugin.getIWM().getOverWorlds().stream()
.filter(w -> event.getPlayer().getLastPlayed() < plugin.getIWM().getResetEpoch(w))
@ -75,7 +115,7 @@ public class JoinLeaveListener implements Listener {
}
// Clear inventory if required
clearPlayersInventory(Util.getWorld(event.getPlayer().getWorld()), User.getInstance(event.getPlayer()));
clearPlayersInventory(Util.getWorld(event.getPlayer().getWorld()), user);
}

View File

@ -799,4 +799,33 @@ public class IslandWorldManager {
return !gameModes.containsKey(world) || gameModes.get(world).getWorldSettings().isKickedKeepInventory();
}
/**
*
* @param world
* @return
* @since 1.9.0
*/
public boolean isCreateIslandOnFirstLoginEnabled(@NonNull World world) {
return gameModes.containsKey(world) && gameModes.get(world).getWorldSettings().isCreateIslandOnFirstLoginEnabled();
}
/**
*
* @param world
* @return
* @since 1.9.0
*/
public int getCreateIslandOnFirstLoginDelay(@NonNull World world) {
return gameModes.containsKey(world) ? gameModes.get(world).getWorldSettings().getCreateIslandOnFirstLoginDelay() : 0;
}
/**
*
* @param world
* @return
* @since 1.9.0
*/
public boolean isCreateIslandOnFirstLoginAbortOnLogout(@NonNull World world) {
return gameModes.containsKey(world) && gameModes.get(world).getWorldSettings().isCreateIslandOnFirstLoginAbortOnLogout();
}
}

View File

@ -39,7 +39,7 @@ public class IslandCreationPanel {
PanelBuilder pb = new PanelBuilder().name(user.getTranslation("commands.island.create.pick")).user(user);
// Get the bundles
Comparator<BlueprintBundle> sortByDisplayName = (p, o) -> p.getDisplayName().compareToIgnoreCase(o.getDisplayName());
List<BlueprintBundle> bbs = plugin.getBlueprintsManager().getBlueprintBundles((@NonNull GameModeAddon) command.getAddon()).values()
List<BlueprintBundle> bbs = plugin.getBlueprintsManager().getBlueprintBundles(command.getAddon()).values()
.stream().sorted(sortByDisplayName).collect(Collectors.toList());
// Loop through them and create items in the panel
for (BlueprintBundle bb : bbs) {

View File

@ -432,6 +432,7 @@ commands:
done: "&aDone! Your island is ready and waiting for you!"
pick: "&2Pick an island"
unknown-blueprint: "&cThat blueprint has not been loaded yet."
on-first-login: "&aWelcome! We will start preparing your island in a few seconds."
info:
description: "display info about your island or the player's island"
parameters: "<player>"

View File

@ -732,6 +732,21 @@ public class IslandGoCommandTest {
return false;
}
@Override
public boolean isCreateIslandOnFirstLoginEnabled() {
return false;
}
@Override
public int getCreateIslandOnFirstLoginDelay() {
return 0;
}
@Override
public boolean isCreateIslandOnFirstLoginAbortOnLogout() {
return false;
}
@Override
public @NonNull List<String> getOnJoinCommands() {
return null;

View File

@ -672,5 +672,20 @@ public class BannedCommandsTest {
return false;
}
@Override
public boolean isCreateIslandOnFirstLoginEnabled() {
return false;
}
@Override
public int getCreateIslandOnFirstLoginDelay() {
return 0;
}
@Override
public boolean isCreateIslandOnFirstLoginAbortOnLogout() {
return false;
}
}
}

View File

@ -633,5 +633,20 @@ public class BlockEndDragonTest {
return false;
}
@Override
public boolean isCreateIslandOnFirstLoginEnabled() {
return false;
}
@Override
public int getCreateIslandOnFirstLoginDelay() {
return 0;
}
@Override
public boolean isCreateIslandOnFirstLoginAbortOnLogout() {
return false;
}
}
}

View File

@ -396,4 +396,19 @@ public class TestWorldSettings implements WorldSettings {
return false;
}
@Override
public boolean isCreateIslandOnFirstLoginEnabled() {
return false;
}
@Override
public int getCreateIslandOnFirstLoginDelay() {
return 0;
}
@Override
public boolean isCreateIslandOnFirstLoginAbortOnLogout() {
return false;
}
}