Search in sources :

Example 26 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class FastSchematicWriter method write2.

/**
 * Writes a version 2 schematic file.
 *
 * @param clipboard The clipboard
 */
private void write2(Clipboard clipboard) throws IOException {
    Region region = clipboard.getRegion();
    BlockVector3 origin = clipboard.getOrigin();
    BlockVector3 min = region.getMinimumPoint();
    BlockVector3 offset = min.subtract(origin);
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > MAX_SIZE) {
        throw new IllegalArgumentException("Width of region too large for a .schematic");
    }
    if (height > MAX_SIZE) {
        throw new IllegalArgumentException("Height of region too large for a .schematic");
    }
    if (length > MAX_SIZE) {
        throw new IllegalArgumentException("Length of region too large for a .schematic");
    }
    final DataOutput rawStream = outputStream.getOutputStream();
    outputStream.writeLazyCompoundTag("Schematic", out -> {
        out.writeNamedTag("DataVersion", WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion());
        out.writeNamedTag("Version", CURRENT_VERSION);
        out.writeNamedTag("Width", (short) width);
        out.writeNamedTag("Height", (short) height);
        out.writeNamedTag("Length", (short) length);
        // The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
        out.writeNamedTag("Offset", new int[] { min.getBlockX(), min.getBlockY(), min.getBlockZ() });
        out.writeLazyCompoundTag("Metadata", out1 -> {
            out1.writeNamedTag("WEOffsetX", offset.getBlockX());
            out1.writeNamedTag("WEOffsetY", offset.getBlockY());
            out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
            out1.writeNamedTag("FAWEVersion", Fawe.instance().getVersion().build);
        });
        ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
        FaweOutputStream blocksOut = new FaweOutputStream(new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed)));
        ByteArrayOutputStream tilesCompressed = new ByteArrayOutputStream();
        NBTOutputStream tilesOut = new NBTOutputStream(new LZ4BlockOutputStream(tilesCompressed));
        List<Integer> paletteList = new ArrayList<>();
        char[] palette = new char[BlockTypesCache.states.length];
        Arrays.fill(palette, Character.MAX_VALUE);
        int paletteMax = 0;
        int numTiles = 0;
        Clipboard finalClipboard;
        if (clipboard instanceof BlockArrayClipboard) {
            finalClipboard = ((BlockArrayClipboard) clipboard).getParent();
        } else {
            finalClipboard = clipboard;
        }
        Iterator<BlockVector3> iterator = finalClipboard.iterator(Order.YZX);
        while (iterator.hasNext()) {
            BlockVector3 pos = iterator.next();
            BaseBlock block = pos.getFullBlock(finalClipboard);
            CompoundTag nbt = block.getNbtData();
            if (nbt != null) {
                Map<String, Tag> values = new HashMap<>(nbt.getValue());
                // Positions are kept in NBT, we don't want that.
                values.remove("x");
                values.remove("y");
                values.remove("z");
                values.put("Id", new StringTag(block.getNbtId()));
                // Remove 'id' if it exists. We want 'Id'.
                // Do this after we get "getNbtId" cos otherwise "getNbtId" doesn't work.
                // Dum.
                values.remove("id");
                values.put("Pos", new IntArrayTag(new int[] { pos.getX(), pos.getY(), pos.getZ() }));
                numTiles++;
                tilesOut.writeTagPayload(new CompoundTag(values));
            }
            int ordinal = block.getOrdinal();
            if (ordinal == 0) {
                ordinal = 1;
            }
            char value = palette[ordinal];
            if (value == Character.MAX_VALUE) {
                int size = paletteMax++;
                palette[ordinal] = value = (char) size;
                paletteList.add(ordinal);
            }
            blocksOut.writeVarInt(value);
        }
        // close
        tilesOut.close();
        blocksOut.close();
        out.writeNamedTag("PaletteMax", paletteMax);
        out.writeLazyCompoundTag("Palette", out12 -> {
            for (int i = 0; i < paletteList.size(); i++) {
                int stateOrdinal = paletteList.get(i);
                BlockState state = BlockTypesCache.states[stateOrdinal];
                out12.writeNamedTag(state.getAsString(), i);
            }
        });
        out.writeNamedTagName("BlockData", NBTConstants.TYPE_BYTE_ARRAY);
        rawStream.writeInt(blocksOut.size());
        try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(blocksCompressed.toByteArray()))) {
            IOUtil.copy(in, rawStream);
        }
        if (numTiles != 0) {
            out.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST);
            rawStream.write(NBTConstants.TYPE_COMPOUND);
            rawStream.writeInt(numTiles);
            try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(tilesCompressed.toByteArray()))) {
                IOUtil.copy(in, rawStream);
            }
        } else {
            out.writeNamedEmptyList("BlockEntities");
        }
        if (finalClipboard.hasBiomes()) {
            writeBiomes(finalClipboard, out);
        }
        List<Tag> entities = new ArrayList<>();
        for (Entity entity : finalClipboard.getEntities()) {
            BaseEntity state = entity.getState();
            if (state != null) {
                Map<String, Tag> values = new HashMap<>();
                // Put NBT provided data
                CompoundTag rawTag = state.getNbtData();
                if (rawTag != null) {
                    values.putAll(rawTag.getValue());
                }
                // Store our location data, overwriting any
                values.remove("id");
                Location loc = entity.getLocation();
                if (!brokenEntities) {
                    loc = loc.setPosition(loc.add(min.toVector3()));
                }
                values.put("Id", new StringTag(state.getType().getId()));
                values.put("Pos", writeVector(loc));
                values.put("Rotation", writeRotation(entity.getLocation()));
                CompoundTag entityTag = new CompoundTag(values);
                entities.add(entityTag);
            }
        }
        if (entities.isEmpty()) {
            out.writeNamedEmptyList("Entities");
        } else {
            out.writeNamedTag("Entities", new ListTag(CompoundTag.class, entities));
        }
    });
}
Also used : StringTag(com.sk89q.jnbt.StringTag) DataOutput(java.io.DataOutput) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Entity(com.sk89q.worldedit.entity.Entity) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) ListTag(com.sk89q.jnbt.ListTag) BlockState(com.sk89q.worldedit.world.block.BlockState) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.sk89q.worldedit.regions.Region) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) StringTag(com.sk89q.jnbt.StringTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) FaweOutputStream(com.fastasyncworldedit.core.internal.io.FaweOutputStream) Location(com.sk89q.worldedit.util.Location)

Example 27 with Location

use of com.sk89q.worldedit.util.Location in project WorldGuard by EngineHub.

the class Session method initialize.

/**
 * Initialize the session.
 *
 * @param player The player
 */
public void initialize(LocalPlayer player) {
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    Location location = player.getLocation();
    ApplicableRegionSet set = query.getApplicableRegions(location);
    lastValid = location;
    lastRegionSet = set.getRegions();
    ConfigurationManager cfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager();
    disableBypass = cfg.disableDefaultBypass;
    if (cfg.announceBypassStatus && player.hasPermission("worldguard.region.toggle-bypass")) {
        player.printInfo(TextComponent.of("You are " + (disableBypass ? "not" : "") + " bypassing region protection. " + "You can toggle this with /rg bypass", TextColor.DARK_PURPLE));
    }
    for (Handler handler : handlers.values()) {
        handler.initialize(player, location, set);
    }
}
Also used : RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) Handler(com.sk89q.worldguard.session.handler.Handler) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) Location(com.sk89q.worldedit.util.Location) ApplicableRegionSet(com.sk89q.worldguard.protection.ApplicableRegionSet)

Example 28 with Location

use of com.sk89q.worldedit.util.Location in project WorldGuard by EngineHub.

the class SignChestProtection method isProtectedSignAround.

private boolean isProtectedSignAround(Location searchBlock, LocalPlayer player) {
    Location side;
    Boolean res;
    side = searchBlock;
    res = isProtectedSign(side, player);
    if (res != null && res)
        return res;
    side = searchBlock.setX(searchBlock.getX() - 1);
    res = isProtectedSignAndChest(side, player);
    if (res != null && res)
        return res;
    side = searchBlock.setX(searchBlock.getX() + 1);
    res = isProtectedSignAndChest(side, player);
    if (res != null && res)
        return res;
    side = searchBlock.setZ(searchBlock.getZ() - 1);
    res = isProtectedSignAndChest(side, player);
    if (res != null && res)
        return res;
    side = searchBlock.setZ(searchBlock.getZ() + 1);
    res = isProtectedSignAndChest(side, player);
    if (res != null && res)
        return res;
    return false;
}
Also used : Location(com.sk89q.worldedit.util.Location)

Example 29 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class WorldEditListener method onPlayerInteract.

/**
 * Called when a player interacts.
 *
 * @param event Relevant event details
 */
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!plugin.getInternalPlatform().isHookingEvents()) {
        return;
    }
    if (event.useItemInHand() == Result.DENY) {
        return;
    }
    if (event.getHand() == EquipmentSlot.OFF_HAND) {
        return;
    }
    final Player player = plugin.wrapPlayer(event.getPlayer());
    final World world = player.getWorld();
    final WorldEdit we = plugin.getWorldEdit();
    final Direction direction = BukkitAdapter.adapt(event.getBlockFace());
    Action action = event.getAction();
    if (action == Action.LEFT_CLICK_BLOCK) {
        final Block clickedBlock = event.getClickedBlock();
        final Location pos = new Location(world, clickedBlock.getX(), clickedBlock.getY(), clickedBlock.getZ());
        if (we.handleBlockLeftClick(player, pos, direction)) {
            event.setCancelled(true);
        }
        if (we.handleArmSwing(player)) {
            event.setCancelled(true);
        }
    } else if (action == Action.LEFT_CLICK_AIR) {
        if (we.handleArmSwing(player)) {
            event.setCancelled(true);
        }
    } else if (action == Action.RIGHT_CLICK_BLOCK) {
        final Block clickedBlock = event.getClickedBlock();
        final Location pos = new Location(world, clickedBlock.getX(), clickedBlock.getY(), clickedBlock.getZ());
        if (we.handleBlockRightClick(player, pos, direction)) {
            event.setCancelled(true);
        }
        if (we.handleRightClick(player)) {
            event.setCancelled(true);
        }
    } else if (action == Action.RIGHT_CLICK_AIR) {
        if (we.handleRightClick(player)) {
            event.setCancelled(true);
        }
    }
}
Also used : Player(com.sk89q.worldedit.entity.Player) Action(org.bukkit.event.block.Action) WorldEdit(com.sk89q.worldedit.WorldEdit) Block(org.bukkit.block.Block) World(com.sk89q.worldedit.world.World) Direction(com.sk89q.worldedit.util.Direction) Location(com.sk89q.worldedit.util.Location) EventHandler(org.bukkit.event.EventHandler)

Example 30 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class BiomeCommands method biomeInfo.

@Command(name = "biomeinfo", desc = "Get the biome of the targeted block.", descFooter = "By default, uses all blocks in your selection.")
@CommandPermissions("worldedit.biome.info")
public void biomeInfo(Actor actor, World world, LocalSession session, @Switch(name = 't', desc = "Use the block you are looking at.") boolean useLineOfSight, @Switch(name = 'p', desc = "Use the block you are currently in.") boolean usePosition) throws WorldEditException {
    BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
    Set<BiomeType> biomes = new HashSet<>();
    String messageKey;
    if (useLineOfSight) {
        if (actor instanceof Player) {
            Location blockPosition = ((Player) actor).getBlockTrace(300);
            if (blockPosition == null) {
                actor.print(Caption.of("worldedit.raytrace.noblock"));
                return;
            }
            BiomeType biome = world.getBiome(blockPosition.toVector().toBlockPoint());
            biomes.add(biome);
            messageKey = "worldedit.biomeinfo.lineofsight";
        } else {
            actor.print(Caption.of("worldedit.raytrace.require-player"));
            return;
        }
    } else if (usePosition) {
        if (actor instanceof Locatable) {
            BiomeType biome = world.getBiome(((Locatable) actor).getLocation().toVector().toBlockPoint());
            biomes.add(biome);
            messageKey = "worldedit.biomeinfo.position";
        } else {
            actor.print(Caption.of("worldedit.biomeinfo.not-locatable"));
            return;
        }
    } else {
        Region region = session.getSelection(world);
        for (BlockVector3 pt : region) {
            biomes.add(world.getBiome(pt));
        }
        messageKey = "worldedit.biomeinfo.selection";
    }
    List<Component> components = biomes.stream().map(biome -> biomeRegistry.getRichName(biome).hoverEvent(HoverEvent.showText(TextComponent.of(biome.getId())))).collect(Collectors.toList());
    actor.print(Caption.of(messageKey, TextUtils.join(components, TextComponent.of(", "))));
}
Also used : EditSession(com.sk89q.worldedit.EditSession) WorldEditAsyncCommandBuilder(com.sk89q.worldedit.command.util.WorldEditAsyncCommandBuilder) RegionFunction(com.sk89q.worldedit.function.RegionFunction) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Player(com.sk89q.worldedit.entity.Player) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) World(com.sk89q.worldedit.world.World) Switch(org.enginehub.piston.annotation.param.Switch) Caption(com.fastasyncworldedit.core.configuration.Caption) Logging(com.sk89q.worldedit.command.util.Logging) RegionMaskingFilter(com.sk89q.worldedit.function.RegionMaskingFilter) Arg(org.enginehub.piston.annotation.param.Arg) HoverEvent(com.sk89q.worldedit.util.formatting.text.event.HoverEvent) Component(com.sk89q.worldedit.util.formatting.text.Component) HashSet(java.util.HashSet) Preload(com.sk89q.worldedit.command.util.annotation.Preload) WorldEditException(com.sk89q.worldedit.WorldEditException) CommandContainer(org.enginehub.piston.annotation.CommandContainer) TextUtils(com.sk89q.worldedit.util.formatting.component.TextUtils) Operations(com.sk89q.worldedit.function.operation.Operations) Location(com.sk89q.worldedit.util.Location) WorldEdit(com.sk89q.worldedit.WorldEdit) Region(com.sk89q.worldedit.regions.Region) BiomeRegistry(com.sk89q.worldedit.world.registry.BiomeRegistry) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions) Locatable(com.sk89q.worldedit.extension.platform.Locatable) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Set(java.util.Set) REGION(com.sk89q.worldedit.command.util.Logging.LogMode.REGION) RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) Confirm(com.sk89q.worldedit.command.util.annotation.Confirm) Collectors(java.util.stream.Collectors) ArgFlag(org.enginehub.piston.annotation.param.ArgFlag) Actor(com.sk89q.worldedit.extension.platform.Actor) List(java.util.List) Command(org.enginehub.piston.annotation.Command) CommandPermissionsConditionGenerator(com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator) Capability(com.sk89q.worldedit.extension.platform.Capability) PaginationBox(com.sk89q.worldedit.util.formatting.component.PaginationBox) LocalSession(com.sk89q.worldedit.LocalSession) Mask(com.sk89q.worldedit.function.mask.Mask) BiomeReplace(com.sk89q.worldedit.function.biome.BiomeReplace) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) Player(com.sk89q.worldedit.entity.Player) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) BiomeRegistry(com.sk89q.worldedit.world.registry.BiomeRegistry) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) Component(com.sk89q.worldedit.util.formatting.text.Component) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) HashSet(java.util.HashSet) Location(com.sk89q.worldedit.util.Location) Locatable(com.sk89q.worldedit.extension.platform.Locatable) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

Location (com.sk89q.worldedit.util.Location)65 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)26 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)12 CompoundTag (com.sk89q.jnbt.CompoundTag)11 World (com.sk89q.worldedit.world.World)11 Region (com.sk89q.worldedit.regions.Region)10 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)9 Command (org.enginehub.piston.annotation.Command)9 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)8 Tag (com.sk89q.jnbt.Tag)8 List (java.util.List)8 ListTag (com.sk89q.jnbt.ListTag)7 Vector3 (com.sk89q.worldedit.math.Vector3)7 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)7 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)7 StringTag (com.sk89q.jnbt.StringTag)6 Player (com.sk89q.worldedit.entity.Player)6 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)6 BlockState (com.sk89q.worldedit.world.block.BlockState)6 IntTag (com.sk89q.jnbt.IntTag)5