Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for 1.20.3, fix registryData error for 1.20.2 #647

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/config/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum Version {
V1_19_3(761, 3218),
V1_20(763, 3463),
V1_20_2(764, 3578),
V1_20_3(765, 3698),
ANY(0, 0);

public final int dataVersion;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/game/data/dimension/DimensionCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import game.data.chunk.palette.StateProvider;
import se.llbit.nbt.*;

import java.io.*;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -16,6 +16,7 @@
*/
public class DimensionCodec {
public static final Gson GSON;

static {
/*
* To convert the properties to JSON, we need to register adapters so that GSON knows how to turn our NBT object
Expand Down Expand Up @@ -56,6 +57,7 @@ public class DimensionCodec {
private final Map<Integer, DimensionType> dimensionTypesByHash;
private final Map<String, DimensionType> dimensionTypesByName;
private final Map<String, Biome> biomes;

private DimensionCodec() {
this.dimensions = new HashMap<>();
this.dimensionTypesByHash = new HashMap<>();
Expand Down Expand Up @@ -103,7 +105,7 @@ private void readDimensionTypes(ListTag dimensionList) {
String[] parts = identifier.split(":");
String namespace = parts[0];
String name = parts[1];

DimensionType type = new DimensionType(namespace, name, d);
this.dimensionTypesByHash.put(type.getSignature(), type);
this.dimensionTypesByName.put(type.getName(), type);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/game/protocol/ConfigurationProtocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package game.protocol;

import config.Config;
import config.Version;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -11,7 +14,13 @@ public ConfigurationProtocol() {
clientBound = new HashMap<>();
serverBound = new HashMap<>();

serverBound.put(0x03, "FinishConfiguration");
if (Config.versionReporter().isAtLeast(Version.V1_20_2)) {
serverBound.put(0x02, "FinishConfiguration");

clientBound.put(0x05, "RegistryData");
} else {
serverBound.put(0x03, "FinishConfiguration");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package packets.handler;

import config.Config;
import config.Option;
import config.Version;
import packets.handler.version.ClientBoundConfigurationPacketHandler_1_20_2;
import proxy.ConnectionManager;

import java.util.HashMap;
import java.util.Map;
import proxy.ConnectionManager;

public class ClientBoundConfigurationPacketHandler extends PacketHandler {
private final HashMap<String, PacketOperator> operations = new HashMap<>();

public ClientBoundConfigurationPacketHandler(ConnectionManager connectionManager) {
super(connectionManager);
}

public static PacketHandler of(ConnectionManager connectionManager) {
return Config.versionReporter().select(PacketHandler.class,
Option.of(Version.V1_20_2, () -> new ClientBoundConfigurationPacketHandler_1_20_2(connectionManager)),
Option.of(Version.ANY, () -> new ClientBoundConfigurationPacketHandler(connectionManager))
);
}

@Override
public Map<String, PacketOperator> getOperators() {
return new HashMap<>();
return operations;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/packets/handler/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import packets.DataTypeProvider;
import proxy.ConnectionManager;

import java.util.Map;
import javax.naming.SizeLimitExceededException;
import java.util.Map;

/**
* Family of classes to handle incoming packets and perform appropriate actions based on the packet type and contents.
Expand All @@ -34,6 +34,7 @@ protected ConnectionManager getConnectionManager() {
/**
* Build the given packet, will generate a type provider to parse the contents of the packages to real values. Will
* determine if the packet is to be forwarded using its return value.
*
* @param size the size of the packet to build
* @return true if the packet should be forwarded, otherwise false.
*/
Expand All @@ -58,6 +59,7 @@ public final boolean handle(int size) {
}

public abstract Map<String, PacketOperator> getOperators();

public abstract boolean isClientBound();

public void setReader(DataProvider reader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public ServerBoundGamePacketHandler(ConnectionManager connectionManager) {

PacketOperator updatePlayerRotation = provider -> {
double yaw = provider.readFloat() % 360;
provider.readFloat(); // pitch
provider.readBoolean(); // on ground
WorldManager.getInstance().setPlayerRotation(yaw);
return true;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package packets.handler.version;

import game.data.WorldManager;
import game.data.dimension.DimensionCodec;
import packets.handler.ClientBoundConfigurationPacketHandler;
import packets.handler.PacketOperator;
import proxy.ConnectionManager;
import se.llbit.nbt.SpecificTag;

import java.util.Map;

public class ClientBoundConfigurationPacketHandler_1_20_2 extends ClientBoundConfigurationPacketHandler {
public ClientBoundConfigurationPacketHandler_1_20_2(ConnectionManager connectionManager) {
super(connectionManager);

Map<String, PacketOperator> operators = getOperators();
WorldManager worldManager = WorldManager.getInstance();

operators.put("RegistryData", provider -> {
SpecificTag registryData = provider.readNbtTag();
worldManager.setDimensionCodec(DimensionCodec.fromNbt(registryData));

return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public ClientBoundGamePacketHandler_1_20_2(ConnectionManager connectionManager)
super(connectionManager);

Protocol protocol = Config.versionReporter().getProtocol();
WorldManager worldManager = WorldManager.getInstance();
EntityRegistry entityRegistry = WorldManager.getInstance().getEntityRegistry();

Map<String, PacketOperator> operators = getOperators();
Expand Down Expand Up @@ -68,23 +67,5 @@ public ClientBoundGamePacketHandler_1_20_2(ConnectionManager connectionManager)
entityRegistry.updatePlayerAction(provider);
return true;
});

operators.put("RegistryData", provider -> {
try {
SpecificTag dimensionCodec = provider.readNbtTag();
if (!(dimensionCodec instanceof CompoundTag)) {
return true;
}
for (var nbt : ((CompoundTag) dimensionCodec)) {
if (nbt.name.equals("minecraft:dimension_type")) {
worldManager.setDimensionCodec(DimensionCodec.fromNbt(dimensionCodec));
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return true;
});
}
}
2 changes: 1 addition & 1 deletion src/main/java/proxy/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setMode(NetworkMode mode) {
case CONFIGURATION:
PacketHandler.setProtocol(new ConfigurationProtocol());
serverBoundDataReader.setPacketHandler(new ServerBoundConfigurationPacketHandler(this));
clientBoundDataReader.setPacketHandler(new ClientBoundConfigurationPacketHandler(this));
clientBoundDataReader.setPacketHandler(ClientBoundConfigurationPacketHandler.of(this));
break;
}
}
Expand Down
44 changes: 42 additions & 2 deletions src/main/resources/protocol-versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,6 @@
"dataVersion": 3578,
"clientBound": {
"0x01": "AddEntity",
"0x05": "RegistryData",
"0x3c": "UpdatePlayerInfo",
"0x07": "BlockEntityData",
"0x09": "BlockUpdate",
"0x12": "ContainerClose",
Expand All @@ -420,6 +418,7 @@
"0x2c": "MoveEntityPos",
"0x2d": "MoveEntityPosRot",
"0x31": "OpenScreen",
"0x3c": "UpdatePlayerInfo",
"0x40": "RemoveEntities",
"0x43": "Respawn",
"0x45": "SectionBlocksUpdate",
Expand All @@ -441,6 +440,47 @@
"0x34": "UseItemOn",
"0x35": "UseItem"
}
},
"765": {
"version": "1.20.3",
"dataVersion": 3698,
"clientBound": {
"0x01": "AddEntity",
"0x07": "BlockEntityData",
"0x09": "BlockUpdate",
"0x12": "ContainerClose",
"0x13": "ContainerSetContent",
"0x1f": "ForgetLevelChunk",
"0x25": "LevelChunkWithLight",
"0x28": "LightUpdate",
"0x29": "Login",
"0x2a": "MapItemData",
"0x2b": "TradeList",
"0x2c": "MoveEntityPos",
"0x2d": "MoveEntityPosRot",
"0x31": "OpenScreen",
"0x3c": "UpdatePlayerInfo",
"0x40": "RemoveEntities",
"0x45": "Respawn",
"0x47": "SectionBlocksUpdate",
"0x53": "SetChunkCacheRadius",
"0x56": "SetEntityData",
"0x59": "SetEquipment",
"0x69": "SystemChat",
"0x6d": "TeleportEntity"
},
"serverBound": {
"0x0b": "ConfigurationAcknowledged",
"0x0c": "ContainerClose",
"0x12": "Interact",
"0x16": "MovePlayerPos",
"0x18": "MovePlayerPosRot",
"0x19": "MovePlayerRot",
"0x1b": "MoveVehicle",
"0x2d": "SetCommandBlock",
"0x35": "UseItemOn",
"0x36": "UseItem"
}
}
}
}
Loading