Add support for legacy mod packet IDs (such as 211). May fix issue #32

This commit is contained in:
Kristian S. Stangeland 2013-12-22 20:10:51 +01:00
parent db8d08f602
commit 16dd2d5d1b
3 changed files with 41 additions and 4 deletions

View File

@ -631,6 +631,29 @@ public class PacketType implements Serializable {
return getLookup().getFromCurrent(protocol, sender, packetId) != null;
}
/**
* Retrieve a packet type from a legacy ID.
* <p>
* If no associated packet type could be found, a new will be registered under LEGACY.
* @param id - the legacy ID.
* @param sender - the sender of the packet, or NULL if unknown.
* @return The packet type.
* @throws IllegalArgumentException If the sender is NULL and the packet doesn't exist.
*/
public static PacketType fromLegacy(int id, Sender sender) {
PacketType type = getLookup().getFromLegacy(id);
if (type == null) {
if (sender == null)
throw new IllegalArgumentException("Cannot find legacy packet " + id);
type = newLegacy(sender, id);
// As below
scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString());
}
return type;
}
/**
* Retrieve a packet type from a protocol, sender and packet ID.
* <p>

View File

@ -1018,7 +1018,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok
@Override
@Deprecated
public int getPacketID(Object packet) {
return getPacketType(packet).getLegacyId();
return PacketRegistry.getPacketID(packet.getClass());
}
@Override

View File

@ -396,11 +396,25 @@ public class PacketRegistry {
* @return The packet type, or NULL if not found.
*/
public static PacketType getPacketType(Class<?> packet) {
return getPacketType(packet, null);
}
/**
* Retrieve the packet type of a given packet.
* @param packet - the class of the packet.
* @param sender - the sender of the packet, or NULL.
* @return The packet type, or NULL if not found.
*/
public static PacketType getPacketType(Class<?> packet, Sender sender) {
initialize();
if (NETTY != null)
if (NETTY != null) {
return NETTY.getPacketClassLookup().get(packet);
final int id = LEGACY.getPacketID(packet);
return PacketType.hasLegacy(id) ? PacketType.findLegacy(id) : null;
} else {
final int id = LEGACY.getPacketID(packet);
return PacketType.hasLegacy(id) ?
PacketType.fromLegacy(id, sender) : null;
}
}
}