Search in sources :

Example 6 with Pattern

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

the class PatternFactory method parseFromParsers.

// FAWE start - rich pattern parsing
private void parseFromParsers(final ParserContext context, final List<Pattern> patterns, final String component) {
    Pattern match = null;
    for (InputParser<Pattern> parser : getParsers()) {
        match = parser.parseFromInput(component, context);
        if (match != null) {
            break;
        }
    }
    if (match == null) {
        throw new NoMatchException(Caption.of("worldedit.error.no-match", TextComponent.of(component)));
    }
    patterns.add(match);
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Pattern(com.sk89q.worldedit.function.pattern.Pattern) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException)

Example 7 with Pattern

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

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

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

the class TypeOrStateApplyingPatternParser method parseFromInput.

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
    if (!input.startsWith("^")) {
        return null;
    }
    Extent extent = context.requireExtent();
    input = input.substring(1);
    String[] parts = input.split("\\[", 2);
    String type = parts[0];
    if (parts.length == 1) {
        return new TypeApplyingPattern(extent, worldEdit.getBlockFactory().parseFromInput(type, context).getBlockType().getDefaultState());
    } else {
        // states given
        if (!parts[1].endsWith("]")) {
            throw new InputParseException(Caption.of("worldedit.error.parser.missing-rbracket"));
        }
        final String[] states = parts[1].substring(0, parts[1].length() - 1).split(",");
        Map<String, String> statesToSet = new HashMap<>();
        for (String state : states) {
            if (state.isEmpty()) {
                throw new InputParseException(Caption.of("worldedit.error.parser.empty-state"));
            }
            String[] propVal = state.split("=", 2);
            if (propVal.length != 2) {
                throw new InputParseException(Caption.of("worldedit.error.parser.missing-equals-separator"));
            }
            final String prop = propVal[0];
            if (prop.isEmpty()) {
                throw new InputParseException(Caption.of("worldedit.error.parser.empty-property"));
            }
            final String value = propVal[1];
            if (value.isEmpty()) {
                throw new InputParseException(Caption.of("worldedit.error.parser.empty-value"));
            }
            if (statesToSet.put(prop, value) != null) {
                throw new InputParseException(Caption.of("worldedit.error.parser.duplicate-property", TextComponent.of(prop)));
            }
        }
        if (type.isEmpty()) {
            return new StateApplyingPattern(extent, statesToSet);
        } else {
            Extent buffer = new ExtentBuffer(extent);
            Pattern typeApplier = new TypeApplyingPattern(buffer, worldEdit.getBlockFactory().parseFromInput(type, context).getBlockType().getDefaultState());
            Pattern stateApplier = new StateApplyingPattern(buffer, statesToSet);
            return new ExtentBufferedCompositePattern(buffer, typeApplier, stateApplier);
        }
    }
}
Also used : ExtentBufferedCompositePattern(com.sk89q.worldedit.function.pattern.ExtentBufferedCompositePattern) TypeApplyingPattern(com.sk89q.worldedit.function.pattern.TypeApplyingPattern) StateApplyingPattern(com.sk89q.worldedit.function.pattern.StateApplyingPattern) Pattern(com.sk89q.worldedit.function.pattern.Pattern) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) ExtentBufferedCompositePattern(com.sk89q.worldedit.function.pattern.ExtentBufferedCompositePattern) TypeApplyingPattern(com.sk89q.worldedit.function.pattern.TypeApplyingPattern) Extent(com.sk89q.worldedit.extent.Extent) HashMap(java.util.HashMap) ExtentBuffer(com.sk89q.worldedit.extent.buffer.ExtentBuffer) StateApplyingPattern(com.sk89q.worldedit.function.pattern.StateApplyingPattern)

Example 10 with Pattern

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

the class Scroll method fromArguments.

public static Scroll fromArguments(BrushTool tool, Player player, LocalSession session, Action mode, List<String> arguments, boolean message) throws InputParseException {
    ParserContext parserContext = new ParserContext();
    parserContext.setActor(player);
    parserContext.setWorld(player.getWorld());
    parserContext.setSession(session);
    switch(mode) {
        case CLIPBOARD:
            if (arguments.size() != 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "clipboard [file]"));
                }
                return null;
            }
            String filename = arguments.get(1);
            try {
                MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, filename, null, message);
                if (multi == null) {
                    return null;
                }
                return (new ScrollClipboard(tool, session, multi.getHolders()));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        case MASK:
            if (arguments.size() < 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "mask [mask 1] [mask 2] [mask 3]..."));
                }
                return null;
            }
            Mask[] masks = new Mask[arguments.size() - 1];
            for (int i = 1; i < arguments.size(); i++) {
                String arg = arguments.get(i);
                masks[i - 1] = WorldEdit.getInstance().getMaskFactory().parseFromInput(arg, parserContext);
            }
            return (new ScrollMask(tool, masks));
        case PATTERN:
            if (arguments.size() < 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "pattern [pattern 1] [pattern 2] [pattern 3]..."));
                }
                return null;
            }
            Pattern[] patterns = new Pattern[arguments.size() - 1];
            for (int i = 1; i < arguments.size(); i++) {
                String arg = arguments.get(i);
                patterns[i - 1] = WorldEdit.getInstance().getPatternFactory().parseFromInput(arg, parserContext);
            }
            return (new ScrollPattern(tool, patterns));
        case TARGET_OFFSET:
            return (new ScrollTargetOffset(tool));
        case RANGE:
            return (new ScrollRange(tool));
        case SIZE:
            return (new ScrollSize(tool));
        case TARGET:
            return (new ScrollTarget(tool));
        default:
            return null;
    }
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) Mask(com.sk89q.worldedit.function.mask.Mask) IOException(java.io.IOException) ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Aggregations

Pattern (com.sk89q.worldedit.function.pattern.Pattern)23 RandomPattern (com.sk89q.worldedit.function.pattern.RandomPattern)11 EditSession (com.sk89q.worldedit.EditSession)7 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)7 Region (com.sk89q.worldedit.regions.Region)7 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)6 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)6 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)4 World (com.sk89q.worldedit.world.World)4 IOException (java.io.IOException)4 MineData (me.untouchedodin0.privatemines.mine.data.MineData)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Caption (com.fastasyncworldedit.core.configuration.Caption)2 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)2 WorldEdit (com.sk89q.worldedit.WorldEdit)2 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)2 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)2 Extent (com.sk89q.worldedit.extent.Extent)2 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)2