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] 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) {