diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java index 26f3a3176..d5c8088e6 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java @@ -51,24 +51,25 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public interface Protocol { - default void registerServerbound(State state, int unmappedPacketId, int mappedPacketId) { - registerServerbound(state, unmappedPacketId, mappedPacketId, (PacketHandler) null); - } - - default void registerServerbound(State state, int unmappedPacketId, int mappedPacketId, PacketHandler handler) { - registerServerbound(state, unmappedPacketId, mappedPacketId, handler, false); - } - - default void registerClientbound(State state, ClientboundPacketType packetType, PacketHandler handler) { + default void registerClientbound(State state, ClientboundPacketType packetType, @Nullable PacketHandler handler) { Preconditions.checkArgument(packetType.state() == state); registerClientbound(state, packetType.getId(), packetType.getId(), handler, false); } - default void registerServerbound(State state, ServerboundPacketType packetType, PacketHandler handler) { + default void registerServerbound(State state, ServerboundPacketType packetType, @Nullable PacketHandler handler) { Preconditions.checkArgument(packetType.state() == state); registerServerbound(state, packetType.getId(), packetType.getId(), handler, false); } + @Deprecated/*(forRemoval = true)*/ + default void registerServerbound(State state, int unmappedPacketId, int mappedPacketId) { + registerServerbound(state, unmappedPacketId, mappedPacketId, (PacketHandler) null); + } + + default void registerServerbound(State state, int unmappedPacketId, int mappedPacketId, @Nullable PacketHandler handler) { + registerServerbound(state, unmappedPacketId, mappedPacketId, handler, false); + } + /** * Registers a serverbound packet, with id transformation and remapper. * @@ -79,15 +80,16 @@ public interface Protocol { private static final int STATUS_INTENT = 1; private static final int LOGIN_INTENT = 2; private static final int TRANSFER_INTENT = 3; + public BaseProtocol() { + super(BaseClientboundPacket.class, BaseClientboundPacket.class, BaseServerboundPacket.class, BaseServerboundPacket.class); + } + @Override protected void registerPackets() { // Handshake Packet @@ -127,4 +135,9 @@ public class BaseProtocol extends AbstractProtocol { } } } + + @Override + protected PacketTypesProvider createPacketTypesProvider() { + return BasePacketTypesProvider.INSTANCE; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java index 957b357ca..382af792b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java @@ -27,12 +27,16 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvider; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.VersionProvider; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocol.ProtocolManagerImpl; import com.viaversion.viaversion.protocol.ServerProtocolVersionSingleton; +import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket; +import com.viaversion.viaversion.protocols.base.packet.BasePacketTypesProvider; +import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.GsonUtil; @@ -41,11 +45,15 @@ import java.util.List; import java.util.UUID; import java.util.logging.Level; -public class BaseProtocol1_7 extends AbstractProtocol { +public class BaseProtocol1_7 extends AbstractProtocol { + + public BaseProtocol1_7() { + super(BaseClientboundPacket.class, BaseClientboundPacket.class, BaseServerboundPacket.class, BaseServerboundPacket.class); + } @Override protected void registerPackets() { - registerClientbound(ClientboundStatusPackets.STATUS_RESPONSE, new PacketHandlers() { // Status Response Packet + registerClientbound(ClientboundStatusPackets.STATUS_RESPONSE, new PacketHandlers() { @Override public void register() { map(Type.STRING); @@ -191,4 +199,9 @@ public class BaseProtocol1_7 extends AbstractProtocol { } return UUID.fromString(uuidString); } + + @Override + protected PacketTypesProvider createPacketTypesProvider() { + return BasePacketTypesProvider.INSTANCE; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundLoginPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundLoginPackets.java index 02b9be0a0..3666b5db1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundLoginPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundLoginPackets.java @@ -17,10 +17,10 @@ */ package com.viaversion.viaversion.protocols.base; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket; -public enum ClientboundLoginPackets implements ClientboundPacketType { +public enum ClientboundLoginPackets implements BaseClientboundPacket { LOGIN_DISCONNECT, // 0x00 HELLO, // 0x01 GAME_PROFILE, // 0x02 diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundStatusPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundStatusPackets.java index f4dbc2a24..fe5d12f78 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundStatusPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/ClientboundStatusPackets.java @@ -17,10 +17,10 @@ */ package com.viaversion.viaversion.protocols.base; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket; -public enum ClientboundStatusPackets implements ClientboundPacketType { +public enum ClientboundStatusPackets implements BaseClientboundPacket { STATUS_RESPONSE, // 0x00 PONG_RESPONSE; // 0x01 diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundHandshakePackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundHandshakePackets.java index ac5760813..9aee7d98c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundHandshakePackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundHandshakePackets.java @@ -17,10 +17,10 @@ */ package com.viaversion.viaversion.protocols.base; -import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; -public enum ServerboundHandshakePackets implements ServerboundPacketType { +public enum ServerboundHandshakePackets implements BaseServerboundPacket { CLIENT_INTENTION; // 0x00 @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundLoginPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundLoginPackets.java index ab3296907..30fd09253 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundLoginPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundLoginPackets.java @@ -17,10 +17,10 @@ */ package com.viaversion.viaversion.protocols.base; -import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; -public enum ServerboundLoginPackets implements ServerboundPacketType { +public enum ServerboundLoginPackets implements BaseServerboundPacket { HELLO, // 0x00 ENCRYPTION_KEY, // 0x01 CUSTOM_QUERY_ANSWER, // 0x02 diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundStatusPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundStatusPackets.java index 358ba9921..08c047b5c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundStatusPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/ServerboundStatusPackets.java @@ -17,10 +17,10 @@ */ package com.viaversion.viaversion.protocols.base; -import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; -public enum ServerboundStatusPackets implements ServerboundPacketType { +public enum ServerboundStatusPackets implements BaseServerboundPacket { STATUS_REQUEST, // 0x00 PING_REQUEST; // 0x01 diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseClientboundPacket.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseClientboundPacket.java new file mode 100644 index 000000000..e0dfcf7d8 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseClientboundPacket.java @@ -0,0 +1,23 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion 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 . + */ +package com.viaversion.viaversion.protocols.base.packet; + +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; + +public interface BaseClientboundPacket extends ClientboundPacketType { +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BasePacketTypesProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BasePacketTypesProvider.java new file mode 100644 index 000000000..de7836db9 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BasePacketTypesProvider.java @@ -0,0 +1,64 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion 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 . + */ +package com.viaversion.viaversion.protocols.base.packet; + +import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypeMap; +import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvider; +import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; +import com.viaversion.viaversion.protocols.base.ClientboundStatusPackets; +import com.viaversion.viaversion.protocols.base.ServerboundHandshakePackets; +import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; +import com.viaversion.viaversion.protocols.base.ServerboundStatusPackets; +import java.util.EnumMap; +import java.util.Map; + +public final class BasePacketTypesProvider implements PacketTypesProvider { + + public static final PacketTypesProvider INSTANCE = new BasePacketTypesProvider(); + private final Map> clientboundPacketTypes = new EnumMap<>(State.class); + private final Map> serverboundPacketTypes = new EnumMap<>(State.class); + + private BasePacketTypesProvider() { + clientboundPacketTypes.put(State.STATUS, PacketTypeMap.of(ClientboundStatusPackets.class)); + clientboundPacketTypes.put(State.LOGIN, PacketTypeMap.of(ClientboundLoginPackets.class)); + serverboundPacketTypes.put(State.STATUS, PacketTypeMap.of(ServerboundStatusPackets.class)); + serverboundPacketTypes.put(State.HANDSHAKE, PacketTypeMap.of(ServerboundHandshakePackets.class)); + serverboundPacketTypes.put(State.LOGIN, PacketTypeMap.of(ServerboundLoginPackets.class)); + } + + @Override + public Map> unmappedClientboundPacketTypes() { + return clientboundPacketTypes; + } + + @Override + public Map> unmappedServerboundPacketTypes() { + return serverboundPacketTypes; + } + + @Override + public Map> mappedClientboundPacketTypes() { + return unmappedClientboundPacketTypes(); + } + + @Override + public Map> mappedServerboundPacketTypes() { + return unmappedServerboundPacketTypes(); + } +} \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseServerboundPacket.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseServerboundPacket.java new file mode 100644 index 000000000..8fb96d666 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/packet/BaseServerboundPacket.java @@ -0,0 +1,23 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion 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 . + */ +package com.viaversion.viaversion.protocols.base.packet; + +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; + +public interface BaseServerboundPacket extends ServerboundPacketType { +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 1d2664a2c..83650d3d1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -39,6 +39,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.misc.ParticleType; import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.data.entity.EntityTrackerBase; +import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; @@ -551,7 +552,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol