From a3afd9d5b3b699c02d5ae0c3f45b96bbf21b0d90 Mon Sep 17 00:00:00 2001 From: wizjany Date: Sat, 1 Jun 2019 12:32:14 -0400 Subject: [PATCH] Make suggestions more robust. And fix potential errors in the ^[] pattern parser. --- .../TypeOrStateApplyingPatternParser.java | 21 ++++++++++++++----- .../platform/PlatformCommandManager.java | 13 +++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/TypeOrStateApplyingPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/TypeOrStateApplyingPatternParser.java index 8897b115a..5b368e644 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/TypeOrStateApplyingPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/TypeOrStateApplyingPatternParser.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.extension.factory.parser.pattern; -import com.google.common.base.Splitter; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.command.util.SuggestionHelper; import com.sk89q.worldedit.extension.input.InputParseException; @@ -32,6 +31,7 @@ import com.sk89q.worldedit.function.pattern.StateApplyingPattern; import com.sk89q.worldedit.function.pattern.TypeApplyingPattern; import com.sk89q.worldedit.internal.registry.InputParser; +import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; @@ -82,10 +82,21 @@ public class TypeOrStateApplyingPatternParser extends InputParser { worldEdit.getBlockFactory().parseFromInput(type, context).getBlockType().getDefaultState()); } else { // states given - if (!parts[1].endsWith("]")) throw new InputParseException("Invalid state format."); - Map statesToSet = Splitter.on(',') - .omitEmptyStrings().trimResults().withKeyValueSeparator('=') - .split(parts[1].substring(0, parts[1].length() - 1)); + if (!parts[1].endsWith("]")) throw new InputParseException("State is missing trailing ']'"); + final String[] states = parts[1].substring(0, parts[1].length() - 1).split(","); + Map statesToSet = new HashMap<>(); + for (String state : states) { + if (state.isEmpty()) throw new InputParseException("Empty part in state"); + String[] propVal = state.split("=", 2); + if (propVal.length != 2) throw new InputParseException("Missing '=' separator"); + final String prop = propVal[0]; + if (prop.isEmpty()) throw new InputParseException("Empty property in state"); + final String value = propVal[1]; + if (value.isEmpty()) throw new InputParseException("Empty value in state"); + if (statesToSet.put(prop, value) != null) { + throw new InputParseException("Duplicate properties in state"); + } + } if (type.isEmpty()) { return new StateApplyingPattern(extent, statesToSet); } else { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 9d2e53d8f..87d39d80b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -569,7 +569,16 @@ public final class PlatformCommandManager { .map(Substring::getSubstring) .collect(Collectors.toList()); MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor()); - ImmutableSet suggestions = commandManager.getSuggestions(access, argStrings); + ImmutableSet suggestions; + try { + suggestions = commandManager.getSuggestions(access, argStrings); + } catch (Throwable t) { // catch errors which are *not* command exceptions generated by parsers/suggesters + if (!(t instanceof CommandException)) { + log.debug("Unexpected error occurred while generating suggestions for input: " + arguments, t); + return; + } + throw t; + } event.setSuggestions(suggestions.stream() .map(suggestion -> { @@ -588,8 +597,6 @@ public final class PlatformCommandManager { if (e.getCondition() instanceof PermissionCondition) { event.setSuggestions(new ArrayList<>()); } - } catch (CommandException e) { - event.getActor().printError(e.getMessage()); } }