From 180680f2972496241db7f443e06fefeaea0ff0dc Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:01:26 +0800 Subject: [PATCH 1/3] Add method to get MultiverseWorld from a bukkit world. --- .../mvplugins/multiverse/core/world/WorldManager.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java b/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java index c4064263..1d5d1777 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java @@ -737,6 +737,17 @@ public class WorldManager { return !isLoadedWorld(worldName) && isWorld(worldName); } + /** + * Get a world that may or may not be loaded. It will an {@link LoadedMultiverseWorld} if the world is loaded, + * otherwise returns an {@link MultiverseWorld} instance. + * + * @param world The bukkit world to get. + * @return The world if it exists. + */ + public Option getWorld(@Nullable World world) { + return Option.of(world).flatMap(this::getWorld); + } + /** * Get a world that may or may not be loaded. It will an {@link LoadedMultiverseWorld} if the world is loaded, * otherwise returns an {@link MultiverseWorld} instance. From fb240a19fa8404595d2c1508fec44c5d8e57c35b Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:02:29 +0800 Subject: [PATCH 2/3] Add context resolving for MultiverseWorld class --- .../core/commandtools/MVCommandContexts.java | 126 ++++++++++++------ 1 file changed, 85 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java b/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java index 79a8de64..d5f38894 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java +++ b/src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java @@ -27,10 +27,11 @@ import org.mvplugins.multiverse.core.display.filters.DefaultContentFilter; import org.mvplugins.multiverse.core.display.filters.RegexContentFilter; import org.mvplugins.multiverse.core.utils.PlayerFinder; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; +import org.mvplugins.multiverse.core.world.MultiverseWorld; import org.mvplugins.multiverse.core.world.WorldManager; @Service -public class MVCommandContexts extends PaperCommandContexts { +class MVCommandContexts extends PaperCommandContexts { private final MVCommandManager mvCommandManager; private final DestinationsProvider destinationsProvider; @@ -38,12 +39,11 @@ public class MVCommandContexts extends PaperCommandContexts { private final MVCoreConfig config; @Inject - public MVCommandContexts( + MVCommandContexts( MVCommandManager mvCommandManager, DestinationsProvider destinationsProvider, WorldManager worldManager, - MVCoreConfig config - ) { + MVCoreConfig config) { super(mvCommandManager); this.mvCommandManager = mvCommandManager; this.destinationsProvider = destinationsProvider; @@ -56,9 +56,10 @@ public class MVCommandContexts extends PaperCommandContexts { registerContext(ParsedDestination.class, this::parseDestination); registerContext(GameRule.class, this::parseGameRule); registerContext(GameRuleValue.class, this::parseGameRuleValue); + registerIssuerAwareContext(LoadedMultiverseWorld.class, this::parseLoadedMultiverseWorld); + registerIssuerAwareContext(LoadedMultiverseWorld[].class, this::parseLoadedMultiverseWorldArray); + registerIssuerAwareContext(MultiverseWorld.class, this::parseMultiverseWorld); registerContext(MVConfigValue.class, this::parseMVConfigValue); - registerIssuerAwareContext(LoadedMultiverseWorld.class, this::parseMVWorld); - registerIssuerAwareContext(LoadedMultiverseWorld[].class, this::parseMVWorldArray); registerIssuerAwareContext(Player.class, this::parsePlayer); registerIssuerAwareContext(Player[].class, this::parsePlayerArray); } @@ -129,40 +130,7 @@ public class MVCommandContexts extends PaperCommandContexts { return new GameRuleValue(resolvedValue); } - private MVConfigValue parseMVConfigValue(BukkitCommandExecutionContext context) { - String configName = (String) context.getResolvedArg(String.class); - if (Strings.isNullOrEmpty(configName)) { - throw new InvalidCommandArgument("No config name specified."); - } - Option node = config.getNodes().findNode(configName); - if (node.isEmpty()) { - throw new InvalidCommandArgument("The config " + configName + " is not valid."); - } - - String valueString = context.getFirstArg(); - if (Strings.isNullOrEmpty(valueString)) { - throw new InvalidCommandArgument("No config value specified."); - } - - if (!(node.get() instanceof ValueNode)) { - context.popFirstArg(); - return new MVConfigValue(valueString); - } - - ContextResolver resolver = getResolver(((ValueNode) node.get()).getType()); - if (resolver == null) { - context.popFirstArg(); - return new MVConfigValue(valueString); - } - - Object resolvedValue = resolver.getContext(context); - if (resolvedValue == null) { - throw new InvalidCommandArgument("The config value " + valueString + " is not valid for config " + configName + "."); - } - return new MVConfigValue(resolvedValue); - } - - private LoadedMultiverseWorld parseMVWorld(BukkitCommandExecutionContext context) { + private LoadedMultiverseWorld parseLoadedMultiverseWorld(BukkitCommandExecutionContext context) { String resolve = context.getFlagValue("resolve", ""); // Get world based on sender only @@ -205,7 +173,7 @@ public class MVCommandContexts extends PaperCommandContexts { throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world."); } - private LoadedMultiverseWorld[] parseMVWorldArray(BukkitCommandExecutionContext context) { + private LoadedMultiverseWorld[] parseLoadedMultiverseWorldArray(BukkitCommandExecutionContext context) { String resolve = context.getFlagValue("resolve", ""); LoadedMultiverseWorld playerWorld = null; @@ -265,6 +233,82 @@ public class MVCommandContexts extends PaperCommandContexts { throw new InvalidCommandArgument("World " + worldStrings + " is not a loaded multiverse world."); } + private MultiverseWorld parseMultiverseWorld(BukkitCommandExecutionContext context) { + String resolve = context.getFlagValue("resolve", ""); + + // Get world based on sender only + if (resolve.equals("issuerOnly")) { + if (context.getIssuer().isPlayer()) { + return worldManager.getWorld(context.getIssuer().getPlayer().getWorld()).getOrNull(); + } + if (context.isOptional()) { + return null; + } + throw new InvalidCommandArgument("This command can only be used by a player in a Multiverse World."); + } + + String worldName = context.getFirstArg(); + MultiverseWorld world = worldManager.getWorld(worldName).getOrNull(); + + // Get world based on input, fallback to sender if input is not a world + if (resolve.equals("issuerAware")) { + if (world != null) { + context.popFirstArg(); + return world; + } + if (context.getIssuer().isPlayer()) { + return worldManager.getLoadedWorld(context.getIssuer().getPlayer().getWorld()).getOrNull(); + } + if (context.isOptional()) { + return null; + } + throw new InvalidCommandArgument("Player is not in a Multiverse World."); + } + + // Get world based on input only + if (world != null) { + context.popFirstArg(); + return world; + } + if (context.isOptional()) { + return null; + } + throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world."); + } + + private MVConfigValue parseMVConfigValue(BukkitCommandExecutionContext context) { + String configName = (String) context.getResolvedArg(String.class); + if (Strings.isNullOrEmpty(configName)) { + throw new InvalidCommandArgument("No config name specified."); + } + Option node = config.getNodes().findNode(configName); + if (node.isEmpty()) { + throw new InvalidCommandArgument("The config " + configName + " is not valid."); + } + + String valueString = context.getFirstArg(); + if (Strings.isNullOrEmpty(valueString)) { + throw new InvalidCommandArgument("No config value specified."); + } + + if (!(node.get() instanceof ValueNode)) { + context.popFirstArg(); + return new MVConfigValue(valueString); + } + + ContextResolver resolver = getResolver(((ValueNode) node.get()).getType()); + if (resolver == null) { + context.popFirstArg(); + return new MVConfigValue(valueString); + } + + Object resolvedValue = resolver.getContext(context); + if (resolvedValue == null) { + throw new InvalidCommandArgument("The config value " + valueString + " is not valid for config " + configName + "."); + } + return new MVConfigValue(resolvedValue); + } + private Player parsePlayer(BukkitCommandExecutionContext context) { String resolve = context.getFlagValue("resolve", ""); From b99de26deb1bcb6f3296db6a07da71f2b16b1fe0 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:06:08 +0800 Subject: [PATCH 3/3] Use MultiverseWorld instead of worldname string for RemoveCommand --- .../mvplugins/multiverse/core/commands/RemoveCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/RemoveCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/RemoveCommand.java index 677f55dc..9879dd62 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/RemoveCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/RemoveCommand.java @@ -17,6 +17,7 @@ import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; import org.mvplugins.multiverse.core.commandtools.MVCommandManager; import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; import org.mvplugins.multiverse.core.utils.MVCorei18n; +import org.mvplugins.multiverse.core.world.MultiverseWorld; import org.mvplugins.multiverse.core.world.WorldManager; @Service @@ -43,8 +44,8 @@ class RemoveCommand extends MultiverseCommand { @Conditions("mvworlds:scope=both") @Syntax("") @Description("{@@mv-core.remove.world.description}") - String worldName) { - worldManager.removeWorld(worldName) + MultiverseWorld world) { + worldManager.removeWorld(world) .onSuccess(removedWorldName -> { Logging.fine("World remove success: " + removedWorldName); issuer.sendInfo(MVCorei18n.REMOVEWORLD_REMOVED, "{world}", removedWorldName);