Updated Via API usage

* Update chunk system to work with latest Via again

* Move Chunk type instances into protocol versioned type classes

* Deleted unused type instances and cleaned up some protocols
This commit is contained in:
EnZaXD 2023-10-20 12:25:07 +02:00 committed by GitHub
parent 6d2ae22ff9
commit 8828e45afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 104 additions and 120 deletions

View File

@ -19,11 +19,9 @@ package net.raphimc.vialegacy.protocols.beta.protocol1_0_0_1tob1_8_0_1.types;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ItemArrayType;
public class Typesb1_8_0_1 {
public static final Type<Item> CREATIVE_ITEM = new NbtLessItemType();
public static final Type<Item[]> CREATIVE_ITEM_ARRAY = new ItemArrayType<>(CREATIVE_ITEM);
}

View File

@ -26,7 +26,7 @@ import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.BlockList1_6;
import net.raphimc.vialegacy.api.model.ChunkCoord;
@ -220,8 +220,8 @@ public class Protocolc0_30toc0_30cpe extends StatelessProtocol<ClientboundPacket
final ClassicLevel level = levelStorage.getClassicLevel();
final int count = wrapper.read(Type.UNSIGNED_BYTE) + 1; // count
final byte[] indices = wrapper.read(new CustomByteType(1024)); // indices
final byte[] blocks = wrapper.read(new CustomByteType(256)); // blocks
final byte[] indices = wrapper.read(new FixedByteArrayType(1024)); // indices
final byte[] blocks = wrapper.read(new FixedByteArrayType(256)); // blocks
if (wrapper.user().getProtocolInfo().getPipeline().contains(Protocola1_0_16_2toa1_0_15.class)) {
final Map<ChunkCoord, List<BlockChangeRecord>> records = new HashMap<>();

View File

@ -20,8 +20,7 @@ package net.raphimc.vialegacy.protocols.release.protocol1_2_1_3to1_1.types;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.protocols.release.protocol1_2_1_3to1_1.chunks.NibbleArray1_1;
@ -38,11 +37,6 @@ public class ChunkType1_1 extends Type<Chunk> {
super(Chunk.class);
}
@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkType.class;
}
@Override
public Chunk read(ByteBuf byteBuf) throws Exception {
final int xPosition = byteBuf.readInt();
@ -52,7 +46,7 @@ public class ChunkType1_1 extends Type<Chunk> {
final int ySize = byteBuf.readUnsignedByte() + 1;
final int zSize = byteBuf.readUnsignedByte() + 1;
final int chunkSize = byteBuf.readInt();
final byte[] compressedData = new CustomByteType(chunkSize).read(byteBuf);
final byte[] compressedData = new FixedByteArrayType(chunkSize).read(byteBuf);
final byte[] uncompressedData = new byte[(xSize * ySize * zSize * 5) / 2];
final Inflater inflater = new Inflater();
inflater.setInput(compressedData);

View File

@ -459,7 +459,7 @@ public class Protocol1_3_1_2to1_2_4_5 extends StatelessProtocol<ClientboundPacke
if (!load) {
final Chunk chunk = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>());
wrapper.write(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)), chunk);
wrapper.write(Types1_7_6.getChunk(wrapper.user().get(ClientWorld.class).getEnvironment()), chunk);
} else {
wrapper.cancel();
}
@ -470,7 +470,7 @@ public class Protocol1_3_1_2to1_2_4_5 extends StatelessProtocol<ClientboundPacke
@Override
public void register() {
handler(wrapper -> {
final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment();
Chunk chunk = wrapper.read(Types1_2_4.CHUNK);
wrapper.user().get(ChestStateTracker.class).unload(chunk.getX(), chunk.getZ());
@ -481,7 +481,7 @@ public class Protocol1_3_1_2to1_2_4_5 extends StatelessProtocol<ClientboundPacke
for (int i = 0; i < chunk.getSections().length; i++) {
final ChunkSection section = chunk.getSections()[i] = new ChunkSectionImpl(true);
section.palette(PaletteType.BLOCKS).addId(0);
if (clientWorld.getEnvironment() == Environment.NORMAL) {
if (environment == Environment.NORMAL) {
final byte[] skyLight = new byte[2048];
Arrays.fill(skyLight, (byte) 255);
section.getLight().setSkyLight(skyLight);
@ -489,14 +489,14 @@ public class Protocol1_3_1_2to1_2_4_5 extends StatelessProtocol<ClientboundPacke
}
}
if (clientWorld.getEnvironment() != Environment.NORMAL) {
if (environment != Environment.NORMAL) {
for (ChunkSection section : chunk.getSections()) {
if (section != null) {
section.getLight().setSkyLight(null);
}
}
}
wrapper.write(new ChunkType1_7_6(clientWorld), chunk);
wrapper.write(Types1_7_6.getChunk(environment), chunk);
});
}
});

View File

@ -17,8 +17,6 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_3_1_2to1_2_4_5.types;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
import io.netty.buffer.ByteBuf;
@ -26,35 +24,29 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Chunk
public class ChunkType1_2_4 extends ChunkType1_7_6 {
private static final ClientWorld OVERWORLD = new ClientWorld(null);
static {
OVERWORLD.setEnvironment(Environment.NORMAL.id());
}
public ChunkType1_2_4() {
super(OVERWORLD);
super(true);
}
@Override
protected void readUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld) {
protected void readUnusedInt(ByteBuf byteBuf) {
byteBuf.readInt();
}
@Override
protected void writeUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) {
protected void writeUnusedInt(ByteBuf byteBuf, Chunk chunk) {
byteBuf.writeInt(0);
}
@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws Exception {
public void write(ByteBuf byteBuf, Chunk chunk) throws Exception {
for (ChunkSection section : chunk.getSections()) {
if (section != null && !section.getLight().hasSkyLight()) {
throw new IllegalStateException("Chunk section does not have skylight");
}
}
super.write(byteBuf, clientWorld, chunk);
super.write(byteBuf, chunk);
}
}

View File

@ -26,11 +26,8 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types
public class ItemType extends Type<Item> {
private final boolean compressed;
public ItemType(boolean compressed) {
public ItemType() {
super(Item.class);
this.compressed = compressed;
}
public Item read(ByteBuf buffer) throws Exception {
@ -43,7 +40,7 @@ public class ItemType extends Type<Item> {
item.setAmount(buffer.readByte());
item.setData(buffer.readShort());
if (NbtItemList.hasNbt(id)) {
item.setTag((this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).read(buffer));
item.setTag(Types1_7_6.COMPRESSED_NBT.read(buffer));
}
return item;
}
@ -57,7 +54,7 @@ public class ItemType extends Type<Item> {
buffer.writeByte(item.amount());
buffer.writeShort(item.data());
if (NbtItemList.hasNbt(item.identifier())) {
(this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).write(buffer, item.tag());
Types1_7_6.COMPRESSED_NBT.write(buffer, item.tag());
}
}
}

View File

@ -24,10 +24,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ItemA
public class Types1_2_4 {
public static final Type<Item> ITEM = new ItemType(false);
public static final Type<Item> COMPRESSED_NBT_ITEM = new ItemType(true);
public static final Type<Item[]> ITEM_ARRAY = new ItemArrayType<>(ITEM);
public static final Type<Item> COMPRESSED_NBT_ITEM = new ItemType();
public static final Type<Item[]> COMPRESSED_NBT_ITEM_ARRAY = new ItemArrayType<>(COMPRESSED_NBT_ITEM);
public static final Type<Chunk> CHUNK = new ChunkType1_2_4();

View File

@ -28,7 +28,6 @@ import java.util.List;
public class Types1_3_1 {
public static final Type<Item> NBTLESS_ITEM = new NbtLessItemType();
public static final Type<Item[]> NBTLESS_ITEM_ARRAY = new ItemArrayType<>(NBTLESS_ITEM);
public static final Type<Metadata> METADATA = new MetadataType();
public static final Type<List<Metadata>> METADATA_LIST = new MetaListType(METADATA);

View File

@ -20,6 +20,7 @@ package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5;
import com.google.common.collect.Lists;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
@ -30,13 +31,14 @@ import net.raphimc.vialegacy.api.protocol.StatelessProtocol;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.api.splitter.PreNettySplitter;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.rewriter.ItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.ChunkBulkType1_4_4;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.BulkChunkType1_4_4;
import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.Types1_4_4;
import net.raphimc.vialegacy.protocols.release.protocol1_5_0_1to1_4_6_7.ClientboundPackets1_4_6;
import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.ServerboundPackets1_5_2;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.MetaType1_6_4;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.Types1_6_4;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
public class Protocol1_4_6_7to1_4_4_5 extends StatelessProtocol<ClientboundPackets1_4_4, ClientboundPackets1_4_6, ServerboundPackets1_5_2, ServerboundPackets1_5_2> {
@ -127,10 +129,7 @@ public class Protocol1_4_6_7to1_4_4_5 extends StatelessProtocol<ClientboundPacke
this.registerClientbound(ClientboundPackets1_4_4.MAP_BULK_CHUNK, new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> {
final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
wrapper.write(new ChunkBulkType1_7_6(clientWorld), wrapper.read(new ChunkBulkType1_4_4(clientWorld)));
});
map(Types1_7_6.CHUNK_BULK, Types1_4_4.CHUNK_BULK);
}
});

View File

@ -17,23 +17,18 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
public class ChunkBulkType1_4_4 extends ChunkBulkType1_7_6 {
public ChunkBulkType1_4_4(ClientWorld clientWorld) {
super(clientWorld);
}
public class BulkChunkType1_4_4 extends BulkChunkType1_7_6 {
@Override
protected boolean readHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld) {
protected boolean readHasSkyLight(ByteBuf byteBuf) {
return true;
}
@Override
protected void writeHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld, boolean hasSkyLight) {
protected void writeHasSkyLight(ByteBuf byteBuf, boolean hasSkyLight) {
}
}

View File

@ -0,0 +1,24 @@
/*
* This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types;
public class Types1_4_4 {
public static final BulkChunkType1_4_4 CHUNK_BULK = new BulkChunkType1_4_4();
}

View File

@ -67,7 +67,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.MetaType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
@ -421,7 +421,7 @@ public class Protocol1_7_2_5to1_6_4 extends StatelessTransitionProtocol<Clientbo
@Override
public void register() {
handler(wrapper -> {
final Chunk chunk = wrapper.passthrough(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk chunk = wrapper.passthrough(Types1_7_6.getChunk(wrapper.user().get(ClientWorld.class).getEnvironment()));
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
});
}
@ -486,7 +486,7 @@ public class Protocol1_7_2_5to1_6_4 extends StatelessTransitionProtocol<Clientbo
@Override
public void register() {
handler(wrapper -> {
final Chunk[] chunks = wrapper.passthrough(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk[] chunks = wrapper.passthrough(Types1_7_6.CHUNK_BULK);
for (Chunk chunk : chunks) {
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
}

View File

@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
@ -63,7 +64,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.Ch
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.ItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.TranslationRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.*;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
@ -633,9 +634,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
@Override
public void register() {
handler(wrapper -> {
final Chunk chunk = wrapper.read(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment();
final Chunk chunk = wrapper.read(Types1_7_6.getChunk(environment));
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
wrapper.write(new ChunkType1_8(wrapper.user().get(ClientWorld.class)), chunk);
wrapper.write(ChunkType1_8.forEnvironment(environment), chunk);
});
}
});
@ -696,11 +699,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
@Override
public void register() {
handler(wrapper -> {
final Chunk[] chunks = wrapper.read(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class)));
final Chunk[] chunks = wrapper.read(Types1_7_6.CHUNK_BULK);
for (Chunk chunk : chunks) {
wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk);
}
wrapper.write(new BulkChunkType1_8(wrapper.user().get(ClientWorld.class)), chunks);
wrapper.write(BulkChunkType1_8.TYPE, chunks);
});
}
});

View File

@ -17,12 +17,9 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.type.PartialType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkBulkType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import com.viaversion.viaversion.util.Pair;
import io.netty.buffer.ByteBuf;
@ -32,25 +29,19 @@ import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class ChunkBulkType1_7_6 extends PartialType<Chunk[], ClientWorld> {
public class BulkChunkType1_7_6 extends Type<Chunk[]> {
public ChunkBulkType1_7_6(final ClientWorld clientWorld) {
super(clientWorld, Chunk[].class);
}
@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkBulkType.class;
public BulkChunkType1_7_6() {
super(Chunk[].class);
}
/**
* This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
* @param byteBuf The buffer
* @return Read skylight array or not
*/
protected boolean readHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld) {
protected boolean readHasSkyLight(final ByteBuf byteBuf) {
return byteBuf.readBoolean();
}
@ -58,19 +49,18 @@ public class ChunkBulkType1_7_6 extends PartialType<Chunk[], ClientWorld> {
* This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
* @param hasSkyLight Has skylight
*/
protected void writeHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld, final boolean hasSkyLight) {
protected void writeHasSkyLight(final ByteBuf byteBuf, final boolean hasSkyLight) {
byteBuf.writeBoolean(hasSkyLight);
}
@Override
public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
public Chunk[] read(ByteBuf byteBuf) throws Exception {
final short chunkCount = byteBuf.readShort();
final int compressedSize = byteBuf.readInt();
final boolean hasSkyLight = this.readHasSkyLight(byteBuf, clientWorld);
final byte[] data = new CustomByteType(compressedSize).read(byteBuf);
final boolean hasSkyLight = this.readHasSkyLight(byteBuf);
final byte[] data = new FixedByteArrayType(compressedSize).read(byteBuf);
final int[] chunkX = new int[chunkCount];
final int[] chunkZ = new int[chunkCount];
final short[] primaryBitMask = new short[chunkCount];
@ -106,7 +96,7 @@ public class ChunkBulkType1_7_6 extends PartialType<Chunk[], ClientWorld> {
}
@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) throws Exception {
public void write(ByteBuf byteBuf, Chunk[] chunks) throws Exception {
final int chunkCount = chunks.length;
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final int[] chunkX = new int[chunkCount];
@ -139,7 +129,7 @@ public class ChunkBulkType1_7_6 extends PartialType<Chunk[], ClientWorld> {
byteBuf.writeShort(chunkCount);
byteBuf.writeInt(compressedSize);
this.writeHasSkyLight(byteBuf, clientWorld, true);
this.writeHasSkyLight(byteBuf, true);
byteBuf.writeBytes(compressedData, 0, compressedSize);
for (int i = 0; i < chunkCount; i++) {

View File

@ -17,13 +17,9 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.type.PartialType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import com.viaversion.viaversion.util.Pair;
import io.netty.buffer.ByteBuf;
import net.raphimc.vialegacy.api.model.IdAndData;
@ -36,48 +32,44 @@ import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class ChunkType1_7_6 extends PartialType<Chunk, ClientWorld> {
public class ChunkType1_7_6 extends Type<Chunk> {
public ChunkType1_7_6(final ClientWorld clientWorld) {
super(clientWorld, Chunk.class);
}
private final boolean hasSkyLight;
@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkType.class;
public ChunkType1_7_6(boolean hasSkyLight) {
super(Chunk.class);
this.hasSkyLight = hasSkyLight;
}
/**
* This method is here to allow overriding the code for 1.2.5 -{@literal >} 1.3.2 because it introduced an unused int
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
*/
protected void readUnusedInt(final ByteBuf byteBuf, final ClientWorld clientWorld) {
protected void readUnusedInt(final ByteBuf byteBuf) {
}
/**
* This method is here to allow overriding the code for 1.2.5 -{@literal >} 1.3.2 because it introduced an unused int
*
* @param byteBuf The buffer
* @param clientWorld The ClientWorld
* @param chunk The Chunk
*/
protected void writeUnusedInt(final ByteBuf byteBuf, final ClientWorld clientWorld, final Chunk chunk) {
protected void writeUnusedInt(final ByteBuf byteBuf, final Chunk chunk) {
}
@Override
public Chunk read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
public Chunk read(ByteBuf byteBuf) throws Exception {
final int chunkX = byteBuf.readInt();
final int chunkZ = byteBuf.readInt();
final boolean fullChunk = byteBuf.readBoolean();
final short primaryBitMask = byteBuf.readShort();
final short additionalBitMask = byteBuf.readShort();
final int compressedSize = byteBuf.readInt();
this.readUnusedInt(byteBuf, clientWorld);
final byte[] data = new CustomByteType(compressedSize).read(byteBuf);
this.readUnusedInt(byteBuf);
final byte[] data = new FixedByteArrayType(compressedSize).read(byteBuf);
final byte[] uncompressedData = new byte[getSize(primaryBitMask, additionalBitMask, fullChunk, clientWorld.getEnvironment() == Environment.NORMAL)];
final byte[] uncompressedData = new byte[getSize(primaryBitMask, additionalBitMask, fullChunk, this.hasSkyLight)];
final Inflater inflater = new Inflater();
try {
inflater.setInput(data, 0, compressedSize);
@ -93,11 +85,11 @@ public class ChunkType1_7_6 extends PartialType<Chunk, ClientWorld> {
return new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>());
}
return deserialize(chunkX, chunkZ, fullChunk, clientWorld.getEnvironment() == Environment.NORMAL, primaryBitMask, additionalBitMask, uncompressedData);
return deserialize(chunkX, chunkZ, fullChunk, this.hasSkyLight, primaryBitMask, additionalBitMask, uncompressedData);
}
@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws Exception {
public void write(ByteBuf byteBuf, Chunk chunk) throws Exception {
final Pair<byte[], Short> chunkData = serialize(chunk);
final byte[] data = chunkData.key();
final short additionalBitMask = chunkData.value();
@ -120,7 +112,7 @@ public class ChunkType1_7_6 extends PartialType<Chunk, ClientWorld> {
byteBuf.writeShort(chunk.getBitmask());
byteBuf.writeShort(additionalBitMask);
byteBuf.writeInt(compressedSize);
this.writeUnusedInt(byteBuf, clientWorld, chunk);
this.writeUnusedInt(byteBuf, chunk);
byteBuf.writeBytes(compressedData, 0, compressedSize);
}

View File

@ -24,11 +24,8 @@ import io.netty.buffer.ByteBuf;
public class ItemType extends Type<Item> {
private final boolean compressed;
public ItemType(boolean compressed) {
public ItemType() {
super(Item.class);
this.compressed = compressed;
}
@Override
@ -42,7 +39,7 @@ public class ItemType extends Type<Item> {
item.setIdentifier(id);
item.setAmount(buffer.readByte());
item.setData(buffer.readShort());
item.setTag((this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).read(buffer));
item.setTag(Types1_7_6.COMPRESSED_NBT.read(buffer));
return item;
}
@ -54,7 +51,7 @@ public class ItemType extends Type<Item> {
buffer.writeShort(item.identifier());
buffer.writeByte(item.amount());
buffer.writeShort(item.data());
(this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).write(buffer, item.tag());
Types1_7_6.COMPRESSED_NBT.write(buffer, item.tag());
}
}

View File

@ -18,6 +18,7 @@
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.Environment;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
@ -34,10 +35,7 @@ public class Types1_7_6 {
public static final Type<CompoundTag> NBT = new NBTType(false);
public static final Type<CompoundTag> COMPRESSED_NBT = new NBTType(true);
public static final Type<Item> ITEM = new ItemType(false);
public static final Type<Item> COMPRESSED_ITEM = new ItemType(true);
public static final Type<Item[]> ITEM_ARRAY = new ItemArrayType<>(ITEM);
public static final Type<Item> COMPRESSED_ITEM = new ItemType();
public static final Type<Item[]> COMPRESSED_ITEM_ARRAY = new ItemArrayType<>(COMPRESSED_ITEM);
public static final Type<Metadata> METADATA = new MetadataType();
@ -50,4 +48,13 @@ public class Types1_7_6 {
public static final Type<Position> POSITION_SHORT = new PositionVarYType<>(Type.SHORT, i -> (short) i);
public static final Type<Position> POSITION_INT = new PositionVarYType<>(Type.INT, i -> i);
private static final ChunkType1_7_6 CHUNK_WITH_SKYLIGHT = new ChunkType1_7_6(true);
private static final ChunkType1_7_6 CHUNK_WITHOUT_SKYLIGHT = new ChunkType1_7_6(false);
public static final BulkChunkType1_7_6 CHUNK_BULK = new BulkChunkType1_7_6();
public static ChunkType1_7_6 getChunk(Environment environment) {
return environment == Environment.NORMAL ? CHUNK_WITH_SKYLIGHT : CHUNK_WITHOUT_SKYLIGHT;
}
}