From 3c17b76b1c5ee5f59dc4fcf02026f1a031dca55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Mon, 11 May 2020 11:21:32 +0200 Subject: [PATCH 1/3] Remove dum-dum line break --- .../main/java/com/plotsquared/bukkit/listener/PlayerEvents.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java index fb78d7ccd..1c7b9d5f5 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java @@ -2993,7 +2993,6 @@ public class PlayerEvents extends PlotListener implements Listener { } } - @EventHandler public void onDeath(final PlayerDeathEvent event) { final Plot plot = BukkitUtil.getPlayer(event.getEntity()).getCurrentPlot(); if (plot != null && plot.getFlag(KeepInventoryFlag.class)) { From df01f9bea794317ca6f320ef4b71b84c1fdc015d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Mon, 11 May 2020 18:35:06 +0200 Subject: [PATCH 2/3] Add a list of blocks that cannot be used in `/plot set ` --- .../com/plotsquared/core/command/Set.java | 36 ++++++++++++++++++- .../core/configuration/Captions.java | 2 ++ .../core/configuration/Settings.java | 3 ++ .../plotsquared/core/util/PatternUtil.java | 6 +++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Set.java b/Core/src/main/java/com/plotsquared/core/command/Set.java index ca301a2fa..f3503b82d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Set.java +++ b/Core/src/main/java/com/plotsquared/core/command/Set.java @@ -27,6 +27,7 @@ package com.plotsquared.core.command; import com.plotsquared.core.configuration.CaptionUtility; import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotManager; @@ -36,10 +37,14 @@ import com.plotsquared.core.util.PatternUtil; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.StringMan; import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.world.block.BlockCategory; +import com.sk89q.worldedit.world.block.BlockTypes; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; +import java.util.Locale; @CommandDeclaration(command = "set", description = "Set a plot value", @@ -71,6 +76,34 @@ public class Set extends SubCommand { String material = StringMan.join(Arrays.copyOfRange(args, 1, args.length), ",").trim(); + final List forbiddenTypes = Settings.General.INVALID_BLOCKS; + if (!Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_ALLOW_UNSAFE) && + !forbiddenTypes.isEmpty()) { + for (String forbiddenType : forbiddenTypes) { + forbiddenType = forbiddenType.toLowerCase(Locale.ENGLISH); + if (forbiddenType.startsWith("minecraft:")) { + forbiddenType = forbiddenType.substring(10); + } + for (String blockType : material.split(",")) { + blockType = blockType.toLowerCase(Locale.ENGLISH); + if (blockType.startsWith("minecraft:")) { + blockType = blockType.substring(10); + } + + if (blockType.startsWith("##")) { + final BlockCategory category = BlockCategory.REGISTRY.get(blockType.substring(2).toLowerCase(Locale.ROOT)); + if (category == null || !category.contains(BlockTypes.get(forbiddenType))) { + continue; + } + } else if (!blockType.contains(forbiddenType)) { + continue; + } + Captions.COMPONENT_ILLEGAL_BLOCK.send(player, forbiddenType); + return true; + } + } + } + for (String component : components) { if (component.equalsIgnoreCase(args[0])) { if (!Permissions.hasPermission(player, CaptionUtility @@ -86,7 +119,8 @@ public class Set extends SubCommand { return true; } - Pattern pattern = PatternUtil.parse(player, material); + Pattern pattern = PatternUtil.parse(player, material, false); + if (plot.getRunning() > 0) { MainUtil.sendMessage(player, Captions.WAIT_FOR_TIMER); return false; diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java index bee58ed27..a37839a30 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java @@ -184,6 +184,7 @@ public enum Captions implements Caption { PERMISSION_ALIAS_SET("plots.alias.set", "static.permissions"), PERMISSION_ALIAS_REMOVE("plots.alias.remove", "static.permissions"), PERMISSION_ADMIN_CHAT_BYPASS("plots.admin.chat.bypass", "static.permissions"), + PERMISSION_ADMIN_ALLOW_UNSAFE("plots.admin.unsafe", "static.permissions"), // // EXPIRED_CONFIRM("$2Confirmation has expired, please run the command again!", "Confirm"), @@ -425,6 +426,7 @@ public enum Captions implements Caption { COMMAND_SYNTAX("$1Usage: $2%s", "CommandConfig"), // // + COMPONENT_ILLEGAL_BLOCK("$2You are not allowed to generate a component containg the block '%s'", "Invalid"), INVALID_PLAYER("$2Player not found: $1%s$2.", "Errors"), INVALID_PLAYER_OFFLINE("$2The player must be online: $1%s.", "Errors"), INVALID_COMMAND_FLAG("$2Invalid command flag: %s0", "Errors"), diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 3b1fe6b22..dca23f8a4 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -29,6 +29,7 @@ import com.plotsquared.core.configuration.file.YamlConfiguration; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -245,6 +246,8 @@ public class Settings extends Config { public static final class General { @Comment("Display scientific numbers (4.2E8)") public static boolean SCIENTIFIC = false; @Comment("Replace wall when merging") public static boolean MERGE_REPLACE_WALL = true; + @Comment("Blocks that may not be used in plot components") public static List INVALID_BLOCKS = + Arrays.asList("command_block", "chain_command_block", "repeating_command_block"); } diff --git a/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java b/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java index a5a3911cc..bbcfbbd52 100644 --- a/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java @@ -56,6 +56,10 @@ public class PatternUtil { } public static Pattern parse(PlotPlayer plotPlayer, String input) { + return parse(plotPlayer, input, true); + } + + public static Pattern parse(PlotPlayer plotPlayer, String input, boolean allowLegacy) { ParserContext context = new ParserContext(); if (plotPlayer != null) { Actor actor = plotPlayer.toActor(); @@ -69,7 +73,7 @@ public class PatternUtil { context.setRestricted(false); } context.setPreferringWildcard(false); - context.setTryLegacy(true); + context.setTryLegacy(allowLegacy); try { return WorldEdit.getInstance().getPatternFactory().parseFromInput(input, context); } catch (InputParseException e) { From 8944be53198f1b27f0f92cdc9a4f8e4cd4a138bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Mon, 11 May 2020 19:37:25 +0200 Subject: [PATCH 3/3] (Complex) command suggestions for `/plot set` --- .../com/plotsquared/core/command/Biome.java | 14 ++++++ .../com/plotsquared/core/command/Set.java | 47 +++++++++++++++++++ .../plotsquared/core/util/PatternUtil.java | 11 +++++ 3 files changed, 72 insertions(+) diff --git a/Core/src/main/java/com/plotsquared/core/command/Biome.java b/Core/src/main/java/com/plotsquared/core/command/Biome.java index c7df94a48..4af7fdfd3 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Biome.java +++ b/Core/src/main/java/com/plotsquared/core/command/Biome.java @@ -30,9 +30,14 @@ import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.util.MainUtil; 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.BiomeTypes; +import java.util.Collection; +import java.util.Locale; +import java.util.stream.Collectors; + @CommandDeclaration(command = "setbiome", permission = "plots.set.biome", description = "Set the plot biome", @@ -68,4 +73,13 @@ public class Biome extends SetCommand { }); return true; } + + @Override public Collection 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()); + } + } diff --git a/Core/src/main/java/com/plotsquared/core/command/Set.java b/Core/src/main/java/com/plotsquared/core/command/Set.java index f3503b82d..a87f2eebc 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Set.java +++ b/Core/src/main/java/com/plotsquared/core/command/Set.java @@ -42,9 +42,12 @@ import com.sk89q.worldedit.world.block.BlockTypes; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; @CommandDeclaration(command = "set", description = "Set a plot value", @@ -136,6 +139,16 @@ public class Set extends SubCommand { } return false; } + + @Override + public Collection 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); } + + @Override public Collection 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 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); + } } diff --git a/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java b/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java index bbcfbbd52..ee390cc04 100644 --- a/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/PatternUtil.java @@ -43,6 +43,9 @@ import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; + public class PatternUtil { 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); } + public static List 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) { ParserContext context = new ParserContext(); if (plotPlayer != null) {