Search in sources :

Example 1 with RandomTransform

use of com.fastasyncworldedit.core.extent.transform.RandomTransform 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)

Example 2 with RandomTransform

use of com.fastasyncworldedit.core.extent.transform.RandomTransform in project FastAsyncWorldEdit by IntellectualSites.

the class RandomTransformParser method parseFromInput.

@Override
public ResettableExtent parseFromInput(String input, ParserContext context) throws InputParseException {
    if (input.isEmpty()) {
        return null;
    }
    List<String> split = StringUtil.split(input, ',', '[', ']');
    if (split.size() == 1) {
        return null;
    }
    RandomTransform randomTransform = new RandomTransform();
    for (String s : split) {
        ResettableExtent transform = worldEdit.getTransformFactory().parseFromInput(s, context);
        randomTransform.add(transform, 1d);
    }
    return randomTransform;
}
Also used : RandomTransform(com.fastasyncworldedit.core.extent.transform.RandomTransform) ResettableExtent(com.fastasyncworldedit.core.extent.ResettableExtent)

Aggregations

ResettableExtent (com.fastasyncworldedit.core.extent.ResettableExtent)2 RandomTransform (com.fastasyncworldedit.core.extent.transform.RandomTransform)2 SuggestInputParseException (com.fastasyncworldedit.core.command.SuggestInputParseException)1 MultiTransform (com.fastasyncworldedit.core.extent.transform.MultiTransform)1 TrueRandom (com.fastasyncworldedit.core.math.random.TrueRandom)1 CommandLocals (com.sk89q.minecraft.util.commands.CommandLocals)1 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)1 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)1 Actor (com.sk89q.worldedit.extension.platform.Actor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1