Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-16 21:10:30 +01:00
chore: refactor packet registration
Dieser Commit ist enthalten in:
Ursprung
784806848d
Commit
7048cabaf8
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2023 Velocity 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.proxy.protocol;
|
||||
|
||||
import com.velocitypowered.api.network.ProtocolVersion;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
interface PacketMapper {
|
||||
default void readWrite(final int id, final ProtocolVersion from) {
|
||||
this.readWrite(id, from, null);
|
||||
}
|
||||
|
||||
void readWrite(final int id, final ProtocolVersion from, final @Nullable ProtocolVersion to);
|
||||
|
||||
default void writeOnly(final int id, final ProtocolVersion from) {
|
||||
this.writeOnly(id, from, null);
|
||||
}
|
||||
|
||||
void writeOnly(final int id, final ProtocolVersion from, final @Nullable ProtocolVersion to);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2023 Velocity 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.proxy.protocol;
|
||||
|
||||
import com.velocitypowered.api.network.ProtocolVersion;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Packet mapping.
|
||||
*/
|
||||
public record PacketMapping(
|
||||
int id,
|
||||
ProtocolVersion from,
|
||||
@Nullable ProtocolVersion to,
|
||||
boolean writeOnly
|
||||
) {
|
||||
}
|
Datei-Diff unterdrückt, da er zu groß ist
Diff laden
@ -45,10 +45,11 @@ class PacketRegistryTest {
|
||||
private StateRegistry.PacketRegistry setupRegistry() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, null, false),
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false),
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_15, MINECRAFT_1_16, false));
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_8, null);
|
||||
m.readWrite(0x00, MINECRAFT_1_12, null);
|
||||
m.readWrite(0x00, MINECRAFT_1_15, MINECRAFT_1_16);
|
||||
});
|
||||
return registry;
|
||||
}
|
||||
|
||||
@ -86,7 +87,7 @@ class PacketRegistryTest {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {}));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN)
|
||||
.getPacketId(new HandshakePacket()));
|
||||
@ -97,54 +98,62 @@ class PacketRegistryTest {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false),
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false)));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_13, null);
|
||||
m.readWrite(0x00, MINECRAFT_1_8, null);
|
||||
}));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false),
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false)));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_13, null);
|
||||
m.readWrite(0x01, MINECRAFT_1_13, null);
|
||||
}));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, MINECRAFT_1_8, false)));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_13, MINECRAFT_1_8);
|
||||
}));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, MINECRAFT_1_14, false),
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_16, null, false)));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_8, MINECRAFT_1_14);
|
||||
m.readWrite(0x00, MINECRAFT_1_16, null);
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failOnDuplicate() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false));
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x00, MINECRAFT_1_8, null);
|
||||
});
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12, null, false)));
|
||||
() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x01, MINECRAFT_1_12, null);
|
||||
}));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> registry.register(StatusPingPacket.class, StatusPingPacket::new,
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_13, null, false)));
|
||||
() -> registry.register(StatusPingPacket.class, StatusPingPacket::new, m -> {
|
||||
m.readWrite(0x00, MINECRAFT_1_13, null);
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFailWhenRegisterLatestProtocolVersion() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false),
|
||||
new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS),
|
||||
null, false)));
|
||||
assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x00, MINECRAFT_1_8, null);
|
||||
m.readWrite(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), null);
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registrySuppliesCorrectPacketsByProtocol() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
|
||||
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new,
|
||||
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false),
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, null, false),
|
||||
new StateRegistry.PacketMapping(0x02, MINECRAFT_1_13, null, false));
|
||||
registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
|
||||
m.readWrite(0x00, MINECRAFT_1_12, null);
|
||||
m.readWrite(0x01, MINECRAFT_1_12_1, null);
|
||||
m.readWrite(0x02, MINECRAFT_1_13, null);
|
||||
});
|
||||
assertEquals(HandshakePacket.class,
|
||||
registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0x00).getClass());
|
||||
assertEquals(HandshakePacket.class,
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren