From 46f6395279f0453c2d3e1b9fdaaf93fcd0cf970a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 17 Aug 2021 17:02:11 +0100 Subject: [PATCH] Improve mask parsing - Fixes #1230 --- .../factory/parser/mask/RichMaskParser.java | 44 ++++++++++++------ .../core/function/mask/IdDataMask.java | 46 ------------------- .../factory/parser/mask/BiomeMaskParser.java | 34 +++++++++++++- .../parser/mask/BlockStateMaskParser.java | 16 ++++++- .../parser/mask/ExpressionMaskParser.java | 32 ++++++++++++- .../factory/parser/mask/NegateMaskParser.java | 16 ++++++- .../factory/parser/mask/NoiseMaskParser.java | 31 ++++++++++++- .../factory/parser/mask/OffsetMaskParser.java | 16 ++++++- .../src/main/resources/lang/strings.json | 2 - 9 files changed, 165 insertions(+), 72 deletions(-) delete mode 100644 worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdDataMask.java diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/mask/RichMaskParser.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/mask/RichMaskParser.java index 92f95c14f..9a434c7fb 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/mask/RichMaskParser.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/mask/RichMaskParser.java @@ -29,6 +29,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.regex.PatternSyntaxException; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -108,11 +109,11 @@ public class RichMaskParser extends FaweParser { // Legacy syntax if (charMask) { switch (char0) { - case '\\': // - case '/': // - case '{': // - case '$': // - case '%': { + case '\\': + case '/': + case '{': + case '|': + case '~': { String value = command.substring(1) + ((entry.getValue().isEmpty()) ? "" : "[" + StringMan.join( @@ -125,23 +126,16 @@ public class RichMaskParser extends FaweParser { } value = value.replaceAll(":", "]["); } - mask = parseFromInput("#" + char0 + "[" + value + "]", context); + mask = parseFromInput(char0 + "[" + value + "]", context); break; } - case '|': - case '~': + case '%': + case '$': case '<': case '>': case '!': input = input.substring(input.indexOf(char0) + 1); mask = parseFromInput(char0 + "[" + input + "]", context); - if (actor != null) { - actor.print(Caption.of( - "fawe.worldedit.help.command.clarifying.bracket", - char0 + "[" + input + - "]" - )); - } return mask; } } @@ -154,6 +148,26 @@ public class RichMaskParser extends FaweParser { try { builder.addRegex(full); } catch (InputParseException ignored) { + } catch (PatternSyntaxException e) { + throw new SuggestInputParseException( + new NoMatchException(Caption.of("fawe.error.parse.unknown-mask", full, + TextComponent + .of("https://github.com/IntellectualSites/FastAsyncWorldEdit-Documentation/wiki/Transforms" + ) + .clickEvent(ClickEvent.openUrl( + "https://github.com/IntellectualSites/FastAsyncWorldEdit-Documentation/wiki/Transforms" + )) + )), + full, + () -> { + if (full.length() == 1) { + return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("")); + } + return new ArrayList<>(worldEdit + .getMaskFactory() + .getSuggestions(command.toLowerCase(Locale.ROOT))); + } + ); } context.setPreferringWildcard(false); context.setRestricted(false); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdDataMask.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdDataMask.java deleted file mode 100644 index 2098c9c26..000000000 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/mask/IdDataMask.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fastasyncworldedit.core.function.mask; - -import com.sk89q.worldedit.extent.Extent; -import com.sk89q.worldedit.function.mask.AbstractExtentMask; -import com.sk89q.worldedit.function.mask.Mask; -import com.sk89q.worldedit.math.BlockVector3; - -public class IdDataMask extends AbstractExtentMask implements ResettableMask { - - private transient int combined = -1; - - public IdDataMask(Extent extent) { - super(extent); - } - - @Override - public boolean test(Extent extent, BlockVector3 vector) { - if (combined != -1) { - return getExtent().getBlock(vector).getInternalId() == combined; - } else { - combined = getExtent().getBlock(vector).getInternalId(); - return true; - } - } - - @Override - public boolean test(BlockVector3 vector) { - return test(getExtent(), vector); - } - - @Override - public void reset() { - this.combined = -1; - } - - @Override - public Mask copy() { - return new IdDataMask(getExtent()); - } - - @Override - public boolean replacesAir() { - return true; - } - -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java index 1ad3c4158..60e2e3d82 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java @@ -20,7 +20,10 @@ package com.sk89q.worldedit.extension.factory.parser.mask; import com.fastasyncworldedit.core.configuration.Caption; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; +import com.fastasyncworldedit.core.util.StringMan; import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.util.SuggestionHelper; import com.sk89q.worldedit.extension.input.InputParseException; @@ -33,12 +36,18 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.world.biome.BiomeType; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -public class BiomeMaskParser extends InputParser { +//FAWE start - aliased +public class BiomeMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of("$"); + //FAWE end public BiomeMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -70,8 +79,22 @@ public class BiomeMaskParser extends InputParser { return null; } + //FAWE start - richer parsing + if (input.charAt(1) == '[') { + int end = input.lastIndexOf(']'); + if (end == -1) { + return null; + } + input = input.substring(2, end); + } else { + input = input.substring(1); + } + //FAWE end + Set biomes = new HashSet<>(); - for (String biomeName : Splitter.on(",").split(input.substring(1))) { + //FAWE start - richer parsing + for (String biomeName : Splitter.on(",").split(input)) { + //FAWE end BiomeType biome = BiomeType.REGISTRY.get(biomeName); if (biome == null) { throw new NoMatchException(Caption.of("worldedit.error.unknown-biome", TextComponent.of(biomeName))); @@ -82,4 +105,11 @@ public class BiomeMaskParser extends InputParser { return new BiomeMask(context.requireExtent(), biomes); } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockStateMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockStateMaskParser.java index 483354c4d..eedf1bdfb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockStateMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockStateMaskParser.java @@ -20,7 +20,9 @@ package com.sk89q.worldedit.extension.factory.parser.mask; import com.fastasyncworldedit.core.configuration.Caption; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; @@ -29,9 +31,14 @@ import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import java.util.List; import java.util.stream.Stream; -public class BlockStateMaskParser extends InputParser { +//FAWE start - aliased +public class BlockStateMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of("^", "^="); + //FAWE end public BlockStateMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -67,4 +74,11 @@ public class BlockStateMaskParser extends InputParser { } } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java index a892f9a5b..3e5b9d74c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java @@ -20,6 +20,8 @@ package com.sk89q.worldedit.extension.factory.parser.mask; import com.fastasyncworldedit.core.configuration.Caption; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; @@ -33,10 +35,15 @@ import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment; import com.sk89q.worldedit.session.SessionOwner; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import java.util.List; import java.util.function.IntSupplier; import java.util.stream.Stream; -public class ExpressionMaskParser extends InputParser { +//FAWE start - aliased +public class ExpressionMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of("="); + //FAWE end public ExpressionMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -56,8 +63,22 @@ public class ExpressionMaskParser extends InputParser { return null; } + //FAWE start - richer parsing + if (input.charAt(1) == '[') { + int end = input.lastIndexOf(']'); + if (end == -1) { + return null; + } + input = input.substring(2, end); + } else { + input = input.substring(1); + } + //FAWE end + try { - Expression exp = Expression.compile(input.substring(1), "x", "y", "z"); + //FAWE start - richer parsing + Expression exp = Expression.compile(input, "x", "y", "z"); + //FAWE end WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment( context.requireExtent(), Vector3.ONE, Vector3.ZERO); exp.setEnvironment(env); @@ -75,4 +96,11 @@ public class ExpressionMaskParser extends InputParser { } } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NegateMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NegateMaskParser.java index aa88ac2e5..cc91f2d18 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NegateMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NegateMaskParser.java @@ -20,6 +20,8 @@ package com.sk89q.worldedit.extension.factory.parser.mask; import com.fastasyncworldedit.core.configuration.Caption; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; @@ -27,9 +29,14 @@ import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Masks; import com.sk89q.worldedit.internal.registry.InputParser; +import java.util.List; import java.util.stream.Stream; -public class NegateMaskParser extends InputParser { +//FAWE start - aliased +public class NegateMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of("!"); + //FAWE end public NegateMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -59,4 +66,11 @@ public class NegateMaskParser extends InputParser { } } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NoiseMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NoiseMaskParser.java index 713e43e46..7852e87b4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NoiseMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NoiseMaskParser.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.extension.factory.parser.mask; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.function.mask.Mask; @@ -26,9 +28,14 @@ import com.sk89q.worldedit.function.mask.NoiseFilter; import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.math.noise.RandomNoise; +import java.util.List; import java.util.stream.Stream; -public class NoiseMaskParser extends InputParser { +//FAWE start - aliased +public class NoiseMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of("%"); + //FAWE end public NoiseMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -51,8 +58,28 @@ public class NoiseMaskParser extends InputParser { return null; } - int i = Integer.parseInt(input.substring(1)); + //FAWE start - richer parsing + if (input.charAt(1) == '[') { + int end = input.lastIndexOf(']'); + if (end == -1) { + return null; + } + input = input.substring(2, end); + } else { + input = input.substring(1); + } + + int i = Integer.parseInt(input); + //FAWE end + return new NoiseFilter(new RandomNoise(), ((double) i) / 100); } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/OffsetMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/OffsetMaskParser.java index 07f686c84..ccf875833 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/OffsetMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/OffsetMaskParser.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.extension.factory.parser.mask; +import com.fastasyncworldedit.core.extension.factory.parser.AliasedParser; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; @@ -28,9 +30,14 @@ import com.sk89q.worldedit.function.mask.OffsetMask; import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.math.BlockVector3; +import java.util.List; import java.util.stream.Stream; -public class OffsetMaskParser extends InputParser { +//FAWE start - aliased +public class OffsetMaskParser extends InputParser implements AliasedParser { + + private final List aliases = ImmutableList.of(">", "<"); + //FAWE end public OffsetMaskParser(WorldEdit worldEdit) { super(worldEdit); @@ -66,4 +73,11 @@ public class OffsetMaskParser extends InputParser { //FAWE end } + //FAWE start - aliased + @Override + public List getMatchedAliases() { + return aliases; + } + //FAWE end + } diff --git a/worldedit-core/src/main/resources/lang/strings.json b/worldedit-core/src/main/resources/lang/strings.json index 8c97636a3..01d618d94 100644 --- a/worldedit-core/src/main/resources/lang/strings.json +++ b/worldedit-core/src/main/resources/lang/strings.json @@ -90,8 +90,6 @@ "fawe.worldedit.selector.selector.fuzzy.pos1": "Region set and expanded from {0} {1}.", "fawe.worldedit.selector.selector.fuzzy.pos2": "Added expansion of {0} {1}.", - "fawe.worldedit.help.command.clarifying.bracket": "Added clarifying bracket for {0}", - "fawe.progress.progress.message": "{1}/{0} ({2}%) @{3}cps {4}s left", "fawe.progress.progress.finished": "[ Done! ]",