Search in sources :

Example 11 with ParserContext

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

the class ConsumeBindings method baseBlock.

@Binding
public BaseBlock baseBlock(Actor actor, String argument) {
    ParserContext parserContext = new ParserContext();
    parserContext.setActor(actor);
    if (actor instanceof Entity) {
        Extent extent = ((Entity) actor).getExtent();
        if (extent instanceof World) {
            parserContext.setWorld((World) extent);
        }
    }
    parserContext.setSession(getWorldEdit().getSessionManager().get(actor));
    try {
        return getWorldEdit().getBlockFactory().parseFromInput(argument, parserContext);
    } catch (NoMatchException e) {
        throw new InputParseException(TextComponent.of(e.getMessage()));
    }
}
Also used : Entity(com.sk89q.worldedit.entity.Entity) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Extent(com.sk89q.worldedit.extent.Extent) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) World(com.sk89q.worldedit.world.World) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException)

Example 12 with ParserContext

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

the class FactoryConverter method convert.

@Override
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
    Actor actor = context.injectedValue(Key.of(Actor.class)).orElseThrow(() -> new IllegalStateException("No actor"));
    LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
    ParserContext parserContext = new ParserContext();
    parserContext.setActor(actor);
    if (actor instanceof Locatable) {
        Extent extent = ((Locatable) actor).getExtent();
        if (extent instanceof World) {
            parserContext.setWorld((World) extent);
        }
        parserContext.setExtent(new SupplyingExtent(((Locatable) actor)::getExtent));
    } else if (session.hasWorldOverride()) {
        parserContext.setWorld(session.getWorldOverride());
        parserContext.setExtent(new SupplyingExtent(session::getWorldOverride));
    }
    parserContext.setSession(session);
    parserContext.setRestricted(true);
    parserContext.setInjected(context);
    if (contextTweaker != null) {
        contextTweaker.accept(parserContext);
    }
    try {
        return SuccessfulConversion.fromSingle(factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext));
    } catch (InputParseException e) {
        return FailedConversion.from(e);
    }
}
Also used : InputParseException(com.sk89q.worldedit.extension.input.InputParseException) ResettableExtent(com.fastasyncworldedit.core.extent.ResettableExtent) SupplyingExtent(com.fastasyncworldedit.core.extent.SupplyingExtent) Extent(com.sk89q.worldedit.extent.Extent) BlockTransformExtent(com.sk89q.worldedit.extent.transform.BlockTransformExtent) Actor(com.sk89q.worldedit.extension.platform.Actor) LocalSession(com.sk89q.worldedit.LocalSession) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) World(com.sk89q.worldedit.world.World) SupplyingExtent(com.fastasyncworldedit.core.extent.SupplyingExtent) Locatable(com.sk89q.worldedit.extension.platform.Locatable)

Example 13 with ParserContext

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

the class LegacyMapper method loadFromResource.

/**
 * Attempt to load the data from file.
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    URL url = resourceLoader.getResource(LegacyMapper.class, "legacy.json");
    if (url == null) {
        throw new IOException("Could not find legacy.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
    }.getType());
    DataFixer fixer = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataFixer();
    ParserContext parserContext = new ParserContext();
    parserContext.setPreferringWildcard(false);
    parserContext.setRestricted(false);
    // This is legacy. Don't match itself.
    parserContext.setTryLegacy(false);
    for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
        String id = blockEntry.getKey();
        final String value = blockEntry.getValue();
        // FAWE start
        Integer combinedId = getCombinedId(blockEntry.getKey());
        blockEntries.put(id, value);
        // FAWE end
        BlockState state = null;
        // FAWE start
        try {
            state = BlockState.get(null, blockEntry.getValue());
            BlockType type = state.getBlockType();
            if (type.hasProperty(PropertyKey.WATERLOGGED)) {
                state = state.with(PropertyKey.WATERLOGGED, false);
            }
        } catch (InputParseException f) {
            BlockFactory blockFactory = WorldEdit.getInstance().getBlockFactory();
            // if fixer is available, try using that first, as some old blocks that were renamed share names with new blocks
            if (fixer != null) {
                try {
                    String newEntry = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, value, Constants.DATA_VERSION_MC_1_13_2);
                    state = blockFactory.parseFromInput(newEntry, parserContext).toImmutableState();
                } catch (InputParseException ignored) {
                }
            }
            // if it's still null, the fixer was unavailable or failed
            if (state == null) {
                try {
                    state = blockFactory.parseFromInput(value, parserContext).toImmutableState();
                } catch (InputParseException ignored) {
                }
            }
            // if it's still null, both fixer and default failed
            if (state == null) {
                LOGGER.error("Unknown block: {}. Neither the DataFixer nor defaulting worked to recognize this block.", value);
            } else {
                // it's not null so one of them succeeded, now use it
                blockToStringMap.put(state, id);
                stringToBlockMap.put(id, state);
            }
        }
        // FAWE start
        if (state != null) {
            blockArr[combinedId] = state.getInternalId();
            blockStateToLegacyId4Data.put(state.getInternalId(), combinedId);
            blockStateToLegacyId4Data.putIfAbsent(state.getInternalBlockTypeId(), combinedId);
        }
    }
    for (int id = 0; id < 256; id++) {
        int combinedId = id << 4;
        int base = blockArr[combinedId];
        if (base != 0) {
            for (int data_ = 0; data_ < 16; data_++, combinedId++) {
                if (blockArr[combinedId] == 0) {
                    blockArr[combinedId] = base;
                }
            }
        }
    }
    for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
        String id = itemEntry.getKey();
        String value = itemEntry.getValue();
        ItemType type = ItemTypes.get(value);
        if (type == null && fixer != null) {
            value = fixer.fixUp(DataFixer.FixTypes.ITEM_TYPE, value, Constants.DATA_VERSION_MC_1_13_2);
            type = ItemTypes.get(value);
        }
        if (type == null) {
            LOGGER.error("Unknown item: {}. Neither the DataFixer nor defaulting worked to recognize this item.", value);
        } else {
            try {
                itemMap.put(getCombinedId(id), type);
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) BlockFactory(com.sk89q.worldedit.extension.factory.BlockFactory) VectorAdapter(com.sk89q.worldedit.util.gson.VectorAdapter) ItemType(com.sk89q.worldedit.world.item.ItemType) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) IOException(java.io.IOException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) TypeToken(com.google.gson.reflect.TypeToken) DataFixer(com.sk89q.worldedit.world.DataFixer) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap)

Example 14 with ParserContext

use of com.sk89q.worldedit.extension.input.ParserContext in project bteConoSurCore by BTEConoSur.

the class Tree method place.

// PLACE
public EditSession place(Vector loc, Player player, EditSession editSession) {
    if (editSession == null) {
        editSession = getEditSession(player);
    }
    com.sk89q.worldedit.entity.Player actor = new BukkitPlayer((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), ((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit")).getServerInterface(), player);
    LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(actor);
    Clipboard clipboard;
    ClipboardFormat format = ClipboardFormat.SCHEMATIC;
    try {
        ClipboardReader reader = format.getReader(new FileInputStream(schematic));
        clipboard = reader.read(actor.getWorld().getWorldData());
        clipboard.setOrigin(new Vector(xOffset, yOffset, zOffset));
        Region region = clipboard.getRegion();
        // PASTE SCHEMATIC
        // Get Maxs and Mins
        int xMax = clipboard.getMaximumPoint().getBlockX();
        int yMax = clipboard.getMaximumPoint().getBlockY();
        int zMax = clipboard.getMaximumPoint().getBlockZ();
        // MASK
        Mask mask = localSession.getMask();
        if (mask == null) {
            ParserContext parserContext = new ParserContext();
            parserContext.setActor(actor);
            Extent extent = actor.getExtent();
            if (extent instanceof World) {
                parserContext.setWorld((World) extent);
            }
            parserContext.setSession(WorldEdit.getInstance().getSessionManager().get(actor));
            mask = WorldEdit.getInstance().getMaskFactory().parseFromInput("0", parserContext);
        }
        // ROTATION
        int degrees = new Random().nextInt(4);
        // ORIGEN
        int xOg = loc.getBlockX();
        int zOg = loc.getBlockZ();
        for (BlockVector p : region) {
            if (clipboard.getBlock(p).getType() != 0) {
                int x = loc.getBlockX() + p.getBlockX() - xMax + xOffset;
                int y = loc.getBlockY() + p.getBlockY() - yMax + yOffset;
                int z = loc.getBlockZ() + p.getBlockZ() - zMax + zOffset;
                int x0 = x - xOg;
                int z0 = z - zOg;
                if (degrees == 1) {
                    x = z0 + xOg;
                    z = -x0 + zOg;
                } else if (degrees == 2) {
                    x = -x0 + xOg;
                    z = -z0 + zOg;
                } else if (degrees == 3) {
                    x = -z0 + xOg;
                    z = x0 + zOg;
                }
                Vector newVector = new Vector(x, y + clipboard.getDimensions().getBlockY() - 1, z);
                if (mask.test(newVector) && getWorldGuard().canBuild(player, mainWorld.getBlockAt(newVector.getBlockX(), newVector.getBlockY(), newVector.getBlockZ()))) {
                    editSession.setBlock(newVector, clipboard.getBlock(p));
                }
            }
        }
        return editSession;
    } catch (IOException | MaxChangedBlocksException | InputParseException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Extent(com.sk89q.worldedit.extent.Extent) Mask(com.sk89q.worldedit.function.mask.Mask) IOException(java.io.IOException) BteConoSur.mainWorld(pizzaaxx.bteconosur.BteConoSur.mainWorld) World(com.sk89q.worldedit.world.World) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) FileInputStream(java.io.FileInputStream) BukkitPlayer(com.sk89q.worldedit.bukkit.BukkitPlayer) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Random(java.util.Random) Region(com.sk89q.worldedit.regions.Region) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Example 15 with ParserContext

use of com.sk89q.worldedit.extension.input.ParserContext in project bteConoSurCore by BTEConoSur.

the class Polywall method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (command.getName().equals("/polywalls")) {
        if (sender instanceof Player) {
            Player p = (Player) sender;
            // 
            // GET REGION
            Region region = null;
            try {
                region = getSelection(p);
            } catch (IncompleteRegionException e) {
                p.sendMessage(wePrefix + "Selecciona un área primero.");
                return true;
            }
            if (args.length > 0) {
                // PARSE PATTERN
                com.sk89q.worldedit.entity.Player actor = new BukkitPlayer((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), ((WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit")).getServerInterface(), p);
                LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(actor);
                Mask mask = localSession.getMask();
                ParserContext parserContext = new ParserContext();
                parserContext.setActor(actor);
                Extent extent = ((Entity) actor).getExtent();
                if (extent instanceof World) {
                    parserContext.setWorld((World) extent);
                }
                parserContext.setSession(WorldEdit.getInstance().getSessionManager().get(actor));
                Pattern pattern;
                try {
                    pattern = WorldEdit.getInstance().getPatternFactory().parseFromInput(args[0], parserContext);
                } catch (InputParseException e) {
                    p.sendMessage(wePrefix + "Patrón inválido.");
                    return true;
                }
                // GET POINTS
                List<BlockVector2D> points = new ArrayList<>();
                int maxY;
                int minY;
                if (region instanceof CuboidRegion) {
                    CuboidRegion cuboidRegion = (CuboidRegion) region;
                    Vector first = cuboidRegion.getPos1();
                    Vector second = cuboidRegion.getPos2();
                    maxY = cuboidRegion.getMaximumY();
                    minY = cuboidRegion.getMinimumY();
                    points.add(new BlockVector2D(first.getX(), first.getZ()));
                    points.add(new BlockVector2D(second.getX(), first.getZ()));
                    points.add(new BlockVector2D(second.getX(), second.getZ()));
                    points.add(new BlockVector2D(first.getX(), second.getZ()));
                } else if (region instanceof Polygonal2DRegion) {
                    maxY = ((Polygonal2DRegion) region).getMaximumY();
                    minY = ((Polygonal2DRegion) region).getMinimumY();
                    points = ((Polygonal2DRegion) region).getPoints();
                } else {
                    p.sendMessage(wePrefix + "Debes seleccionar una region cúbica o poligonal.");
                    return true;
                }
                if (points.size() < 3) {
                    p.sendMessage(wePrefix + "Selecciona un área primero.");
                    return true;
                }
                List<BlockVector2D> pointsFinal = new ArrayList<>(points);
                pointsFinal.add(points.get(0));
                EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession((World) new BukkitWorld(mainWorld), WorldEdit.getInstance().getSessionManager().get(actor).getBlockChangeLimit());
                for (int i = minY; i <= maxY; i++) {
                    for (int j = 0; j < pointsFinal.size() - 1; j++) {
                        BlockVector2D v1 = pointsFinal.get(j);
                        BlockVector2D v2 = pointsFinal.get(j + 1);
                        setBlocksInLine(p, actor, editSession, pattern, mask, v1.toVector(i), v2.toVector(i));
                    }
                }
                p.sendMessage(wePrefix + "Paredes de la selección creadas.");
            } else {
                p.sendMessage(wePrefix + "Introduce un patrón de bloques.");
            }
        }
    }
    return true;
}
Also used : Entity(com.sk89q.worldedit.entity.Entity) Pattern(com.sk89q.worldedit.function.pattern.Pattern) BukkitPlayer(com.sk89q.worldedit.bukkit.BukkitPlayer) Player(org.bukkit.entity.Player) Extent(com.sk89q.worldedit.extent.Extent) Mask(com.sk89q.worldedit.function.mask.Mask) ArrayList(java.util.ArrayList) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BteConoSur.mainWorld(pizzaaxx.bteconosur.BteConoSur.mainWorld) World(com.sk89q.worldedit.world.World) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) BukkitPlayer(com.sk89q.worldedit.bukkit.BukkitPlayer) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Polygonal2DRegion(com.sk89q.worldedit.regions.Polygonal2DRegion) Polygonal2DRegion(com.sk89q.worldedit.regions.Polygonal2DRegion) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Aggregations

ParserContext (com.sk89q.worldedit.extension.input.ParserContext)16 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)12 Actor (com.sk89q.worldedit.extension.platform.Actor)4 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 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