Skip to content

Commit

Permalink
Update for 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnlog committed Nov 24, 2023
1 parent 344488b commit 0d21edb
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/main/java/config/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum Version {
V1_19(759, 3105),
V1_19_3(761, 3218),
V1_20(763, 3463),
V1_20_2(764, 3578),
ANY(0, 0);

public final int dataVersion;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/game/data/chunk/ChunkFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void clear() {
*/
private static Chunk getVersionedChunk(int dataVersion, CoordinateDim2D chunkPos) {
return VersionReporter.select(dataVersion, Chunk.class,
Option.of(Version.V1_20_2, () -> new Chunk_1_20_2(chunkPos, dataVersion)),
Option.of(Version.V1_20, () -> new Chunk_1_20(chunkPos, dataVersion)),
Option.of(Version.V1_18, () -> new Chunk_1_18(chunkPos, dataVersion)),
Option.of(Version.V1_17, () -> new Chunk_1_17(chunkPos, dataVersion)),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/game/data/chunk/version/Chunk_1_18.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void readChunkColumn(DataTypeProvider dataProvider) {
}
}

private void findBlockEntities(ChunkSection section, int sectionY) {
protected void findBlockEntities(ChunkSection section, int sectionY) {
BlockEntityRegistry blockEntities = RegistryManager.getInstance().getBlockEntityRegistry();
GlobalPalette globalPalette = GlobalPaletteProvider.getGlobalPalette(getDataVersion());

Expand All @@ -151,7 +151,7 @@ private void findBlockEntities(ChunkSection section, int sectionY) {
}
}

private boolean containsBlockEntities(Palette p) {
protected boolean containsBlockEntities(Palette p) {
BlockEntityRegistry blockEntities = RegistryManager.getInstance().getBlockEntityRegistry();
for (SpecificTag tag : p.toNbt()) {
if (blockEntities.isBlockEntity(tag.get("Name").stringValue())) {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/game/data/chunk/version/Chunk_1_20_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package game.data.chunk.version;

import game.data.chunk.palette.Palette;
import game.data.chunk.palette.PaletteType;
import game.data.coordinates.CoordinateDim2D;
import packets.DataTypeProvider;

public class Chunk_1_20_2 extends Chunk_1_20 {


public Chunk_1_20_2(CoordinateDim2D location, int version) {
super(location, version);
}

/**
* Read a chunk column for 1.20.2
*/
public void readChunkColumn(DataTypeProvider dataProvider) {
// Loop through section Y values, starting from the lowest section that has blocks inside it.
for (int sectionY = getMinBlockSection(); sectionY <= getMaxBlockSection() && dataProvider.hasNext(); sectionY++) {
ChunkSection_1_18 section = (ChunkSection_1_18) getChunkSection(sectionY);

dataProvider.readShort();
Palette blockPalette = Palette.readPalette(dataProvider, PaletteType.BLOCKS);

if (section == null) {
section = (ChunkSection_1_18) createNewChunkSection((byte) (sectionY & 0xFF), blockPalette);
} else {
section.setBlockPalette(blockPalette);
}

section.setBlocks(dataProvider.readLongArray(dataProvider.readVarInt()));

Palette biomePalette = Palette.readPalette(dataProvider, PaletteType.BIOMES);
section.setBiomePalette(biomePalette);

// check how many longs we expect, if there's more discard the rest
int longsExpectedBiomes = ChunkSection_1_18.longsRequiredBiomes(biomePalette.getBitsPerBlock());
section.setBiomes(dataProvider.readLongArray(dataProvider.readVarInt()));

// May replace an existing section or a null one
setChunkSection(sectionY, section);

// servers don't (always?) include containers in the list of block_entities. We need to know that these block
// entities exist, otherwise we'll end up not writing block entity data for them
if (containsBlockEntities(blockPalette)) {
findBlockEntities(section, sectionY);
}
}
}
}
8 changes: 6 additions & 2 deletions src/main/java/game/data/dimension/DimensionCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ private DimensionCodec() {
this.biomes = new HashMap<>();
}

public static DimensionCodec fromNbt(String[] dimensionNames, SpecificTag tag) {
public static DimensionCodec fromNbt(SpecificTag tag) {
DimensionCodec codec = new DimensionCodec();

codec.readDimensions(dimensionNames);
codec.readDimensionTypes(tag.get("minecraft:dimension_type").asCompound().get("value").asList());
codec.readBiomes(tag.get("minecraft:worldgen/biome").asCompound().get("value").asList());

return codec;
}

public DimensionCodec setDimensionNames(String[] dimensionNames) {
this.readDimensions(dimensionNames);
return this;
}

public Collection<Dimension> getDimensions() {
return dimensions.values();
}
Expand Down
79 changes: 66 additions & 13 deletions src/main/java/game/data/entity/EntityRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import static util.ExceptionHandling.attempt;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -18,11 +14,12 @@
import game.data.coordinates.CoordinateDim2D;
import game.data.entity.specific.Villager;
import packets.DataTypeProvider;
import packets.UUID;
import se.llbit.nbt.SpecificTag;

public class EntityRegistry {

private final Map<Integer, PlayerEntity> players;
private final Map<UUID, PlayerEntity> players;
private final Map<CoordinateDim2D, Set<Entity>> perChunk;
private final Map<Integer, Entity> entities;
private final WorldManager worldManager;
Expand Down Expand Up @@ -73,16 +70,71 @@ public void addEntity(DataTypeProvider provider, Function<DataTypeProvider, Enti
});

if (ent instanceof Villager villager) {
WorldManager.getInstance().getVillagerManager().loadPreviousTradeAt(villager);
villager.registerOnTradeUpdate((pos) -> markUnsaved(pos.globalToDimChunk()));
}
}));
}

public void addPlayer(DataTypeProvider provider) {
executor.execute(() -> attempt(() -> {
int entId = provider.readVarInt();
players.put(entId, PlayerEntity.parse(provider));
PlayerEntity player = PlayerEntity.parse(provider);
players.put(player.getUUID(), player);
}));
}

public void updatePlayerAction(DataTypeProvider provider) {
executor.execute(() -> attempt(() -> {
byte actions = provider.readNext();
int playerCnt = provider.readVarInt();

for (int i = 0; i < playerCnt; i++) {
UUID uuid = provider.readUUID();

if ((actions & 0x01) > 0) {
PlayerEntity player = new PlayerEntity(uuid);
players.put(uuid, player);

String name = provider.readString();
int properties = provider.readVarInt();
for (int j = 0; j < properties; j++) {
provider.readString();
provider.readString();
boolean signed = provider.readBoolean();
if (signed) provider.readString();
}
}

if ((actions & 0x02) > 0) {
boolean signature = provider.readBoolean();
if (signature) {
provider.readUUID();
provider.readLong();
int encKeySz = provider.readVarInt();
provider.readByteArray(encKeySz);
int pubKeySz = provider.readVarInt();
provider.readByteArray(pubKeySz);
}
}

if ((actions & 0x04) > 0) {
provider.readVarInt();
}

if ((actions & 0x08) > 0) {
provider.readBoolean();
}

if ((actions & 0x10) > 0) {
provider.readVarInt();
}

if ((actions & 0x20) > 0) {
boolean displayName = provider.readBoolean();
if (displayName) {
provider.readChat();
}
}
}
}));
}

Expand Down Expand Up @@ -139,12 +191,14 @@ public void updatePositionAbsolute(DataTypeProvider provider) {
}

public IMovableEntity getMovableEntity(int entId) {
IMovableEntity ent = players.get(entId);
Entity tmpEnt = entities.get(entId);
if (tmpEnt == null) return null;
IMovableEntity ent = players.get(tmpEnt.uuid);
if (ent != null) {
return ent;
}

return entities.get(entId);
return tmpEnt;
}

public List<SpecificTag> getEntitiesNbt(CoordinateDim2D location) {
Expand Down Expand Up @@ -183,9 +237,8 @@ public void destroyEntities(DataTypeProvider provider) {
while (count-- > 0) {
int id = provider.readVarInt();
if (entities.containsKey(id)) {
players.remove(entities.get(id).uuid);
entities.remove(id);
} else {
players.remove(id);
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/game/data/entity/PlayerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public class PlayerEntity implements IMovableEntity {
private boolean hasRequestedName = false;
private String name;

PlayerEntity(UUID uuid) {
this.uuid = uuid;
}

public static PlayerEntity parse(DataTypeProvider provider) {
PlayerEntity ent = new PlayerEntity();
ent.uuid = provider.readUUID();
PlayerEntity ent = new PlayerEntity(provider.readUUID());
ent.readPosition(provider);

return ent;
Expand Down Expand Up @@ -86,4 +89,8 @@ public String getName() {
}
return name;
}

public UUID getUUID() {
return uuid;
}
}
4 changes: 3 additions & 1 deletion src/main/java/packets/DataTypeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import game.data.coordinates.CoordinateDouble3D;
import packets.version.DataTypeProvider_1_13;
import packets.version.DataTypeProvider_1_14;
import packets.version.DataTypeProvider_1_20_2;
import se.llbit.nbt.NamedTag;
import se.llbit.nbt.SpecificTag;

Expand Down Expand Up @@ -37,6 +38,7 @@ public DataTypeProvider(byte[] finalFullPacket) {

public static DataTypeProvider ofPacket(byte[] finalFullPacket) {
return Config.versionReporter().select(DataTypeProvider.class,
Option.of(Version.V1_20_2, () -> new DataTypeProvider_1_20_2(finalFullPacket)),
Option.of(Version.V1_14, () -> new DataTypeProvider_1_14(finalFullPacket)),
Option.of(Version.V1_13, () -> new DataTypeProvider_1_13(finalFullPacket)),
Option.of(Version.ANY, () -> new DataTypeProvider(finalFullPacket))
Expand Down Expand Up @@ -223,7 +225,7 @@ public double readDouble() {
public UUID readUUID() {
return new UUID(readLong(), readLong());
}

public UUID readOptUUID() {
if (readBoolean()) {
return readUUID();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/packets/builder/PacketBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public void writeBoolean(boolean val) {

/**
* Writes an NBT tag. We need to wrap this in a NamedTag, as the named tag is not written itself.
* TODO: update on 1.20.2
*/
public void writeNbt(SpecificTag nbt) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
import game.data.entity.EntityRegistry;
import game.data.entity.MobEntity;
import game.data.entity.ObjectEntity;
import packets.handler.version.ClientBoundGamePacketHandler_1_14;
import packets.handler.version.ClientBoundGamePacketHandler_1_15;
import packets.handler.version.ClientBoundGamePacketHandler_1_16;
import packets.handler.version.ClientBoundGamePacketHandler_1_17;
import packets.handler.version.ClientBoundGamePacketHandler_1_18;
import packets.handler.version.ClientBoundGamePacketHandler_1_19;
import packets.handler.version.*;
import packets.handler.plugins.PluginChannelHandler;
import proxy.ConnectionManager;
import se.llbit.nbt.SpecificTag;
Expand Down Expand Up @@ -184,6 +179,7 @@ public ClientBoundGamePacketHandler(ConnectionManager connectionManager) {

public static PacketHandler of(ConnectionManager connectionManager) {
return Config.versionReporter().select(PacketHandler.class,
Option.of(Version.V1_20_2, () -> new ClientBoundGamePacketHandler_1_20_2(connectionManager)),
Option.of(Version.V1_19, () -> new ClientBoundGamePacketHandler_1_19(connectionManager)),
Option.of(Version.V1_18, () -> new ClientBoundGamePacketHandler_1_18(connectionManager)),
Option.of(Version.V1_17, () -> new ClientBoundGamePacketHandler_1_17(connectionManager)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ClientBoundGamePacketHandler_1_16(ConnectionManager connectionManager) {
String[] dimensionNames = provider.readStringArray(numDimensions);

SpecificTag dimensionCodec = provider.readNbtTag();
WorldManager.getInstance().setDimensionCodec(DimensionCodec.fromNbt(dimensionNames, dimensionCodec));
WorldManager.getInstance().setDimensionCodec(DimensionCodec.fromNbt(dimensionCodec).setDimensionNames(dimensionNames));

SpecificTag dimensionNbt = provider.readNbtTag();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ClientBoundGamePacketHandler_1_19(ConnectionManager connectionManager) {
String[] dimensionNames = provider.readStringArray(numDimensions);

SpecificTag dimensionCodec = provider.readNbtTag();
WorldManager.getInstance().setDimensionCodec(DimensionCodec.fromNbt(dimensionNames, dimensionCodec));
WorldManager.getInstance().setDimensionCodec(DimensionCodec.fromNbt(dimensionCodec).setDimensionNames(dimensionNames));

String dimensionType = provider.readString();
// current active dimension
Expand Down
Loading

0 comments on commit 0d21edb

Please sign in to comment.