From ef61ecccaab13481aaa9e6c305c200980d098387 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 18 Nov 2021 18:42:19 +0000 Subject: [PATCH] Fix tab completion when *not* using %. - Also allow tab-completion of % pattern when it's the first/only pattern being used - Also don't error when attempting to tab complete an invalid percent value --- .../parser/pattern/RichPatternParser.java | 4 ++- .../parser/pattern/RandomPatternParser.java | 25 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/pattern/RichPatternParser.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/pattern/RichPatternParser.java index 3dabc70c0..3f17f493f 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/pattern/RichPatternParser.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/factory/parser/pattern/RichPatternParser.java @@ -32,6 +32,8 @@ import java.util.stream.Stream; public class RichPatternParser extends FaweParser { + private static final java.util.regex.Pattern percentPatternRegex = java.util.regex.Pattern.compile("[0-9]+(\\.[0-9]*)?%.*"); + /** * Create a new rich pattern-parser. * @@ -110,7 +112,7 @@ public class RichPatternParser extends FaweParser { pattern = parseFromInput(command.substring(1, end == -1 ? command.length() : end), context); } else { int percentIndex = command.indexOf('%'); - if (percentIndex != -1) { // Legacy percent pattern + if (percentIndex != -1 && percentPatternRegex.matcher(command).matches()) { // Legacy percent pattern chance = Expression.compile(command.substring(0, percentIndex)).evaluate(); String value = command.substring(percentIndex + 1); if (!entry.getValue().isEmpty()) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/RandomPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/RandomPatternParser.java index e3dc0feb2..b3227096c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/RandomPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/RandomPatternParser.java @@ -15,6 +15,8 @@ import java.util.stream.Stream; public class RandomPatternParser extends InputParser { + private static final java.util.regex.Pattern regex = java.util.regex.Pattern.compile("[0-9]+(\\.[0-9]*)?%.*"); + /** * Create a new input parser. * @@ -28,26 +30,35 @@ public class RandomPatternParser extends InputParser { public Stream getSuggestions(String input) { //FAWE start List patterns = StringUtil.split(input, ',', '[', ']'); - if (patterns.size() == 1) { + if (patterns.size() == 0) { return Stream.empty(); } // get suggestions for the last token only String token = patterns.get(patterns.size() - 1); - String randString = ""; - String previous = String.join(",", patterns.subList(0, patterns.size() - 1)); - if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) { + String percent; + String previous; + if (patterns.size() != 1) { + previous = String.join(",", patterns.subList(0, patterns.size() - 1)); + percent = ","; + } else { + previous = ""; + percent = ""; + } + if (regex.matcher(token).matches()) { String[] p = token.split("%"); if (p.length < 2) { return Stream.empty(); } else { - randString = p[0]; + percent += p[0] + "%"; token = p[1]; } + } else { + return Stream.empty(); } final List innerSuggestions = worldEdit.getPatternFactory().getSuggestions(token); - String prev = previous + "," + randString + "%"; - return innerSuggestions.stream().map(s -> prev + s); + String prefix = previous + percent; //FAWE end + return innerSuggestions.stream().map(s -> prefix + s); } @Override