feat: allow captions in SuggestInputParseException (#2239)

- Deprecate for removal methods using string message
 - Fixes #2026
This commit is contained in:
Jordan 2023-05-22 19:32:56 +01:00 committed by GitHub
parent 74aff920a8
commit 7c01c1e95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 25 deletions

View File

@ -1,6 +1,8 @@
package com.fastasyncworldedit.core.command; package com.fastasyncworldedit.core.command;
import com.fastasyncworldedit.core.configuration.Caption;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.util.formatting.text.Component;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
@ -13,33 +15,81 @@ public class SuggestInputParseException extends InputParseException {
private final InputParseException cause; private final InputParseException cause;
private final Supplier<List<String>> getSuggestions; private final Supplier<List<String>> getSuggestions;
private String prefix;
/**
* @deprecated Use {@link SuggestInputParseException#SuggestInputParseException(Component, Supplier)}
*/
@Deprecated(forRemoval = true, since = "TODO")
public SuggestInputParseException(String msg, String prefix, Supplier<List<String>> getSuggestions) { public SuggestInputParseException(String msg, String prefix, Supplier<List<String>> getSuggestions) {
this(new InputParseException(msg), prefix, getSuggestions); this(new InputParseException(msg), getSuggestions);
} }
/**
* @deprecated Use {@link SuggestInputParseException#of(Throwable, Supplier)}
*/
@Deprecated(forRemoval = true, since = "TODO")
public static SuggestInputParseException of(Throwable other, String prefix, Supplier<List<String>> getSuggestions) { public static SuggestInputParseException of(Throwable other, String prefix, Supplier<List<String>> getSuggestions) {
if (other instanceof InputParseException) { if (other instanceof InputParseException) {
return of((InputParseException) other, prefix, getSuggestions); return of((InputParseException) other, getSuggestions);
} }
return of(new InputParseException(other.getMessage()), prefix, getSuggestions); return of(new InputParseException(other.getMessage()), getSuggestions);
} }
/**
* @deprecated Use {@link SuggestInputParseException#of(InputParseException, Supplier)}
*/
@Deprecated(forRemoval = true, since = "TODO")
public static SuggestInputParseException of(InputParseException other, String prefix, Supplier<List<String>> getSuggestions) { public static SuggestInputParseException of(InputParseException other, String prefix, Supplier<List<String>> getSuggestions) {
if (other instanceof SuggestInputParseException) { if (other instanceof SuggestInputParseException) {
return (SuggestInputParseException) other; return (SuggestInputParseException) other;
} }
return new SuggestInputParseException(other, prefix, getSuggestions); return new SuggestInputParseException(other, getSuggestions);
} }
/**
* @deprecated Use {@link SuggestInputParseException#SuggestInputParseException(InputParseException, Supplier)}
*/
@Deprecated(forRemoval = true, since = "TODO")
public SuggestInputParseException(InputParseException other, String prefix, Supplier<List<String>> getSuggestions) { public SuggestInputParseException(InputParseException other, String prefix, Supplier<List<String>> getSuggestions) {
super(other.getMessage()); super(other.getRichMessage());
checkNotNull(getSuggestions);
checkNotNull(other);
this.cause = other;
this.getSuggestions = getSuggestions;
}
/**
* Create a new SuggestInputParseException instance
*
* @param message Message to send
* @param getSuggestions Supplier of list of sugegstions to give to user
* @since TODO
*/
public SuggestInputParseException(Component message, Supplier<List<String>> getSuggestions) {
this(new InputParseException(message), getSuggestions);
}
public static SuggestInputParseException of(Throwable other, Supplier<List<String>> getSuggestions) {
if (other instanceof InputParseException) {
return of((InputParseException) other, getSuggestions);
}
//noinspection deprecation
return of(new InputParseException(other.getMessage()), getSuggestions);
}
public static SuggestInputParseException of(InputParseException other, Supplier<List<String>> getSuggestions) {
if (other instanceof SuggestInputParseException) {
return (SuggestInputParseException) other;
}
return new SuggestInputParseException(other, getSuggestions);
}
public SuggestInputParseException(InputParseException other, Supplier<List<String>> getSuggestions) {
super(other.getRichMessage());
checkNotNull(getSuggestions); checkNotNull(getSuggestions);
checkNotNull(other); checkNotNull(other);
this.cause = other; this.cause = other;
this.getSuggestions = getSuggestions; this.getSuggestions = getSuggestions;
this.prefix = prefix;
} }
public static SuggestInputParseException get(InvocationTargetException e) { public static SuggestInputParseException get(InvocationTargetException e) {
@ -54,7 +104,7 @@ public class SuggestInputParseException extends InputParseException {
} }
public static SuggestInputParseException of(String input, List<Object> values) { public static SuggestInputParseException of(String input, List<Object> values) {
throw new SuggestInputParseException("No value: " + input, input, () -> throw new SuggestInputParseException(Caption.of("fawe.error.no-value-for-input", input), () ->
values.stream() values.stream()
.map(Object::toString) .map(Object::toString)
.filter(v -> v.startsWith(input)) .filter(v -> v.startsWith(input))
@ -76,8 +126,12 @@ public class SuggestInputParseException extends InputParseException {
return getSuggestions.get(); return getSuggestions.get();
} }
/**
* @deprecated Unused
*/
@Deprecated(forRemoval = true, since = "TODO")
public SuggestInputParseException prepend(String input) { public SuggestInputParseException prepend(String input) {
this.prefix = input + prefix; // Do nothing
return this; return this;
} }

View File

@ -50,7 +50,7 @@ public class RichMaskParser extends FaweParser<Mask> {
@Override @Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException { public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) { if (input.isEmpty()) {
throw new SuggestInputParseException("No input provided", "", () -> Stream throw new SuggestInputParseException(Caption.of("fawe.error.no-input-provided"), () -> Stream
.of("#", ",", "&") .of("#", ",", "&")
.map(n -> n + ":") .map(n -> n + ":")
.collect(Collectors.toList()) .collect(Collectors.toList())
@ -95,7 +95,6 @@ public class RichMaskParser extends FaweParser<Mask> {
"https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns" "https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"
)) ))
)), )),
full,
() -> { () -> {
if (full.length() == 1) { if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("")); return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions(""));
@ -162,7 +161,6 @@ public class RichMaskParser extends FaweParser<Mask> {
"https://intellectualsites.github.io/fastasyncworldedit-documentation/masks/masks" "https://intellectualsites.github.io/fastasyncworldedit-documentation/masks/masks"
)) ))
)), )),
full,
() -> { () -> {
if (full.length() == 1) { if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("")); return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions(""));

View File

@ -47,8 +47,7 @@ public class RichPatternParser extends FaweParser<Pattern> {
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) { if (input.isEmpty()) {
throw new SuggestInputParseException( throw new SuggestInputParseException(
"No input provided", Caption.of("fawe.error.no-input-provided"),
"",
() -> Stream () -> Stream
.concat(Stream.of("#", ",", "&"), BlockTypes.getNameSpaces().stream().map(n -> n + ":")) .concat(Stream.of("#", ",", "&"), BlockTypes.getNameSpaces().stream().map(n -> n + ":"))
.collect(Collectors.toList()) .collect(Collectors.toList())
@ -88,7 +87,6 @@ public class RichPatternParser extends FaweParser<Pattern> {
"https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns" "https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"
)) ))
)), )),
full,
() -> { () -> {
if (full.length() == 1) { if (full.length() == 1) {
return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions("")); return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(""));

View File

@ -178,8 +178,7 @@ public class BlockMaskBuilder {
} }
if (operator == null) { if (operator == null) {
throw new SuggestInputParseException( throw new SuggestInputParseException(
"No operator for " + input, Caption.of("fawe.error.no-operator-for-input", input),
"",
() -> Arrays.asList("=", "~", "!", "<", ">", "<=", ">=") () -> Arrays.asList("=", "~", "!", "<", ">", "<=", ">=")
); );
} }
@ -195,7 +194,7 @@ public class BlockMaskBuilder {
String value = charSequence.toString(); String value = charSequence.toString();
final PropertyKey fKey = key; final PropertyKey fKey = key;
Collection<BlockType> types = type != null ? Collections.singleton(type) : blockTypeList; Collection<BlockType> types = type != null ? Collections.singleton(type) : blockTypeList;
throw new SuggestInputParseException("No value for " + input, input, () -> { throw new SuggestInputParseException(Caption.of("fawe.error.no-value-for-input", input), () -> {
HashSet<String> values = new HashSet<>(); HashSet<String> values = new HashSet<>();
types.stream().filter(t -> t.hasProperty(fKey)).forEach(t -> { types.stream().filter(t -> t.hasProperty(fKey)).forEach(t -> {
Property<Object> p = t.getProperty(fKey); Property<Object> p = t.getProperty(fKey);
@ -287,7 +286,7 @@ public class BlockMaskBuilder {
} }
private void suggest(String input, String property, Collection<BlockType> finalTypes) throws InputParseException { private void suggest(String input, String property, Collection<BlockType> finalTypes) throws InputParseException {
throw new SuggestInputParseException(input + " does not have: " + property, input, () -> { throw new SuggestInputParseException(Caption.of("worldedit.error.parser.unknown-property", property, input), () -> {
Set<PropertyKey> keys = PropertyKeySet.empty(); Set<PropertyKey> keys = PropertyKeySet.empty();
finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey()))); finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey())));
return keys.stream().map(PropertyKey::getName) return keys.stream().map(PropertyKey::getName)

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.world.block; package com.sk89q.worldedit.world.block;
import com.fastasyncworldedit.core.command.SuggestInputParseException; import com.fastasyncworldedit.core.command.SuggestInputParseException;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.function.mask.SingleBlockStateMask; import com.fastasyncworldedit.core.function.mask.SingleBlockStateMask;
import com.fastasyncworldedit.core.queue.ITileInput; import com.fastasyncworldedit.core.queue.ITileInput;
import com.fastasyncworldedit.core.registry.state.PropertyKey; import com.fastasyncworldedit.core.registry.state.PropertyKey;
@ -43,6 +44,7 @@ import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.AbstractProperty; import com.sk89q.worldedit.registry.state.AbstractProperty;
import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.concurrency.LazyReference; import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.world.registry.BlockMaterial;
@ -150,7 +152,7 @@ public class BlockState implements BlockStateHolder<BlockState>, Pattern {
type = BlockTypes.get(key); type = BlockTypes.get(key);
if (type == null) { if (type == null) {
String input = key.toString(); String input = key.toString();
throw new SuggestInputParseException("Does not match a valid block type: " + input, input, () -> Stream.of( throw new SuggestInputParseException(Caption.of("fawe.error.invalid-block-type", TextComponent.of(input)), () -> Stream.of(
BlockTypesCache.values) BlockTypesCache.values)
.map(BlockType::getId) .map(BlockType::getId)
.filter(id -> StringMan.blockStateMatches(input, id)) .filter(id -> StringMan.blockStateMatches(input, id))
@ -211,8 +213,7 @@ public class BlockState implements BlockStateHolder<BlockState>, Pattern {
String input = charSequence.toString(); String input = charSequence.toString();
BlockType finalType = type; BlockType finalType = type;
throw new SuggestInputParseException( throw new SuggestInputParseException(
"Invalid property " + key + ":" + input + " for type " + type, Caption.of("worldedit.error.parser.unknown-property", key + ":" + input, type),
input,
() -> () ->
finalType.getProperties().stream() finalType.getProperties().stream()
.map(Property::getName) .map(Property::getName)
@ -222,8 +223,7 @@ public class BlockState implements BlockStateHolder<BlockState>, Pattern {
); );
} else { } else {
throw new SuggestInputParseException( throw new SuggestInputParseException(
"No operator for " + state, Caption.of("fawe.error.no-operator-for-input", state),
"",
() -> Collections.singletonList("=") () -> Collections.singletonList("=")
); );
} }

View File

@ -20,9 +20,11 @@
package com.sk89q.worldedit.world.block; package com.sk89q.worldedit.world.block;
import com.fastasyncworldedit.core.command.SuggestInputParseException; import com.fastasyncworldedit.core.command.SuggestInputParseException;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.util.StringMan; import com.fastasyncworldedit.core.util.StringMan;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.registry.LegacyMapper;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -1967,7 +1969,7 @@ public final class BlockTypes {
} }
} }
throw new SuggestInputParseException("Does not match a valid block type: " + inputLower, inputLower, () -> Stream.of( throw new SuggestInputParseException(Caption.of("fawe.error.invalid-block-type", TextComponent.of(input)), () -> Stream.of(
BlockTypesCache.values) BlockTypesCache.values)
.filter(b -> StringMan.blockStateMatches(inputLower, b.getId())) .filter(b -> StringMan.blockStateMatches(inputLower, b.getId()))
.map(BlockType::getId) .map(BlockType::getId)