Search in sources :

Example 1 with SuggestInputParseException

use of com.fastasyncworldedit.core.command.SuggestInputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class BlockState method get.

/**
 * Returns a temporary BlockState for a given type and string.
 *
 * <p>It's faster if a BlockType is provided compared to parsing the string.</p>
 *
 * @param type  BlockType e.g., BlockTypes.STONE (or null)
 * @param state String e.g., minecraft:water[level=4]
 * @return BlockState
 */
public static BlockState get(@Nullable BlockType type, String state, BlockState defaultState) throws InputParseException {
    int propStrStart = state.indexOf('[');
    if (type == null) {
        CharSequence key;
        if (propStrStart == -1) {
            key = state;
        } else {
            MutableCharSequence charSequence = MutableCharSequence.getTemporal();
            charSequence.setString(state);
            charSequence.setSubstring(0, propStrStart);
            key = charSequence;
        }
        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(BlockTypesCache.values).map(BlockType::getId).filter(id -> StringMan.blockStateMatches(input, id)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
        }
    }
    if (propStrStart == -1) {
        return type.getDefaultState();
    }
    List<? extends Property<?>> propList = type.getProperties();
    if (state.charAt(state.length() - 1) != ']') {
        state = state + "]";
    }
    MutableCharSequence charSequence = MutableCharSequence.getTemporal();
    charSequence.setString(state);
    if (propList.size() == 1) {
        AbstractProperty<?> property = (AbstractProperty<?>) propList.get(0);
        String name = property.getName();
        charSequence.setSubstring(propStrStart + name.length() + 2, state.length() - 1);
        int index = charSequence.length() <= 0 ? -1 : property.getIndexFor(charSequence);
        if (index != -1) {
            return type.withPropertyId(index);
        }
    }
    int stateId;
    if (defaultState != null) {
        stateId = defaultState.getInternalId();
    } else {
        stateId = type.getDefaultState().getInternalId();
    }
    int length = state.length();
    AbstractProperty<?> property = null;
    int last = propStrStart + 1;
    for (int i = last; i < length; i++) {
        char c = state.charAt(i);
        switch(c) {
            case ']':
            case ',':
                {
                    charSequence.setSubstring(last, i);
                    if (property != null) {
                        int index = property.getIndexFor(charSequence);
                        if (index == -1) {
                            throw SuggestInputParseException.of(charSequence.toString(), (List<Object>) property.getValues());
                        }
                        stateId = property.modifyIndex(stateId, index);
                    } else {
                        // suggest
                        PropertyKey key = PropertyKey.getByName(charSequence);
                        if (key == null || !type.hasProperty(key)) {
                            // Suggest property
                            String input = charSequence.toString();
                            BlockType finalType = type;
                            throw new SuggestInputParseException("Invalid property " + key + ":" + input + " for type " + type, input, () -> finalType.getProperties().stream().map(Property::getName).filter(p -> StringMan.blockStateMatches(input, p)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
                        } else {
                            throw new SuggestInputParseException("No operator for " + state, "", () -> Collections.singletonList("="));
                        }
                    }
                    property = null;
                    last = i + 1;
                    break;
                }
            case '=':
                {
                    charSequence.setSubstring(last, i);
                    property = (AbstractProperty) type.getPropertyMap().get(charSequence);
                    last = i + 1;
                    break;
                }
            default:
                continue;
        }
    }
    return type.withPropertyId(stateId >> BlockTypesCache.BIT_OFFSET);
}
Also used : SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) Property(com.sk89q.worldedit.registry.state.Property) BlockVector3(com.sk89q.worldedit.math.BlockVector3) SingleBlockStateMask(com.fastasyncworldedit.core.function.mask.SingleBlockStateMask) BlanketBaseBlock(com.fastasyncworldedit.core.world.block.BlanketBaseBlock) LazyReference(com.sk89q.worldedit.util.concurrency.LazyReference) StringMan(com.fastasyncworldedit.core.util.StringMan) WorldEditException(com.sk89q.worldedit.WorldEditException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Map(java.util.Map) PropertyKey(com.fastasyncworldedit.core.registry.state.PropertyKey) WorldEdit(com.sk89q.worldedit.WorldEdit) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Function(com.google.common.base.Function) NullExtent(com.sk89q.worldedit.extent.NullExtent) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) OutputExtent(com.sk89q.worldedit.extent.OutputExtent) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Capability(com.sk89q.worldedit.extension.platform.Capability) CompoundInput(com.fastasyncworldedit.core.world.block.CompoundInput) CompoundTag(com.sk89q.jnbt.CompoundTag) Mask(com.sk89q.worldedit.function.mask.Mask) ITileInput(com.fastasyncworldedit.core.queue.ITileInput) Pattern(com.sk89q.worldedit.function.pattern.Pattern) Collections(java.util.Collections) Extent(com.sk89q.worldedit.extent.Extent) BlockMaterial(com.sk89q.worldedit.world.registry.BlockMaterial) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) List(java.util.List) PropertyKey(com.fastasyncworldedit.core.registry.state.PropertyKey)

Example 2 with SuggestInputParseException

use of com.fastasyncworldedit.core.command.SuggestInputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class RichPatternParser method parseFromInput.

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
    if (input.isEmpty()) {
        throw new SuggestInputParseException("No input provided", "", () -> Stream.concat(Stream.of("#", ",", "&"), BlockTypes.getNameSpaces().stream().map(n -> n + ":")).collect(Collectors.toList()));
    }
    List<Double> chances = new ArrayList<>();
    List<Pattern> patterns = new ArrayList<>();
    final CommandLocals locals = new CommandLocals();
    Actor actor = context != null ? context.getActor() : null;
    if (actor != null) {
        locals.put(Actor.class, actor);
    }
    try {
        for (Map.Entry<ParseEntry, List<String>> entry : parse(input)) {
            ParseEntry pe = entry.getKey();
            final String command = pe.getInput();
            String full = pe.getFull();
            Pattern pattern = null;
            double chance = 1;
            if (command.isEmpty()) {
                pattern = parseFromInput(StringMan.join(entry.getValue(), ','), context);
            } else if (!worldEdit.getPatternFactory().containsAlias(command)) {
                // Legacy patterns
                char char0 = command.charAt(0);
                boolean charPattern = input.length() > 1 && input.charAt(1) != '[';
                if (charPattern && input.charAt(0) == '=') {
                    pattern = parseFromInput(char0 + "[" + input.substring(1) + "]", context);
                }
                if (char0 == '#' && command.length() > 1 && command.charAt(1) != '#') {
                    throw new SuggestInputParseException(new NoMatchException(Caption.of("fawe.error.parse.unknown-pattern", full, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns").clickEvent(ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns")))), full, () -> {
                        if (full.length() == 1) {
                            return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(""));
                        }
                        return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(command.toLowerCase(Locale.ROOT)));
                    });
                }
                if (charPattern) {
                    if (char0 == '$' || char0 == '^' || char0 == '*' || (char0 == '#' && input.charAt(1) == '#')) {
                        pattern = worldEdit.getPatternFactory().parseWithoutRich(full, context);
                    }
                }
                if (pattern == null) {
                    if (command.startsWith("[")) {
                        int end = command.lastIndexOf(']');
                        pattern = parseFromInput(command.substring(1, end == -1 ? command.length() : end), context);
                    } else {
                        int percentIndex = command.indexOf('%');
                        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()) {
                                boolean addBrackets = !value.isEmpty();
                                if (addBrackets) {
                                    value += "[";
                                }
                                value += StringMan.join(entry.getValue(), " ");
                                if (addBrackets) {
                                    value += "]";
                                }
                            }
                            pattern = parseFromInput(value, context);
                        } else {
                            // legacy block pattern
                            try {
                                pattern = worldEdit.getBlockFactory().parseFromInput(pe.getFull(), context);
                            } catch (NoMatchException e) {
                                throw new NoMatchException(Caption.of("fawe.error.parse.unknown-pattern", full, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns").clickEvent(com.sk89q.worldedit.util.formatting.text.event.ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"))));
                            }
                        }
                    }
                }
            } else {
                List<String> args = entry.getValue();
                try {
                    pattern = worldEdit.getPatternFactory().parseWithoutRich(full, context);
                } catch (InputParseException rethrow) {
                    throw rethrow;
                } catch (Throwable e) {
                    throw SuggestInputParseException.of(e, full, () -> {
                        try {
                            String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " "));
                            List<Substring> split = CommandArgParser.forArgString(cmdArgs).parseArgs().toList();
                            List<String> argStrings = split.stream().map(Substring::getSubstring).collect(Collectors.toList());
                            MemoizingValueAccess access = getPlatform().initializeInjectedValues(() -> cmdArgs, actor, null, true);
                            List<String> suggestions = getPlatform().getCommandManager().getSuggestions(access, argStrings).stream().map(Suggestion::getSuggestion).collect(Collectors.toUnmodifiableList());
                            List<String> result = new ArrayList<>();
                            if (suggestions.size() <= 2) {
                                for (int i = 0; i < suggestions.size(); i++) {
                                    String suggestion = suggestions.get(i);
                                    if (suggestion.indexOf(' ') != 0) {
                                        String[] splitSuggestion = suggestion.split(" ");
                                        suggestion = "[" + StringMan.join(splitSuggestion, "][") + "]";
                                        result.set(i, suggestion);
                                    }
                                }
                            }
                            return result;
                        } catch (Throwable e2) {
                            e2.printStackTrace();
                            throw new InputParseException(TextComponent.of(e2.getMessage()));
                        }
                    });
                }
            }
            if (pattern != null) {
                patterns.add(pattern);
                chances.add(chance);
            }
        }
    } catch (InputParseException rethrow) {
        throw rethrow;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new InputParseException(TextComponent.of(e.getMessage()), e);
    }
    if (patterns.isEmpty()) {
        return null;
    }
    if (patterns.size() == 1) {
        return patterns.get(0);
    }
    RandomPattern random = new RandomPattern(new TrueRandom());
    for (int i = 0; i < patterns.size(); i++) {
        random.add(patterns.get(i), chances.get(i));
    }
    return random;
}
Also used : SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) Suggestion(org.enginehub.piston.suggestion.Suggestion) Caption(com.fastasyncworldedit.core.configuration.Caption) MemoizingValueAccess(org.enginehub.piston.inject.MemoizingValueAccess) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) FaweParser(com.fastasyncworldedit.core.extension.factory.parser.FaweParser) TrueRandom(com.fastasyncworldedit.core.math.random.TrueRandom) StringMan(com.fastasyncworldedit.core.util.StringMan) ArrayList(java.util.ArrayList) CommandLocals(com.sk89q.minecraft.util.commands.CommandLocals) Substring(com.sk89q.worldedit.internal.util.Substring) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Locale(java.util.Locale) ClickEvent(com.sk89q.worldedit.util.formatting.text.event.ClickEvent) Map(java.util.Map) WorldEdit(com.sk89q.worldedit.WorldEdit) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) CommandArgParser(com.sk89q.worldedit.internal.command.CommandArgParser) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Expression(com.sk89q.worldedit.internal.expression.Expression) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Collectors(java.util.stream.Collectors) Actor(com.sk89q.worldedit.extension.platform.Actor) List(java.util.List) Stream(java.util.stream.Stream) Pattern(com.sk89q.worldedit.function.pattern.Pattern) Collections(java.util.Collections) ArrayList(java.util.ArrayList) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Actor(com.sk89q.worldedit.extension.platform.Actor) TrueRandom(com.fastasyncworldedit.core.math.random.TrueRandom) ArrayList(java.util.ArrayList) List(java.util.List) Substring(com.sk89q.worldedit.internal.util.Substring) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Pattern(com.sk89q.worldedit.function.pattern.Pattern) MemoizingValueAccess(org.enginehub.piston.inject.MemoizingValueAccess) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) CommandLocals(com.sk89q.minecraft.util.commands.CommandLocals) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) Map(java.util.Map)

Example 3 with SuggestInputParseException

use of com.fastasyncworldedit.core.command.SuggestInputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class RichTransformParser method parseFromInput.

@Override
public ResettableExtent parseFromInput(String input, ParserContext context) throws InputParseException {
    if (input.isEmpty()) {
        return null;
    }
    List<Double> unionChances = new ArrayList<>();
    List<Double> intersectionChances = new ArrayList<>();
    List<ResettableExtent> intersection = new ArrayList<>();
    List<ResettableExtent> union = new ArrayList<>();
    final CommandLocals locals = new CommandLocals();
    Actor actor = context != null ? context.getActor() : null;
    if (actor != null) {
        locals.put(Actor.class, actor);
    }
    try {
        List<Map.Entry<ParseEntry, List<String>>> parsed = parse(input);
        for (Map.Entry<ParseEntry, List<String>> entry : parsed) {
            ParseEntry pe = entry.getKey();
            String command = pe.getInput();
            ResettableExtent transform;
            double chance = 1;
            if (command.isEmpty()) {
                transform = parseFromInput(StringMan.join(entry.getValue(), ','), context);
            } else if (!worldEdit.getTransformFactory().containsAlias(command)) {
                // Legacy syntax
                int percentIndex = command.indexOf('%');
                if (percentIndex != -1) {
                    // Legacy percent pattern
                    chance = Expression.compile(command.substring(0, percentIndex)).evaluate();
                    command = command.substring(percentIndex + 1);
                    if (!entry.getValue().isEmpty()) {
                        if (!command.isEmpty()) {
                            command += " ";
                        }
                        command += StringMan.join(entry.getValue(), " ");
                    }
                    transform = parseFromInput(command, context);
                } else {
                    throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", pe.getFull(), TextComponent.of("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms"))));
                }
            } else {
                try {
                    transform = worldEdit.getTransformFactory().parseWithoutRich(pe.getFull(), context);
                } catch (SuggestInputParseException rethrow) {
                    throw rethrow;
                } catch (Throwable e) {
                    throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", pe.getFull(), TextComponent.of("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms"))));
                }
            }
            if (pe.isAnd()) {
                // &
                intersectionChances.add(chance);
                intersection.add(transform);
            } else {
                if (!intersection.isEmpty()) {
                    if (intersection.size() == 1) {
                        throw new InputParseException(Caption.of("fawe.error.parse.invalid-dangling-character", "&"));
                    }
                    MultiTransform multi = new MultiTransform();
                    double total = 0;
                    for (int i = 0; i < intersection.size(); i++) {
                        Double value = intersectionChances.get(i);
                        total += value;
                        multi.add(intersection.get(i), value);
                    }
                    union.add(multi);
                    unionChances.add(total);
                    intersection.clear();
                    intersectionChances.clear();
                }
                unionChances.add(chance);
                union.add(transform);
            }
        }
    } catch (Throwable e) {
        throw new InputParseException(TextComponent.of(e.getMessage()), e);
    }
    if (!intersection.isEmpty()) {
        if (intersection.size() == 1) {
            throw new InputParseException(Caption.of("fawe.error.parse.invalid-dangling-character", "&"));
        }
        MultiTransform multi = new MultiTransform();
        double total = 0;
        for (int i = 0; i < intersection.size(); i++) {
            Double value = intersectionChances.get(i);
            total += value;
            multi.add(intersection.get(i), value);
        }
        union.add(multi);
        unionChances.add(total);
        intersection.clear();
        intersectionChances.clear();
    }
    if (union.isEmpty()) {
        throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", input, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/transforms/transforms"))));
    } else if (union.size() == 1) {
        return union.get(0);
    } else {
        RandomTransform random = new RandomTransform(new TrueRandom());
        for (int i = 0; i < union.size(); i++) {
            random.add(union.get(i), unionChances.get(i));
        }
        return random;
    }
}
Also used : CommandLocals(com.sk89q.minecraft.util.commands.CommandLocals) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) ArrayList(java.util.ArrayList) MultiTransform(com.fastasyncworldedit.core.extent.transform.MultiTransform) RandomTransform(com.fastasyncworldedit.core.extent.transform.RandomTransform) ResettableExtent(com.fastasyncworldedit.core.extent.ResettableExtent) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Actor(com.sk89q.worldedit.extension.platform.Actor) TrueRandom(com.fastasyncworldedit.core.math.random.TrueRandom) ArrayList(java.util.ArrayList) List(java.util.List) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) Map(java.util.Map)

Aggregations

SuggestInputParseException (com.fastasyncworldedit.core.command.SuggestInputParseException)3 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)3 List (java.util.List)3 Map (java.util.Map)3 TrueRandom (com.fastasyncworldedit.core.math.random.TrueRandom)2 StringMan (com.fastasyncworldedit.core.util.StringMan)2 CommandLocals (com.sk89q.minecraft.util.commands.CommandLocals)2 WorldEdit (com.sk89q.worldedit.WorldEdit)2 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)2 Actor (com.sk89q.worldedit.extension.platform.Actor)2 Pattern (com.sk89q.worldedit.function.pattern.Pattern)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Caption (com.fastasyncworldedit.core.configuration.Caption)1 FaweParser (com.fastasyncworldedit.core.extension.factory.parser.FaweParser)1 ResettableExtent (com.fastasyncworldedit.core.extent.ResettableExtent)1 MultiTransform (com.fastasyncworldedit.core.extent.transform.MultiTransform)1 RandomTransform (com.fastasyncworldedit.core.extent.transform.RandomTransform)1