use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method registerPackets.
@Override
protected void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_13.COOLDOWN, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int itemId = wrapper.read(Type.VAR_INT);
int oldId = protocol.getMappingData().getItemMappings().get(itemId);
if (oldId != -1) {
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) {
itemId = 383 << 16;
} else {
itemId = (oldId >> 4) << 16 | oldId & 0xF;
}
}
wrapper.write(Type.VAR_INT, itemId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.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 {
int blockId = wrapper.get(Type.VAR_INT, 0);
if (blockId == 73)
blockId = 25;
else if (blockId == 99)
blockId = 33;
else if (blockId == 92)
blockId = 29;
else if (blockId == 142)
blockId = 54;
else if (blockId == 305)
blockId = 146;
else if (blockId == 249)
blockId = 130;
else if (blockId == 257)
blockId = 138;
else if (blockId == 140)
blockId = 52;
else if (blockId == 472)
blockId = 209;
else if (blockId >= 483 && blockId <= 498)
blockId = blockId - 483 + 219;
wrapper.set(Type.VAR_INT, 0, blockId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
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 {
BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
// TODO conduit handling
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 5) {
wrapper.cancel();
}
wrapper.set(Type.NBT, 0, provider.transform(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0)));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int chunkMinX = wrapper.passthrough(Type.INT) << 4;
int chunkMinZ = wrapper.passthrough(Type.INT) << 4;
int chunkMaxX = chunkMinX + 15;
int chunkMaxZ = chunkMinZ + 15;
BackwardsBlockStorage blockStorage = wrapper.user().get(BackwardsBlockStorage.class);
blockStorage.getBlocks().entrySet().removeIf(entry -> {
Position position = entry.getKey();
return position.getX() >= chunkMinX && position.getZ() >= chunkMinZ && position.getX() <= chunkMaxX && position.getZ() <= chunkMaxZ;
});
}
});
}
});
// Block Change
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
map(Type.POSITION);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int blockState = wrapper.read(Type.VAR_INT);
Position position = wrapper.get(Type.POSITION, 0);
// Store blocks
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
storage.checkAndStore(position, blockState);
wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockState));
// Flower pot special treatment
flowerPotSpecialTreatment(wrapper.user(), blockState, position);
}
});
}
});
// Multi Block Change
protocol.registerClientbound(ClientboundPackets1_13.MULTI_BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Chunk X
map(Type.INT);
// 1 - Chunk Z
map(Type.INT);
map(Type.BLOCK_CHANGE_RECORD_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
int chunkX = wrapper.get(Type.INT, 0);
int chunkZ = wrapper.get(Type.INT, 1);
int block = record.getBlockId();
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
// Store if needed
storage.checkAndStore(position, block);
// Flower pot special treatment
flowerPotSpecialTreatment(wrapper.user(), block, position);
// Change to old id
record.setBlockId(protocol.getMappingData().getNewBlockStateId(block));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY);
handler(itemArrayHandler(Type.ITEM_ARRAY));
}
});
protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.SHORT);
map(Type.FLAT_ITEM, Type.ITEM);
handler(itemToClientHandler(Type.ITEM));
}
});
protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk1_9_3_4Type type_old = new Chunk1_9_3_4Type(clientWorld);
Chunk1_13Type type = new Chunk1_13Type(clientWorld);
Chunk chunk = wrapper.read(type);
// Handle Block Entities before block rewrite
BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
for (CompoundTag tag : chunk.getBlockEntities()) {
Tag idTag = tag.get("id");
if (idTag == null)
continue;
String id = (String) idTag.getValue();
// Ignore if we don't handle it
if (!provider.isHandled(id))
continue;
int sectionIndex = ((NumberTag) tag.get("y")).asInt() >> 4;
if (sectionIndex < 0 || sectionIndex > 15) {
// 1.17 chunks
continue;
}
ChunkSection section = chunk.getSections()[sectionIndex];
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);
int block = section.getFlatBlock(x & 0xF, y & 0xF, z & 0xF);
storage.checkAndStore(position, block);
provider.transform(wrapper.user(), position, tag);
}
// Rewrite new blocks to old blocks
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null) {
continue;
}
// Flower pots require a special treatment, they are no longer block entities :(
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);
// Check if the block is a flower
if (FlowerPotHandler.isFlowah(block)) {
Position pos = new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4)));
// Store block
storage.checkAndStore(pos, block);
CompoundTag nbt = provider.transform(wrapper.user(), pos, "minecraft:flower_pot");
chunk.getBlockEntities().add(nbt);
}
}
}
}
for (int p = 0; p < section.getPaletteSize(); p++) {
int old = section.getPaletteEntry(p);
if (old != 0) {
int oldId = protocol.getMappingData().getNewBlockStateId(old);
section.setPaletteEntry(p, oldId);
}
}
}
if (chunk.isBiomeData()) {
for (int i = 0; i < 256; i++) {
int biome = chunk.getBiomeData()[i];
int newId = -1;
switch(biome) {
// end biomes
case 40:
case 41:
case 42:
case 43:
newId = 9;
break;
// deep ocean biomes
case 47:
case 48:
case 49:
newId = 24;
break;
case // deep frozen... let's just pick the frozen variant
50:
newId = 10;
break;
// the other new ocean biomes
case 44:
case 45:
case 46:
newId = 0;
break;
}
if (newId != -1) {
chunk.getBiomeData()[i] = newId;
}
}
}
wrapper.write(type_old, chunk);
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect Id
map(Type.INT);
// Location
map(Type.POSITION);
// Data
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) {
// Play record
wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4);
} else if (id == 2001) {
// Block break + block break sound
data = protocol.getMappingData().getNewBlockStateId(data);
int blockId = data >> 4;
int blockData = data & 0xF;
wrapper.set(Type.INT, 1, (blockId & 0xFFF) | (blockData << 12));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int iconCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < iconCount; i++) {
int type = wrapper.read(Type.VAR_INT);
byte x = wrapper.read(Type.BYTE);
byte z = wrapper.read(Type.BYTE);
byte direction = wrapper.read(Type.BYTE);
if (wrapper.read(Type.BOOLEAN)) {
wrapper.read(Type.COMPONENT);
}
if (type > 9) {
wrapper.set(Type.VAR_INT, 1, wrapper.get(Type.VAR_INT, 1) - 1);
continue;
}
wrapper.write(Type.BYTE, (byte) ((type << 4) | (direction & 0x0F)));
wrapper.write(Type.BYTE, x);
wrapper.write(Type.BYTE, z);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.VAR_INT);
map(Type.FLAT_ITEM, Type.ITEM);
handler(itemToClientHandler(Type.ITEM));
}
});
protocol.registerClientbound(ClientboundPackets1_13.WINDOW_PROPERTY, new PacketRemapper() {
@Override
public void registerMap() {
// Window Id
map(Type.UNSIGNED_BYTE);
// Property
map(Type.SHORT);
// Value
map(Type.SHORT);
handler(wrapper -> {
short property = wrapper.get(Type.SHORT, 0);
// Enchantment table
if (property >= 4 && property <= 6) {
short oldId = wrapper.get(Type.SHORT, 1);
wrapper.set(Type.SHORT, 1, (short) protocol.getMappingData().getEnchantmentMappings().getNewId(oldId));
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_12_1.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.SHORT);
map(Type.ITEM, Type.FLAT_ITEM);
handler(itemToServerHandler(Type.FLAT_ITEM));
}
});
protocol.registerServerbound(ServerboundPackets1_12_1.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.SHORT);
map(Type.BYTE);
map(Type.SHORT);
map(Type.VAR_INT);
map(Type.ITEM, Type.FLAT_ITEM);
handler(itemToServerHandler(Type.FLAT_ITEM));
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project Geyser by GeyserMC.
the class GeyserSpigot1_12NativeWorldManager method getBlockAt.
@Override
public int getBlockAt(GeyserSession session, int x, int y, int z) {
Player player = Bukkit.getPlayer(session.getPlayerEntity().getUsername());
if (player == null) {
return BlockStateValues.JAVA_AIR_ID;
}
// Get block entity storage
BlockStorage storage = Via.getManager().getConnectionManager().getConnectedClient(player.getUniqueId()).get(BlockStorage.class);
int blockId = adapter.getBlockAt(player.getWorld(), x, y, z);
return getLegacyBlock(storage, blockId, x, y, z);
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class ProtocolRelease39To29 method onConnect.
@Override
public void onConnect(ServerSession session) {
ProtocolStorage storage = session.getStorage();
storage.set(UpdateTask.class, new UpdateTask(session));
storage.set(OpenChestTracker.class, new OpenChestTracker());
if (!storage.hasObject(BlockStorage.class)) {
storage.set(BlockStorage.class, new BlockStorage(MinecraftVersion.R1_2_4));
}
if (!storage.hasObject(EntityTracker.class)) {
storage.set(EntityTracker.class, new EntityTracker());
}
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class WorldPackets method registerTranslators.
@Override
public void registerTranslators() {
// block change
addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int x = data.read(Type.INT, 0);
byte y = data.read(Type.BYTE, 1);
int z = data.read(Type.INT, 2);
int chunkX = x >> 4;
int chunkZ = z >> 4;
byte blockId = data.read(Type.BYTE, 3);
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
}
}
return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.SHORT, data.read(Type.BYTE, 3).shortValue()), data.read(4) });
}
});
// multi block change
addTranslator(0x34, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
V1_2MultiBlockArray blocks = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
DataInput dis = new DataInputStream(new ByteArrayInputStream(blocks.getData()));
for (int i = 0; i < blocks.getRecordCount(); i++) {
try {
short pos = dis.readShort();
int x = pos >> 12 & 15;
int y = pos & 255;
int z = pos >> 8 & 15;
int xPos = x + (chunkX << 4);
int zPos = z + (chunkZ << 4);
int blockId = dis.readShort() >> 4 & 4095;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
});
// chunk data
addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
V1_2Chunk chunk = data.read(Type.V1_2_CHUNK, 0);
if (blockStorage.getVersion() == getTo()) {
List<ExtendedBlockStorage> parts = new ArrayList<>();
V1_2RChunkStorage storage;
if (chunk.getStorage() != null) {
storage = chunk.getStorage();
} else {
// create chunk storage
storage = new V1_2RChunkStorage(true, true, chunk.getChunkX(), chunk.getChunkZ());
storage.readChunk(chunk.isGroundUp(), chunk.getPrimaryBitmap(), chunk.getUncompressedData());
// cache for later use (r1.8 -> r1.7)
chunk.setStorage(storage);
}
ExtendedBlockStorage[] columns = storage.getColumnStorage();
for (int i = 0; i < columns.length; ++i) {
ExtendedBlockStorage e = columns[i];
boolean f = e != null && !columns[i].isEmpty();
if (e != null && (chunk.getPrimaryBitmap() & 1 << i) != 0 && (!chunk.isGroundUp() || f)) {
parts.add(e);
}
}
for (int i = 0; i < parts.size(); ++i) {
byte[] blockArray = parts.get(i).getBlockLSBArray();
for (int j = 0; j < blockArray.length; ++j) {
int x = j & 15;
int y = (j >> 8) + i * 16 & 255;
int z = j >> 4 & 15;
int blockId = blockArray[j] & 255;
if (shouldCache(blockId)) {
blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
}
}
}
}
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
}
});
// pre chunk
addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
byte mode = data.read(Type.BYTE, 2);
if (mode == 1) {
return cancel();
}
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
for (BlockLocation entry : chestTracker.getChestStates().keySet()) {
int x = entry.getX() >> 4;
int z = entry.getZ() >> 4;
if (chunkX == x && chunkZ == z) {
chestTracker.remove(entry);
}
}
BlockStorage blockStorage = storage.get(BlockStorage.class);
if (blockStorage.getVersion() == getTo()) {
blockStorage.removeChunk(chunkX, chunkZ);
}
V1_2Chunk chunk = new V1_2Chunk(chunkX, chunkZ, true, (short) 0, (short) 0, 0, new byte[0], new byte[0], null);
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
}
});
// play noteblock
addTranslator(0x36, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
int y = data.read(Type.SHORT, 1);
int z = data.read(Type.INT, 2);
byte type = data.read(Type.BYTE, 3);
byte pitch = data.read(Type.BYTE, 4);
ProtocolStorage storage = session.getStorage();
short blockId = (short) storage.get(BlockStorage.class).getBlockAt(x, y, z);
WorldSound worldSound;
OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
switch(type) {
case 0:
if (blockId == 33 || blockId == 29) {
worldSound = WorldSound.PISTON_OUT;
} else {
worldSound = WorldSound.NOTE_HARP;
}
break;
case 1:
if (blockId == 54) {
BlockLocation loc = new BlockLocation(x, y, z);
if (pitch == 1) {
if (!chestTracker.getState(loc)) {
chestTracker.setOpen(loc, true);
worldSound = WorldSound.CHEST_OPEN;
pitch = 12;
} else {
worldSound = WorldSound.NO_SOUND;
}
} else {
if (chestTracker.getState(loc)) {
worldSound = WorldSound.CHEST_CLOSE;
chestTracker.setOpen(loc, false);
} else {
worldSound = WorldSound.NO_SOUND;
}
}
} else if (blockId == 33 || blockId == 29) {
worldSound = WorldSound.PISTON_IN;
} else {
worldSound = WorldSound.NOTE_CLICK;
}
break;
case 2:
worldSound = WorldSound.NOTE_SNARE;
break;
case 3:
worldSound = WorldSound.NOTE_HAT;
break;
case 4:
worldSound = WorldSound.NOTE_BASS_ATTACK;
break;
default:
worldSound = WorldSound.NOTE_HARP;
break;
}
float correctedPitch = (float) (0.5f * (Math.pow(2, pitch / 12.0f)));
WorldEntityEvent.playSoundAt(session, new Location(x, y, z), worldSound, 3.0f, correctedPitch);
return PacketUtil.createPacket(0x36, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.SHORT, blockId) });
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage in project DirtMultiversion by DirtPowered.
the class ProtocolBeta17to14 method registerTranslators.
@Override
public void registerTranslators() {
// keep-alive
addTranslator(0x00, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x00, new TypeHolder[0]);
}
});
// ping request
addTranslator(0xFE, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String message = session.getMain().getConfiguration().preReleaseMOTD();
message = ChatUtils.stripColor(message);
int max = session.getMain().getConfiguration().getMaxOnline();
int online = session.getConnectionCount();
PacketData packetData = PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, message + "§" + online + "§" + max) });
// I wanna know who broke server latency calculation in release 1.8, really
if (session.getUserData().getClientVersion() == MinecraftVersion.R1_8) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
session.sendPacket(packetData, PacketDirection.TO_CLIENT, getFrom());
}
}, session.getMain().getSharedRandom().nextInt(70));
} else {
session.sendPacket(packetData, PacketDirection.TO_CLIENT, getFrom());
}
// cancel sending
return cancel();
}
});
// login
addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { // INT
set(Type.INT, 14), // STRING
data.read(1), // LONG
data.read(2), // BYTE
data.read(4) });
}
});
// login
addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
session.getMain().getSessionRegistry().getSessions().forEach((uuid, multiSession) -> {
String s = multiSession.getServerSession().getUserData().getUsername();
session.queuePacket(createTabEntryPacket(s, true), PacketDirection.TO_CLIENT, getFrom());
});
int max = session.getMain().getConfiguration().getMaxOnline();
// b1.8 client is rendering tablist grid wrong when above 100
if (max > 100)
max = 100;
return PacketUtil.createPacket(0x01, new TypeHolder[] { // INT - entityId
data.read(0), // STRING - empty
data.read(1), // LONG - world seed
data.read(2), // INT - gameMode
set(Type.INT, 0), // BYTE - dimension
data.read(3), // BYTE - difficulty
set(Type.BYTE, (byte) 1), // BYTE - world height
set(Type.BYTE, (byte) -128), // BYTE - maxPlayers
set(Type.BYTE, (byte) max) });
}
});
// update health
addTranslator(0x08, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(PlayerHealthTracker.class)) {
storage.get(PlayerHealthTracker.class).setHealth(data.read(Type.SHORT, 0));
}
return PacketUtil.createPacket(0x08, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 6), set(Type.FLOAT, 0.0F) });
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0) });
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 1), set(Type.BYTE, (byte) 0), set(Type.SHORT, (short) 128), set(Type.LONG, 0L) });
}
});
// open window
addTranslator(0x64, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x64, new TypeHolder[] { data.read(0), data.read(1), set(Type.STRING, data.read(Type.STRING, 2)), data.read(3) });
}
});
// game state
addTranslator(0x46, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x46, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 0) });
}
});
// block place
addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(PlayerHealthTracker.class)) {
PacketData updateHealth = PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.SHORT, storage.get(PlayerHealthTracker.class).getHealth()), set(Type.SHORT, (short) 6), set(Type.FLOAT, 0.0F) });
session.sendPacket(updateHealth, PacketDirection.TO_CLIENT, getFrom());
}
return data;
}
});
// entity action
addTranslator(0x13, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
byte state = data.read(Type.BYTE, 1);
if (state == 5 || state == 4) {
// cancel sending
return cancel();
}
return data;
}
});
// named entity spawn
addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
String username = data.read(Type.STRING, 1);
if (!isConnectedThroughProxy(session.getMain(), username)) {
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
if (cache != null) {
session.sendPacket(createTabEntryPacket(username, true), PacketDirection.TO_CLIENT, getFrom());
cache.getTabPlayers().put(entityId, username);
}
}
return data;
}
});
// entity destroy
addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
if (cache != null && cache.getTabPlayers().containsKey(entityId)) {
String username = cache.getTabPlayers().get(entityId);
session.sendPacket(createTabEntryPacket(username, false), PacketDirection.TO_CLIENT, getFrom());
cache.getTabPlayers().remove(entityId);
}
return data;
}
});
// mob spawn
addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
byte entityType = data.read(Type.BYTE, 1);
if (entityType == EntityType.HUMAN_MOB.getEntityTypeId()) {
PlayerTabListCache cache = session.getStorage().get(PlayerTabListCache.class);
// cache empty name, so the tab entry will be removed after killing human mob
cache.getTabPlayers().put(data.read(Type.INT, 0), StringUtil.EMPTY_STRING);
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), set(Type.STRING, StringUtil.EMPTY_STRING), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), set(Type.SHORT, (short) 0) });
}
return data;
}
});
// block change
addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
byte y = data.read(Type.BYTE, 1);
int z = data.read(Type.INT, 2);
byte blockId = data.read(Type.BYTE, 3);
byte blockData = data.read(Type.BYTE, 4);
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage != null) {
blockStorage.setBlockAt(x >> 4, z >> 4, x, y, z, blockId);
if (blockId == 54) {
blockData = RotationUtil.fixBlockRotation(session, x, y, z);
}
}
return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.BYTE, blockData) });
}
});
// unload chunk
addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
if (blockStorage != null) {
byte mode = data.read(Type.BYTE, 2);
if (mode == 0) {
int chunkX = data.read(Type.INT, 0);
int chunkZ = data.read(Type.INT, 1);
blockStorage.removeChunk(chunkX, chunkZ);
}
}
return data;
}
});
// chunk data
addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_3BChunk chunk = data.read(Type.V1_3B_CHUNK, 0);
int chunkX = chunk.getX() >> 4;
int chunkZ = chunk.getZ() >> 4;
// skip non-full chunk updates
if (chunk.getXSize() * chunk.getYSize() * chunk.getZSize() != 32768) {
return data;
}
BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
boolean reduceBlockStorageMemory = session.getMain().getConfiguration().reduceBlockStorageMemory();
if (blockStorage != null) {
List<BlockLocation> locationList = new ArrayList<>();
try {
byte[] chunkData = chunk.getChunk();
for (int x = 0; x < 16; x++) {
for (int y = reduceBlockStorageMemory ? 20 : 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
int blockId = chunkData[getBlockIndexAt(x, y, z)];
if (SolidBlockList.isSolid(blockId) || HardnessTable.needsToBeCached(session, blockId) || blockId == 85 || blockId == 29 || blockId == 33) {
if (blockId == 54) {
locationList.add(new BlockLocation(x, y, z));
}
blockStorage.setBlockAt(chunkX, chunkZ, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z, blockId);
}
}
}
}
for (BlockLocation location : locationList) {
int x = location.getX();
int y = location.getY();
int z = location.getZ();
byte rotation = RotationUtil.fixBlockRotation(session, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z);
int blockLightOffset = 65536;
setNibble(chunkData, x, y, z, (byte) 15, blockLightOffset);
sendDelayedBlockUpdate(session, chunk.getX() + x, chunk.getY() + y, chunk.getZ() + z, rotation);
}
chunk.setChunk(chunkData);
} catch (ArrayIndexOutOfBoundsException ignored) {
}
}
return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3B_CHUNK, chunk) });
}
});
}
Aggregations