Skip to content

Commit

Permalink
Updated Via API usage
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FlorianMichael authored Oct 20, 2023
1 parent 6d2ae22 commit 8828e45
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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;
Expand Down Expand Up @@ -220,8 +220,8 @@ public void register() {
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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
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;
Expand All @@ -38,11 +37,6 @@ public ChunkType1_1() {
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();
Expand All @@ -52,7 +46,7 @@ public Chunk read(ByteBuf byteBuf) throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public void register() {

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();
}
Expand All @@ -470,7 +470,7 @@ public void register() {
@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());
Expand All @@ -481,22 +481,22 @@ public void register() {
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);
}
}
}

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);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,36 @@
*/
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;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6;

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@

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 {
Expand All @@ -43,7 +40,7 @@ public Item read(ByteBuf buffer) throws Exception {
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;
}
Expand All @@ -57,7 +54,7 @@ public void write(ByteBuf buffer, Item item) throws Exception {
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -30,13 +31,14 @@
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> {
Expand Down Expand Up @@ -127,10 +129,7 @@ public void register() {
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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

}
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
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;

Expand Down Expand Up @@ -421,7 +421,7 @@ public void register() {
@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);
});
}
Expand Down Expand Up @@ -486,7 +486,7 @@ public void register() {
@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -63,7 +64,7 @@
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;

Expand Down Expand Up @@ -633,9 +634,11 @@ public void register() {
@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);
});
}
});
Expand Down Expand Up @@ -696,11 +699,11 @@ public void register() {
@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);
});
}
});
Expand Down
Loading

0 comments on commit 8828e45

Please sign in to comment.