feat: Add support for comma separated MVWorlds

This commit is contained in:
Ben Woo 2023-02-15 11:10:35 +08:00
parent bf5ab8d1b3
commit 28d71f7bc1
3 changed files with 44 additions and 13 deletions

View File

@ -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("<Gamerule> <Gamerule value> [World or *]")
@Description("Changes a gamerule in one or more worlds")
public void onGameruleCommand(BukkitCommandIssuer issuer,

View File

@ -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<String> currentWorlds = Sets.newHashSet(input.split(","));
return getMVWorldNames(context).stream()
.filter(world -> !currentWorlds.contains(world))
.map(world -> currentWorldsString + world)
.collect(Collectors.toList());
}
private List<String> getMVWorldNames(BukkitCommandCompletionContext context) {
String scope = context.getConfig("scope", "loaded");
List<String> worlds = new ArrayList<>();
switch (scope) {
@ -64,7 +85,6 @@ public class MVCommandCompletions extends PaperCommandCompletions {
worlds.addAll(worldManager.getUnloadedWorlds());
break;
}
return worlds;
}

View File

@ -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<MVWorld> 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) {