From 28d71f7bc1ddd5615b89cc42578e97d66a97ad67 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:10:35 +0800 Subject: [PATCH] feat: Add support for comma separated MVWorlds --- .../commands/GameruleCommand.java | 2 +- .../commandtools/MVCommandCompletions.java | 24 ++++++++++++-- .../commandtools/MVCommandContexts.java | 31 +++++++++++++------ 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java index b1dadb73..003a453c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java @@ -23,7 +23,7 @@ public class GameruleCommand extends MultiverseCoreCommand { @Subcommand("gamerule") @CommandPermission("multiverse.core.gamerule") - @CommandCompletion("@gamerules true|false|@range:1-10 @mvworlds|*") + @CommandCompletion("@gamerules true|false|@range:1-10 @mvworlds:multiple|*") @Syntax(" [World or *]") @Description("Changes a gamerule in one or more worlds") public void onGameruleCommand(BukkitCommandIssuer issuer, diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index 22e268a4..b5000270 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -1,11 +1,17 @@ package com.onarandombox.MultiverseCore.commandtools; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import co.aikar.commands.BukkitCommandCompletionContext; import co.aikar.commands.BukkitCommandIssuer; import co.aikar.commands.PaperCommandCompletions; +import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; @@ -49,6 +55,21 @@ public class MVCommandCompletions extends PaperCommandCompletions { return Collections.emptyList(); } + if (!context.hasConfig("multiple")) { + return getMVWorldNames(context); + } + + String input = context.getInput(); + int lastComma = input.lastIndexOf(','); + String currentWorldsString = input.substring(0, lastComma + 1); + Set currentWorlds = Sets.newHashSet(input.split(",")); + return getMVWorldNames(context).stream() + .filter(world -> !currentWorlds.contains(world)) + .map(world -> currentWorldsString + world) + .collect(Collectors.toList()); + } + + private List getMVWorldNames(BukkitCommandCompletionContext context) { String scope = context.getConfig("scope", "loaded"); List worlds = new ArrayList<>(); switch (scope) { @@ -64,7 +85,6 @@ public class MVCommandCompletions extends PaperCommandCompletions { worlds.addAll(worldManager.getUnloadedWorlds()); break; } - return worlds; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index 87aa8642..03b81a7b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -1,5 +1,8 @@ package com.onarandombox.MultiverseCore.commandtools; +import java.util.HashSet; +import java.util.Set; + import co.aikar.commands.BukkitCommandExecutionContext; import co.aikar.commands.BukkitCommandIssuer; import co.aikar.commands.InvalidCommandArgument; @@ -156,17 +159,25 @@ public class MVCommandContexts extends PaperCommandContexts { throw new InvalidCommandArgument("This command can only be used by a player in a Multiverse World."); } - String worldName = context.getFirstArg(); - MVWorld world = plugin.getMVWorldManager().getMVWorld(worldName); - MVWorld[] worlds = "*".equals(worldName) - ? plugin.getMVWorldManager().getMVWorlds().toArray(new MVWorld[0]) - : (world != null ? new MVWorld[]{world} : null); + String worldStrings = context.getFirstArg(); + String[] worldNames = worldStrings.split(","); + Set worlds = new HashSet<>(worldNames.length); + for (String worldName : worldNames) { + if ("*".equals(worldName)) { + return plugin.getMVWorldManager().getMVWorlds().toArray(new MVWorld[0]); + } + MVWorld world = plugin.getMVWorldManager().getMVWorld(worldName); + if (world == null) { + throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world."); + } + worlds.add(world); + } // Get world based on input, fallback to sender if input is not a world if (resolve.equals("issuerAware")) { - if (worlds != null) { + if (!worlds.isEmpty()) { context.popFirstArg(); - return worlds; + return worlds.toArray(new MVWorld[0]); } if (playerWorld != null) { return new MVWorld[]{playerWorld}; @@ -178,14 +189,14 @@ public class MVCommandContexts extends PaperCommandContexts { } // Get world based on input only - if (worlds != null) { + if (!worlds.isEmpty()) { context.popFirstArg(); - return worlds; + return worlds.toArray(new MVWorld[0]); } if (!context.isOptional()) { return null; } - throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world."); + throw new InvalidCommandArgument("World " + worldStrings + " is not a loaded multiverse world."); } private Player parsePlayer(BukkitCommandExecutionContext context) {