Search in sources :

Example 1 with InputParseException

use of com.sk89q.worldedit.extension.input.InputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class OffsetTransformParser method parseFromInput.

@Override
protected ResettableExtent parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException {
    if (arguments.length != 3 && arguments.length != 4) {
        throw new InputParseException(Caption.of("fawe.error.command.syntax", TextComponent.of("#offset[x][y][z]")));
    }
    int xOffset = Integer.parseInt(arguments[0]);
    int yOffset = Integer.parseInt(arguments[1]);
    int zOffset = Integer.parseInt(arguments[2]);
    Extent extent;
    extent = arguments.length == 4 ? worldEdit.getTransformFactory().parseFromInput(arguments[3], context) : context.requireExtent();
    return new OffsetTransform(extent, xOffset, yOffset, zOffset);
}
Also used : OffsetTransform(com.fastasyncworldedit.core.extent.transform.OffsetTransform) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) ResettableExtent(com.fastasyncworldedit.core.extent.ResettableExtent) Extent(com.sk89q.worldedit.extent.Extent)

Example 2 with InputParseException

use of com.sk89q.worldedit.extension.input.InputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class RandomFullClipboardPatternParser method parseFromInput.

@Override
protected Pattern parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException {
    if (arguments.length == 0 || arguments.length > 3) {
        throw new InputParseException(Caption.of("fawe.error.command.syntax", TextComponent.of(getPrefix() + "[pattern] (e.g. " + getPrefix() + "[#copy][true][false])")));
    }
    try {
        boolean rotate = arguments.length >= 2 && Boolean.getBoolean(arguments[1]);
        boolean flip = arguments.length == 3 && Boolean.getBoolean(arguments[2]);
        List<ClipboardHolder> clipboards;
        if ("#copy".startsWith(arguments[0].toUpperCase(Locale.ROOT)) || "#clipboard".startsWith(arguments[0].toUpperCase(Locale.ROOT))) {
            ClipboardHolder clipboard = context.requireSession().getExistingClipboard();
            if (clipboard == null) {
                throw new InputParseException(Caption.of("fawe.error.parse.no-clipboard", getPrefix()));
            }
            clipboards = Collections.singletonList(clipboard);
        } else {
            Actor player = context.requireActor();
            MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("fast"), false);
            if (multi == null) {
                multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("mcedit"), false);
            }
            if (multi == null) {
                multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("sponge"), false);
            }
            if (multi == null) {
                throw new InputParseException(Caption.of("fawe.error.parse.no-clipboard-source", arguments[0]));
            }
            clipboards = multi.getHolders();
        }
        return new RandomFullClipboardPattern(clipboards, rotate, flip);
    } catch (IOException e) {
        throw new InputParseException(TextComponent.of(e.getMessage()), e);
    }
}
Also used : RandomFullClipboardPattern(com.fastasyncworldedit.core.function.pattern.RandomFullClipboardPattern) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) Actor(com.sk89q.worldedit.extension.platform.Actor) IOException(java.io.IOException)

Example 3 with InputParseException

use of com.sk89q.worldedit.extension.input.InputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class PlatformCommandManager method handleCommandSuggestion.

@Subscribe
public void handleCommandSuggestion(CommandSuggestionEvent event) {
    try {
        String rawArgs = event.getArguments();
        // FAWE start
        // Increase the resulting positions by 1 if we remove a leading `/`
        final int posOffset = rawArgs.startsWith("/") ? 1 : 0;
        String arguments = rawArgs.startsWith("/") ? rawArgs.substring(1) : rawArgs;
        // FAWE end
        List<Substring> split = parseArgs(arguments).collect(Collectors.toList());
        List<String> argStrings = split.stream().map(Substring::getSubstring).collect(Collectors.toList());
        // FAWE start - event & suggestion
        MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event, true);
        // FAWE end
        ImmutableSet<Suggestion> suggestions;
        try {
            suggestions = commandManager.getSuggestions(access, argStrings);
        } catch (Throwable t) {
            // catch errors which are *not* command exceptions generated by parsers/suggesters
            if (!(t instanceof InputParseException) && !(t instanceof CommandException)) {
                Throwable cause = t;
                // These exceptions can be very nested, and should not be printed as they are not an actual error.
                boolean printError = true;
                while ((cause = cause.getCause()) != null) {
                    if (cause instanceof InputParseException || cause instanceof CommandException) {
                        printError = false;
                        break;
                    }
                }
                if (printError) {
                    LOGGER.error("Unexpected error occurred while generating suggestions for input: {}", arguments, t);
                    return;
                }
            }
            return;
        }
        event.setSuggestions(suggestions.stream().map(suggestion -> {
            int noSlashLength = arguments.length();
            Substring original = suggestion.getReplacedArgument() == split.size() ? Substring.from(arguments, noSlashLength, noSlashLength) : split.get(suggestion.getReplacedArgument());
            return Substring.wrap(suggestion.getSuggestion(), original.getStart() + posOffset, original.getEnd() + posOffset);
        }).collect(Collectors.toList()));
    } catch (ConditionFailedException e) {
        if (e.getCondition() instanceof PermissionCondition) {
            event.setSuggestions(new ArrayList<>());
        }
    }
}
Also used : Substring(com.sk89q.worldedit.internal.util.Substring) MemoizingValueAccess(org.enginehub.piston.inject.MemoizingValueAccess) ArrayList(java.util.ArrayList) CommandException(org.enginehub.piston.exception.CommandException) Suggestion(org.enginehub.piston.suggestion.Suggestion) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) ConditionFailedException(org.enginehub.piston.exception.ConditionFailedException) PermissionCondition(com.sk89q.worldedit.command.util.PermissionCondition) SubCommandPermissionCondition(com.sk89q.worldedit.command.util.SubCommandPermissionCondition) Subscribe(com.sk89q.worldedit.util.eventbus.Subscribe)

Example 4 with InputParseException

use of com.sk89q.worldedit.extension.input.InputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class BlockCategoryPatternParser method parseFromInput.

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
    if (!input.startsWith("##")) {
        return null;
    }
    String tag = input.substring(2).toLowerCase(Locale.ROOT);
    boolean anyState = false;
    if (tag.startsWith("*")) {
        tag = tag.substring(1);
        anyState = true;
    }
    BlockCategory category = BlockCategory.REGISTRY.get(tag);
    if (category == null) {
        throw new InputParseException(Caption.of("worldedit.error.unknown-tag", TextComponent.of(tag)));
    }
    RandomPattern randomPattern = new RandomPattern();
    Set<BlockType> blocks = category.getAll();
    if (blocks.isEmpty()) {
        throw new InputParseException(Caption.of("worldedit.error.empty-tag", TextComponent.of(category.getId())));
    }
    if (anyState) {
        blocks.stream().flatMap(blockType -> blockType.getAllStates().stream()).forEach(state -> randomPattern.add(state, 1.0));
    } else {
        for (BlockType blockType : blocks) {
            randomPattern.add(blockType.getDefaultState(), 1.0);
        }
    }
    return randomPattern;
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Set(java.util.Set) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Caption(com.fastasyncworldedit.core.configuration.Caption) BlockCategory(com.sk89q.worldedit.world.block.BlockCategory) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) AliasedParser(com.fastasyncworldedit.core.extension.factory.parser.AliasedParser) List(java.util.List) Stream(java.util.stream.Stream) InputParser(com.sk89q.worldedit.internal.registry.InputParser) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Locale(java.util.Locale) WorldEdit(com.sk89q.worldedit.WorldEdit) SuggestionHelper(com.sk89q.worldedit.command.util.SuggestionHelper) Pattern(com.sk89q.worldedit.function.pattern.Pattern) Collections(java.util.Collections) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) BlockType(com.sk89q.worldedit.world.block.BlockType) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) BlockCategory(com.sk89q.worldedit.world.block.BlockCategory)

Example 5 with InputParseException

use of com.sk89q.worldedit.extension.input.InputParseException in project FastAsyncWorldEdit by IntellectualSites.

the class RandomPatternParser method parseFromInput.

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
    RandomPattern randomPattern = new RandomPattern();
    // FAWE start
    List<String> patterns = StringUtil.split(input, ',', '[', ']');
    // FAWE end
    if (patterns.size() == 1) {
        // let a 'single'-pattern parser handle it
        return null;
    }
    for (String token : patterns) {
        double chance;
        Pattern innerPattern;
        // Parse special percentage syntax
        if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) {
            // FAWE start
            String[] p = token.split("%", 2);
            if (p.length < 2) {
                throw new InputParseException(Caption.of("worldedit.error.parser.missing-random-type", TextComponent.of(input)));
            } else {
                chance = Double.parseDouble(p[0]);
                innerPattern = worldEdit.getPatternFactory().parseFromInput(p[1], context);
            }
        } else {
            chance = 1;
            innerPattern = worldEdit.getPatternFactory().parseFromInput(token, context);
        }
        randomPattern.add(innerPattern, chance);
    }
    return randomPattern;
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Pattern(com.sk89q.worldedit.function.pattern.Pattern) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern)

Aggregations

InputParseException (com.sk89q.worldedit.extension.input.InputParseException)29 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)11 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)7 Extent (com.sk89q.worldedit.extent.Extent)7 Pattern (com.sk89q.worldedit.function.pattern.Pattern)7 Map (java.util.Map)7 Actor (com.sk89q.worldedit.extension.platform.Actor)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 BlanketBaseBlock (com.fastasyncworldedit.core.world.block.BlanketBaseBlock)4 CompoundTag (com.sk89q.jnbt.CompoundTag)4 WorldEdit (com.sk89q.worldedit.WorldEdit)4 WorldEditException (com.sk89q.worldedit.WorldEditException)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 World (com.sk89q.worldedit.world.World)4 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)4 List (java.util.List)4 Stream (java.util.stream.Stream)4 SuggestInputParseException (com.fastasyncworldedit.core.command.SuggestInputParseException)3 Caption (com.fastasyncworldedit.core.configuration.Caption)3