Search in sources :

Example 6 with ChunkPos

use of org.dragonet.common.maths.ChunkPos in project DragonProxy by DragonetMC.

the class ChunkCache method getBlock.

public final ItemStack getBlock(Position position) {
    ChunkPos columnPos = new ChunkPos(position.getX() >> 4, position.getZ() >> 4);
    if (chunkCache.containsKey(columnPos))
        try {
            Column column = chunkCache.get(columnPos);
            BlockPosition blockPos = columnPos.getBlockInChunk(position.getX(), position.getY(), position.getZ());
            Chunk chunk = column.getChunks()[position.getY() >> 4];
            if (chunk != null) {
                BlockState block = chunk.getBlocks().get(blockPos.x, blockPos.y, blockPos.z);
                return new ItemStack(block.getId(), 1, block.getData());
            }
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
            this.session.getProxy().getLogger().severe("(getBlock(" + position.toString() + ")) fail to get chunk " + (position.getX() >> 4) + "/" + (position.getY() >> 4) + "/" + (position.getZ() >> 4));
            ex.printStackTrace();
        }
    return null;
}
Also used : BlockState(com.github.steveice10.mc.protocol.data.game.world.block.BlockState) Column(com.github.steveice10.mc.protocol.data.game.chunk.Column) BlockPosition(org.dragonet.common.maths.BlockPosition) ChunkPos(org.dragonet.common.maths.ChunkPos) Chunk(com.github.steveice10.mc.protocol.data.game.chunk.Chunk) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)

Example 7 with ChunkPos

use of org.dragonet.common.maths.ChunkPos in project DragonProxy by DragonetMC.

the class ChunkCache method getDebugGrid.

// debug
public void getDebugGrid() {
    CachedEntity player = session.getEntityCache().getClientEntity();
    int viewDistance = (int) session.getDataCache().getOrDefault(CacheKey.PLAYER_REQUESTED_CHUNK_RADIUS, 5);
    // center
    int centerX = (int) player.x >> 4;
    int centerZ = (int) player.z >> 4;
    ChunkPos playerChunkPos = new ChunkPos(centerX, centerZ);
    // find corners
    int minX = centerX;
    int maxX = centerX;
    int minZ = centerZ;
    int maxZ = centerZ;
    for (ChunkPos pos : chunkCache.keySet()) {
        if (pos.chunkXPos < minX)
            minX = pos.chunkXPos;
        if (pos.chunkXPos > maxX)
            maxX = pos.chunkXPos;
        if (pos.chunkZPos < minZ)
            minZ = pos.chunkZPos;
        if (pos.chunkZPos > maxZ)
            maxZ = pos.chunkZPos;
    }
    int serverViewDistance = maxX - centerX;
    int width = maxX - minX;
    int height = maxZ - minZ;
    String playerChunk = "x";
    String emptyChunk = "⬚";
    String cachedChunk = "⬜";
    String loadedChunk = "⬛";
    System.out.println("Player chunk : " + playerChunk + " (The chunk the player stands in)");
    System.out.println("Empty chunk  : " + emptyChunk + " (Chunk not in cache)");
    System.out.println("Cached chunk : " + cachedChunk + " (Chunk in cache not send to player)");
    System.out.println("Loaded chunk : " + loadedChunk + " (Chunk in cache and sent to player)");
    System.out.println("Player View Distance : " + viewDistance);
    System.out.println("Server View Distance : " + serverViewDistance);
    System.out.println("Player Chunk : " + playerChunkPos.toString());
    System.out.println("Chunk in cache : " + chunkCache.size());
    System.out.println("Chunk loaded : " + loadedChunks.size());
    System.out.println("Chunk grid : " + width + " * " + height + " = " + (width * height));
    System.out.println("Chunk X : " + minX + " to " + maxX);
    System.out.println("Chunk Z : " + minZ + " to " + maxZ);
    StringBuilder frame = new StringBuilder();
    for (int z = minZ; z <= maxZ; z++) {
        // lines
        StringBuilder line = new StringBuilder();
        if (z == minZ)
            line.append(pad("" + minX, 5)).append(pad("", width * 2 - 3)).append(pad("" + maxX, 3)).append("\n");
        line.append(pad("" + z, 3) + " ");
        for (int x = minX; x <= maxX; x++) {
            // columns
            ChunkPos chunkPos = new ChunkPos(x, z);
            if (chunkCache.keySet().contains(chunkPos))
                if (chunkPos.equals(playerChunkPos))
                    line.append(playerChunk);
                else if (loadedChunks.contains(chunkPos))
                    line.append(loadedChunk);
                else
                    line.append(cachedChunk);
            else
                line.append(emptyChunk);
            line.append(" ");
        }
        frame.append(line).append("\n");
    }
    System.out.println(frame.toString());
}
Also used : ChunkPos(org.dragonet.common.maths.ChunkPos)

Example 8 with ChunkPos

use of org.dragonet.common.maths.ChunkPos in project DragonProxy by DragonetMC.

the class ChunkCache method purge.

public void purge() {
    chunkCache.clear();
    for (ChunkPos chunk : loadedChunks) sendEmptyChunk(chunk.chunkXPos, chunk.chunkZPos);
    loadedChunks.clear();
}
Also used : ChunkPos(org.dragonet.common.maths.ChunkPos)

Example 9 with ChunkPos

use of org.dragonet.common.maths.ChunkPos in project DragonProxy by DragonetMC.

the class ChunkCache method translateChunk.

public final ChunkData translateChunk(int columnX, int columnZ) {
    ChunkPos columnPos = new ChunkPos(columnX, columnZ);
    if (chunkCache.containsKey(columnPos)) {
        Column column = chunkCache.get(columnPos);
        ChunkData chunk = new ChunkData();
        chunk.sections = new Section[16];
        for (int i = 0; i < 16; i++) chunk.sections[i] = new Section();
        // Blocks
        for (int y = 0; y < 256; y++) {
            int cy = y >> 4;
            Chunk c = null;
            try {
                c = column.getChunks()[cy];
            } catch (Exception ex) {
                DragonProxy.getInstance().getLogger().info("Chunk " + columnX + ", " + cy + ", " + columnZ + " not exist !");
            }
            if (c == null || c.isEmpty())
                continue;
            BlockStorage blocks = c.getBlocks();
            for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) {
                BlockState block = blocks.get(x, y & 0xF, z);
                ItemEntry entry = ItemBlockTranslator.translateToPE(block.getId(), block.getData());
                Section section = chunk.sections[cy];
                // Block id
                section.blockIds[index(x, y, z)] = (byte) (entry.getId() & 0xFF);
                // Data value
                int i = dataIndex(x, y, z);
                byte data = section.blockMetas[i];
                int newValue = entry.getPEDamage().byteValue();
                if ((y & 1) == 0)
                    data = (byte) ((data & 0xf0) | (newValue & 0x0f));
                else
                    data = (byte) (((newValue & 0x0f) << 4) | (data & 0x0f));
                section.blockMetas[i] = data;
            }
        }
        // Blocks entities
        try {
            List<CompoundTag> blockEntities = new ArrayList<>();
            for (int i = 0; i < column.getTileEntities().length; i++) {
                CompoundTag peTag = ItemBlockTranslator.translateBlockEntityToPE(column.getTileEntities()[i]);
                if (// filter non handled blocks entities
                peTag != null)
                    blockEntities.add(peTag);
            // else // debug
            // DragonProxy.getInstance().getLogger().debug("NBT null for " + pc.getTileEntities()[i].toString());
            }
            chunk.blockEntities = NBTIO.write(blockEntities, ByteOrder.LITTLE_ENDIAN, true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        chunk.encode();
        return chunk;
    }
    // System.out.println("Chunk " + columnX + ", " + columnZ + " not in cache !!!!!!!!!!!!!");
    return null;
}
Also used : ChunkData(org.dragonet.protocol.type.chunk.ChunkData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Chunk(com.github.steveice10.mc.protocol.data.game.chunk.Chunk) Section(org.dragonet.protocol.type.chunk.Section) IOException(java.io.IOException) BlockStorage(com.github.steveice10.mc.protocol.data.game.chunk.BlockStorage) BlockState(com.github.steveice10.mc.protocol.data.game.world.block.BlockState) Column(com.github.steveice10.mc.protocol.data.game.chunk.Column) ChunkPos(org.dragonet.common.maths.ChunkPos) ItemEntry(org.dragonet.common.data.itemsblocks.ItemEntry) CompoundTag(org.dragonet.common.data.nbt.tag.CompoundTag)

Example 10 with ChunkPos

use of org.dragonet.common.maths.ChunkPos in project DragonProxy by DragonetMC.

the class ChunkCache method translateBlock.

public final ItemEntry translateBlock(Position position) {
    ChunkPos columnPos = new ChunkPos(position.getX() >> 4, position.getZ() >> 4);
    if (chunkCache.containsKey(columnPos))
        try {
            Column column = chunkCache.get(columnPos);
            BlockPosition blockPos = columnPos.getBlockInChunk(position.getX(), position.getY(), position.getZ());
            Chunk chunk = column.getChunks()[position.getY() >> 4];
            if (chunk != null) {
                BlockState block = chunk.getBlocks().get(blockPos.x, blockPos.y, blockPos.z);
                return ItemBlockTranslator.translateToPE(block.getId(), block.getData());
            }
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
            this.session.getProxy().getLogger().info("getBlock(" + position.toString() + ")) fail to get chunk " + (position.getX() >> 4) + "/" + (position.getY() >> 4) + "/" + (position.getZ() >> 4));
            ex.printStackTrace();
        }
    return null;
}
Also used : BlockState(com.github.steveice10.mc.protocol.data.game.world.block.BlockState) Column(com.github.steveice10.mc.protocol.data.game.chunk.Column) BlockPosition(org.dragonet.common.maths.BlockPosition) ChunkPos(org.dragonet.common.maths.ChunkPos) Chunk(com.github.steveice10.mc.protocol.data.game.chunk.Chunk)

Aggregations

ChunkPos (org.dragonet.common.maths.ChunkPos)10 Chunk (com.github.steveice10.mc.protocol.data.game.chunk.Chunk)4 Column (com.github.steveice10.mc.protocol.data.game.chunk.Column)4 BlockPosition (org.dragonet.common.maths.BlockPosition)4 BlockState (com.github.steveice10.mc.protocol.data.game.world.block.BlockState)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 PlayerListEntry (com.github.steveice10.mc.protocol.data.game.PlayerListEntry)1 BlockStorage (com.github.steveice10.mc.protocol.data.game.chunk.BlockStorage)1 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)1 ClientPluginMessagePacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientPluginMessagePacket)1 ClientSettingsPacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientSettingsPacket)1 ClientTeleportConfirmPacket (com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientTeleportConfirmPacket)1 ServerJoinGamePacket (com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket)1 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 IOException (java.io.IOException)1 ItemEntry (org.dragonet.common.data.itemsblocks.ItemEntry)1 CompoundTag (org.dragonet.common.data.nbt.tag.CompoundTag)1 Vector3F (org.dragonet.common.maths.Vector3F)1 BinaryStream (org.dragonet.common.utilities.BinaryStream)1