use of com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage in project DirtMultiversion by DirtPowered.
the class WorldPackets method registerTranslators.
@Override
public void registerTranslators() {
// block change
addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int x = data.read(Type.INT, 0);
byte y = data.read(Type.BYTE, 1);
int z = data.read(Type.INT, 2);
int chunkX = x >> 4;
int chunkZ = z >> 4;
byte blockId = data.read(Type.BYTE, 3);
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
}
}
return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.SHORT, data.read(Type.BYTE, 3).shortValue()), data.read(4) });
}
});
// multi block change
addTranslator(0x34, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
V1_2MultiBlockArray blocks = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
DataInput dis = new DataInputStream(new ByteArrayInputStream(blocks.getData()));
for (int i = 0; i < blocks.getRecordCount(); i++) {
try {
short pos = dis.readShort();
int x = pos >> 12 & 15;
int y = pos & 255;
int z = pos >> 8 & 15;
int xPos = x + (chunkX << 4);
int zPos = z + (chunkZ << 4);
int blockId = dis.readShort() >> 4 & 4095;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
});
// chunk data
addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
V1_2Chunk chunk = data.read(Type.V1_2_CHUNK, 0);
if (blockStorage.getVersion() == getTo()) {
List<ExtendedBlockStorage> parts = new ArrayList<>();
V1_2RChunkStorage storage;
if (chunk.getStorage() != null) {
storage = chunk.getStorage();
} else {
// create chunk storage
storage = new V1_2RChunkStorage(true, true, chunk.getChunkX(), chunk.getChunkZ());
storage.readChunk(chunk.isGroundUp(), chunk.getPrimaryBitmap(), chunk.getUncompressedData());
// cache for later use (r1.8 -> r1.7)
chunk.setStorage(storage);
}
ExtendedBlockStorage[] columns = storage.getColumnStorage();
for (int i = 0; i < columns.length; ++i) {
ExtendedBlockStorage e = columns[i];
boolean f = e != null && !columns[i].isEmpty();
if (e != null && (chunk.getPrimaryBitmap() & 1 << i) != 0 && (!chunk.isGroundUp() || f)) {
parts.add(e);
}
}
for (int i = 0; i < parts.size(); ++i) {
byte[] blockArray = parts.get(i).getBlockLSBArray();
for (int j = 0; j < blockArray.length; ++j) {
int x = j & 15;
int y = (j >> 8) + i * 16 & 255;
int z = j >> 4 & 15;
int blockId = blockArray[j] & 255;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
}
}
}
}
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
}
});
// pre chunk
addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
byte mode = data.read(Type.BYTE, 2);
if (mode == 1) {
return cancel();
}
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
for (BlockLocation entry : chestTracker.getChestStates().keySet()) {
int x = entry.getX() >> 4;
int z = entry.getZ() >> 4;
if (chunkX == x && chunkZ == z) {
chestTracker.remove(entry);
}
}
BlockStorage blockStorage = storage.get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
blockStorage.removeChunk(chunkX, chunkZ);
}
V1_2Chunk chunk = new V1_2Chunk(chunkX, chunkZ, true, (short) 0, (short) 0, 0, new byte[0], new byte[0], null);
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
}
});
// play noteblock
addTranslator(0x36, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
int y = data.read(Type.SHORT, 1);
int z = data.read(Type.INT, 2);
byte type = data.read(Type.BYTE, 3);
byte pitch = data.read(Type.BYTE, 4);
ProtocolStorage storage = session.getStorage();
short blockId = (short) storage.get(BlockStorage.class).getBlockAt(x, y, z);
WorldSound worldSound;
OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
switch(type) {
case 0:
if (blockId == 33 || blockId == 29) {
worldSound = WorldSound.PISTON_OUT;
} else {
worldSound = WorldSound.NOTE_HARP;
}
break;
case 1:
if (blockId == 54) {
BlockLocation loc = new BlockLocation(x, y, z);
if (pitch == 1) {
if (!chestTracker.getState(loc)) {
chestTracker.setOpen(loc, true);
worldSound = WorldSound.CHEST_OPEN;
pitch = 12;
} else {
worldSound = WorldSound.NO_SOUND;
}
} else {
if (chestTracker.getState(loc)) {
worldSound = WorldSound.CHEST_CLOSE;
chestTracker.setOpen(loc, false);
} else {
worldSound = WorldSound.NO_SOUND;
}
}
} else if (blockId == 33 || blockId == 29) {
worldSound = WorldSound.PISTON_IN;
} else {
worldSound = WorldSound.NOTE_CLICK;
}
break;
case 2:
worldSound = WorldSound.NOTE_SNARE;
break;
case 3:
worldSound = WorldSound.NOTE_HAT;
break;
case 4:
worldSound = WorldSound.NOTE_BASS_ATTACK;
break;
default:
worldSound = WorldSound.NOTE_HARP;
break;
}
float correctedPitch = (float) (0.5f * (Math.pow(2, pitch / 12.0f)));
WorldEntityEvent.playSoundAt(session, new Location(x, y, z), worldSound, 3.0f, correctedPitch);
return PacketUtil.createPacket(0x36, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.SHORT, blockId) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage in project DirtMultiversion by DirtPowered.
the class BetaToV1_2ChunkTranslator method translate.
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
LoadedChunkTracker chunkTracker = storage.get(LoadedChunkTracker.class);
DimensionTracker dimensionTracker = storage.get(DimensionTracker.class);
boolean hasSkylight = dimensionTracker.getDimension() == 0;
V1_3BChunk oldChunk = (V1_3BChunk) data.read(0).getObject();
boolean groundUp = true;
if (oldChunk.getXSize() * oldChunk.getYSize() * oldChunk.getZSize() != 32768) {
groundUp = false;
}
int chunkX = oldChunk.getX() >> 4;
int chunkZ = oldChunk.getZ() >> 4;
if (groundUp) {
V1_3BChunkStorage oldChunkStorage = new V1_3BChunkStorage(chunkX, chunkZ);
V1_2RChunkStorage newChunkStorage = new V1_2RChunkStorage(hasSkylight, true, chunkX, chunkZ);
oldChunkStorage.setChunkData(oldChunk.getChunk(), oldChunk.getX(), oldChunk.getY(), oldChunk.getZ(), oldChunk.getXSize(), oldChunk.getYSize(), oldChunk.getZSize(), 0, true);
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
int oldBlockId = oldChunkStorage.getBlockId(x, y, z);
int oldBlockData = oldChunkStorage.getBlockData(x, y, z);
Block replacement = blockDataTransformer.replaceBlock(oldBlockId, oldBlockData);
newChunkStorage.setBlockId(x, y, z, replacement.getBlockId());
newChunkStorage.setBlockMetadata(x, y, z, replacement.getBlockData());
newChunkStorage.setBlockLight(x, y, z, oldChunkStorage.getBlockLight(x, y, z));
if (hasSkylight) {
newChunkStorage.setSkyLight(x, y, z, oldChunkStorage.getSkyLight(x, y, z));
}
}
}
}
byte[] biomes = new byte[256];
if (storage.hasObject(OldChunkData.class)) {
OldChunkData biomeData = storage.get(OldChunkData.class);
biomes = biomeData.getBiomeDataAt(chunkX, chunkZ, !hasSkylight);
} else {
// forest
Arrays.fill(biomes, (byte) 0x04);
}
newChunkStorage.setBiomeData(biomes);
byte[] compressedData = newChunkStorage.getCompressedData(true, 0xff);
session.sendPacket(PacketUtil.createPacket(0x33, new TypeHolder[] { new TypeHolder<>(Type.V1_2_CHUNK, new V1_2Chunk(chunkX, chunkZ, true, (short) newChunkStorage.getPrimaryBitmap(), (short) 0, newChunkStorage.getCompressedSize(), compressedData, new byte[0], newChunkStorage)) }), PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
chunkTracker.setChunkLoaded(chunkX, chunkZ);
return ServerProtocol.cancel();
} else {
if (!chunkTracker.isChunkLoaded(chunkX, chunkZ)) {
return ServerProtocol.cancel();
}
Configuration c = session.getMain().getConfiguration();
boolean replaceChests = session.getUserData().getClientVersion().getRegistryId() >= 39 && c.replaceChests();
List<WorldBlock> worldBlocks = getUpdatedBlockList(oldChunk.getX(), oldChunk.getY(), oldChunk.getZ(), oldChunk.getXSize(), oldChunk.getYSize(), oldChunk.getZSize(), oldChunk.getChunk(), replaceChests);
int records = worldBlocks.size();
if (worldBlocks.isEmpty()) {
return ServerProtocol.cancel();
}
List<List<WorldBlock>> slicedList = getSlicedData(worldBlocks);
if (records > MAX_SINGLE_BLOCK_UPDATE_PACKETS) {
for (List<WorldBlock> slicedData : slicedList) {
chunkX = slicedData.get(0).getX() >> 4;
chunkZ = slicedData.get(0).getZ() >> 4;
int totalDataSize = 4 * slicedData.size();
ByteArrayOutputStream baos = new ByteArrayOutputStream(totalDataSize);
DataOutputStream dos = new DataOutputStream(baos);
V1_2MultiBlockArray blockArray = null;
try {
for (WorldBlock record : slicedData) {
dos.writeShort((record.getX() - (chunkX << 4)) << 12 | (record.getZ() - (chunkZ << 4)) << 8 | record.getY());
dos.writeShort((record.getBlockId() & 4095) << 4 | record.getBlockData() & 15);
}
byte[] bytes = baos.toByteArray();
blockArray = new V1_2MultiBlockArray(slicedData.size(), bytes.length, bytes);
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
PacketData multiBlockChange = PacketUtil.createPacket(0x34, new TypeHolder[] { new TypeHolder<>(Type.INT, chunkX), new TypeHolder<>(Type.INT, chunkZ), new TypeHolder<>(Type.V1_2MULTIBLOCK_ARRAY, blockArray) });
session.sendPacket(multiBlockChange, PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
}
} else {
for (WorldBlock block : worldBlocks) {
PacketData blockUpdate = PacketUtil.createPacket(0x35, new TypeHolder[] { new TypeHolder<>(Type.INT, block.getX()), new TypeHolder<>(Type.BYTE, (byte) block.getY()), new TypeHolder<>(Type.INT, block.getZ()), new TypeHolder<>(Type.BYTE, (byte) block.getBlockId()), new TypeHolder<>(Type.BYTE, (byte) block.getBlockData()) });
session.sendPacket(blockUpdate, PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
}
}
}
return ServerProtocol.cancel();
}
use of com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage in project DirtMultiversion by DirtPowered.
the class WorldPackets method registerTranslators.
@Override
public void registerTranslators() {
// block change
addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int x = data.read(Type.INT, 0);
byte y = data.read(Type.BYTE, 1);
int z = data.read(Type.INT, 2);
int chunkX = x >> 4;
int chunkZ = z >> 4;
short blockId = data.read(Type.SHORT, 3);
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
}
}
return data;
}
});
addTranslator(0x34, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
V1_2MultiBlockArray blocks = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
DataInput dis = new DataInputStream(new ByteArrayInputStream(blocks.getData()));
for (int i = 0; i < blocks.getRecordCount(); i++) {
try {
short pos = dis.readShort();
int x = pos >> 12 & 15;
int y = pos & 255;
int z = pos >> 8 & 15;
int xPos = x + (chunkX << 4);
int zPos = z + (chunkZ << 4);
int blockId = dis.readShort() >> 4 & 4095;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
});
// chunk data
addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
V1_2Chunk chunk = data.read(Type.V1_2_CHUNK, 0);
if (blockStorage.getVersion() == getTo()) {
List<ExtendedBlockStorage> parts = new ArrayList<>();
boolean skyLight = true;
if (session.getStorage().hasObject(DimensionTracker.class)) {
DimensionTracker tracker = session.getStorage().get(DimensionTracker.class);
skyLight = tracker.getDimension() == 0;
}
// create chunk storage
V1_2RChunkStorage storage = new V1_2RChunkStorage(skyLight, true, chunk.getChunkX(), chunk.getChunkZ());
storage.readChunk(chunk.isGroundUp(), chunk.getPrimaryBitmap(), chunk.getUncompressedData());
// cache for later use (r1.8 -> r1.7)
chunk.setStorage(storage);
ExtendedBlockStorage[] columns = storage.getColumnStorage();
for (int i = 0; i < columns.length; ++i) {
ExtendedBlockStorage e = columns[i];
boolean f = e != null && !columns[i].isEmpty();
if (e != null && (chunk.getPrimaryBitmap() & 1 << i) != 0 && (!chunk.isGroundUp() || f)) {
parts.add(e);
}
}
for (int i = 0; i < parts.size(); ++i) {
byte[] blockArray = parts.get(i).getBlockLSBArray();
for (int j = 0; j < blockArray.length; ++j) {
int x = j & 15;
int y = (j >> 8) + i * 16 & 127;
int z = j >> 4 & 15;
int blockId = blockArray[j] & 255;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
}
}
}
}
return data;
}
});
}
Aggregations