diff --git a/api/src/main/java/net/azisaba/azipluginmessaging/api/protocol/Protocol.java b/api/src/main/java/net/azisaba/azipluginmessaging/api/protocol/Protocol.java index cc1fc6a..725f1a5 100644 --- a/api/src/main/java/net/azisaba/azipluginmessaging/api/protocol/Protocol.java +++ b/api/src/main/java/net/azisaba/azipluginmessaging/api/protocol/Protocol.java @@ -105,6 +105,9 @@ public T getHandler() { * @return true if the message was sent successfully, false otherwise. */ public boolean sendPacket(@NotNull PacketSender sender, @NotNull M msg) { + if (id != 0 && !sender.isEncrypted()) { + throw new IllegalStateException("Cannot send packet " + id + " without encryption"); + } try (ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout)) { out.writeByte(id); @@ -148,6 +151,9 @@ public static void handleProxySide(ServerConnection server, byte[] rawData) { try (ByteArrayInputStream bin = new ByteArrayInputStream(data); DataInputStream in = new DataInputStream(bin)) { byte id = (byte) (in.readByte() & 0xFF); + if (id != 0 && !server.isEncrypted()) { + throw new RuntimeException("Packet " + id + " must be sent encrypted (server: " + server + ")"); + } Protocol protocol = Protocol.getById(PacketFlow.TO_PROXY, id); if (protocol == null) { Logger.getCurrentLogger().warn( @@ -190,6 +196,9 @@ public static void handleServerSide(@NotNull PacketSender sender, byte[] rawData try (ByteArrayInputStream bin = new ByteArrayInputStream(data); DataInputStream in = new DataInputStream(bin)) { byte id = (byte) (in.readByte() & 0xFF); + if (id != 0 && !sender.isEncrypted()) { + throw new RuntimeException("Packet " + id + " must be sent encrypted (sender: " + sender + ")"); + } Protocol protocol = Protocol.getById(PacketFlow.TO_SERVER, id); if (protocol == null) { Logger.getCurrentLogger().warn("Received unknown protocol id from {}: {}", sender, id); diff --git a/build.gradle.kts b/build.gradle.kts index ca3493d..f243433 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "net.azisaba.azipluginmessaging" -version = "2.0.1" +version = "2.0.2" repositories { mavenCentral() diff --git a/spigot/src/main/java/net/azisaba/azipluginmessaging/spigot/entity/PlayerImpl.java b/spigot/src/main/java/net/azisaba/azipluginmessaging/spigot/entity/PlayerImpl.java index cad1a16..fc70d51 100644 --- a/spigot/src/main/java/net/azisaba/azipluginmessaging/spigot/entity/PlayerImpl.java +++ b/spigot/src/main/java/net/azisaba/azipluginmessaging/spigot/entity/PlayerImpl.java @@ -17,7 +17,7 @@ public class PlayerImpl implements Player, PacketSender { private static final Map MAP = new ConcurrentHashMap<>(); - private final org.bukkit.entity.Player handle; + private org.bukkit.entity.Player handle; private KeyPair keyPair; private PublicKey remotePublicKey; private boolean encrypted = false; @@ -33,7 +33,9 @@ private PlayerImpl(@Nullable org.bukkit.entity.Player handle) { @NotNull public static PlayerImpl of(@NotNull org.bukkit.entity.Player player) { - return MAP.computeIfAbsent(player.getUniqueId(), u -> new PlayerImpl(player)); + PlayerImpl p = MAP.computeIfAbsent(player.getUniqueId(), u -> new PlayerImpl(player)); + p.handle = player; + return p; } @NotNull