use of com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity in project ViaVersion by ViaVersion.
the class Chunk1_18Type method write.
@Override
public void write(final ByteBuf buffer, final Chunk chunk) throws Exception {
buffer.writeInt(chunk.getX());
buffer.writeInt(chunk.getZ());
Type.NBT.write(buffer, chunk.getHeightMap());
final ByteBuf sectionBuffer = buffer.alloc().buffer();
try {
for (final ChunkSection section : chunk.getSections()) {
sectionType.write(sectionBuffer, section);
}
sectionBuffer.readerIndex(0);
Type.VAR_INT.writePrimitive(buffer, sectionBuffer.readableBytes());
buffer.writeBytes(sectionBuffer);
} finally {
// release buffer
sectionBuffer.release();
}
Type.VAR_INT.writePrimitive(buffer, chunk.blockEntities().size());
for (final BlockEntity blockEntity : chunk.blockEntities()) {
Types1_18.BLOCK_ENTITY.write(buffer, blockEntity);
}
}
use of com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity in project ViaVersion by ViaVersion.
the class Chunk1_18Type method read.
@Override
public Chunk read(final ByteBuf buffer) throws Exception {
final int chunkX = buffer.readInt();
final int chunkZ = buffer.readInt();
final CompoundTag heightMap = Type.NBT.read(buffer);
// Read sections
final ByteBuf sectionsBuf = buffer.readBytes(Type.VAR_INT.readPrimitive(buffer));
final ChunkSection[] sections = new ChunkSection[ySectionCount];
try {
for (int i = 0; i < ySectionCount; i++) {
sections[i] = sectionType.read(sectionsBuf);
}
} finally {
if (sectionsBuf.readableBytes() > 0 && Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Found " + sectionsBuf.readableBytes() + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ);
}
sectionsBuf.release();
}
final int blockEntitiesLength = Type.VAR_INT.readPrimitive(buffer);
final List<BlockEntity> blockEntities = new ArrayList<>(blockEntitiesLength);
for (int i = 0; i < blockEntitiesLength; i++) {
blockEntities.add(Types1_18.BLOCK_ENTITY.read(buffer));
}
return new Chunk1_18(chunkX, chunkZ, sections, heightMap, blockEntities);
}
use of com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_18 method registerPackets.
@Override
protected void registerPackets() {
new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_18.DECLARE_RECIPES);
registerSetCooldown(ClientboundPackets1_18.COOLDOWN);
registerWindowItems1_17_1(ClientboundPackets1_18.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, Type.FLAT_VAR_INT_ITEM);
registerSetSlot1_17_1(ClientboundPackets1_18.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipmentArray(ClientboundPackets1_18.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerTradeList(ClientboundPackets1_18.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_18.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
registerClickWindow1_17_1(ServerboundPackets1_17.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_18.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect id
map(Type.INT);
// Location
map(Type.POSITION1_14);
// Data
map(Type.INT);
handler(wrapper -> {
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().getNewItemId(data));
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_18.SPAWN_PARTICLE, new PacketRemapper() {
@Override
public void registerMap() {
// Particle id
map(Type.INT);
// Override limiter
map(Type.BOOLEAN);
// X
map(Type.DOUBLE);
// Y
map(Type.DOUBLE);
// Z
map(Type.DOUBLE);
// Offset X
map(Type.FLOAT);
// Offset Y
map(Type.FLOAT);
// Offset Z
map(Type.FLOAT);
// Max speed
map(Type.FLOAT);
// Particle Count
map(Type.INT);
handler(wrapper -> {
int id = wrapper.get(Type.INT, 0);
if (id == 3) {
// Block marker
int blockState = wrapper.read(Type.VAR_INT);
if (blockState == 7786) {
// Light block
wrapper.set(Type.INT, 0, 3);
} else {
// Else assume barrier block
wrapper.set(Type.INT, 0, 2);
}
return;
}
ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
if (mappings.isBlockParticle(id)) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (mappings.isItemParticle(id)) {
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
int newId = protocol.getMappingData().getNewParticleId(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_18.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
handler(wrapper -> {
final int id = wrapper.read(Type.VAR_INT);
final CompoundTag tag = wrapper.read(Type.NBT);
final int mappedId = BlockEntityIds.mappedId(id);
if (mappedId == -1) {
wrapper.cancel();
return;
}
final String identifier = protocol.getMappingData().blockEntities().get(id);
if (identifier == null) {
wrapper.cancel();
return;
}
// The 1.18 server doesn't include the id and positions (x, y, z) in the NBT anymore
// If those were the only fields on the block entity (e.g.: skull, bed), we'll receive a null NBT
// We initialize one and add the missing fields, so it can be handled correctly down the line
final CompoundTag newTag = tag == null ? new CompoundTag() : tag;
final Position pos = wrapper.get(Type.POSITION1_14, 0);
// The protocol converters downstream rely on this field, let's add it back
newTag.put("id", new StringTag("minecraft:" + identifier));
// Weird glitches happen with the 1.17 client and below if these fields are missing
// Some examples are block entity models becoming invisible (e.g.: signs, banners)
newTag.put("x", new IntTag(pos.x()));
newTag.put("y", new IntTag(pos.y()));
newTag.put("z", new IntTag(pos.z()));
handleSpawner(id, newTag);
wrapper.write(Type.UNSIGNED_BYTE, (short) mappedId);
wrapper.write(Type.NBT, newTag);
});
}
});
protocol.registerClientbound(ClientboundPackets1_18.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().size()), MathUtil.ceilLog2(tracker.biomesSent()));
final Chunk oldChunk = wrapper.read(chunkType);
final ChunkSection[] sections = oldChunk.getSections();
final BitSet mask = new BitSet(oldChunk.getSections().length);
final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE];
int biomeIndex = 0;
for (int j = 0; j < sections.length; j++) {
final ChunkSection section = sections[j];
// Write biome palette into biome array
final DataPalette biomePalette = section.palette(PaletteType.BIOMES);
for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) {
biomeData[biomeIndex++] = biomePalette.idAt(i);
}
// Rewrite to empty section
if (section.getNonAirBlocksCount() == 0) {
sections[j] = null;
} else {
mask.set(j);
}
}
final List<CompoundTag> blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size());
for (final BlockEntity blockEntity : oldChunk.blockEntities()) {
final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId());
if (id == null) {
// Shrug
continue;
}
final CompoundTag tag;
if (blockEntity.tag() != null) {
tag = blockEntity.tag();
handleSpawner(blockEntity.typeId(), tag);
} else {
tag = new CompoundTag();
}
blockEntityTags.add(tag);
tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX()));
tag.put("y", new IntTag(blockEntity.y()));
tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ()));
tag.put("id", new StringTag("minecraft:" + id));
}
final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags);
wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk);
// Create and send light packet first
final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT);
lightPacket.write(Type.VAR_INT, chunk.getX());
lightPacket.write(Type.VAR_INT, chunk.getZ());
// Trust edges
lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN));
// Sky light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Block light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Empty sky light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Empty block light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
final int skyLightLength = wrapper.read(Type.VAR_INT);
lightPacket.write(Type.VAR_INT, skyLightLength);
for (int i = 0; i < skyLightLength; i++) {
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
}
final int blockLightLength = wrapper.read(Type.VAR_INT);
lightPacket.write(Type.VAR_INT, blockLightLength);
for (int i = 0; i < blockLightLength; i++) {
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
}
lightPacket.send(Protocol1_17_1To1_18.class);
});
}
});
protocol.cancelClientbound(ClientboundPackets1_18.SET_SIMULATION_DISTANCE);
}
use of com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(final Protocol1_18To1_17_1 protocol) {
protocol.registerClientbound(ClientboundPackets1_17_1.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
handler(wrapper -> {
final short id = wrapper.read(Type.UNSIGNED_BYTE);
final int newId = BlockEntityIds.newId(id);
wrapper.write(Type.VAR_INT, newId);
handleSpawners(newId, wrapper.passthrough(Type.NBT));
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.UPDATE_LIGHT, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final int chunkX = wrapper.passthrough(Type.VAR_INT);
final int chunkZ = wrapper.passthrough(Type.VAR_INT);
if (wrapper.user().get(ChunkLightStorage.class).isLoaded(chunkX, chunkZ)) {
if (!Via.getConfig().cache1_17Light()) {
// Light packets updating already sent chunks are the same as before
return;
}
// Pass through and cache light data
} else {
// Cancel and cache the light data
wrapper.cancel();
}
final boolean trustEdges = wrapper.passthrough(Type.BOOLEAN);
final long[] skyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] blockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] emptySkyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] emptyBlockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final int skyLightLenght = wrapper.passthrough(Type.VAR_INT);
final byte[][] skyLight = new byte[skyLightLenght][];
for (int i = 0; i < skyLightLenght; i++) {
skyLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
}
final int blockLightLength = wrapper.passthrough(Type.VAR_INT);
final byte[][] blockLight = new byte[blockLightLength][];
for (int i = 0; i < blockLightLength; i++) {
blockLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
}
final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
lightStorage.storeLight(chunkX, chunkZ, new ChunkLightStorage.ChunkLight(trustEdges, skyLightMask, blockLightMask, emptySkyLightMask, emptyBlockLightMask, skyLight, blockLight));
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
final Chunk oldChunk = wrapper.read(new Chunk1_17Type(tracker.currentWorldSectionHeight()));
final List<BlockEntity> blockEntities = new ArrayList<>(oldChunk.getBlockEntities().size());
for (final CompoundTag tag : oldChunk.getBlockEntities()) {
final NumberTag xTag = tag.get("x");
final NumberTag yTag = tag.get("y");
final NumberTag zTag = tag.get("z");
final StringTag idTag = tag.get("id");
if (xTag == null || yTag == null || zTag == null || idTag == null) {
continue;
}
final String id = idTag.getValue();
final int typeId = protocol.getMappingData().blockEntityIds().getInt(id.replace("minecraft:", ""));
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Unknown block entity: " + id);
}
handleSpawners(typeId, tag);
final byte packedXZ = (byte) ((xTag.asInt() & 15) << 4 | (zTag.asInt() & 15));
blockEntities.add(new BlockEntityImpl(packedXZ, yTag.asShort(), typeId, tag));
}
final int[] biomeData = oldChunk.getBiomeData();
final ChunkSection[] sections = oldChunk.getSections();
for (int i = 0; i < sections.length; i++) {
ChunkSection section = sections[i];
if (section == null) {
// There's no section mask anymore
section = new ChunkSectionImpl();
sections[i] = section;
section.setNonAirBlocksCount(0);
final DataPaletteImpl blockPalette = new DataPaletteImpl(ChunkSection.SIZE);
blockPalette.addId(0);
section.addPalette(PaletteType.BLOCKS, blockPalette);
}
// Fill biome palette
final DataPaletteImpl biomePalette = new DataPaletteImpl(ChunkSection.BIOME_SIZE);
section.addPalette(PaletteType.BIOMES, biomePalette);
final int offset = i * ChunkSection.BIOME_SIZE;
for (int biomeIndex = 0, biomeArrayIndex = offset; biomeIndex < ChunkSection.BIOME_SIZE; biomeIndex++, biomeArrayIndex++) {
// Also catch invalid biomes with id -1
final int biome = biomeData[biomeArrayIndex];
biomePalette.setIdAt(biomeIndex, biome != -1 ? biome : 0);
}
}
final Chunk chunk = new Chunk1_18(oldChunk.getX(), oldChunk.getZ(), sections, oldChunk.getHeightMap(), blockEntities);
wrapper.write(new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), MathUtil.ceilLog2(tracker.biomesSent())), chunk);
final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
final boolean alreadyLoaded = !lightStorage.addLoadedChunk(chunk.getX(), chunk.getZ());
// Append light data to chunk packet
final ChunkLightStorage.ChunkLight light = Via.getConfig().cache1_17Light() ? lightStorage.getLight(chunk.getX(), chunk.getZ()) : lightStorage.removeLight(chunk.getX(), chunk.getZ());
if (light == null) {
Via.getPlatform().getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded);
final BitSet emptyLightMask = new BitSet();
emptyLightMask.set(0, tracker.currentWorldSectionHeight() + 2);
wrapper.write(Type.BOOLEAN, false);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
} else {
wrapper.write(Type.BOOLEAN, light.trustEdges());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.skyLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.blockLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptySkyLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptyBlockLightMask());
wrapper.write(Type.VAR_INT, light.skyLight().length);
for (final byte[] skyLight : light.skyLight()) {
wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, skyLight);
}
wrapper.write(Type.VAR_INT, light.blockLight().length);
for (final byte[] blockLight : light.blockLight()) {
wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, blockLight);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final int chunkX = wrapper.passthrough(Type.INT);
final int chunkZ = wrapper.passthrough(Type.INT);
wrapper.user().get(ChunkLightStorage.class).clear(chunkX, chunkZ);
});
}
});
}
Aggregations