Search in sources :

Example 1 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)

Example 2 with Pattern

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

the class FaweDelegateRegionManager method handleClear.

public boolean handleClear(@Nonnull Plot plot, @Nullable Runnable whenDone, @Nonnull PlotManager manager) {
    TaskManager.taskManager().async(() -> {
        synchronized (FaweDelegateRegionManager.class) {
            final HybridPlotWorld hybridPlotWorld = ((HybridPlotManager) manager).getHybridPlotWorld();
            World world = BukkitAdapter.adapt(getWorld(hybridPlotWorld.getWorldName()));
            EditSession editSession = WorldEdit.getInstance().newEditSessionBuilder().world(world).checkMemory(false).fastMode(true).limitUnlimited().changeSetNull().build();
            if (!hybridPlotWorld.PLOT_SCHEMATIC || !Settings.Schematics.PASTE_ON_TOP) {
                final BlockType bedrock;
                final BlockType air = BlockTypes.AIR;
                if (hybridPlotWorld.PLOT_BEDROCK) {
                    bedrock = BlockTypes.BEDROCK;
                } else {
                    bedrock = air;
                }
                final Pattern filling = hybridPlotWorld.MAIN_BLOCK.toPattern();
                final Pattern plotfloor = hybridPlotWorld.TOP_BLOCK.toPattern();
                final BiomeType biome = hybridPlotWorld.getPlotBiome();
                BlockVector3 pos1 = plot.getBottomAbs().getBlockVector3();
                BlockVector3 pos2 = pos1.add(BlockVector3.at(hybridPlotWorld.PLOT_WIDTH - 1, hybridPlotWorld.getMaxGenHeight(), hybridPlotWorld.PLOT_WIDTH - 1));
                if (hybridPlotWorld.PLOT_BEDROCK) {
                    Region bedrockRegion = new CuboidRegion(pos1, pos2.withY(hybridPlotWorld.getMinGenHeight()));
                    editSession.setBlocks(bedrockRegion, bedrock);
                }
                Region fillingRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.getMinGenHeight() + 1), pos2.withY(hybridPlotWorld.PLOT_HEIGHT - 1));
                Region floorRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.PLOT_HEIGHT), pos2.withY(hybridPlotWorld.PLOT_HEIGHT));
                Region airRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.PLOT_HEIGHT + 1), pos2.withY(hybridPlotWorld.getMaxGenHeight()));
                editSession.setBlocks(fillingRegion, filling);
                editSession.setBlocks(floorRegion, plotfloor);
                editSession.setBlocks(airRegion, air);
            }
            if (hybridPlotWorld.PLOT_SCHEMATIC) {
                // We cannot reuse the editsession
                EditSession scheditsession = !Settings.Schematics.PASTE_ON_TOP ? editSession : WorldEdit.getInstance().newEditSessionBuilder().world(world).checkMemory(false).fastMode(true).limitUnlimited().changeSetNull().build();
                File schematicFile = new File(hybridPlotWorld.getRoot(), "plot.schem");
                if (!schematicFile.exists()) {
                    schematicFile = new File(hybridPlotWorld.getRoot(), "plot.schematic");
                }
                BlockVector3 to = plot.getBottomAbs().getBlockVector3().withY(Settings.Schematics.PASTE_ON_TOP ? hybridPlotWorld.SCHEM_Y : hybridPlotWorld.getMinBuildHeight());
                try {
                    Clipboard clip = ClipboardFormats.findByFile(schematicFile).getReader(new FileInputStream(schematicFile)).read();
                    clip.paste(scheditsession, to, true, true, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Be verbose in editsession flushing
                scheditsession.flushQueue();
            }
            editSession.flushQueue();
            FaweAPI.fixLighting(world, new CuboidRegion(plot.getBottomAbs().getBlockVector3(), plot.getTopAbs().getBlockVector3()), null, RelightMode.valueOf(com.fastasyncworldedit.core.configuration.Settings.settings().LIGHTING.MODE));
            if (whenDone != null) {
                TaskManager.taskManager().task(whenDone);
            }
        }
    });
    return true;
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) HybridPlotWorld(com.plotsquared.core.generator.HybridPlotWorld) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) IOException(java.io.IOException) World(com.sk89q.worldedit.world.World) Bukkit.getWorld(org.bukkit.Bukkit.getWorld) HybridPlotWorld(com.plotsquared.core.generator.HybridPlotWorld) BlockVector3(com.sk89q.worldedit.math.BlockVector3) FileInputStream(java.io.FileInputStream) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) BlockType(com.sk89q.worldedit.world.block.BlockType) HybridPlotManager(com.plotsquared.core.generator.HybridPlotManager) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) EditSession(com.sk89q.worldedit.EditSession) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) File(java.io.File)

Example 3 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 4 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 5 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)

Aggregations

Pattern (com.sk89q.worldedit.function.pattern.Pattern)15 RandomPattern (com.sk89q.worldedit.function.pattern.RandomPattern)7 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)6 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)4 EditSession (com.sk89q.worldedit.EditSession)3 Extent (com.sk89q.worldedit.extent.Extent)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 Mask (com.sk89q.worldedit.function.mask.Mask)2 RecursiveVisitor (com.sk89q.worldedit.function.visitor.RecursiveVisitor)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 Region (com.sk89q.worldedit.regions.Region)2 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)2 World (com.sk89q.worldedit.world.World)2 BlockType (com.sk89q.worldedit.world.block.BlockType)2 IOException (java.io.IOException)2