(Complex) command suggestions for /plot set

This commit is contained in:
Alexander Söderberg 2020-05-11 19:37:25 +02:00
parent df01f9bea7
commit 8944be5319
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
3 changed files with 72 additions and 0 deletions

View File

@ -30,9 +30,14 @@ import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.StringMan;
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.biome.BiomeTypes;
import java.util.Collection;
import java.util.Locale;
import java.util.stream.Collectors;
@CommandDeclaration(command = "setbiome", @CommandDeclaration(command = "setbiome",
permission = "plots.set.biome", permission = "plots.set.biome",
description = "Set the plot biome", description = "Set the plot biome",
@ -68,4 +73,13 @@ public class Biome extends SetCommand {
}); });
return true; return true;
} }
@Override public Collection<Command> tab(final PlotPlayer player, final String[] args, final boolean space) {
return SuggestionHelper.getNamespacedRegistrySuggestions(BiomeType.REGISTRY, args[0])
.map(value -> value.toLowerCase(Locale.ENGLISH).replace("minecraft:", ""))
.filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
.map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {})
.collect(Collectors.toList());
}
} }

View File

@ -42,9 +42,12 @@ import com.sk89q.worldedit.world.block.BlockTypes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@CommandDeclaration(command = "set", @CommandDeclaration(command = "set",
description = "Set a plot value", description = "Set a plot value",
@ -136,6 +139,16 @@ public class Set extends SubCommand {
} }
return false; return false;
} }
@Override
public Collection<Command> tab(final PlotPlayer player, final String[] args, final boolean space) {
return PatternUtil.getSuggestions(player, StringMan.join(args, ",").trim())
.stream()
.map(value -> value.toLowerCase(Locale.ENGLISH).replace("minecraft:", ""))
.filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
.map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {})
.collect(Collectors.toList());
}
}; };
} }
@ -177,4 +190,38 @@ public class Set extends SubCommand {
} }
return noArgs(player); return noArgs(player);
} }
@Override public Collection<Command> tab(final PlotPlayer player, final String[] args, final boolean space) {
if (args.length == 1) {
return Stream
.of("biome", "alias", "home", "main", "floor", "air", "all", "border", "wall", "outline", "middle")
.filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
.map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {})
.collect(Collectors.toList());
} else if (args.length > 1) {
// Additional checks
Plot plot = player.getCurrentPlot();
if (plot == null) {
return new ArrayList<>();
}
final String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
final Command cmd = MainCommand.getInstance().getCommand("set" + args[0]);
if (cmd != null) {
if (!Permissions.hasPermission(player, cmd.getPermission(), true)) {
return new ArrayList<>();
}
return cmd.tab(player, newArgs, space);
}
// components
HashSet<String> components = new HashSet<>(Arrays.asList(plot.getManager().getPlotComponents(plot.getId())));
if (components.contains(args[0].toLowerCase())) {
return this.component.tab(player, newArgs, space);
}
}
return tabOf(player, args, space);
}
} }

View File

@ -43,6 +43,9 @@ import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class PatternUtil { public class PatternUtil {
public static BaseBlock apply(@NotNull Pattern pattern, int x, int y, int z) { public static BaseBlock apply(@NotNull Pattern pattern, int x, int y, int z) {
@ -59,6 +62,14 @@ public class PatternUtil {
return parse(plotPlayer, input, true); return parse(plotPlayer, input, true);
} }
public static List<String> getSuggestions(PlotPlayer plotPlayer, String input) {
try {
return WorldEdit.getInstance().getPatternFactory().getSuggestions(input);
} catch (final Exception ignored) {
}
return new ArrayList<>();
}
public static Pattern parse(PlotPlayer plotPlayer, String input, boolean allowLegacy) { public static Pattern parse(PlotPlayer plotPlayer, String input, boolean allowLegacy) {
ParserContext context = new ParserContext(); ParserContext context = new ParserContext();
if (plotPlayer != null) { if (plotPlayer != null) {