Search in sources :

Example 1 with BlockVector

use of org.bukkit.util.BlockVector in project Denizen-For-Bukkit by DenizenScript.

the class CuboidBlockSet method fromMCEditStream.

public static CuboidBlockSet fromMCEditStream(InputStream is) {
    CuboidBlockSet cbs = new CuboidBlockSet();
    try {
        NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(is));
        NamedTag rootTag = nbtStream.readNamedTag();
        nbtStream.close();
        if (!rootTag.getName().equals("Schematic")) {
            throw new Exception("Tag 'Schematic' does not exist or is not first!");
        }
        CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
        Map<String, Tag> schematic = schematicTag.getValue();
        short width = (getChildTag(schematic, "Width", ShortTag.class).getValue());
        short length = (getChildTag(schematic, "Length", ShortTag.class).getValue());
        short height = (getChildTag(schematic, "Height", ShortTag.class).getValue());
        int originX = 0;
        int originY = 0;
        int originZ = 0;
        try {
            originX = getChildTag(schematic, "DenizenOriginX", IntTag.class).getValue();
            originY = getChildTag(schematic, "DenizenOriginY", IntTag.class).getValue();
            originZ = getChildTag(schematic, "DenizenOriginZ", IntTag.class).getValue();
        } catch (Exception e) {
        // Default origin, why not
        }
        cbs.x_width = width;
        cbs.z_height = length;
        cbs.y_length = height;
        cbs.center_x = originX;
        cbs.center_y = originY;
        cbs.center_z = originZ;
        // Disregard Offset
        String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
        if (!materials.equals("Alpha")) {
            throw new Exception("Schematic file is not an Alpha schematic!");
        }
        byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
        byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
        byte[] addId = new byte[0];
        short[] blocks = new short[blockId.length];
        if (schematic.containsKey("AddBlocks")) {
            addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
        }
        for (int index = 0; index < blockId.length; index++) {
            if ((index >> 1) >= addId.length) {
                blocks[index] = (short) (blockId[index] & 0xFF);
            } else {
                if ((index & 1) == 0) {
                    blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
                } else {
                    blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
                }
            }
        }
        List<Tag> tileEntities = getChildTag(schematic, "TileEntities", ListTag.class).getValue();
        Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<BlockVector, Map<String, Tag>>();
        for (Tag tag : tileEntities) {
            if (!(tag instanceof CompoundTag)) {
                continue;
            }
            CompoundTag t = (CompoundTag) tag;
            int x = 0;
            int y = 0;
            int z = 0;
            Map<String, Tag> values = new HashMap<String, Tag>();
            for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
                if (entry.getKey().equals("x")) {
                    if (entry.getValue() instanceof IntTag) {
                        x = ((IntTag) entry.getValue()).getValue();
                    }
                } else if (entry.getKey().equals("y")) {
                    if (entry.getValue() instanceof IntTag) {
                        y = ((IntTag) entry.getValue()).getValue();
                    }
                } else if (entry.getKey().equals("z")) {
                    if (entry.getValue() instanceof IntTag) {
                        z = ((IntTag) entry.getValue()).getValue();
                    }
                }
                values.put(entry.getKey(), entry.getValue());
            }
            BlockVector vec = new BlockVector(x, y, z);
            tileEntitiesMap.put(vec, values);
        }
        org.bukkit.util.Vector vec = new Vector(width, height, length);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                for (int z = 0; z < length; z++) {
                    int index = y * width * length + z * width + x;
                    BlockVector pt = new BlockVector(x, y, z);
                    BlockData block = NMSHandler.getInstance().getBlockHelper().getBlockData(blocks[index], blockData[index]);
                    if (tileEntitiesMap.containsKey(pt)) {
                        CompoundTag otag = NMSHandler.getInstance().createCompoundTag(tileEntitiesMap.get(pt));
                        block.setCompoundTag(otag);
                    }
                    cbs.blocks.add(block);
                }
            }
        }
    } catch (Exception e) {
        dB.echoError(e);
    }
    return cbs;
}
Also used : HashMap(java.util.HashMap) GZIPInputStream(java.util.zip.GZIPInputStream) BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector) Vector(org.bukkit.util.Vector) BlockVector(org.bukkit.util.BlockVector) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BlockVector

use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.

the class GlowPlayer method processBlockChanges.

/**
     * Process and send pending BlockChangeMessages.
     */
private void processBlockChanges() {
    synchronized (blockChanges) {
        List<BlockChangeMessage> messages = new ArrayList<>(blockChanges);
        blockChanges.clear();
        // separate messages by chunk
        // inner map is used to only send one entry for same coordinates
        Map<Key, Map<BlockVector, BlockChangeMessage>> chunks = new HashMap<>();
        for (BlockChangeMessage message : messages) {
            if (message != null) {
                Key key = new Key(message.getX() >> 4, message.getZ() >> 4);
                if (canSeeChunk(key)) {
                    Map<BlockVector, BlockChangeMessage> map = chunks.computeIfAbsent(key, k -> new HashMap<>());
                    map.put(new BlockVector(message.getX(), message.getY(), message.getZ()), message);
                }
            }
        }
        // send away
        for (Map.Entry<Key, Map<BlockVector, BlockChangeMessage>> entry : chunks.entrySet()) {
            Key key = entry.getKey();
            List<BlockChangeMessage> value = new ArrayList<>(entry.getValue().values());
            if (value.size() == 1) {
                session.send(value.get(0));
            } else if (value.size() > 1) {
                session.send(new MultiBlockChangeMessage(key.getX(), key.getZ(), value));
            }
        }
        // now send post-block-change messages
        List<Message> postMessages = new ArrayList<>(afterBlockChanges);
        afterBlockChanges.clear();
        postMessages.forEach(session::send);
    }
}
Also used : UseBedMessage(net.glowstone.net.message.play.player.UseBedMessage) Message(com.flowpowered.network.Message) PlayerAbilitiesMessage(net.glowstone.net.message.play.player.PlayerAbilitiesMessage) TextMessage(net.glowstone.util.TextMessage) ResourcePackSendMessage(net.glowstone.net.message.play.player.ResourcePackSendMessage) BlockVector(org.bukkit.util.BlockVector) StatisticMap(net.glowstone.util.StatisticMap) MetadataMap(net.glowstone.entity.meta.MetadataMap) Key(net.glowstone.chunk.GlowChunk.Key)

Example 3 with BlockVector

use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.

the class Explosion method explodeWithEvent.

public boolean explodeWithEvent() {
    if (power < 0.1f)
        return true;
    Set<BlockVector> droppedBlocks = calculateBlocks();
    EntityExplodeEvent event = EventFactory.callEvent(new EntityExplodeEvent(source, location, toBlockList(droppedBlocks), yield));
    if (event.isCancelled())
        return false;
    yield = event.getYield();
    playOutSoundAndParticles();
    List<Block> blocks = toBlockList(droppedBlocks);
    for (Block block : blocks) {
        handleBlockExplosion((GlowBlock) block);
    }
    if (incendiary) {
        for (Block block : blocks) {
            setBlockOnFire((GlowBlock) block);
        }
    }
    Collection<GlowPlayer> affectedPlayers = damageEntities();
    for (GlowPlayer player : affectedPlayers) {
        playOutExplosion(player, droppedBlocks);
    }
    return true;
}
Also used : EntityExplodeEvent(org.bukkit.event.entity.EntityExplodeEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) BlockVector(org.bukkit.util.BlockVector)

Example 4 with BlockVector

use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.

the class UseBedCodec method decode.

@Override
public UseBedMessage decode(ByteBuf buf) throws IOException {
    int id = ByteBufUtils.readVarInt(buf);
    BlockVector pos = GlowBufUtils.readBlockPosition(buf);
    return new UseBedMessage(id, pos.getBlockX(), pos.getBlockY(), pos.getBlockZ());
}
Also used : BlockVector(org.bukkit.util.BlockVector) UseBedMessage(net.glowstone.net.message.play.player.UseBedMessage)

Example 5 with BlockVector

use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.

the class TabCompleteCodec method decode.

@Override
public TabCompleteMessage decode(ByteBuf buf) throws IOException {
    String text = ByteBufUtils.readUTF8(buf);
    boolean assumeCommand = buf.readBoolean();
    boolean hasLocation = buf.readBoolean();
    BlockVector location = null;
    if (hasLocation) {
        location = GlowBufUtils.readBlockPosition(buf);
    }
    return new TabCompleteMessage(text, assumeCommand, location);
}
Also used : TabCompleteMessage(net.glowstone.net.message.play.player.TabCompleteMessage) BlockVector(org.bukkit.util.BlockVector)

Aggregations

BlockVector (org.bukkit.util.BlockVector)17 Vector (org.bukkit.util.Vector)3 GlowBlock (net.glowstone.block.GlowBlock)2 UseBedMessage (net.glowstone.net.message.play.player.UseBedMessage)2 TextMessage (net.glowstone.util.TextMessage)2 Message (com.flowpowered.network.Message)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 BlockData (net.aufdemrand.denizen.nms.interfaces.BlockData)1 Key (net.glowstone.chunk.GlowChunk.Key)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 MetadataIndex (net.glowstone.entity.meta.MetadataIndex)1 MetadataMap (net.glowstone.entity.meta.MetadataMap)1 Entry (net.glowstone.entity.meta.MetadataMap.Entry)1 SpawnPaintingMessage (net.glowstone.net.message.play.entity.SpawnPaintingMessage)1 BlockActionMessage (net.glowstone.net.message.play.game.BlockActionMessage)1 BlockBreakAnimationMessage (net.glowstone.net.message.play.game.BlockBreakAnimationMessage)1 BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)1 ExplosionMessage (net.glowstone.net.message.play.game.ExplosionMessage)1