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

Log protocol phase in case of trying to obtain a packet id not existing in the phase #1107

Merged
merged 1 commit into from
Oct 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ public enum StateRegistry {

public static final int STATUS_ID = 1;
public static final int LOGIN_ID = 2;
protected final PacketRegistry clientbound = new PacketRegistry(CLIENTBOUND);
protected final PacketRegistry serverbound = new PacketRegistry(SERVERBOUND);
protected final PacketRegistry clientbound = new PacketRegistry(CLIENTBOUND, this);
protected final PacketRegistry serverbound = new PacketRegistry(SERVERBOUND, this);

public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(Direction direction,
ProtocolVersion version) {
Expand All @@ -562,11 +562,13 @@ public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(Directi
public static class PacketRegistry {

private final Direction direction;
private final StateRegistry registry;
private final Map<ProtocolVersion, ProtocolRegistry> versions;
private boolean fallback = true;

PacketRegistry(Direction direction) {
PacketRegistry(Direction direction, StateRegistry registry) {
this.direction = direction;
this.registry = registry;

Map<ProtocolVersion, ProtocolRegistry> mutableVersions = new EnumMap<>(ProtocolVersion.class);
for (ProtocolVersion version : ProtocolVersion.values()) {
Expand Down Expand Up @@ -693,8 +695,9 @@ public int getPacketId(final MinecraftPacket packet) {
final int id = this.packetClassToId.getInt(packet.getClass());
if (id == Integer.MIN_VALUE) {
throw new IllegalArgumentException(String.format(
"Unable to find id for packet of type %s in %s protocol %s",
packet.getClass().getName(), PacketRegistry.this.direction, this.version
"Unable to find id for packet of type %s in %s protocol %s phase %s",
packet.getClass().getName(), PacketRegistry.this.direction,
this.version, PacketRegistry.this.registry
));
}
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PacketRegistryTest {

private StateRegistry.PacketRegistry setupRegistry() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, null, false),
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false),
Expand Down Expand Up @@ -84,7 +84,7 @@ void packetRegistryLinkingWorks() {
@Test
void failOnNoMappings() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertThrows(IllegalArgumentException.class,
() -> registry.register(Handshake.class, Handshake::new));
assertThrows(IllegalArgumentException.class,
Expand All @@ -94,7 +94,7 @@ void failOnNoMappings() {
@Test
void failOnWrongOrder() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertThrows(IllegalArgumentException.class,
() -> registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false),
Expand All @@ -115,7 +115,7 @@ void failOnWrongOrder() {
@Test
void failOnDuplicate() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false));
assertThrows(IllegalArgumentException.class,
Expand All @@ -129,7 +129,7 @@ void failOnDuplicate() {
@Test
void shouldNotFailWhenRegisterLatestProtocolVersion() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertDoesNotThrow(() -> registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false),
new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS),
Expand All @@ -139,7 +139,7 @@ void shouldNotFailWhenRegisterLatestProtocolVersion() {
@Test
void registrySuppliesCorrectPacketsByProtocol() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND);
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false),
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, null, false),
Expand Down