Skip to content

Commit

Permalink
Ensure that ProtocolLib discoveres mod-specific packets.
Browse files Browse the repository at this point in the history
  • Loading branch information
aadnk committed Feb 6, 2013
1 parent 224b5d7 commit 351dc7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Expand Up @@ -133,8 +133,8 @@ public enum PlayerInjectHooks {
private AsyncFilterManager asyncFilterManager;

// Valid server and client packets
private Set<Integer> serverPackets;
private Set<Integer> clientPackets;
private boolean knowsServerPackets;
private boolean knowsClientPackets;

// Ensure that we're not performing too may injections
private AtomicInteger phaseLoginCount = new AtomicInteger(0);
Expand Down Expand Up @@ -214,8 +214,8 @@ public boolean apply(@Nullable GamePhase phase) {

// Attempt to load the list of server and client packets
try {
this.serverPackets = PacketRegistry.getServerPackets();
this.clientPackets = PacketRegistry.getClientPackets();
knowsServerPackets = PacketRegistry.getServerPackets() != null;
knowsClientPackets = PacketRegistry.getClientPackets() != null;
} catch (FieldAccessException e) {
reporter.reportWarning(this, "Cannot load server and client packet list.", e);
}
Expand Down Expand Up @@ -466,7 +466,8 @@ private void enablePacketFilters(PacketListener listener, ConnectionSide side, I
for (int packetID : packets) {
// Only register server packets that are actually supported by Minecraft
if (side.isForServer()) {
if (serverPackets != null && serverPackets.contains(packetID))
// Note that we may update the packet list here
if (!knowsServerPackets || PacketRegistry.getServerPackets().contains(packetID))
playerInjection.addPacketHandler(packetID);
else
reporter.reportWarning(this, String.format(
Expand All @@ -477,7 +478,7 @@ private void enablePacketFilters(PacketListener listener, ConnectionSide side, I

// As above, only for client packets
if (side.isForClient() && packetInjector != null) {
if (clientPackets != null && clientPackets.contains(packetID))
if (!knowsClientPackets || PacketRegistry.getClientPackets().contains(packetID))
packetInjector.addPacketHandler(packetID);
else
reporter.reportWarning(this, String.format(
Expand Down
Expand Up @@ -47,8 +47,12 @@ public class PacketRegistry {
private static Map<Class, Integer> packetToID;

// Whether or not certain packets are sent by the client or the server
private static Set<Integer> serverPackets;
private static Set<Integer> clientPackets;
private static ImmutableSet<Integer> serverPackets;
private static ImmutableSet<Integer> clientPackets;

// The underlying sets
private static Set<Integer> serverPacketsRef;
private static Set<Integer> clientPacketsRef;

// New proxy values
private static Map<Integer, Class> overwrittenPackets = new HashMap<Integer, Class>();
Expand Down Expand Up @@ -120,21 +124,21 @@ public static Set<Integer> getClientPackets() throws FieldAccessException {

@SuppressWarnings("unchecked")
private static void initializeSets() throws FieldAccessException {
if (serverPackets == null || clientPackets == null) {
if (serverPacketsRef == null || clientPacketsRef == null) {
List<Field> sets = getPacketRegistry().getFieldListByType(Set.class);

try {
if (sets.size() > 1) {
serverPackets = (Set<Integer>) FieldUtils.readStaticField(sets.get(0), true);
clientPackets = (Set<Integer>) FieldUtils.readStaticField(sets.get(1), true);
serverPacketsRef = (Set<Integer>) FieldUtils.readStaticField(sets.get(0), true);
clientPacketsRef = (Set<Integer>) FieldUtils.readStaticField(sets.get(1), true);

// Impossible
if (serverPackets == null || clientPackets == null)
if (serverPacketsRef == null || clientPacketsRef == null)
throw new FieldAccessException("Packet sets are in an illegal state.");

// NEVER allow callers to modify the underlying sets
serverPackets = ImmutableSet.copyOf(serverPackets);
clientPackets = ImmutableSet.copyOf(clientPackets);
serverPackets = ImmutableSet.copyOf(serverPacketsRef);
clientPackets = ImmutableSet.copyOf(clientPacketsRef);

} else {
throw new FieldAccessException("Cannot retrieve packet client/server sets.");
Expand All @@ -143,6 +147,13 @@ private static void initializeSets() throws FieldAccessException {
} catch (IllegalAccessException e) {
throw new FieldAccessException("Cannot access field.", e);
}

} else {
// Copy over again if it has changed
if (serverPacketsRef != null && serverPacketsRef.size() != serverPackets.size())
serverPackets = ImmutableSet.copyOf(serverPacketsRef);
if (clientPacketsRef != null && clientPacketsRef.size() != clientPackets.size())
clientPackets = ImmutableSet.copyOf(clientPacketsRef);
}
}

Expand Down

0 comments on commit 351dc7f

Please sign in to comment.