Add SendablePacket#extractServerPacket

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-17 19:02:29 +01:00
parent 076b11b77a
commit 5b4ffa0846
2 changed files with 17 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package net.minestom.server.network.packet.server;
import net.minestom.server.network.player.PlayerConnection;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
* Represents a packet that can be sent to a {@link PlayerConnection}.
@ -9,4 +10,17 @@ import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Experimental
public sealed interface SendablePacket
permits ServerPacket, CachedPacket, FramedPacket {
@ApiStatus.Experimental
static @NotNull ServerPacket extractServerPacket(@NotNull SendablePacket packet) {
if (packet instanceof ServerPacket serverPacket) {
return serverPacket;
} else if (packet instanceof CachedPacket cachedPacket) {
return cachedPacket.packet();
} else if (packet instanceof FramedPacket framedPacket) {
return framedPacket.packet();
} else {
throw new RuntimeException("Unknown packet type: " + packet.getClass().getName());
}
}
}

View File

@ -4,8 +4,6 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.fakeplayer.FakePlayer;
import net.minestom.server.entity.fakeplayer.FakePlayerController;
import net.minestom.server.network.packet.server.CachedPacket;
import net.minestom.server.network.packet.server.FramedPacket;
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.utils.validate.Check;
@ -19,16 +17,9 @@ public class FakePlayerConnection extends PlayerConnection {
@Override
public void sendPacket(@NotNull SendablePacket packet) {
FakePlayerController controller = getFakePlayer().getController();
if (packet instanceof ServerPacket serverPacket) {
if (!shouldSendPacket(serverPacket)) return;
controller.consumePacket(serverPacket);
} else if (packet instanceof FramedPacket framedPacket) {
controller.consumePacket(framedPacket.packet());
} else if (packet instanceof CachedPacket cachedPacket) {
controller.consumePacket(cachedPacket.packet());
} else {
throw new RuntimeException("Unknown packet type: " + packet.getClass().getName());
}
final ServerPacket serverPacket = SendablePacket.extractServerPacket(packet);
if (!shouldSendPacket(serverPacket)) return;
controller.consumePacket(serverPacket);
}
@NotNull