Search in sources :

Example 1 with ExtentBuffer

use of com.sk89q.worldedit.extent.buffer.ExtentBuffer 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)

Aggregations

InputParseException (com.sk89q.worldedit.extension.input.InputParseException)1 Extent (com.sk89q.worldedit.extent.Extent)1 ExtentBuffer (com.sk89q.worldedit.extent.buffer.ExtentBuffer)1 ExtentBufferedCompositePattern (com.sk89q.worldedit.function.pattern.ExtentBufferedCompositePattern)1 Pattern (com.sk89q.worldedit.function.pattern.Pattern)1 StateApplyingPattern (com.sk89q.worldedit.function.pattern.StateApplyingPattern)1 TypeApplyingPattern (com.sk89q.worldedit.function.pattern.TypeApplyingPattern)1 HashMap (java.util.HashMap)1