use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class ProtocolRelease47To5 method registerTranslators.
@Override
public void registerTranslators() {
// status ping
addTranslator(0x00, ProtocolState.STATUS, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String json = data.read(Type.V1_7_STRING, 0);
ServerPing serverPing = DirtMultiVersion.GSON.fromJson(json, ServerPing.class);
ServerPing.Version versionObj = new ServerPing.Version();
versionObj.setName("1.8.x");
versionObj.setProtocol(47);
serverPing.setVersion(versionObj);
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.V1_7_STRING, serverPing.toString()) });
}
});
// keep alive
addTranslator(0x00, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)) });
}
});
// join game
addTranslator(0x01, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), set(Type.BOOLEAN, false) });
}
});
// login success
addTranslator(0x02, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {
@SneakyThrows
@Override
public PacketData translate(ServerSession session, PacketData data) {
String username = data.read(Type.STRING, 1);
// block connection thread until profile is fetched
UUID uuid = GameProfileFetcher.getProfile(username).get().getId();
String uniqueId = uuid.toString();
session.getUserData().setUniqueId(uuid);
// set compression
enableCompression(session);
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.V1_7_STRING, uniqueId), set(Type.V1_7_STRING, username) });
}
});
// spawn position
addTranslator(0x05, ProtocolState.PLAY, 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.INT, 1);
int z = data.read(Type.INT, 2);
return PacketUtil.createPacket(0x05, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)) });
}
});
// update health
addTranslator(0x06, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x06, new TypeHolder[] { data.read(0), set(Type.VAR_INT, (int) data.read(Type.SHORT, 1)), data.read(2) });
}
});
// chat
addTranslator(0x02, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 0) });
}
});
// chunk data
addTranslator(0x21, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_2Chunk chunk = data.read(Type.V1_3_CHUNK, 0);
int chunkX = chunk.getChunkX();
int chunkZ = chunk.getChunkZ();
short bitmap = chunk.getPrimaryBitmap();
boolean groundUp = chunk.isGroundUp();
if (groundUp && bitmap == 0) {
V1_8Chunk emptyChunk = new V1_8Chunk(chunkX, chunkZ, true, bitmap, new byte[0]);
ProtocolStorage storage = session.getStorage();
PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
portalFrameCache.removeChunk(chunkX, chunkZ);
return PacketUtil.createPacket(0x21, new TypeHolder[] { new TypeHolder<>(Type.V1_8R_CHUNK, emptyChunk) });
}
V1_3ToV1_8ChunkTranslator chunkTransformer;
if (chunk.getStorage() != null) {
// use existing chunk storage (pre 1.2 servers)
chunkTransformer = new V1_3ToV1_8ChunkTranslator(session, chunk.getStorage(), groundUp, bitmap);
} else {
ProtocolStorage storage = session.getStorage();
boolean skyLight = true;
if (storage.hasObject(DimensionTracker.class)) {
DimensionTracker tracker = storage.get(DimensionTracker.class);
skyLight = tracker.getDimension() == 0;
}
chunkTransformer = new V1_3ToV1_8ChunkTranslator(session, chunk.getUncompressedData(), bitmap, skyLight, groundUp);
}
V1_8Chunk newChunk = new V1_8Chunk(chunkX, chunkZ, groundUp, bitmap, chunkTransformer.getChunkData());
return PacketUtil.createPacket(0x21, new TypeHolder[] { new TypeHolder<>(Type.V1_8R_CHUNK, newChunk) });
}
});
// chunk bulk
addTranslator(0x26, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_3_4ChunkBulk oldChunkBulk = data.read(Type.V1_4CHUNK_BULK, 0);
int[] x = oldChunkBulk.getColumnX();
int[] z = oldChunkBulk.getColumnZ();
int columnAmount = oldChunkBulk.getChunks().length;
V1_3ToV1_8ChunkTranslator[] bulks = new V1_3ToV1_8ChunkTranslator[columnAmount];
for (int i = 0; i < columnAmount; i++) {
bulks[i] = new V1_3ToV1_8ChunkTranslator(session, oldChunkBulk.getChunks()[i], oldChunkBulk.getPrimaryBitmaps()[i], oldChunkBulk.isSkylight(), true);
}
V1_8ChunkBulk.Chunk[] chunks = new V1_8ChunkBulk.Chunk[columnAmount];
for (int i = 0; i < columnAmount; i++) {
chunks[i] = new V1_8ChunkBulk.Chunk();
chunks[i].setData(bulks[i].getChunkData());
chunks[i].setDataSize(oldChunkBulk.getPrimaryBitmaps()[i]);
}
V1_8ChunkBulk chunkBulk = new V1_8ChunkBulk(oldChunkBulk.isSkylight(), x, z, chunks);
return PacketUtil.createPacket(0x26, new TypeHolder[] { set(Type.V1_8R_CHUNK_BULK, chunkBulk) });
}
});
// multi block change
addTranslator(0x22, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_2MultiBlockArray blockArray = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
DataInput dis = new DataInputStream(new ByteArrayInputStream(blockArray.getData()));
BlockChangeRecord[] blockChangeRecords = new BlockChangeRecord[blockArray.getRecordCount()];
for (int i = 0; i < blockArray.getRecordCount(); i++) {
try {
short pos = dis.readShort();
short packedBlock = dis.readShort();
int blockId = packedBlock >> 4;
int blockData = packedBlock & 15;
if (DataFixers.shouldCache(blockId)) {
ProtocolStorage storage = session.getStorage();
PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
int x = pos >> 12 & 15;
int y = pos & 255;
int z = pos >> 8 & 15;
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
int xPos = x + (chunkX << 4);
int zPos = z + (chunkZ << 4);
portalFrameCache.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
blockData = DataFixers.getCorrectedDataFor(portalFrameCache, xPos, y, zPos, blockId, blockData);
} else {
blockData = DataFixers.fixInvalidData(blockId, blockData);
}
blockChangeRecords[i] = new BlockChangeRecord(pos, (short) (blockId & 4095) << 4 | blockData & 15);
} catch (IOException e) {
e.printStackTrace();
}
}
return PacketUtil.createPacket(0x22, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_8R_MULTIBLOCK_ARRAY, blockChangeRecords) });
}
});
// block change
addTranslator(0x23, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
short y = data.read(Type.UNSIGNED_BYTE, 1);
int z = data.read(Type.INT, 2);
int blockId = data.read(Type.VAR_INT, 3);
int blockData = data.read(Type.UNSIGNED_BYTE, 4);
if (DataFixers.shouldCache(blockId)) {
ProtocolStorage storage = session.getStorage();
PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
portalFrameCache.setBlockAt(x >> 4, z >> 4, x, y, z, blockId);
blockData = DataFixers.getCorrectedDataFor(portalFrameCache, x, y, z, blockId, blockData);
} else {
blockData = DataFixers.fixInvalidData(blockId, blockData);
}
return PacketUtil.createPacket(0x23, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), set(Type.VAR_INT, blockId << 4 | blockData & 15) });
}
});
// block action
addTranslator(0x24, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
short y = data.read(Type.SHORT, 1);
int z = data.read(Type.INT, 2);
return PacketUtil.createPacket(0x24, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), data.read(3), data.read(4), data.read(5) });
}
});
// block break animation
addTranslator(0x25, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 1);
int y = data.read(Type.INT, 2);
int z = data.read(Type.INT, 3);
return PacketUtil.createPacket(0x25, new TypeHolder[] { data.read(0), set(Type.LONG, toBlockPosition(x, y, z)), data.read(4) });
}
});
// effect
addTranslator(0x28, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 1);
byte y = data.read(Type.BYTE, 2);
int z = data.read(Type.INT, 3);
int effectType = data.read(Type.INT, 0);
if (effectType == 2006) {
double dist = Math.min(0.2F + data.read(Type.INT, 4) / 15.0F, 10.0F);
if (dist > 2.5D) {
dist = 2.5D;
}
int i = (int) (150.0D * dist);
return PacketUtil.createPacket(0x2A, new TypeHolder[] { set(Type.INT, 38), set(Type.BOOLEAN, false), set(Type.FLOAT, (float) x + 0.5f), set(Type.FLOAT, (float) y + 1.0f), set(Type.FLOAT, (float) z + 0.5f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0.15000000596046448f), set(Type.INT, i), // stone
set(Type.VAR_INT, 1) });
}
return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.LONG, toBlockPosition(x, y, z)), data.read(4), data.read(5) });
}
});
// update sign
addTranslator(0x33, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
short y = data.read(Type.SHORT, 1);
int z = data.read(Type.INT, 2);
String[] lines = new String[4];
for (int i = 0; i < 4; i++) {
lines[i] = ChatUtils.legacyToJsonString(data.read(Type.V1_7_STRING, 3 + i));
}
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), set(Type.V1_7_STRING, lines[0]), set(Type.V1_7_STRING, lines[1]), set(Type.V1_7_STRING, lines[2]), set(Type.V1_7_STRING, lines[3]) });
}
});
// sign editor
addTranslator(0x36, ProtocolState.PLAY, 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.INT, 1);
int z = data.read(Type.INT, 2);
return PacketUtil.createPacket(0x36, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)) });
}
});
// tab list item
addTranslator(0x38, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String u = data.read(Type.V1_7_STRING, 0);
if (u == null) {
// skip if server is updating ping or something
return cancel();
}
String username = ChatUtils.stripColor(u);
boolean online = data.read(Type.BOOLEAN, 1);
UUID uuid = UUID.nameUUIDFromBytes(("fake:" + username).getBytes(Charsets.UTF_8));
if (username.equals(session.getUserData().getUsername())) {
uuid = session.getUserData().getUniqueId();
}
if (online) {
TabListEntry tabAddListEntry = new TabListEntry(TabListAction.ADD_PLAYER, new PlayerListEntry[] { new PlayerListEntry(new GameProfile(uuid, username), new Property[0], 0, 0, null) });
return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabAddListEntry) });
} else {
TabListEntry tabRemoveListEntry = new TabListEntry(TabListAction.REMOVE_PLAYER, new PlayerListEntry[] { new PlayerListEntry(new GameProfile(uuid, username)) });
return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabRemoveListEntry) });
}
}
});
// set experience
addTranslator(0x1F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x1F, new TypeHolder[] { data.read(0), set(Type.VAR_INT, data.read(Type.SHORT, 1).intValue()), set(Type.VAR_INT, data.read(Type.SHORT, 2).intValue()) });
}
});
// use bed
addTranslator(0x0A, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 1);
byte y = data.read(Type.BYTE, 2);
int z = data.read(Type.INT, 3);
return PacketUtil.createPacket(0x0A, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.LONG, toBlockPosition(x, y, z)) });
}
});
// collect item
addTranslator(0x0D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0D, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.VAR_INT, data.read(Type.INT, 1)) });
}
});
// custom payload
addTranslator(0x3F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@SneakyThrows
@Override
public PacketData translate(ServerSession session, PacketData data) {
String channel = data.read(Type.V1_7_STRING, 0);
byte[] bytes = data.read(Type.SHORT_BYTE_ARRAY, 1);
byte[] remappedData;
NettyOutputWrapper fixedPayload = new NettyOutputWrapper(Unpooled.buffer());
switch(channel) {
case "MC|TrList":
NettyInputWrapper buf = new NettyInputWrapper(Unpooled.wrappedBuffer(bytes));
fixedPayload.writeInt(buf.readInt());
int items = buf.readByte();
fixedPayload.writeByte(items);
for (int i = 0; i < items; i++) {
V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
boolean hasAdditionalItem = buf.readBoolean();
fixedPayload.writeBoolean(hasAdditionalItem);
if (hasAdditionalItem) {
V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
}
fixedPayload.writeBoolean(buf.readBoolean());
fixedPayload.writeInt(0);
fixedPayload.writeInt(7);
}
remappedData = fixedPayload.array();
break;
case "MC|RPack":
return cancel();
case "MC|Brand":
V1_7_2RProtocol.STRING.write(set(Type.V1_7_STRING, new String(bytes, StandardCharsets.UTF_8)), fixedPayload);
remappedData = fixedPayload.array();
break;
default:
remappedData = bytes;
break;
}
return PacketUtil.createPacket(0x3F, new TypeHolder[] { data.read(0), set(Type.READABLE_BYTES, remappedData) });
}
});
// entity attributes
addTranslator(0x20, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x20, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.V1_8_ENTITY_ATTRIBUTES, data.read(Type.V1_7_ENTITY_ATTRIBUTES, 1)) });
}
});
// client packets
// keep alive
addTranslator(0x00, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)) });
}
});
// use entity
addTranslator(0x02, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
OptionalPosition optPos = data.read(Type.V1_8R_USE_ENTITY_OPTIONAL_POSITION, 1);
int action = optPos.getAction();
if (action == 2) {
return cancel();
}
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)), set(Type.BYTE, (byte) action) });
}
});
// player digging
addTranslator(0x07, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
long encodedPosition = data.read(Type.LONG, 1);
int action = data.read(Type.UNSIGNED_BYTE, 0);
BlockLocation l = fromBlockPosition(encodedPosition);
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(BlockMiningTimeFixer.class) && storage.hasObject(BlockStorage.class)) {
BlockMiningTimeFixer blockMiningTimeFixer = storage.get(BlockMiningTimeFixer.class);
BlockStorage blockStorage = storage.get(BlockStorage.class);
switch(action) {
case // start digging
0:
blockMiningTimeFixer.onBlockStartBreaking(l);
break;
case // cancel digging
1:
blockMiningTimeFixer.onBlockCancelBreaking(l);
break;
case // finish digging
2:
if (HardnessTable.exist(blockStorage.getBlockAt(l.getX(), l.getY(), l.getZ()))) {
return cancel();
}
break;
}
}
return PacketUtil.createPacket(0x07, new TypeHolder[] { data.read(0), set(Type.INT, l.getX()), set(Type.UNSIGNED_BYTE, (short) l.getY()), set(Type.INT, l.getZ()), set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 2).shortValue()) });
}
});
// place block
addTranslator(0x08, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
long encodedPosition = data.read(Type.LONG, 0);
BlockLocation l = fromBlockPosition(encodedPosition);
ItemStack itemStack = data.read(Type.V1_8R_ITEM, 2);
// 1.0+ servers are kicking players when placed block is 0. Should be null
if (itemStack != null && itemStack.getItemId() == 0)
itemStack = null;
if (itemStack != null && itemStack.getItemId() == 387) /* written book */
{
PacketData payload = PacketUtil.createPacket(0x3F, new TypeHolder[] { set(Type.V1_7_STRING, "MC|BOpen") });
session.sendPacket(payload, PacketDirection.TO_CLIENT, getFrom());
}
return PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.INT, l.getX()), set(Type.UNSIGNED_BYTE, (short) l.getY()), set(Type.INT, l.getZ()), data.read(1), set(Type.V1_3R_ITEM, itemStack), data.read(3), data.read(4), data.read(5) });
}
});
// animation
addTranslator(0x0A, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0A, new TypeHolder[] { set(Type.INT, 0), set(Type.BYTE, (byte) 1) });
}
});
// entity action
addTranslator(0x0B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int action = data.read(Type.VAR_INT, 1);
return PacketUtil.createPacket(0x0B, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)), set(Type.BYTE, (byte) (action + 1)), set(Type.INT, data.read(Type.VAR_INT, 2)) });
}
});
// update sign
addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
long encodedPosition = data.read(Type.LONG, 0);
BlockLocation l = fromBlockPosition(encodedPosition);
String[] lines = new String[4];
for (int i = 0; i < 4; i++) {
String msg = data.read(Type.V1_7_STRING, 1 + i);
if (msg.startsWith("\"")) {
msg = ChatUtils.createChatComponentFromInvalidJson(msg);
}
msg = ChatUtils.jsonToLegacy(msg);
msg = StringUtils.substring(msg, 0, 15);
lines[i] = msg;
}
return PacketUtil.createPacket(0x12, new TypeHolder[] { set(Type.INT, l.getX()), set(Type.SHORT, (short) l.getY()), set(Type.INT, l.getZ()), set(Type.V1_7_STRING, lines[0]), set(Type.V1_7_STRING, lines[1]), set(Type.V1_7_STRING, lines[2]), set(Type.V1_7_STRING, lines[3]) });
}
});
// tab complete
addTranslator(0x14, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0) });
}
});
// client settings
addTranslator(0x15, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x15, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.BOOLEAN, false) });
}
});
// custom payload
addTranslator(0x17, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) throws IOException {
byte[] bytes = data.read(Type.READABLE_BYTES, 1);
String channel = data.read(Type.V1_7_STRING, 0);
if (channel.equals("MC|BEdit") || channel.equals("MC|BSign")) {
NettyInputWrapper in = new NettyInputWrapper(Unpooled.wrappedBuffer(bytes));
NettyOutputWrapper out = new NettyOutputWrapper(Unpooled.buffer());
ItemStack compressedItem = V1_8RProtocol.ITEM.read(in);
V1_3_1RProtocol.ITEM.write(new TypeHolder<>(Type.V1_3R_ITEM, compressedItem), out);
bytes = out.array();
}
return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), set(Type.SHORT_BYTE_ARRAY, bytes) });
}
});
// player input
addTranslator(0x0C, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
byte status = data.read(Type.BYTE, 2);
return PacketUtil.createPacket(0x0C, new TypeHolder[] { data.read(0), data.read(1), set(Type.BOOLEAN, (status & 1) == 1), set(Type.BOOLEAN, (status & 2) == 2) });
}
});
// map data
addTranslator(0x34, -1, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// spawn particle
addTranslator(0x2A, -1, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class BlockMiningTimeFixer method onBlockStartBreaking.
@Override
public void onBlockStartBreaking(BlockLocation position) {
if (!storage.hasObject(BlockStorage.class)) {
return;
}
BlockStorage blockStorage = storage.get(BlockStorage.class);
int typeId = blockStorage.getBlockAt(position.getX(), position.getY(), position.getZ());
if (HardnessTable.exist(typeId)) {
this.currentMiningTicks = HardnessTable.getMiningTicks(typeId, getItemInHand());
this.pos = position;
this.blockId = typeId;
this.randomEid = session.getMain().getSharedRandom().nextInt(100);
sendSlownessEffect();
}
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class MovementTranslator method getPossibleCollisions.
private static ArrayList<BlockCollision> getPossibleCollisions(Location position, ServerSession session) {
ProtocolStorage protocolStorage = session.getStorage();
BoundingBox boundingBox = protocolStorage.get(BoundingBox.class);
BlockStorage storage = protocolStorage.get(BlockStorage.class);
ArrayList<BlockCollision> possibleCollisions = new ArrayList<>();
int minCollisionX = (int) Math.floor(position.getX() - (boundingBox.getSizeX() / 2));
int maxCollisionX = (int) Math.floor(position.getX() + (boundingBox.getSizeX() / 2));
int minCollisionY = (int) Math.floor(position.getY() - 0.5);
int maxCollisionY = (int) Math.floor(position.getY() + boundingBox.getSizeY());
int minCollisionZ = (int) Math.floor(position.getZ() - (boundingBox.getSizeZ() / 2));
int maxCollisionZ = (int) Math.floor(position.getZ() + (boundingBox.getSizeZ() / 2));
for (int y = minCollisionY; y < maxCollisionY + 1; y++) {
for (int x = minCollisionX; x < maxCollisionX + 1; x++) {
for (int z = minCollisionZ; z < maxCollisionZ + 1; z++) {
BlockLocation loc = new BlockLocation(x, y, z);
int blockId = storage.getBlockAt(loc.getX(), loc.getY(), loc.getZ());
if (needsCorrection(blockId)) {
FULL_BLOCK_COLLISION.setPosition(loc.getX(), loc.getY(), loc.getZ());
possibleCollisions.add(FULL_BLOCK_COLLISION);
}
}
}
}
return possibleCollisions;
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol protocol) {
// Outgoing packets
protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PAINTING, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Entity UUID
map(Type.UUID);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
PaintingProvider provider = Via.getManager().getProviders().get(PaintingProvider.class);
String motive = wrapper.read(Type.STRING);
Optional<Integer> id = provider.getIntByIdentifier(motive);
if (!id.isPresent() && (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug())) {
Via.getPlatform().getLogger().warning("Could not find painting motive: " + motive + " falling back to default (0)");
}
wrapper.write(Type.VAR_INT, id.orElse(0));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Location
map(Type.POSITION);
// 1 - Action
map(Type.UNSIGNED_BYTE);
// 2 - NBT data
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position position = wrapper.get(Type.POSITION, 0);
short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
CompoundTag tag = wrapper.get(Type.NBT, 0);
BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
int newId = provider.transform(wrapper.user(), position, tag, true);
if (newId != -1) {
BlockStorage storage = wrapper.user().get(BlockStorage.class);
BlockStorage.ReplacementData replacementData = storage.get(position);
if (replacementData != null) {
replacementData.setReplacement(newId);
}
}
if (action == 5) {
// Set type of flower in flower pot
// Removed
wrapper.cancel();
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// Location
map(Type.POSITION);
// Action Id
map(Type.UNSIGNED_BYTE);
// Action param
map(Type.UNSIGNED_BYTE);
// Block Id - /!\ NOT BLOCK STATE ID
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position pos = wrapper.get(Type.POSITION, 0);
short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
short param = wrapper.get(Type.UNSIGNED_BYTE, 1);
int blockId = wrapper.get(Type.VAR_INT, 0);
if (blockId == 25)
blockId = 73;
else if (blockId == 33)
blockId = 99;
else if (blockId == 29)
blockId = 92;
else if (blockId == 54)
blockId = 142;
else if (blockId == 146)
blockId = 305;
else if (blockId == 130)
blockId = 249;
else if (blockId == 138)
blockId = 257;
else if (blockId == 52)
blockId = 140;
else if (blockId == 209)
blockId = 472;
else if (blockId >= 219 && blockId <= 234)
blockId = blockId - 219 + 483;
if (blockId == 73) {
// Note block
// block change
PacketWrapper blockChange = wrapper.create(0x0B);
blockChange.write(Type.POSITION, pos);
blockChange.write(Type.VAR_INT, 249 + (action * 24 * 2) + (param * 2));
blockChange.send(Protocol1_13To1_12_2.class);
}
wrapper.set(Type.VAR_INT, 0, blockId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION);
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position position = wrapper.get(Type.POSITION, 0);
int newId = toNewId(wrapper.get(Type.VAR_INT, 0));
UserConnection userConnection = wrapper.user();
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newId);
newId = ConnectionData.connect(userConnection, position, newId);
}
wrapper.set(Type.VAR_INT, 0, checkStorage(wrapper.user(), position, newId));
if (Via.getConfig().isServersideBlockConnections()) {
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
ConnectionData.update(userConnection, position);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.MULTI_BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Chunk X
map(Type.INT);
// 1 - Chunk Z
map(Type.INT);
// 2 - Records
map(Type.BLOCK_CHANGE_RECORD_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int chunkX = wrapper.get(Type.INT, 0);
int chunkZ = wrapper.get(Type.INT, 1);
UserConnection userConnection = wrapper.user();
BlockChangeRecord[] records = wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0);
// Convert ids
for (BlockChangeRecord record : records) {
int newBlock = toNewId(record.getBlockId());
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newBlock);
}
record.setBlockId(checkStorage(wrapper.user(), position, newBlock));
}
if (Via.getConfig().isServersideBlockConnections()) {
for (BlockChangeRecord record : records) {
int blockState = record.getBlockId();
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
ConnectionHandler handler = ConnectionData.getConnectionHandler(blockState);
if (handler != null) {
blockState = handler.connect(userConnection, position, blockState);
record.setBlockId(blockState);
}
}
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (BlockChangeRecord record : records) {
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
ConnectionData.update(userConnection, position);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.EXPLOSION, new PacketRemapper() {
@Override
public void registerMap() {
if (!Via.getConfig().isServersideBlockConnections())
return;
// X
map(Type.FLOAT);
// Y
map(Type.FLOAT);
// Z
map(Type.FLOAT);
// Radius
map(Type.FLOAT);
// Record Count
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
UserConnection userConnection = wrapper.user();
int x = (int) Math.floor(wrapper.get(Type.FLOAT, 0));
int y = (int) Math.floor(wrapper.get(Type.FLOAT, 1));
int z = (int) Math.floor(wrapper.get(Type.FLOAT, 2));
int recordCount = wrapper.get(Type.INT, 0);
Position[] records = new Position[recordCount];
for (int i = 0; i < recordCount; i++) {
Position position = new Position(x + wrapper.passthrough(Type.BYTE), (short) (y + wrapper.passthrough(Type.BYTE)), z + wrapper.passthrough(Type.BYTE));
records[i] = position;
// Set to air
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), 0);
}
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (int i = 0; i < recordCount; i++) {
ConnectionData.update(userConnection, records[i]);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
if (Via.getConfig().isServersideBlockConnections()) {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int x = wrapper.passthrough(Type.INT);
int z = wrapper.passthrough(Type.INT);
ConnectionData.blockConnectionProvider.unloadChunk(wrapper.user(), x, z);
}
});
}
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.NAMED_SOUND, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String sound = wrapper.get(Type.STRING, 0).replace("minecraft:", "");
String newSoundId = NamedSoundRewriter.getNewId(sound);
wrapper.set(Type.STRING, 0, newSoundId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
BlockStorage storage = wrapper.user().get(BlockStorage.class);
Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
Chunk1_13Type type1_13 = new Chunk1_13Type(clientWorld);
Chunk chunk = wrapper.read(type);
wrapper.write(type1_13, chunk);
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int p = 0; p < section.getPaletteSize(); p++) {
int old = section.getPaletteEntry(p);
int newId = toNewId(old);
section.setPaletteEntry(p, newId);
}
boolean willSaveToStorage = false;
for (int p = 0; p < section.getPaletteSize(); p++) {
int newId = section.getPaletteEntry(p);
if (storage.isWelcome(newId)) {
willSaveToStorage = true;
break;
}
}
boolean willSaveConnection = false;
if (Via.getConfig().isServersideBlockConnections() && ConnectionData.needStoreBlocks()) {
for (int p = 0; p < section.getPaletteSize(); p++) {
int newId = section.getPaletteEntry(p);
if (ConnectionData.isWelcome(newId)) {
willSaveConnection = true;
break;
}
}
}
if (willSaveToStorage) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
if (storage.isWelcome(block)) {
storage.store(new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4))), block);
}
}
}
}
}
if (willSaveConnection) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
if (ConnectionData.isWelcome(block)) {
ConnectionData.blockConnectionProvider.storeBlock(wrapper.user(), x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block);
}
}
}
}
}
}
// Rewrite biome id 255 to plains
if (chunk.isBiomeData()) {
int latestBiomeWarn = Integer.MIN_VALUE;
for (int i = 0; i < 256; i++) {
int biome = chunk.getBiomeData()[i];
if (!VALID_BIOMES.contains(biome)) {
if (// is it generated naturally? *shrug*
biome != 255 && latestBiomeWarn != biome) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Received invalid biome id " + biome);
}
latestBiomeWarn = biome;
}
// Plains
chunk.getBiomeData()[i] = 1;
}
}
}
// Rewrite BlockEntities to normal blocks
BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
final Iterator<CompoundTag> iterator = chunk.getBlockEntities().iterator();
while (iterator.hasNext()) {
CompoundTag tag = iterator.next();
int newId = provider.transform(wrapper.user(), null, tag, false);
if (newId != -1) {
int x = ((NumberTag) tag.get("x")).asInt();
int y = ((NumberTag) tag.get("y")).asInt();
int z = ((NumberTag) tag.get("z")).asInt();
Position position = new Position(x, (short) y, z);
// Store the replacement blocks for blockupdates
BlockStorage.ReplacementData replacementData = storage.get(position);
if (replacementData != null) {
replacementData.setReplacement(newId);
}
chunk.getSections()[y >> 4].setFlatBlock(x & 0xF, y & 0xF, z & 0xF, newId);
}
final Tag idTag = tag.get("id");
if (idTag instanceof StringTag) {
// No longer block entities
final String id = ((StringTag) idTag).getValue();
if (id.equals("minecraft:noteblock") || id.equals("minecraft:flower_pot")) {
iterator.remove();
}
}
}
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.connectBlocks(wrapper.user(), chunk);
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
ConnectionData.updateChunkSectionNeighbours(wrapper.user(), chunk.getX(), chunk.getZ(), i);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PARTICLE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Particle ID
map(Type.INT);
// 1 - Long Distance
map(Type.BOOLEAN);
// 2 - X
map(Type.FLOAT);
// 3 - Y
map(Type.FLOAT);
// 4 - Z
map(Type.FLOAT);
// 5 - Offset X
map(Type.FLOAT);
// 6 - Offset Y
map(Type.FLOAT);
// 7 - Offset Z
map(Type.FLOAT);
// 8 - Particle Data
map(Type.FLOAT);
// 9 - Particle Count
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int particleId = wrapper.get(Type.INT, 0);
// Get the data (Arrays are overrated)
int dataCount = 0;
// Particles with 1 data [BlockCrack,BlockDust,FallingDust]
if (particleId == 37 || particleId == 38 || particleId == 46)
dataCount = 1;
else // Particles with 2 data [IconCrack]
if (particleId == 36)
dataCount = 2;
Integer[] data = new Integer[dataCount];
for (int i = 0; i < data.length; i++) data[i] = wrapper.read(Type.VAR_INT);
Particle particle = ParticleRewriter.rewriteParticle(particleId, data);
// Cancel if null or completely removed
if (particle == null || particle.getId() == -1) {
wrapper.cancel();
return;
}
// Handle reddust particle color
if (particle.getId() == 11) {
int count = wrapper.get(Type.INT, 1);
float speed = wrapper.get(Type.FLOAT, 6);
// Only handle for count = 0
if (count == 0) {
wrapper.set(Type.INT, 1, 1);
wrapper.set(Type.FLOAT, 6, 0f);
List<Particle.ParticleData> arguments = particle.getArguments();
for (int i = 0; i < 3; i++) {
// RGB values are represented by the X/Y/Z offset
float colorValue = wrapper.get(Type.FLOAT, i + 3) * speed;
if (colorValue == 0 && i == 0) {
// https://minecraft.gamepedia.com/User:Alphappy/reddust
colorValue = 1;
}
arguments.get(i).setValue(colorValue);
wrapper.set(Type.FLOAT, i + 3, 0f);
}
}
}
wrapper.set(Type.INT, 0, particle.getId());
for (Particle.ParticleData particleData : particle.getArguments()) wrapper.write(particleData.getType(), particleData.getValue());
}
});
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project ViaVersion by ViaVersion.
the class BedHandler method transform.
@Override
public int transform(UserConnection user, CompoundTag tag) {
BlockStorage storage = user.get(BlockStorage.class);
Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));
if (!storage.contains(position)) {
Via.getPlatform().getLogger().warning("Received an bed color update packet, but there is no bed! O_o " + tag);
return -1;
}
// RED_BED + FIRST_BED
int blockId = storage.get(position).getOriginal() - 972 + 748;
Tag color = tag.get("color");
if (color != null) {
blockId += (((NumberTag) color).asInt() * 16);
}
return blockId;
}
Aggregations