Skip to content

Commit

Permalink
feat: allow captions in SuggestInputParseException (#2239)
Browse files Browse the repository at this point in the history
- Deprecate for removal methods using string message
 - Fixes #2026
  • Loading branch information
dordsor21 committed May 22, 2023
1 parent 74aff92 commit 7c01c1e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fastasyncworldedit.core.command;

import com.fastasyncworldedit.core.configuration.Caption;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.util.formatting.text.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand All @@ -13,33 +15,81 @@ public class SuggestInputParseException extends InputParseException {

private final InputParseException cause;
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) {
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) {
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) {
if (other instanceof SuggestInputParseException) {
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) {
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(other);
this.cause = other;
this.getSuggestions = getSuggestions;
this.prefix = prefix;
}

public static SuggestInputParseException get(InvocationTargetException e) {
Expand All @@ -54,7 +104,7 @@ public static SuggestInputParseException get(InvocationTargetException e) {
}

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()
.map(Object::toString)
.filter(v -> v.startsWith(input))
Expand All @@ -76,8 +126,12 @@ public List<String> getSuggestions() {
return getSuggestions.get();
}

/**
* @deprecated Unused
*/
@Deprecated(forRemoval = true, since = "TODO")
public SuggestInputParseException prepend(String input) {
this.prefix = input + prefix;
// Do nothing
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public RichMaskParser(WorldEdit worldEdit) {
@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) {
throw new SuggestInputParseException("No input provided", "", () -> Stream
throw new SuggestInputParseException(Caption.of("fawe.error.no-input-provided"), () -> Stream
.of("#", ",", "&")
.map(n -> n + ":")
.collect(Collectors.toList())
Expand Down Expand Up @@ -95,7 +95,6 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
"https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"
))
)),
full,
() -> {
if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions(""));
Expand Down Expand Up @@ -162,7 +161,6 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
"https://intellectualsites.github.io/fastasyncworldedit-documentation/masks/masks"
))
)),
full,
() -> {
if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public RichPatternParser(WorldEdit worldEdit) {
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) {
throw new SuggestInputParseException(
"No input provided",
"",
Caption.of("fawe.error.no-input-provided"),
() -> Stream
.concat(Stream.of("#", ",", "&"), BlockTypes.getNameSpaces().stream().map(n -> n + ":"))
.collect(Collectors.toList())
Expand Down Expand Up @@ -88,7 +87,6 @@ public Pattern parseFromInput(String input, ParserContext context) throws InputP
"https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"
))
)),
full,
() -> {
if (full.length() == 1) {
return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public BlockMaskBuilder addRegex(final String input) throws InputParseException
}
if (operator == null) {
throw new SuggestInputParseException(
"No operator for " + input,
"",
Caption.of("fawe.error.no-operator-for-input", input),
() -> Arrays.asList("=", "~", "!", "<", ">", "<=", ">=")
);
}
Expand All @@ -195,7 +194,7 @@ public BlockMaskBuilder addRegex(final String input) throws InputParseException
String value = charSequence.toString();
final PropertyKey fKey = key;
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<>();
types.stream().filter(t -> t.hasProperty(fKey)).forEach(t -> {
Property<Object> p = t.getProperty(fKey);
Expand Down Expand Up @@ -287,7 +286,7 @@ private <T> boolean has(BlockType type, Property<T> property, int index) {
}

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();
finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey())));
return keys.stream().map(PropertyKey::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.sk89q.worldedit.world.block;

import com.fastasyncworldedit.core.command.SuggestInputParseException;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.function.mask.SingleBlockStateMask;
import com.fastasyncworldedit.core.queue.ITileInput;
import com.fastasyncworldedit.core.registry.state.PropertyKey;
Expand All @@ -43,6 +44,7 @@
import com.sk89q.worldedit.registry.state.AbstractProperty;
import com.sk89q.worldedit.registry.state.Property;
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.world.registry.BlockMaterial;

Expand Down Expand Up @@ -150,7 +152,7 @@ public static BlockState get(@Nullable BlockType type, String state, BlockState
type = BlockTypes.get(key);
if (type == null) {
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)
.map(BlockType::getId)
.filter(id -> StringMan.blockStateMatches(input, id))
Expand Down Expand Up @@ -211,8 +213,7 @@ public static BlockState get(@Nullable BlockType type, String state, BlockState
String input = charSequence.toString();
BlockType finalType = type;
throw new SuggestInputParseException(
"Invalid property " + key + ":" + input + " for type " + type,
input,
Caption.of("worldedit.error.parser.unknown-property", key + ":" + input, type),
() ->
finalType.getProperties().stream()
.map(Property::getName)
Expand All @@ -222,8 +223,7 @@ public static BlockState get(@Nullable BlockType type, String state, BlockState
);
} else {
throw new SuggestInputParseException(
"No operator for " + state,
"",
Caption.of("fawe.error.no-operator-for-input", state),
() -> Collections.singletonList("=")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
package com.sk89q.worldedit.world.block;

import com.fastasyncworldedit.core.command.SuggestInputParseException;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.util.StringMan;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.registry.LegacyMapper;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -1967,7 +1969,7 @@ public static BlockType parse(final String type, final ParserContext context) th
}
}

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)
.filter(b -> StringMan.blockStateMatches(inputLower, b.getId()))
.map(BlockType::getId)
Expand Down

0 comments on commit 7c01c1e

Please sign in to comment.