use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3BChunk in project DirtMultiversion by DirtPowered.
the class V1_3BChunkDataType method write.
@Override
public void write(TypeHolder typeHolder, PacketOutput packetOutput) {
V1_3BChunk data1 = (V1_3BChunk) typeHolder.getObject();
packetOutput.writeInt(data1.getX());
packetOutput.writeShort(data1.getY());
packetOutput.writeInt(data1.getZ());
packetOutput.writeByte(data1.getXSize() - 1);
packetOutput.writeByte(data1.getYSize() - 1);
packetOutput.writeByte(data1.getZSize() - 1);
byte[] data = data1.getChunk();
Deflater deflater = new Deflater(Deflater.BEST_SPEED);
try {
deflater.setInput(data);
deflater.finish();
byte[] chunk = new byte[(data1.getXSize() + 1) * (data1.getZSize() + 1) * (data1.getYSize() + 1) * 5 / 2];
int chunkSize = deflater.deflate(chunk);
packetOutput.writeInt(chunkSize);
packetOutput.writeBytes(chunk, chunkSize);
} finally {
deflater.end();
}
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3BChunk in project DirtMultiversion by DirtPowered.
the class ProtocolBeta17to14 method registerTranslators.
@Override
public void registerTranslators() {
// keep-alive
addTranslator(0x00, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x00, new TypeHolder[0]);
}
});
// ping request
addTranslator(0xFE, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String message = session.getMain().getConfiguration().preReleaseMOTD();
message = ChatUtils.stripColor(message);
int max = session.getMain().getConfiguration().getMaxOnline();
int online = session.getConnectionCount();
PacketData packetData = PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, message + "§" + online + "§" + max) });
// I wanna know who broke server latency calculation in release 1.8, really
if (session.getUserData().getClientVersion() == MinecraftVersion.R1_8) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
session.sendPacket(packetData, PacketDirection.TO_CLIENT, getFrom());
}
}, session.getMain().getSharedRandom().nextInt(70));
} else {
session.sendPacket(packetData, PacketDirection.TO_CLIENT, getFrom());
}
// cancel sending
return cancel();
}
});
// login
addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { // INT
set(Type.INT, 14), // STRING
data.read(1), // LONG
data.read(2), // BYTE
data.read(4) });
}
});
// login
addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
session.getMain().getSessionRegistry().getSessions().forEach((uuid, multiSession) -> {
String s = multiSession.getServerSession().getUserData().getUsername();
session.queuePacket(createTabEntryPacket(s, true), PacketDirection.TO_CLIENT, getFrom());
});
int max = session.getMain().getConfiguration().getMaxOnline();
// b1.8 client is rendering tablist grid wrong when above 100
if (max > 100)
max = 100;
return PacketUtil.createPacket(0x01, new TypeHolder[] { // INT - entityId
data.read(0), // STRING - empty
data.read(1), // LONG - world seed
data.read(2), // INT - gameMode
set(Type.INT, 0), // BYTE - dimension
data.read(3), // BYTE - difficulty
set(Type.BYTE, (byte) 1), // BYTE - world height
set(Type.BYTE, (byte) -128), // BYTE - maxPlayers
set(Type.BYTE, (byte) max) });
}
});
// update health
addTranslator(0x08, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(PlayerHealthTracker.class)) {
storage.get(PlayerHealthTracker.class).setHealth(data.read(Type.SHORT, 0));
}
return PacketUtil.createPacket(0x08, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 6), set(Type.FLOAT, 0.0F) });
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0) });
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 1), set(Type.BYTE, (byte) 0), set(Type.SHORT, (short) 128), set(Type.LONG, 0L) });
}
});
// open window
addTranslator(0x64, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x64, new TypeHolder[] { data.read(0), data.read(1), set(Type.STRING, data.read(Type.STRING, 2)), data.read(3) });
}
});
// game state
addTranslator(0x46, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x46, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 0) });
}
});
// block place
addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(PlayerHealthTracker.class)) {
PacketData updateHealth = PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.SHORT, storage.get(PlayerHealthTracker.class).getHealth()), set(Type.SHORT, (short) 6), set(Type.FLOAT, 0.0F) });
session.sendPacket(updateHealth, PacketDirection.TO_CLIENT, getFrom());
}
return data;
}
});
// entity action
addTranslator(0x13, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
byte state = data.read(Type.BYTE, 1);
if (state == 5 || state == 4) {
// cancel sending
return cancel();
}
return data;
}
});
// named entity spawn
addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
String username = data.read(Type.STRING, 1);
if (!isConnectedThroughProxy(session.getMain(), username)) {
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
if (cache != null) {
session.sendPacket(createTabEntryPacket(username, true), PacketDirection.TO_CLIENT, getFrom());
cache.getTabPlayers().put(entityId, username);
}
}
return data;
}
});
// entity destroy
addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
if (cache != null && cache.getTabPlayers().containsKey(entityId)) {
String username = cache.getTabPlayers().get(entityId);
session.sendPacket(createTabEntryPacket(username, false), PacketDirection.TO_CLIENT, getFrom());
cache.getTabPlayers().remove(entityId);
}
return data;
}
});
// mob spawn
addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
byte entityType = data.read(Type.BYTE, 1);
if (entityType == EntityType.HUMAN_MOB.getEntityTypeId()) {
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
// cache empty name, so the tab entry will be removed after killing human mob
cache.getTabPlayers().put(data.read(Type.INT, 0), StringUtil.EMPTY_STRING);
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), set(Type.STRING, StringUtil.EMPTY_STRING), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), set(Type.SHORT, (short) 0) });
}
return data;
}
});
// block change
addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
byte y = data.read(Type.BYTE, 1);
int z = data.read(Type.INT, 2);
byte blockId = data.read(Type.BYTE, 3);
byte blockData = data.read(Type.BYTE, 4);
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage != null) {
blockStorage.setBlockAt(x >> 4, z >> 4, x, y, z, blockId);
if (blockId == 54) {
blockData = RotationUtil.fixBlockRotation(session, x, y, z);
}
}
return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.BYTE, blockData) });
}
});
// unload chunk
addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage != null) {
byte mode = data.read(Type.BYTE, 2);
if (mode == 0) {
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
blockStorage.removeChunk(chunkX, chunkZ);
}
}
return data;
}
});
// chunk data
addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_3BChunk chunk = data.read(Type.V1_3B_CHUNK, 0);
int chunkX = chunk.getX() >> 4;
int chunkZ = chunk.getZ() >> 4;
// skip non-full chunk updates
if (chunk.getXSize() * chunk.getYSize() * chunk.getZSize() != 32768) {
return data;
}
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
boolean reduceBlockStorageMemory = session.getMain().getConfiguration().reduceBlockStorageMemory();
if (blockStorage != null) {
List<BlockLocation> locationList = new ArrayList<>();
try {
byte[] chunkData = chunk.getChunk();
for (int x = 0; x < 16; x++) {
for (int y = reduceBlockStorageMemory ? 20 : 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
int blockId = chunkData[getBlockIndexAt(x, y, z)];
if (SolidBlockList.isSolid(blockId) || HardnessTable.needsToBeCached(session, blockId) || blockId == 85 || blockId == 29 || blockId == 33) {
if (blockId == 54) {
locationList.add(new BlockLocation(x, y, z));
}
blockStorage.setBlockAt(chunkX, chunkZ, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z, blockId);
}
}
}
}
for (BlockLocation location : locationList) {
int x = location.getX();
int y = location.getY();
int z = location.getZ();
byte rotation = RotationUtil.fixBlockRotation(session, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z);
int blockLightOffset = 65536;
setNibble(chunkData, x, y, z, (byte) 15, blockLightOffset);
sendDelayedBlockUpdate(session, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z, rotation);
}
chunk.setChunk(chunkData);
} catch (ArrayIndexOutOfBoundsException ignored) {
}
}
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3B_CHUNK, chunk) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3BChunk in project DirtMultiversion by DirtPowered.
the class V1_3BChunkDataType method read.
@Override
public V1_3BChunk read(PacketInput packetInput) {
int x = packetInput.readInt();
int y = packetInput.readShort();
int z = packetInput.readInt();
int xSize = packetInput.readUnsignedByte() + 1;
int ySize = packetInput.readUnsignedByte() + 1;
int zSize = packetInput.readUnsignedByte() + 1;
byte[] chunk;
int chunkSize = packetInput.readInt();
byte[] buf = packetInput.readBytes(chunkSize);
chunk = new byte[xSize * ySize * zSize * 5 / 2];
Inflater inflater = new Inflater();
inflater.setInput(buf);
try {
inflater.inflate(chunk);
} catch (DataFormatException ignored) {
} finally {
inflater.end();
}
return new V1_3BChunk(x, y, z, xSize, ySize, zSize, chunk);
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3BChunk 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();
}
Aggregations