Search in sources :

Example 6 with RandomPattern

use of com.sk89q.worldedit.function.pattern.RandomPattern 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)

Example 7 with RandomPattern

use of com.sk89q.worldedit.function.pattern.RandomPattern 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 8 with RandomPattern

use of com.sk89q.worldedit.function.pattern.RandomPattern in project FastAsyncWorldEdit by IntellectualSites.

the class FloraGenerator method mushroomPattern.

// FAWE start
/**
 * Get a pattern for plants to place inside a mushroom environment.
 *
 * @return a pattern that places flora
 */
public static Pattern mushroomPattern() {
    RandomPattern pattern = new RandomPattern();
    pattern.add(BlockTypes.RED_MUSHROOM.getDefaultState(), 10);
    pattern.add(BlockTypes.BROWN_MUSHROOM.getDefaultState(), 10);
    return pattern;
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern)

Example 9 with RandomPattern

use of com.sk89q.worldedit.function.pattern.RandomPattern in project FastAsyncWorldEdit by IntellectualSites.

the class FloraGenerator method netherPattern.

/**
 * Get a pattern for plants to place inside a nether environment.
 *
 * @return a pattern that places flora
 */
public static Pattern netherPattern() {
    RandomPattern pattern = new RandomPattern();
    pattern.add(BlockTypes.CRIMSON_ROOTS.getDefaultState(), 10);
    pattern.add(BlockTypes.CRIMSON_FUNGUS.getDefaultState(), 20);
    pattern.add(BlockTypes.WARPED_FUNGUS.getDefaultState(), 5);
    return pattern;
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern)

Example 10 with RandomPattern

use of com.sk89q.worldedit.function.pattern.RandomPattern in project FastAsyncWorldEdit by IntellectualSites.

the class FloraGenerator method warpedNyliumPattern.

/**
 * Get a pattern for plants to place inside a nether environment.
 *
 * @return a pattern that places flora
 */
public static Pattern warpedNyliumPattern() {
    RandomPattern pattern = new RandomPattern();
    pattern.add(BlockTypes.WARPED_ROOTS.getDefaultState(), 15);
    pattern.add(BlockTypes.NETHER_SPROUTS.getDefaultState(), 20);
    pattern.add(BlockTypes.WARPED_FUNGUS.getDefaultState(), 7);
    pattern.add(BlockTypes.CRIMSON_ROOTS.getDefaultState(), 10);
    return pattern;
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern)

Aggregations

RandomPattern (com.sk89q.worldedit.function.pattern.RandomPattern)17 Pattern (com.sk89q.worldedit.function.pattern.Pattern)9 EditSession (com.sk89q.worldedit.EditSession)5 Region (com.sk89q.worldedit.regions.Region)5 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)4 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)3 MineData (me.untouchedodin0.privatemines.mine.data.MineData)3 Caption (com.fastasyncworldedit.core.configuration.Caption)2 WorldEdit (com.sk89q.worldedit.WorldEdit)2 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)2 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)2 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)2 World (com.sk89q.worldedit.world.World)2 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Stream (java.util.stream.Stream)2 MineType (me.untouchedodin0.kotlin.mine.type.MineType)2 CuboidRegion (redempt.redlib.region.CuboidRegion)2