From 7b9a5e9db498e831cf4d303ab8bfc4a746dd0509 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 19 Jul 2013 19:31:04 +0200 Subject: [PATCH] Make the clientSide/serverSide builder options additative. --- .../protocol/events/ConnectionSide.java | 26 +++++++++++++++++++ .../protocol/events/PacketAdapter.java | 8 +++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/ConnectionSide.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/ConnectionSide.java index e1255b81..ed7aef2a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/ConnectionSide.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/ConnectionSide.java @@ -45,4 +45,30 @@ public enum ConnectionSide { public boolean isForServer() { return this == SERVER_SIDE || this == BOTH; } + + /** + * If both connection sides are present, return {@link #BOTH} - otherwise, return the one valud connection side. + *

+ * NULL is not a valid connection side. + * @param a - the first connection side. + * @param b - the second connection side. + * @return BOTH or the one valid side, or NULL. + */ + public static ConnectionSide add(ConnectionSide a, ConnectionSide b) { + if (a == null) + return b; + if (b == null) + return a; + + // Now merge them together + boolean client = a.isForClient() || b.isForClient(); + boolean server = a.isForServer() || b.isForServer(); + + if (client && server) + return BOTH; + else if (client) + return CLIENT_SIDE; + else + return SERVER_SIDE; + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java index e5ea38e0..c07fb9c4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java @@ -301,19 +301,19 @@ public abstract class PacketAdapter implements PacketListener { } /** - * Set this adapter to look for client-side packets only. + * Set this adapter to also look for client-side packets. * @return This builder, for chaining. */ public AdapterParameteters clientSide() { - return connectionSide(ConnectionSide.CLIENT_SIDE); + return connectionSide(ConnectionSide.add(connectionSide, ConnectionSide.CLIENT_SIDE)); } /** - * Set this adapter to look for client-side packets only. + * Set this adapter to also look for client-side packets. * @return This builder, for chaining. */ public AdapterParameteters serverSide() { - return connectionSide(ConnectionSide.SERVER_SIDE); + return connectionSide(ConnectionSide.add(connectionSide, ConnectionSide.SERVER_SIDE)); } /**