From 3b7a801a0c770559eadb8d260cc6e17a0bb3d14f Mon Sep 17 00:00:00 2001 From: zax71 Date: Mon, 4 Sep 2023 12:30:50 +0100 Subject: [PATCH] Implement JoinDestination and EnableJoinDestination config options --- .../MultiverseCore/api/MVConfig.java | 24 +++++++ .../MultiverseCore/config/MVCoreConfig.java | 26 ++++++++ .../config/MVCoreConfigNodes.java | 16 ++++- .../destination/DestinationsProvider.java | 3 +- .../listeners/MVPlayerListener.java | 63 ++++++++++++++++--- 5 files changed, 123 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 55d11a78..aaf3a330 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -125,6 +125,30 @@ public interface MVConfig { */ String getFirstSpawnLocation(); + /** + * Sets alwaysSpawnDestination + * @param alwaysSpawnWorld The new value + */ + void setJoinDestination(String alwaysSpawnWorld); + + /** + * Gets alwaysSpawnDestination + * @return alwaysSpawnLocation + */ + String getJoinDestination(); + + /** + * Sets alwaysSpawnDestination + * @param enableAlwaysSpawnDestination The new value + */ + void setEnableJoinDestination(boolean enableAlwaysSpawnDestination); + + /** + * Gets enableAlwaysSpawnDestination + * @return enableAlwaysSpawnDestination + */ + boolean getEnableJoinDestination(); + /** * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 8afb8a18..b0257341 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore.config; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; @@ -19,6 +20,7 @@ import io.vavr.control.Try; import jakarta.inject.Inject; import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jvnet.hk2.annotations.Service; @Service @@ -180,6 +182,30 @@ public class MVCoreConfig implements MVConfig { return configHandle.get(configNodes.FIRST_SPAWN_LOCATION); } + @Override + public void setJoinDestination(String alwaysSpawnWorld) { + configHandle.set(configNodes.JOIN_DESTINATION, alwaysSpawnWorld); + } + + @Override + @Nullable + public String getJoinDestination() { + if (Objects.equals(configHandle.get(configNodes.JOIN_DESTINATION), "")) { + return null; + } + return configHandle.get(configNodes.JOIN_DESTINATION); + } + + @Override + public void setEnableJoinDestination(boolean enableJoinDestination) { + configHandle.set(configNodes.ENABLE_JOIN_DESTINATION, enableJoinDestination); + } + + @Override + public boolean getEnableJoinDestination() { + return configHandle.get(configNodes.ENABLE_JOIN_DESTINATION); + } + @Override public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) { configHandle.set(configNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java index 9bdae949..c878a7b6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java @@ -7,7 +7,6 @@ import com.onarandombox.MultiverseCore.configuration.node.Node; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; import com.onarandombox.MultiverseCore.exceptions.MultiverseException; -import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.vavr.control.Try; import org.bukkit.plugin.PluginManager; @@ -113,6 +112,21 @@ class MVCoreConfigNodes { .name("first-spawn-location") .build()); + public final ConfigNode JOIN_DESTINATION = node(ConfigNode.builder("spawn.always-spawn-destination", String.class) + .comment("") + .comment("Sets the destination that Multiverse will use to spawn players on every login") + .comment("Set blank to disable") + .defaultValue("") + .name("always-spawn-destination") + .build()); + + public final ConfigNode ENABLE_JOIN_DESTINATION = node(ConfigNode.builder("spawn.enable-always-spawn-destination", Boolean.class) + .comment("") + .comment("Enables always-spawn-destination") + .defaultValue(false) + .name("enable-always-spawn-destination") + .build()); + private final ConfigHeaderNode PORTAL_HEADER = node(ConfigHeaderNode.builder("portal") .comment("") .comment("") diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java index af6a7ea1..2b886948 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java @@ -85,7 +85,8 @@ public class DestinationsProvider { * @param destinationString The destination string. * @return The destination object, or null if invalid format. */ - public ParsedDestination parseDestination(String destinationString) { + @Nullable + public ParsedDestination parseDestination(@NotNull String destinationString) { String[] items = destinationString.split(SEPARATOR, 2); String idString = items[0]; diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index eb59d129..6b698a09 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -18,6 +18,8 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.config.MVCoreConfig; +import com.onarandombox.MultiverseCore.destination.DestinationsProvider; +import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.economy.MVEconomist; import com.onarandombox.MultiverseCore.event.MVRespawnEvent; import com.onarandombox.MultiverseCore.inject.InjectableListener; @@ -43,6 +45,7 @@ import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; /** @@ -53,6 +56,7 @@ public class MVPlayerListener implements InjectableListener { private final Plugin plugin; private final MVCoreConfig config; private final Provider worldManagerProvider; + private final DestinationsProvider destinationsProvider; private final SafeTTeleporter safeTTeleporter; private final Server server; private final TeleportQueue teleportQueue; @@ -68,6 +72,7 @@ public class MVPlayerListener implements InjectableListener { MultiverseCore plugin, MVCoreConfig config, Provider worldManagerProvider, + DestinationsProvider destinationsProvider, SafeTTeleporter safeTTeleporter, Server server, TeleportQueue teleportQueue, @@ -79,6 +84,7 @@ public class MVPlayerListener implements InjectableListener { this.plugin = plugin; this.config = config; this.worldManagerProvider = worldManagerProvider; + this.destinationsProvider = destinationsProvider; this.safeTTeleporter = safeTTeleporter; this.server = server; this.teleportQueue = teleportQueue; @@ -154,26 +160,69 @@ public class MVPlayerListener implements InjectableListener { */ @EventHandler public void playerJoin(PlayerJoinEvent event) { - Player p = event.getPlayer(); - if (!p.hasPlayedBefore()) { + Player player = event.getPlayer(); + if (!player.hasPlayedBefore()) { Logging.finer("Player joined for the FIRST time!"); if (config.getFirstSpawnOverride()) { Logging.fine("Moving NEW player to(firstspawnoverride): " + getWorldManager().getFirstSpawnWorld().getSpawnLocation()); - this.sendPlayerToDefaultWorld(p); + this.sendPlayerToDefaultWorld(player); } return; + } else { Logging.finer("Player joined AGAIN!"); + // Ensure the player still has permission to access the world they were in if (config.getEnforceAccess() // check this only if we're enforcing access! - && !permissionsChecker.hasWorldAccessPermission(p, getWorldManager().getFirstSpawnWorld())) { - p.sendMessage("[MV] - Sorry you can't be in this world anymore!"); - this.sendPlayerToDefaultWorld(p); + && !permissionsChecker.hasWorldAccessPermission(player, getWorldManager().getFirstSpawnWorld())) { + player.sendMessage("[MV] - Sorry you can't be in this world anymore!"); + this.sendPlayerToDefaultWorld(player); } + + handleJoinDestination(player); } + // Handle the Players GameMode setting for the new world. this.handleGameModeAndFlight(event.getPlayer(), event.getPlayer().getWorld()); - playerWorld.put(p.getName(), p.getWorld().getName()); + playerWorld.put(player.getName(), player.getWorld().getName()); + } + + /** + * Will teleport the player to the destination specified in config + * @param player The {@link Player} to teleport + */ + private void handleJoinDestination(@NotNull Player player) { + if (!config.getEnableJoinDestination()) { + Logging.finer("JoinDestination is disabled"); + // User has disabled the feature in config + return; + } + + if (config.getJoinDestination() == null) { + Logging.warning("Joindestination is enabled but no destination has been specified in config!"); + return; + } + + + + Logging.finer("JoinDestination is " + config.getJoinDestination()); + + ParsedDestination joinDestination = destinationsProvider.parseDestination(config.getJoinDestination()); + + if (joinDestination == null) { + Logging.warning("The destination in JoinDestination in config is invalid"); + return; + } + + Location joinDestinationLocation = joinDestination.getLocation(player); + + if (joinDestinationLocation == null) { + Logging.finer("Not teleporting " + player.getName() + " because joinDestination is null"); + return; + } + + // Finally, teleport the player + player.teleport(joinDestinationLocation); } /**