Search in sources :

Example 1 with ParserContext

use of com.sk89q.worldedit.extension.input.ParserContext 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)

Example 2 with ParserContext

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

the class CraftScriptContext method getBlocks.

/**
 * Get a list of blocks as a set.
 *
 * @param list             a list
 * @param allBlocksAllowed true if all blocks are allowed
 * @return set
 * @throws NoMatchException         if the blocks couldn't be found
 * @throws DisallowedUsageException if the block is disallowed
 */
public Set<BaseBlock> getBlocks(String list, boolean allBlocksAllowed) throws WorldEditException {
    ParserContext context = new ParserContext();
    context.setActor(player);
    context.setWorld(player.getWorld());
    context.setSession(session);
    context.setRestricted(!allBlocksAllowed);
    return controller.getBlockFactory().parseFromListInput(list, context);
}
Also used : ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Example 3 with ParserContext

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

the class CraftScriptContext method getBlockPattern.

/**
 * Get a list of blocks as a set. This returns a Pattern.
 *
 * @param list the input
 * @return pattern
 * @throws NoMatchException         if the pattern was invalid
 * @throws DisallowedUsageException if the block is disallowed
 */
public Pattern getBlockPattern(String list) throws WorldEditException {
    ParserContext context = new ParserContext();
    context.setActor(player);
    context.setWorld(player.getWorld());
    context.setSession(session);
    return controller.getPatternFactory().parseFromInput(list, context);
}
Also used : ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Example 4 with ParserContext

use of com.sk89q.worldedit.extension.input.ParserContext 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 ParserContext

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

the class DefaultBlockParser method parseProperties.

// FAWE start - make public
public static Map<Property<?>, Object> parseProperties(// FAWE end
BlockType type, String[] stateProperties, ParserContext context, // FAWE start - if null should be returned instead of throwing an error
boolean nullNotError) throws // FAWE end
NoMatchException {
    Map<Property<?>, Object> blockStates = new HashMap<>();
    // FAWE start - disallowed states
    if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
        for (String input : context.getActor().getLimit().DISALLOWED_BLOCKS) {
            if (input.indexOf('[') == -1 && input.indexOf(']') == -1) {
                continue;
            }
            if (!type.getId().equalsIgnoreCase(input.substring(0, input.indexOf('[')))) {
                continue;
            }
            String[] properties = input.substring(input.indexOf('[') + 1, input.indexOf(']')).split(",");
            Set<String> blocked = Arrays.stream(properties).filter(s -> {
                for (String in : stateProperties) {
                    if (in.equalsIgnoreCase(s)) {
                        return true;
                    }
                }
                return false;
            }).collect(Collectors.toSet());
            if (!blocked.isEmpty()) {
                throw new DisallowedUsageException(Caption.of("fawe.error.limit.disallowed-block", TextComponent.of(input)));
            }
        }
    }
    if (stateProperties.length > 0) {
        // Parse the block data (optional)
        for (String parseableData : stateProperties) {
            try {
                String[] parts = parseableData.split("=");
                if (parts.length != 2) {
                    // FAWE start - if null should be returned instead of throwing an error
                    if (nullNotError) {
                        return null;
                    }
                    // FAWE end
                    throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
                }
                @SuppressWarnings("unchecked") Property<Object> propertyKey = (Property<Object>) type.getPropertyMap().get(parts[0]);
                if (propertyKey == null) {
                    // FAWE start - nullable context
                    if (context != null && context.getActor() != null) {
                        // FAWE start - if null should be returned instead of throwing an error
                        if (nullNotError) {
                            return null;
                        }
                        // FAWE end
                        throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-property", TextComponent.of(parts[0]), TextComponent.of(type.getId())));
                    } else {
                        WorldEdit.logger.debug("Unknown property " + parts[0] + " for block " + type.getId());
                    }
                    return Maps.newHashMap();
                }
                if (blockStates.containsKey(propertyKey)) {
                    // FAWE start - if null should be returned instead of throwing an error
                    if (nullNotError) {
                        return null;
                    }
                    // FAWE end
                    throw new InputParseException(Caption.of("worldedit.error.parser.duplicate-property", TextComponent.of(parts[0])));
                }
                Object value;
                try {
                    value = propertyKey.getValueFor(parts[1]);
                } catch (IllegalArgumentException e) {
                    // FAWE start - if null should be returned instead of throwing an error
                    if (nullNotError) {
                        return null;
                    }
                    // FAWE end
                    throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-value", TextComponent.of(parts[1]), TextComponent.of(propertyKey.getName())));
                }
                // FAWE start - blocked states
                if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
                    if (context.getActor().getLimit().REMAP_PROPERTIES != null && !context.getActor().getLimit().REMAP_PROPERTIES.isEmpty()) {
                        for (PropertyRemap remap : context.getActor().getLimit().REMAP_PROPERTIES) {
                            Object newValue = remap.apply(type, value);
                            if (newValue != value) {
                                value = newValue;
                                break;
                            }
                        }
                    }
                }
                // FAWE end
                blockStates.put(propertyKey, value);
            } catch (NoMatchException | DisallowedUsageException e) {
                // Pass-through
                throw e;
            } catch (Exception e) {
                // FAWE start - if null should be returned instead of throwing an error
                if (nullNotError) {
                    return null;
                }
                // FAWE end
                throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
            }
        }
    }
    return blockStates;
}
Also used : Property(com.sk89q.worldedit.registry.state.Property) Arrays(java.util.Arrays) SignBlock(com.sk89q.worldedit.blocks.SignBlock) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Player(com.sk89q.worldedit.entity.Player) World(com.sk89q.worldedit.world.World) Caption(com.fastasyncworldedit.core.configuration.Caption) SkullBlock(com.sk89q.worldedit.blocks.SkullBlock) EntityType(com.sk89q.worldedit.world.entity.EntityType) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) JSON2NBT(com.fastasyncworldedit.core.jnbt.JSON2NBT) Locale(java.util.Locale) DisallowedUsageException(com.sk89q.worldedit.extension.input.DisallowedUsageException) Map(java.util.Map) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) BlockStateHolder(com.sk89q.worldedit.world.block.BlockStateHolder) BlockType(com.sk89q.worldedit.world.block.BlockType) BaseItem(com.sk89q.worldedit.blocks.BaseItem) Set(java.util.Set) Collectors(java.util.stream.Collectors) BlockBag(com.sk89q.worldedit.extent.inventory.BlockBag) MathMan(com.fastasyncworldedit.core.util.MathMan) NotABlockException(com.sk89q.worldedit.NotABlockException) Stream(java.util.stream.Stream) InputParser(com.sk89q.worldedit.internal.registry.InputParser) LegacyMapper(com.sk89q.worldedit.world.registry.LegacyMapper) CompoundTag(com.sk89q.jnbt.CompoundTag) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) SuggestionHelper(com.sk89q.worldedit.command.util.SuggestionHelper) BlockCategories(com.sk89q.worldedit.world.block.BlockCategories) HashMap(java.util.HashMap) BlanketBaseBlock(com.fastasyncworldedit.core.world.block.BlanketBaseBlock) PropertyRemap(com.fastasyncworldedit.core.limit.PropertyRemap) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) StringMan(com.fastasyncworldedit.core.util.StringMan) FuzzyBlockState(com.sk89q.worldedit.world.block.FuzzyBlockState) WorldEditException(com.sk89q.worldedit.WorldEditException) MobSpawnerBlock(com.sk89q.worldedit.blocks.MobSpawnerBlock) WorldEdit(com.sk89q.worldedit.WorldEdit) HandSide(com.sk89q.worldedit.util.HandSide) IncompleteRegionException(com.sk89q.worldedit.IncompleteRegionException) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) NBTException(com.fastasyncworldedit.core.jnbt.NBTException) FaweLimit(com.fastasyncworldedit.core.limit.FaweLimit) Maps(com.google.common.collect.Maps) Actor(com.sk89q.worldedit.extension.platform.Actor) EntityTypes(com.sk89q.worldedit.world.entity.EntityTypes) SlottableBlockBag(com.fastasyncworldedit.core.extent.inventory.SlottableBlockBag) Capability(com.sk89q.worldedit.extension.platform.Capability) BlockState(com.sk89q.worldedit.world.block.BlockState) HashMap(java.util.HashMap) PropertyRemap(com.fastasyncworldedit.core.limit.PropertyRemap) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) DisallowedUsageException(com.sk89q.worldedit.extension.input.DisallowedUsageException) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) NotABlockException(com.sk89q.worldedit.NotABlockException) WorldEditException(com.sk89q.worldedit.WorldEditException) IncompleteRegionException(com.sk89q.worldedit.IncompleteRegionException) NBTException(com.fastasyncworldedit.core.jnbt.NBTException) DisallowedUsageException(com.sk89q.worldedit.extension.input.DisallowedUsageException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) Property(com.sk89q.worldedit.registry.state.Property)

Aggregations

ParserContext (com.sk89q.worldedit.extension.input.ParserContext)15 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)11 Extent (com.sk89q.worldedit.extent.Extent)4 Mask (com.sk89q.worldedit.function.mask.Mask)4 World (com.sk89q.worldedit.world.World)4 Map (java.util.Map)4 Caption (com.fastasyncworldedit.core.configuration.Caption)3 WorldEdit (com.sk89q.worldedit.WorldEdit)3 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)3 Actor (com.sk89q.worldedit.extension.platform.Actor)3 Pattern (com.sk89q.worldedit.function.pattern.Pattern)3 Region (com.sk89q.worldedit.regions.Region)3 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)3 IOException (java.io.IOException)3 Locale (java.util.Locale)3 Stream (java.util.stream.Stream)3 StringMan (com.fastasyncworldedit.core.util.StringMan)2 BukkitPlayer (com.sk89q.worldedit.bukkit.BukkitPlayer)2 SuggestionHelper (com.sk89q.worldedit.command.util.SuggestionHelper)2 Entity (com.sk89q.worldedit.entity.Entity)2