use of com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack in project DirtMultiversion by DirtPowered.
the class ProtocolRelease22To17 method registerTranslators.
@Override
public void registerTranslators() {
// login
addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { // protocol version
set(Type.INT, 17), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
}
});
// block place
addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0F, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.V1_3B_ITEM, data.read(Type.V1_0R_ITEM, 4)) });
}
});
// window click
addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.V1_3B_ITEM, data.read(Type.V1_0R_ITEM, 5)) });
}
});
// creative set slot
addTranslator(0x6B, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_0R_ITEM, 1);
boolean notNull = item != null;
if (notNull && !LegacyItemList.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_8B_ITEM, item) });
}
});
// set experience
addTranslator(0x2B, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
float exp = data.read(Type.BYTE, 0).floatValue();
short level = data.read(Type.BYTE, 1).shortValue();
exp = (exp - 1) / (10 * level);
return PacketUtil.createPacket(0x2B, new TypeHolder[] { set(Type.FLOAT, exp), set(Type.SHORT, level), set(Type.SHORT, level) });
}
});
// set slot
addTranslator(0x67, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_3B_ITEM, 2);
if (item != null && LegacyItemList.isEnchantable(item.getItemId()))
item.setCompoundTag(null);
return PacketUtil.createPacket(0x67, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_0R_ITEM, item) });
}
});
// window items
addTranslator(0x68, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack[] items = data.read(Type.V1_3B_ITEM_ARRAY, 1);
for (ItemStack item : items) {
if (item != null && LegacyItemList.isEnchantable(item.getItemId())) {
item.setCompoundTag(null);
}
}
return PacketUtil.createPacket(0x68, new TypeHolder[] { data.read(0), set(Type.V1_0R_ITEM_ARRAY, items) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack in project DirtMultiversion by DirtPowered.
the class InventoryPackets method updateLocalInventory.
/*
* NOTE: quick bar slots are static, so we don't have to check inventory type
*/
private void updateLocalInventory(QuickBarTracker cache, int slot, boolean shiftClick) {
int slotIndex = QuickBarTracker.networkSlotToInventory(slot);
ItemStack currentItem = cache.getItem(slotIndex);
if (shiftClick) {
if (slotIndex < 9) {
for (int i = 9; i < 36; ++i) {
if (cache.getItem(i) == null) {
cache.setItem(i, currentItem);
cache.setItem(slotIndex, null);
return;
}
}
} else {
for (int i = 0; i < 9; ++i) {
if (cache.getItem(i) == null) {
cache.setItem(i, currentItem);
cache.setItem(slotIndex, null);
return;
}
}
}
return;
}
cache.setItem(slotIndex, cache.getItemOnCursor());
cache.setItemOnCursor(currentItem);
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack 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.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack in project DirtMultiversion by DirtPowered.
the class ProtocolRelease4To78 method registerTranslators.
@Override
public void registerTranslators() {
// server info request
addTranslator(0x00, ProtocolState.STATUS, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String address = "localhost";
int port = 25565;
return PacketUtil.createPacket(0xFE, new TypeHolder[] { set(Type.UNSIGNED_BYTE, (short) 1), set(Type.UNSIGNED_BYTE, (short) 0xFA), set(Type.STRING, "MC|PingHost"), set(Type.SHORT, (short) (3 + 2 * address.length() + 4)), set(Type.BYTE, (byte) 78), set(Type.STRING, address), set(Type.INT, port) });
}
});
// ping
addTranslator(0x01, ProtocolState.STATUS, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
PacketData response = PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0) });
session.sendPacket(response, PacketDirection.TO_CLIENT, getFrom());
return cancel();
}
});
// kick disconnect
addTranslator(0xFF, ProtocolState.STATUS, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ServerMotd motd = ServerMotd.deserialize(data.read(Type.STRING, 0));
ServerPing serverPing = new ServerPing();
ServerPing.Version version = new ServerPing.Version();
ServerPing.Players players = new ServerPing.Players();
serverPing.setDescription(motd.getMotd());
serverPing.setVersion(version);
version.setName("1.7.2");
version.setProtocol(4);
players.setMax(motd.getMax());
players.setOnline(motd.getOnline());
List<ServerPing.Player> playerList = new ArrayList<>();
for (UserData userData : session.getServer().getAllConnections()) {
if (userData.getUsername() != null) {
ServerPing.Player player = new ServerPing.Player();
player.setId(getOfflineUuid(userData.getUsername()));
player.setName(userData.getUsername());
playerList.add(player);
}
}
List<ServerPing.Player> list = new ArrayList<>();
long limit = 10;
for (ServerPing.Player player : playerList) {
if (limit-- == 0)
break;
list.add(player);
}
playerList = list;
if (!playerList.isEmpty()) {
players.setSample(playerList);
}
serverPing.setPlayers(players);
String serverIcon = session.getServer().getServerIcon();
if (serverIcon != null && !serverIcon.isEmpty()) {
serverPing.setFavicon(serverIcon);
}
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.V1_7_STRING, serverPing.toString()) });
}
});
// login start
addTranslator(0x00, ProtocolState.LOGIN, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
if (data.getObjects().length > 1)
return cancel();
UserData userData = session.getUserData();
String username = data.read(Type.V1_7_STRING, 0);
// handshake
PacketData handshake = PacketUtil.createPacket(0x02, new TypeHolder[] { // protocol version
set(Type.BYTE, (byte) 78), set(Type.STRING, username), set(Type.STRING, userData.getAddress()), set(Type.INT, userData.getPort()) });
PacketData clientCommand = PacketUtil.createPacket(0xCD, new TypeHolder[] { set(Type.BYTE, (byte) 0) });
userData.setUsername(username);
session.sendPacket(handshake, PacketDirection.TO_SERVER, null);
// client command
session.sendPacket(clientCommand, PacketDirection.TO_SERVER, null);
return cancel();
}
});
// encryption
addTranslator(0xFD, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
UserData userData = session.getUserData();
String username = userData.getUsername();
PacketData loginSuccess = PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.V1_7_STRING, getOfflineUuid(username)), set(Type.V1_7_STRING, username) });
session.sendPacket(loginSuccess, PacketDirection.TO_CLIENT, getFrom());
userData.setProtocolState(ProtocolState.PLAY);
return cancel();
}
});
// login kick disconnect
addTranslator(0xFF, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String message = data.read(Type.STRING, 0);
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.V1_7_STRING, ChatUtils.legacyToJsonString(message)) });
}
});
// pre-netty login
addTranslator(0x01, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { // entity id
data.read(0), // game type
data.read(2), // dimension
data.read(3), // difficulty
data.read(4), // max players
data.read(6), set(Type.V1_7_STRING, data.read(Type.STRING, 1)) });
}
});
// 0x03 SC 0x02 (chat)
addTranslator(0x03, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String translationComponent = ChatUtils.fixTranslationComponent(data.read(Type.STRING, 0));
String message = ChatUtils.jsonToNewChatComponent(translationComponent);
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.V1_7_STRING, message) });
}
});
// 0x05 SC 0x04 (entity equipment)
addTranslator(0x05, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
if (originalItem != null) {
originalItem = itemRemapper.replaceItem(originalItem);
}
return PacketUtil.createPacket(0x04, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_3R_ITEM, originalItem) });
}
});
// 0xFF SC 0x40 (kick disconnect)
addTranslator(0xFF, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String legacyDisconnect = data.read(Type.STRING, 0);
return PacketUtil.createPacket(0x40, new TypeHolder[] { set(Type.V1_7_STRING, ChatUtils.legacyToJsonString(legacyDisconnect)) });
}
});
// 0x35 SC 0x23 (block change)
addTranslator(0x35, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x23, new TypeHolder[] { // x
data.read(0), // y
set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 1).shortValue()), // z
data.read(2), // type
set(Type.VAR_INT, data.read(Type.SHORT, 3).intValue()), // data
set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 4).shortValue()) });
}
});
// 0x18 SC 0x0F (spawn mob)
addTranslator(0x18, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0F, new TypeHolder[] { // entity id
set(Type.VAR_INT, data.read(Type.INT, 0)), // type
data.read(1), // x
data.read(2), // y
data.read(3), // z
data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), data.read(10), set(Type.V1_7R_METADATA, data.read(Type.V1_4R_METADATA, 11)) });
}
});
// 0xC9 SC 0x38 (player tab entry)
addTranslator(0xC9, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.V1_7_STRING, data.read(Type.STRING, 0)), set(Type.BOOLEAN, data.read(Type.BYTE, 1) == 1), data.read(2) });
}
});
// 0x09 SC 0x07 (respawn)
addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x07, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.V1_7_STRING, data.read(Type.STRING, 4)) });
}
});
// 0x0D SC 0x08 (player pos look)
addTranslator(0x0D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x08, new TypeHolder[] { data.read(0), data.read(1), data.read(3), data.read(4), data.read(5), data.read(6) });
}
});
// 0x47 SC 0x2C (spawn global entity)
addTranslator(0x47, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x2C, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3), data.read(4) });
}
});
// 0x17 SC 0x0E (spawn vehicle -> spawn object)
addTranslator(0x17, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0E, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
}
});
// 0x36 SC 0x24 (play note block -> block action)
addTranslator(0x36, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x24, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.VAR_INT, data.read(Type.SHORT, 5).intValue()) });
}
});
// 0x28 SC 0x1C (entity metadata)
addTranslator(0x28, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
WatchableObject[] metadataArray = data.read(Type.V1_4R_METADATA, 1);
for (int i = 0; i < metadataArray.length; i++) {
WatchableObject watchableObject = metadataArray[i];
if (watchableObject.getType() == MetadataType.ITEM) {
ItemStack obj = (ItemStack) watchableObject.getValue();
if (obj != null) {
obj = itemRemapper.replaceItem(obj);
}
metadataArray[i] = new WatchableObject(MetadataType.ITEM, watchableObject.getIndex(), obj);
}
}
return PacketUtil.createPacket(0x1C, new TypeHolder[] { data.read(0), set(Type.V1_7R_METADATA, metadataArray) });
// return cancel();
}
});
// 0x46 SC 0x2B (game event)
addTranslator(0x46, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int reason = data.read(Type.BYTE, 0);
if (reason == 1) {
reason = 2;
} else if (reason == 2) {
reason = 1;
}
return PacketUtil.createPacket(0x2B, new TypeHolder[] { set(Type.UNSIGNED_BYTE, (short) reason), set(Type.FLOAT, data.read(Type.BYTE, 1).floatValue()) });
}
});
// 0x64 SC 0x2D (open window) // TODO: optional horse data
addTranslator(0x64, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x2D, new TypeHolder[] { set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 0).shortValue()), set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 1).shortValue()), set(Type.V1_7_STRING, data.read(Type.STRING, 2)), set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 3).shortValue()), data.read(4) });
}
});
// 0x3C SC 0x27 (explosion)
addTranslator(0x3C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x27, new TypeHolder[] { set(Type.FLOAT, data.read(Type.DOUBLE, 0).floatValue()), set(Type.FLOAT, data.read(Type.DOUBLE, 1).floatValue()), set(Type.FLOAT, data.read(Type.DOUBLE, 2).floatValue()), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
}
});
// 0x3E SC 0x29 (level sound)
addTranslator(0x3E, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String soundName = data.read(Type.STRING, 0);
String newSoundName = soundRemapper.getNewSoundName(soundName);
if (newSoundName.isEmpty())
return cancel();
return PacketUtil.createPacket(0x29, new TypeHolder[] { set(Type.V1_7_STRING, soundName), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5) });
}
});
// 0x14 CS 0x0C (named entity spawn)
addTranslator(0x14, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String username = data.read(Type.STRING, 1);
return PacketUtil.createPacket(0x0C, new TypeHolder[] { // entity id
set(Type.VAR_INT, data.read(Type.INT, 0)), // player UUID,
set(Type.V1_7_STRING, getOfflineUuid(username)), // player name
set(Type.V1_7_STRING, username), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), set(Type.V1_7R_METADATA, data.read(Type.V1_4R_METADATA, 8)) });
}
});
// 0x82 CS 0x33 (update sign)
addTranslator(0x82, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x33, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.V1_7_STRING, data.read(Type.STRING, 3)), set(Type.V1_7_STRING, data.read(Type.STRING, 4)), set(Type.V1_7_STRING, data.read(Type.STRING, 5)), set(Type.V1_7_STRING, data.read(Type.STRING, 6)) });
}
});
// 0x1A SC 0x11 (spawn experience orb)
addTranslator(0x1A, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x11, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3), data.read(4) });
}
});
// 0x19 SC 0x10 (spawn painting)
addTranslator(0x19, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x10, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.V1_7_STRING, data.read(Type.STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5) });
}
});
// 0x10 SC 0x09 (held slot change)
addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x09, new TypeHolder[] { set(Type.BYTE, data.read(Type.SHORT, 0).byteValue()) });
}
});
// 0xCB SC 0x3A (tab complete)
addTranslator(0xCB, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String arr = data.read(Type.STRING, 0);
String[] commands = arr.split("\0");
return PacketUtil.createPacket(0x3A, new TypeHolder[] { set(Type.STRING_ARRAY, commands) });
}
});
// 0x37 SC 0x25 (block break animation)
addTranslator(0x37, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x25, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3), data.read(4) });
}
});
// 0xCE SC 0x3B (set objective)
addTranslator(0xCE, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x3B, new TypeHolder[] { set(Type.V1_7_STRING, data.read(Type.STRING, 0)), set(Type.V1_7_STRING, data.read(Type.STRING, 1)), data.read(2) });
}
});
// 0xCF SC 0x3C (update score)
addTranslator(0xCF, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String itemName = data.read(Type.STRING, 0);
int action = data.read(Type.BYTE, 1);
TypeHolder<?>[] types = new TypeHolder[action != 1 ? 4 : 2];
types[0] = set(Type.V1_7_STRING, itemName);
types[1] = set(Type.BYTE, (byte) action);
if (action != 1) {
types[2] = set(Type.V1_7_STRING, data.read(Type.STRING, 2));
types[3] = data.read(3);
}
return PacketUtil.createPacket(0x3C, types);
}
});
// 0xD0 SC 0x3D (set display objective)
addTranslator(0xD0, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x3D, new TypeHolder[] { data.read(0), set(Type.V1_7_STRING, data.read(Type.STRING, 1)) });
}
});
// 0x3F SC 0x2A (world particles)
addTranslator(0x3F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x2A, new TypeHolder[] { set(Type.V1_7_STRING, data.read(Type.STRING, 0)), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8) });
}
});
// 0x85 SC 0x36 (open sign editor)
addTranslator(0x85, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x36, new TypeHolder[] { data.read(1), data.read(2), data.read(3) });
}
});
// 0x11 SC 0x0A (use bed)
addTranslator(0x11, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0A, new TypeHolder[] { data.read(0), data.read(2), data.read(3), data.read(4) });
}
});
// 0x12 SC 0x0B (entity animation)
addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int animationType = data.read(Type.BYTE, 1);
switch(animationType) {
case 1:
animationType = 0;
break;
case 2:
animationType = 1;
break;
case 3:
animationType = 2;
break;
case 5:
animationType = 3;
break;
case 6:
animationType = 4;
break;
case 7:
animationType = 5;
break;
}
return PacketUtil.createPacket(0x0B, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.UNSIGNED_BYTE, (short) animationType) });
}
});
// 0xFA SC 0x3F (custom payload)
addTranslator(0xFA, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x3F, new TypeHolder[] { set(Type.V1_7_STRING, data.read(Type.STRING, 0)), data.read(1) });
}
});
// 0x83 SC 0x34 (map data)
addTranslator(0x83, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x34, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.SHORT, 1).intValue()), data.read(2) });
}
});
// 0xD1 SC 0x3E (set team) // TODO: translate
addTranslator(0xD1, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return cancel();
}
});
// 0x2C SC 0x20 (entity attributes)
addTranslator(0x2C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x20, new TypeHolder[] { data.read(0), set(Type.V1_7_ENTITY_ATTRIBUTES, data.read(Type.V1_6_2_ENTITY_ATTRIBUTES, 1)) });
}
});
// 0x67 SC 0x2F (inventory set slot)
addTranslator(0x67, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
if (originalItem == null) {
return PacketUtil.createPacket(0x2F, new TypeHolder[] { set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 0).shortValue()), data.read(1), data.read(2) });
}
ItemStack itemStack = itemRemapper.replaceItem(originalItem);
return PacketUtil.createPacket(0x2F, new TypeHolder[] { set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 0).shortValue()), data.read(1), set(Type.V1_3R_ITEM, itemStack) });
}
});
// 0x68 SC 0x30 (inventory window items)
addTranslator(0x68, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack[] itemArray = data.read(Type.V1_3R_ITEM_ARRAY, 1);
for (int i = 0; i < itemArray.length; i++) {
ItemStack originalItem = itemArray[i];
ItemStack item = originalItem;
if (originalItem != null) {
item = itemRemapper.replaceItem(originalItem);
}
itemArray[i] = item;
}
return PacketUtil.createPacket(0x30, new TypeHolder[] { set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 0).shortValue()), set(Type.V1_3R_ITEM_ARRAY, itemArray) });
}
});
// 0x3D SC 0x28 (door change -> effect)
addTranslator(0x3D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), data.read(1), set(Type.BYTE, data.read(Type.SHORT, 2).byteValue()), data.read(3), data.read(4), data.read(5) });
}
});
// 0x84 SC 0x35 (update tile entity)
addTranslator(0x84, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
final CompoundBinaryTag binaryTag = data.read(Type.COMPOUND_TAG, 4);
@NotNull String value = binaryTag.getString("ExtraType");
PacketData packet = PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 3).shortValue()), set(Type.COMPOUND_TAG, binaryTag) });
if (!value.isEmpty()) {
HeadConversion.convert(session, packet, value, getFrom());
return cancel();
}
return packet;
}
});
// 0xC8 SC 0x37 -> cancel (statistics)
addTranslator(0xC8, -1, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x27 SC 0x1B (entity attach)
addTranslator(0x27, 0x1B, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x69 SC 0x31 (update progress bar -> window property)
addTranslator(0x69, 0x31, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x65 SC 0x2E (window close)
addTranslator(0x65, 0x2E, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x29 SC 0x1D (entity effect)
addTranslator(0x29, 0x1D, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x2A SC 0x1E (clear entity effect)
addTranslator(0x2A, 0x1E, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x6A SC 0x32 (inventory transaction)
addTranslator(0x6A, 0x32, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0xCA SC 0x39 (player abilities)
addTranslator(0xCA, 0x39, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x2B SC 0x1F (set experience)
addTranslator(0x2B, 0x1F, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x38 SC 0x26 (chunk bulk)
addTranslator(0x38, 0x26, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x26 SC 0x1A (entity status)
addTranslator(0x26, 0x1A, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x23 SC 0x19 (entity head look)
addTranslator(0x23, 0x19, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x21 SC 0x17 (entity relative move look)
addTranslator(0x21, 0x17, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x1E SC 0x14 (entity ground state)
addTranslator(0x1E, 0x14, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x08 SC 0x06 (health update)
addTranslator(0x08, 0x06, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x04 SC 0x03 (update time)
addTranslator(0x04, 0x03, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x06 SC 0x05 (spawn position)
addTranslator(0x06, 0x05, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x33 SC 0x21 (chunk data)
addTranslator(0x33, 0x21, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x34 SC 0x22 (multi block change)
addTranslator(0x34, 0x22, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x1F SC 0x15 (entity relative move)
addTranslator(0x1F, 0x15, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x1C SC 0x12 (entity velocity)
addTranslator(0x1C, 0x12, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x1D SC 0x13 (entity destroy)
addTranslator(0x1D, 0x13, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x20 SC 0x16 (entity look)
addTranslator(0x20, 0x16, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x22 SC 0x18 (entity teleport)
addTranslator(0x22, 0x18, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x16 SC 0x0D (item collect)
addTranslator(0x16, 0x0D, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
// 0x17 CS 0xFA (custom payload)
addTranslator(0x17, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String channel = data.read(Type.V1_7_STRING, 0);
if (channel.equals("MC|AdvCmd")) {
// TODO: fix command blocks
return cancel();
}
return PacketUtil.createPacket(0xFA, new TypeHolder[] { set(Type.STRING, channel), data.read(1) });
}
});
// 0x01 CS 0x03 (chat)
addTranslator(0x01, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, data.read(Type.V1_7_STRING, 0)) });
}
});
// 0x16 CS 0xCD (client command)
addTranslator(0x16, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int status = data.read(Type.BYTE, 0);
if (status == 0) {
return PacketUtil.createPacket(0xCD, new TypeHolder[] { // perform respawn
set(Type.BYTE, (byte) 1) });
} else {
return cancel();
}
}
});
// 0x02 SC 0x07 (use entity)
addTranslator(0x02, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x07, new TypeHolder[] { set(Type.INT, 0), data.read(0), data.read(1) });
}
});
// 0x14 CS 0xCB (tab complete)
addTranslator(0x14, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCB, new TypeHolder[] { set(Type.STRING, data.read(Type.V1_7_STRING, 0)) });
}
});
// 0x15 CS 0xCC (player settings)
addTranslator(0x15, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCC, new TypeHolder[] { set(Type.STRING, data.read(Type.V1_7_STRING, 0)), data.read(1), data.read(2), data.read(4), data.read(5) });
}
});
// 0x12 CS 0x82 (set sign text)
addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x82, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.STRING, data.read(Type.V1_7_STRING, 3)), set(Type.STRING, data.read(Type.V1_7_STRING, 4)), set(Type.STRING, data.read(Type.V1_7_STRING, 5)), set(Type.STRING, data.read(Type.V1_7_STRING, 6)) });
}
});
// 0x08 CS 0x0F (block placement)
addTranslator(0x08, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0F, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.BYTE, data.read(Type.UNSIGNED_BYTE, 3).byteValue()), data.read(4), data.read(5), data.read(6), data.read(7) });
}
});
// 0x07 CS 0x0E (block digging)
addTranslator(0x07, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x0E, new TypeHolder[] { set(Type.BYTE, data.read(Type.UNSIGNED_BYTE, 0).byteValue()), data.read(1), set(Type.BYTE, data.read(Type.UNSIGNED_BYTE, 2).byteValue()), data.read(3), set(Type.BYTE, data.read(Type.UNSIGNED_BYTE, 4).byteValue()) });
}
});
// creative item get
addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_3R_ITEM, 1);
boolean notNull = item != null;
if (notNull && !creativeTab.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
}
});
// 0x0F CS 0x6A (confirm transaction)
addTranslator(0x0F, 0x6A, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x0C CS 0x1B (steer vehicle / player input)
addTranslator(0x0C, 0x1B, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x13 CS 0xCA (player abilities)
addTranslator(0x13, 0xCA, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x11 CS 0x6C (enchant slot selection)
addTranslator(0x11, 0x6C, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x09 CS 0x10 (held slot change)
addTranslator(0x09, 0x10, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x03 CS 0x0A (player ground state)
addTranslator(0x03, 0x0A, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x04 CS 0x0B (player position)
addTranslator(0x04, 0x0B, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x05 CS 0x0C (player look)
addTranslator(0x05, 0x0C, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x06 CS 0x0D (player position look)
addTranslator(0x06, 0x0D, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x0A CS 0x12 (player animation)
addTranslator(0x0A, 0x12, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x0D CS 0x65 (window close)
addTranslator(0x0D, 0x65, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x0E CS 0x66 (click window)
addTranslator(0x0E, 0x66, ProtocolState.PLAY, PacketDirection.TO_SERVER);
// 0x0B CS 0x13 (entity action)
addTranslator(0x0B, 0x13, ProtocolState.PLAY, PacketDirection.TO_SERVER);
}
use of com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack in project DirtMultiversion by DirtPowered.
the class ProtocolRelease73To61 method registerTranslators.
@Override
public void registerTranslators() {
// login
addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
session.getUserData().setEntityId(entityId);
EntityTracker tracker = new EntityTracker();
tracker.addEntity(entityId, EntityType.HUMAN);
session.getUserData().getProtocolStorage().set(EntityTracker.class, tracker);
// send entity attributes (fixes fast movement)
session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
return cancel();
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = session.getUserData().getEntityId();
session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
return cancel();
}
});
// handshake
addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 61), data.read(1), data.read(2), data.read(3) });
}
});
// kick disconnect
addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
if (session.getUserData().getPreNettyProtocolState() != PreNettyProtocolState.STATUS)
return data;
String reason = data.read(Type.STRING, 0);
ServerMotd pingMessage = ServerMotd.deserialize(reason);
pingMessage.setVersionName("1.6.1");
pingMessage.setProtocol(session.getUserData().getClientVersion().getRegistryId());
return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, ServerMotd.serialize(pingMessage)) });
}
});
// chat
addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String message = data.read(Type.STRING, 0);
return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, ChatUtils.legacyToJsonString(message)) });
}
});
// update health
addTranslator(0x08, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.FLOAT, data.read(Type.SHORT, 0).floatValue()), data.read(1), data.read(2) });
}
});
// mob spawn
addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
tracker.addEntity(entityId, entityType);
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 11);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
return PacketUtil.createPacket(0x18, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), data.read(10), set(Type.V1_4R_METADATA, newMeta) });
}
});
// entity destroy
addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int[] entities = data.read(Type.BYTE_INT_ARRAY, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
for (int entityId : entities) {
tracker.removeEntity(entityId);
}
return data;
}
});
// entity metadata
addTranslator(0x28, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
EntityType entityType = tracker.getEntityById(entityId);
if (entityType == null) {
Logger.warn("[{}] skipping translating metadata for {}. Entity is not tracked", session.getLogTag(), entityId);
return cancel();
}
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 1);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.V1_4R_METADATA, newMeta) });
}
});
// name entity spawn
addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
tracker.addEntity(entityId, EntityType.HUMAN);
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 8);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(EntityType.HUMAN, oldMeta);
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), set(Type.V1_4R_METADATA, newMeta) });
}
});
// vehicle spawn
addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
byte vehicleEntity = data.read(Type.BYTE, 1);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
switch(vehicleEntity) {
case 0x02:
/* item */
tracker.addEntity(entityId, EntityType.ITEM);
break;
case 0x46:
/* sand, gravel */
tracker.addEntity(entityId, EntityType.FALLING_OBJECT);
break;
}
return data;
}
});
// statistics
addTranslator(0xC8, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xC8, new TypeHolder[] { data.read(0), set(Type.INT, data.read(Type.BYTE, 1).intValue()) });
}
});
// level sound
addTranslator(0x3E, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String soundName = data.read(Type.STRING, 0);
String newSoundName = soundRemapper.getNewSoundName(soundName);
if (newSoundName.isEmpty())
return cancel();
return PacketUtil.createPacket(0x3E, new TypeHolder[] { set(Type.STRING, newSoundName), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5) });
}
});
// player abilities
addTranslator(0xCA, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 1) * 255F)), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 2) * 255F)) });
}
});
// player abilities
addTranslator(0xCA, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.FLOAT, (data.read(Type.BYTE, 1) / 255F)), set(Type.FLOAT, (data.read(Type.BYTE, 2) / 255F)) });
}
});
// entity action
addTranslator(0x13, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
// client -> server
return PacketUtil.createPacket(0x13, new TypeHolder[] { data.read(0), data.read(1) });
}
});
// entity attach
addTranslator(0x27, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
int vehicleId = data.read(Type.INT, 1);
if (vehicleId != -1) {
vehicleTracker.setVehicleId(vehicleId);
}
return PacketUtil.createPacket(0x27, new TypeHolder[] { data.read(0), data.read(1), // leash
set(Type.BOOLEAN, false) });
}
});
// creative item get
addTranslator(0x6B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_3R_ITEM, 1);
if (item != null && !creativeTab.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
}
});
// steer vehicle
addTranslator(0x1B, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
boolean dismount = data.read(Type.BOOLEAN, 3);
if (dismount && vehicleTracker.getVehicleId() != -999) {
int cachedId = vehicleTracker.getVehicleId();
vehicleTracker.setVehicleId(-999);
return PacketUtil.createPacket(0x07, new TypeHolder[] { set(Type.INT, session.getUserData().getEntityId()), set(Type.INT, cachedId), set(Type.BYTE, (byte) 0) });
}
// packet doesn't exist in 1.5
return cancel();
}
});
}
Aggregations