Archiviert
13
0

Rework packet type deprecation to actually work properly

Also fix compatibility with 1.8.0
Dieser Commit ist enthalten in:
Dan Mulloy 2017-05-29 22:14:08 -04:00
Ursprung ea7900d64f
Commit 1be94aad78
10 geänderte Dateien mit 222 neuen und 169 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,10 @@
package com.comphenix.protocol; package com.comphenix.protocol;
import java.io.Serializable; import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -16,7 +20,6 @@ import org.bukkit.Bukkit;
import com.comphenix.protocol.PacketTypeLookup.ClassLookup; import com.comphenix.protocol.PacketTypeLookup.ClassLookup;
import com.comphenix.protocol.events.ConnectionSide; import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.injector.packet.PacketRegistry; import com.comphenix.protocol.injector.packet.PacketRegistry;
import com.comphenix.protocol.reflect.ObjectEnum;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.common.base.Objects; import com.google.common.base.Objects;
@ -52,7 +55,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Incoming packets. * Incoming packets.
* @author Kristian * @author Kristian
*/ */
public static class Client extends ObjectEnum<PacketType> { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
public static final PacketType SET_PROTOCOL = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "SetProtocol"); public static final PacketType SET_PROTOCOL = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "SetProtocol");
@ -60,7 +63,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Client INSTANCE = new Client(); private final static Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(PacketType.class); } private Client() { super(); }
public static Client getInstance() { public static Client getInstance() {
return INSTANCE; return INSTANCE;
@ -74,10 +77,10 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* An empty enum, as the server will not send any packets in this protocol. * An empty enum, as the server will not send any packets in this protocol.
* @author Kristian * @author Kristian
*/ */
public static class Server extends ObjectEnum<PacketType> { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
private final static Server INSTANCE = new Server(); private final static Server INSTANCE = new Server();
private Server() { super(PacketType.class); } private Server() { super(); }
public static Server getInstance() { public static Server getInstance() {
return INSTANCE; return INSTANCE;
@ -103,7 +106,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends ObjectEnum<PacketType> { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private final static Sender SENDER = Sender.SERVER;
public static final PacketType SPAWN_ENTITY = new PacketType(PROTOCOL, SENDER, 0x00, 0x0E, "SpawnEntity"); public static final PacketType SPAWN_ENTITY = new PacketType(PROTOCOL, SENDER, 0x00, 0x0E, "SpawnEntity");
@ -192,19 +195,19 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @deprecated Removed in 1.9 * @deprecated Removed in 1.9
*/ */
@Deprecated @Deprecated
public static final PacketType MAP_CHUNK_BULK = new PacketType(PROTOCOL, SENDER, 255, 255, "MapChunkBulk").deprecatedIn(MinecraftVersion.COMBAT_UPDATE); public static final PacketType MAP_CHUNK_BULK = new PacketType(PROTOCOL, SENDER, 255, 255, "MapChunkBulk");
/** /**
* @deprecated Removed in 1.9 * @deprecated Removed in 1.9
*/ */
@Deprecated @Deprecated
public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 254, 254, "SetCompression").deprecatedIn(MinecraftVersion.COMBAT_UPDATE); public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 254, 254, "SetCompression");
/** /**
* @deprecated Removed in 1.9 * @deprecated Removed in 1.9
*/ */
@Deprecated @Deprecated
public static final PacketType UPDATE_ENTITY_NBT = new PacketType(PROTOCOL, SENDER, 253, 253, "UpdateEntityNBT").deprecatedIn(MinecraftVersion.COMBAT_UPDATE); public static final PacketType UPDATE_ENTITY_NBT = new PacketType(PROTOCOL, SENDER, 253, 253, "UpdateEntityNBT");
// ----- Renamed packets // ----- Renamed packets
@ -212,25 +215,25 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @deprecated Renamed to {@link #WINDOW_DATA} * @deprecated Renamed to {@link #WINDOW_DATA}
*/ */
@Deprecated @Deprecated
public static final PacketType CRAFT_PROGRESS_BAR = WINDOW_DATA.deprecated(); public static final PacketType CRAFT_PROGRESS_BAR = WINDOW_DATA.clone();
/** /**
* @deprecated Renamed to {@link #REL_ENTITY_MOVE_LOOK} * @deprecated Renamed to {@link #REL_ENTITY_MOVE_LOOK}
*/ */
@Deprecated @Deprecated
public static final PacketType ENTITY_MOVE_LOOK = REL_ENTITY_MOVE_LOOK.deprecated(); public static final PacketType ENTITY_MOVE_LOOK = REL_ENTITY_MOVE_LOOK.clone();
/** /**
* @deprecated Renamed to {@link #STATISTIC} * @deprecated Renamed to {@link #STATISTIC}
*/ */
@Deprecated @Deprecated
public static final PacketType STATISTICS = STATISTIC.deprecated(); public static final PacketType STATISTICS = STATISTIC.clone();
/** /**
* @deprecated Renamed to {@link #OPEN_SIGN_EDITOR} * @deprecated Renamed to {@link #OPEN_SIGN_EDITOR}
*/ */
@Deprecated @Deprecated
public static final PacketType OPEN_SIGN_ENTITY = OPEN_SIGN_EDITOR.deprecated(); public static final PacketType OPEN_SIGN_ENTITY = OPEN_SIGN_EDITOR.clone();
// ----- Replaced in 1.9.4 // ----- Replaced in 1.9.4
@ -239,12 +242,12 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
*/ */
@Deprecated @Deprecated
public static final PacketType UPDATE_SIGN = MinecraftReflection.signUpdateExists() ? new PacketType(PROTOCOL, SENDER, 252, 252, "UpdateSign") : public static final PacketType UPDATE_SIGN = MinecraftReflection.signUpdateExists() ? new PacketType(PROTOCOL, SENDER, 252, 252, "UpdateSign") :
TILE_ENTITY_DATA.deprecated(); TILE_ENTITY_DATA.clone();
private final static Server INSTANCE = new Server(); private final static Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(PacketType.class); } private Server() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -258,7 +261,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Incoming packets. * Incoming packets.
* @author Kristian * @author Kristian
*/ */
public static class Client extends ObjectEnum<PacketType> { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
public static final PacketType TELEPORT_ACCEPT = new PacketType(PROTOCOL, SENDER, 0x00, 0xF9, "TeleportAccept"); public static final PacketType TELEPORT_ACCEPT = new PacketType(PROTOCOL, SENDER, 0x00, 0xF9, "TeleportAccept");
@ -298,7 +301,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Client INSTANCE = new Client(); private final static Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(PacketType.class); } private Client() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -324,22 +327,24 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends ObjectEnum<PacketType> { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private final static Sender SENDER = Sender.SERVER;
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "ServerInfo").forceAsync(true); @ForceAsync
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "ServerInfo");
public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x01, 0x01, "Pong"); public static final PacketType PONG = new PacketType(PROTOCOL, SENDER, 0x01, 0x01, "Pong");
/** /**
* @deprecated Replaced by {@link #SERVER_INFO} * @deprecated Renamed to {@link #SERVER_INFO}
*/ */
@Deprecated @Deprecated
public static final PacketType OUT_SERVER_INFO = SERVER_INFO.deprecated(); @ForceAsync
public static final PacketType OUT_SERVER_INFO = SERVER_INFO.clone();
private final static Server INSTANCE = new Server(); private final static Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(PacketType.class); } private Server() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -353,7 +358,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Incoming packets. * Incoming packets.
* @author Kristian * @author Kristian
*/ */
public static class Client extends ObjectEnum<PacketType> { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Start"); public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Start");
@ -362,7 +367,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Client INSTANCE = new Client(); private final static Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(PacketType.class); } private Client() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -388,7 +393,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends ObjectEnum<PacketType> { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private final static Sender SENDER = Sender.SERVER;
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Disconnect"); public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Disconnect");
@ -399,7 +404,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Server INSTANCE = new Server(); private final static Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(PacketType.class); } private Server() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -413,7 +418,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* Incoming packets. * Incoming packets.
* @author Kristian * @author Kristian
*/ */
public static class Client extends ObjectEnum<PacketType> { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Start"); public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, 0x00, "Start");
@ -422,7 +427,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Client INSTANCE = new Client(); private final static Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(PacketType.class); } private Client() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -449,7 +454,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
// Missing server packets: [10, 11, 12, 21, 107, 252] // Missing server packets: [10, 11, 12, 21, 107, 252]
public static class Server extends ObjectEnum<PacketType> { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private final static Sender SENDER = Sender.SERVER;
public static final PacketType PLAYER_FLYING = PacketType.newLegacy(SENDER, 10); public static final PacketType PLAYER_FLYING = PacketType.newLegacy(SENDER, 10);
@ -473,7 +478,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
// Prevent accidental construction // Prevent accidental construction
private Server() { private Server() {
super(PacketType.class); super();
} }
public static Sender getSender() { public static Sender getSender() {
@ -489,7 +494,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
// Missing client packets: [1, 9, 255] // Missing client packets: [1, 9, 255]
public static class Client extends ObjectEnum<PacketType> { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private final static Sender SENDER = Sender.CLIENT;
public static final PacketType LOGIN = PacketType.newLegacy(SENDER, 1); public static final PacketType LOGIN = PacketType.newLegacy(SENDER, 1);
@ -499,7 +504,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final static Client INSTANCE = new Client(); private final static Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(PacketType.class); } private Client() { super(); }
public static Sender getSender() { public static Sender getSender() {
return SENDER; return SENDER;
@ -582,6 +587,13 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
} }
} }
/**
* Whether or not packets of this type must be handled asynchronously.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ForceAsync { }
// Lookup of packet types // Lookup of packet types
private static PacketTypeLookup LOOKUP; private static PacketTypeLookup LOOKUP;
@ -597,9 +609,11 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
private final MinecraftVersion version; private final MinecraftVersion version;
private final String[] classNames; private final String[] classNames;
private boolean forceAsync; private String name;
private boolean dynamic;
private boolean deprecated; private boolean deprecated;
private boolean forceAsync;
private boolean dynamic;
/** /**
* Retrieve the current packet/legacy lookup. * Retrieve the current packet/legacy lookup.
@ -891,7 +905,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
Callable<Boolean> callable = new Callable<Boolean>() { Callable<Boolean> callable = new Callable<Boolean>() {
@Override @Override
public Boolean call() throws Exception { public Boolean call() throws Exception {
ObjectEnum<PacketType> objEnum; PacketTypeEnum objEnum;
// A bit ugly, but performance is critical // A bit ugly, but performance is critical
objEnum = getObjectEnum(type); objEnum = getObjectEnum(type);
@ -920,7 +934,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @param type - the packet type. * @param type - the packet type.
* @return The corresponding object enum. * @return The corresponding object enum.
*/ */
public static ObjectEnum<PacketType> getObjectEnum(final PacketType type) { public static PacketTypeEnum getObjectEnum(final PacketType type) {
switch (type.getProtocol()) { switch (type.getProtocol()) {
case HANDSHAKING: case HANDSHAKING:
return type.isClient() ? Handshake.Client.getInstance() : Handshake.Server.getInstance(); return type.isClient() ? Handshake.Client.getInstance() : Handshake.Server.getInstance();
@ -967,7 +981,6 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
for (int i = 0; i < classNames.length; i++) { for (int i = 0; i < classNames.length; i++) {
classNames[i] = format(protocol, sender, names[i]); classNames[i] = format(protocol, sender, names[i]);
} }
//System.out.println(Arrays.toString(classNames));
} }
/** /**
@ -1048,12 +1061,47 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
} }
} }
// Only used by Enum processor
void setName(String name) {
this.name = name;
}
/** /**
* Retrieve the declared enum name of this packet type. * Retrieve the declared enum name of this packet type.
* @return The enum name. * @return The enum name.
*/ */
public String name() { public String name() {
return getObjectEnum(this).getDeclaredName(this); return name;
}
// Only used by enum processor
void setDeprecated() {
this.deprecated = true;
}
/**
* Whether or not this packet is deprecated. Deprecated packet types have either been renamed, replaced, or removed.
* Kind of like the thing they use to tell children to recycle except with packets you probably shouldn't be using.
*
* @return True if the type is deprecated, false if not
*/
public boolean isDeprecated() {
return deprecated;
}
// Only used by enum processor
void forceAsync() {
this.forceAsync = true;
}
/**
* Whether or not the processing of this packet must take place on a thread different than the main thread. You don't
* get a choice. If this is false it's up to you.
*
* @return True if async processing is forced, false if not.
*/
public boolean isAsyncForced() {
return forceAsync;
} }
/** /**
@ -1084,40 +1132,9 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
return dynamic; return dynamic;
} }
private PacketType forceAsync(boolean forceAsync) {
this.forceAsync = forceAsync;
return this;
}
/**
* Whether or not this packet must be processed asynchronously.
* @return True if it must be, false if not.
*/
public boolean forceAsync() {
return forceAsync;
}
private PacketType deprecatedIn(MinecraftVersion version) {
try {
return MinecraftVersion.getCurrentVersion().isAtLeast(version) ? deprecated() : this;
} catch (Throwable ex) {
return deprecated();
}
}
private PacketType deprecated() {
PacketType ret = clone();
ret.deprecated = true;
return ret;
}
public boolean isDeprecated() {
return deprecated;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(protocol, sender, currentId, deprecated); return Objects.hashCode(protocol, sender, currentId);
} }
@Override @Override
@ -1127,8 +1144,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
if (obj instanceof PacketType) { if (obj instanceof PacketType) {
PacketType other = (PacketType) obj; PacketType other = (PacketType) obj;
return deprecated == other.deprecated && return protocol == other.protocol &&
protocol == other.protocol &&
sender == other.sender && sender == other.sender &&
currentId == other.currentId; currentId == other.currentId;
} }
@ -1141,7 +1157,6 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
compare(protocol, other.getProtocol()). compare(protocol, other.getProtocol()).
compare(sender, other.getSender()). compare(sender, other.getSender()).
compare(currentId, other.getCurrentId()). compare(currentId, other.getCurrentId()).
compareTrueFirst(deprecated, other.isDeprecated()).
result(); result();
} }

Datei anzeigen

@ -15,7 +15,7 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.reflect; package com.comphenix.protocol;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -23,8 +23,10 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import com.comphenix.protocol.PacketType;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.common.collect.Sets;
/** /**
* Represents a more modern object-based enum. * Represents a more modern object-based enum.
@ -33,40 +35,53 @@ import com.google.common.collect.HashBiMap;
* want to prevent the creation of additional members dynamically. * want to prevent the creation of additional members dynamically.
* @author Kristian * @author Kristian
*/ */
public class ObjectEnum<T> implements Iterable<T> { public class PacketTypeEnum implements Iterable<PacketType> {
// Used to convert between IDs and names // Used to convert between IDs and names
protected BiMap<T, String> members = HashBiMap.create(); protected Set<PacketType> members = Sets.newHashSet();
/** /**
* Registers every declared integer field. * Registers every declared PacketType field.
* @param fieldType Field type
*/ */
public ObjectEnum(Class<T> fieldType) { public PacketTypeEnum() {
registerAll(fieldType); registerAll();
} }
/** /**
* Registers every public assignable static field as a member. * Registers every public assignable static field as a member.
* @param fieldType Field type
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void registerAll(Class<T> fieldType) { protected void registerAll() {
try { try {
// Register every non-deprecated field // Register every non-deprecated field
for (Field entry : this.getClass().getFields()) { for (Field entry : this.getClass().getFields()) {
if (Modifier.isStatic(entry.getModifiers()) && fieldType.isAssignableFrom(entry.getType())) { if (Modifier.isStatic(entry.getModifiers()) && PacketType.class.isAssignableFrom(entry.getType())) {
T value = (T) entry.get(null); PacketType value = (PacketType) entry.get(null);
if (value == null) {
throw new IllegalArgumentException("Field " + entry.getName() + " was null!");
}
if (value == null) value.setName(entry.getName());
throw new IllegalArgumentException("Field " + entry + " was NULL. Remember to " +
"construct the object after the field has been declared."); if (entry.getAnnotation(PacketType.ForceAsync.class) != null) {
registerMember(value, entry.getName()); value.forceAsync();
}
boolean deprecated = entry.getAnnotation(Deprecated.class) != null;
if (deprecated) value.setDeprecated();
if (members.contains(value)) {
// Replace potentially deprecated packet types with non-deprecated ones
if (!deprecated) {
members.remove(value);
members.add(value);
}
} else {
members.add(value);
} }
} }
} catch (IllegalArgumentException e) { }
e.printStackTrace(); } catch (Exception ex) {
} catch (IllegalAccessException e) { ex.printStackTrace();
e.printStackTrace();
} }
} }
@ -76,11 +91,14 @@ public class ObjectEnum<T> implements Iterable<T> {
* @param name - name of member. * @param name - name of member.
* @return TRUE if the member was registered, FALSE otherwise. * @return TRUE if the member was registered, FALSE otherwise.
*/ */
public boolean registerMember(T instance, String name) { public boolean registerMember(PacketType instance, String name) {
if (!members.containsKey(instance)) { instance.setName(name);
members.put(instance, name);
if (!members.contains(instance)) {
members.add(instance);
return true; return true;
} }
return false; return false;
} }
@ -89,38 +107,36 @@ public class ObjectEnum<T> implements Iterable<T> {
* @param member - the member to check. * @param member - the member to check.
* @return TRUE if the given member has been registered, FALSE otherwise. * @return TRUE if the given member has been registered, FALSE otherwise.
*/ */
public boolean hasMember(T member) { public boolean hasMember(PacketType member) {
return members.containsKey(member); return members.contains(member);
} }
/** /**
* Retrieve a member by name, * Retrieve a member by name,
* @param name - name of member to retrieve. * @param name - name of member to retrieve.
* @return The member, or NULL if not found. * @return The member, or NULL if not found.
* @deprecated Don't use this
*/ */
public T valueOf(String name) { @Deprecated
return members.inverse().get(name); public PacketType valueOf(String name) {
for (PacketType member : members) {
if (member.name().equals(name))
return member;
} }
/** return null;
* Retrieve the name of the given member.
* @param member - the member to retrieve.
* @return Declared name of the member, or NULL if not found.
*/
public String getDeclaredName(T member) {
return members.get(member);
} }
/** /**
* Retrieve every registered member. * Retrieve every registered member.
* @return Enumeration of every value. * @return Enumeration of every value.
*/ */
public Set<T> values() { public Set<PacketType> values() {
return new HashSet<T>(members.keySet()); return new HashSet<>(members);
} }
@Override @Override
public Iterator<T> iterator() { public Iterator<PacketType> iterator() {
return members.keySet().iterator(); return members.iterator();
} }
} }

Datei anzeigen

@ -73,4 +73,10 @@ public class ProtocolLogger {
log("[Debug] " + message, args); log("[Debug] " + message, args);
} }
} }
public static void debug(String message, Throwable ex) {
if (isDebugEnabled()) {
plugin.getLogger().log(Level.WARNING, "[Debug] " + message, ex);
}
}
} }

Datei anzeigen

@ -40,8 +40,6 @@ public class NettyProtocolRegistry extends ProtocolRegistry {
@Override @Override
protected synchronized void initialize() { protected synchronized void initialize() {
ProtocolLogger.debug("Initializing the Netty protocol registry"); // Debug for issue #202
Object[] protocols = enumProtocol.getEnumConstants(); Object[] protocols = enumProtocol.getEnumConstants();
// ID to Packet class maps // ID to Packet class maps
@ -75,8 +73,7 @@ public class NettyProtocolRegistry extends ProtocolRegistry {
result.containers.add(new MapContainer(map)); result.containers.add(new MapContainer(map));
} }
for (int i = 0; i < protocols.length; i++) { for (Object protocol : protocols) {
Object protocol = protocols[i];
Enum<?> enumProtocol = (Enum<?>) protocol; Enum<?> enumProtocol = (Enum<?>) protocol;
Protocol equivalent = Protocol.fromVanilla(enumProtocol); Protocol equivalent = Protocol.fromVanilla(enumProtocol);
@ -103,8 +100,8 @@ public class NettyProtocolRegistry extends ProtocolRegistry {
register.serverPackets.add(type); register.serverPackets.add(type);
if (sender == Sender.CLIENT) if (sender == Sender.CLIENT)
register.clientPackets.add(type); register.clientPackets.add(type);
} catch (IllegalArgumentException ex) { } catch (Exception ex) {
// Sometimes this happens with fake packets, just ignore it ProtocolLogger.debug("Encountered an exception associating packet " + type, ex);
} }
} }
} }

Datei anzeigen

@ -171,16 +171,7 @@ public class PacketRegistry {
initialize(); initialize();
NETTY.synchronize(); NETTY.synchronize();
Set<PacketType> types = new HashSet<>(); return NETTY.getServerPackets();
// Filter out unsupported packets
for (PacketType type : NETTY.getServerPackets()) {
if (!type.isDeprecated()) {
types.add(type);
}
}
return types;
} }
/** /**
@ -207,16 +198,7 @@ public class PacketRegistry {
initialize(); initialize();
NETTY.synchronize(); NETTY.synchronize();
Set<PacketType> types = new HashSet<>(); return NETTY.getClientPackets();
// Filter out unsupported packets
for (PacketType type : NETTY.getClientPackets()) {
if (!type.isDeprecated()) {
types.add(type);
}
}
return types;
} }
/** /**

Datei anzeigen

@ -17,10 +17,7 @@
package com.comphenix.protocol.reflect; package com.comphenix.protocol.reflect;
import java.lang.reflect.Constructor; import java.lang.reflect.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -422,6 +419,30 @@ public class FuzzyReflection {
return fields; return fields;
} }
/**
* Retrieves a field with a given type and parameters. This is most useful
* when dealing with Collections.
*
* @param fieldType Type of the field
* @param params Variable length array of type parameters
* @return The field
*
* @throws IllegalArgumentException If the field cannot be found
*/
public Field getParameterizedField(Class<?> fieldType, Class<?>... params) {
for (Field field : getFields()) {
if (field.getType().equals(fieldType)) {
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
if (Arrays.equals(((ParameterizedType) type).getActualTypeArguments(), params))
return field;
}
}
}
throw new IllegalArgumentException("Unable to find a field with type " + fieldType + " and params " + Arrays.toString(params));
}
/** /**
* Retrieve the first field that matches. * Retrieve the first field that matches.
* <p> * <p>

Datei anzeigen

@ -83,8 +83,8 @@ public final class PacketFilterManager implements ListenerInvoker, InternalManag
new ReportType("%s doesn't depend on ProtocolLib. Check that its plugin.yml has a 'depend' directive."); new ReportType("%s doesn't depend on ProtocolLib. Check that its plugin.yml has a 'depend' directive.");
// Registering packet IDs that are not supported // Registering packet IDs that are not supported
public static final ReportType REPORT_UNSUPPORTED_SERVER_PACKET_ID = new ReportType("[%s] Unsupported server packet ID in current Minecraft version: %s"); public static final ReportType REPORT_UNSUPPORTED_SERVER_PACKET = new ReportType("[%s] Unsupported server packet in current Minecraft version: %s");
public static final ReportType REPORT_UNSUPPORTED_CLIENT_PACKET_ID = new ReportType("[%s] Unsupported client packet ID in current Minecraft version: %s"); public static final ReportType REPORT_UNSUPPORTED_CLIENT_PACKET = new ReportType("[%s] Unsupported client packet in current Minecraft version: %s");
// Problems injecting and uninjecting players // Problems injecting and uninjecting players
public static final ReportType REPORT_CANNOT_UNINJECT_PLAYER = new ReportType("Unable to uninject net handler for player."); public static final ReportType REPORT_CANNOT_UNINJECT_PLAYER = new ReportType("Unable to uninject net handler for player.");
@ -624,7 +624,7 @@ public final class PacketFilterManager implements ListenerInvoker, InternalManag
playerInjection.addPacketHandler(type, listener.getSendingWhitelist().getOptions()); playerInjection.addPacketHandler(type, listener.getSendingWhitelist().getOptions());
else else
reporter.reportWarning(this, reporter.reportWarning(this,
Report.newBuilder(REPORT_UNSUPPORTED_SERVER_PACKET_ID).messageParam(PacketAdapter.getPluginName(listener), type) Report.newBuilder(REPORT_UNSUPPORTED_SERVER_PACKET).messageParam(PacketAdapter.getPluginName(listener), type)
); );
} }
@ -634,7 +634,7 @@ public final class PacketFilterManager implements ListenerInvoker, InternalManag
packetInjector.addPacketHandler(type, listener.getReceivingWhitelist().getOptions()); packetInjector.addPacketHandler(type, listener.getReceivingWhitelist().getOptions());
else else
reporter.reportWarning(this, reporter.reportWarning(this,
Report.newBuilder(REPORT_UNSUPPORTED_CLIENT_PACKET_ID).messageParam(PacketAdapter.getPluginName(listener), type) Report.newBuilder(REPORT_UNSUPPORTED_CLIENT_PACKET).messageParam(PacketAdapter.getPluginName(listener), type)
); );
} }
} }

Datei anzeigen

@ -29,6 +29,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLogger;
import com.comphenix.protocol.concurrency.PacketTypeSet; import com.comphenix.protocol.concurrency.PacketTypeSet;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
@ -132,12 +133,15 @@ public class ProtocolInjector implements ChannelListener {
if (serverConnection != null) { if (serverConnection != null) {
break; break;
} }
} catch (Exception e) { } catch (Exception ex) {
// Try the next though ProtocolLogger.debug("Encountered an exception invoking " + method, ex);
e.printStackTrace();
} }
} }
if (serverConnection == null) {
throw new ReflectiveOperationException("Failed to obtain server connection");
}
// Handle connected channels // Handle connected channels
final ChannelInboundHandler endInitProtocol = new ChannelInitializer<Channel>() { final ChannelInboundHandler endInitProtocol = new ChannelInitializer<Channel>() {
@Override @Override
@ -153,8 +157,8 @@ public class ProtocolInjector implements ChannelListener {
injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject(); injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject();
} }
} }
} catch (Exception e) { } catch (Exception ex) {
reporter.reportDetailed(ProtocolInjector.this, Report.newBuilder(REPORT_CANNOT_INJECT_INCOMING_CHANNEL).messageParam(channel).error(e)); reporter.reportDetailed(ProtocolInjector.this, Report.newBuilder(REPORT_CANNOT_INJECT_INCOMING_CHANNEL).messageParam(channel).error(ex));
} }
} }
}; };
@ -183,21 +187,22 @@ public class ProtocolInjector implements ChannelListener {
FuzzyReflection fuzzy = FuzzyReflection.fromObject(serverConnection, true); FuzzyReflection fuzzy = FuzzyReflection.fromObject(serverConnection, true);
try { try {
List<Field> fields = fuzzy.getFieldListByType(List.class); Field field = fuzzy.getParameterizedField(List.class, MinecraftReflection.getNetworkManagerClass());
for (Field field : fields) {
ParameterizedType param = (ParameterizedType) field.getGenericType();
if (param.getActualTypeArguments()[0].equals(MinecraftReflection.getNetworkManagerClass())) {
field.setAccessible(true); field.setAccessible(true);
networkManagers = (List<Object>) field.get(serverConnection); networkManagers = (List<Object>) field.get(serverConnection);
}
}
} catch (Exception ex) { } catch (Exception ex) {
networkManagers = (List<Object>) fuzzy.getMethodByParameters("getNetworkManagers", List.class, serverConnection.getClass()) ProtocolLogger.debug("Encountered an exception checking list fields", ex);
.invoke(null, serverConnection);
Method method = fuzzy.getMethodByParameters("getNetworkManagers", List.class,
new Class<?>[] { serverConnection.getClass() });
method.setAccessible(true);
networkManagers = (List<Object>) method.invoke(null, serverConnection);
} }
if (networkManagers == null) { if (networkManagers == null) {
throw new RuntimeException("Failed to obtain list of network managers."); throw new ReflectiveOperationException("Failed to obtain list of network managers");
} }
// Insert ProtocolLib's connection interceptor // Insert ProtocolLib's connection interceptor
@ -376,7 +381,7 @@ public class ProtocolInjector implements ChannelListener {
@Override @Override
public void addPacketHandler(PacketType type, Set<ListenerOptions> options) { public void addPacketHandler(PacketType type, Set<ListenerOptions> options) {
if (options != null && !type.forceAsync() && !options.contains(ListenerOptions.ASYNC)) if (!type.isAsyncForced() && (options == null || !options.contains(ListenerOptions.ASYNC)))
mainThreadFilters.addType(type); mainThreadFilters.addType(type);
super.addPacketHandler(type, options); super.addPacketHandler(type, options);
} }

Datei anzeigen

@ -23,6 +23,7 @@ import java.util.TreeMap;
import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.PacketType.Sender; import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import net.minecraft.server.v1_12_R1.EnumProtocol; import net.minecraft.server.v1_12_R1.EnumProtocol;
import net.minecraft.server.v1_12_R1.EnumProtocolDirection; import net.minecraft.server.v1_12_R1.EnumProtocolDirection;
@ -54,6 +55,16 @@ public class PacketTypeTest {
assertEquals(PacketLoginInStart.class, PacketType.Login.Client.START.getPacketClass()); assertEquals(PacketLoginInStart.class, PacketType.Login.Client.START.getPacketClass());
} }
@Test
public void testDeprecation() {
assertTrue("Packet isn't properly deprecated", PacketType.Status.Server.OUT_SERVER_INFO.isDeprecated());
assertTrue("Deprecated packet isn't properly included",
PacketRegistry.getServerPacketTypes().contains(PacketType.Status.Server.OUT_SERVER_INFO));
assertFalse("Packet isn't properly deprecated", PacketType.Play.Server.CHAT.isDeprecated());
assertEquals("Deprecated packets aren't equal", PacketType.Status.Server.OUT_SERVER_INFO,
PacketType.Status.Server.SERVER_INFO);
}
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void ensureTypesAreCorrect() throws Exception { public void ensureTypesAreCorrect() throws Exception {

Datei anzeigen

@ -527,7 +527,7 @@ public class PacketContainerTest {
private static final List<PacketType> BLACKLISTED = Util.asList( private static final List<PacketType> BLACKLISTED = Util.asList(
PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD, PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD,
PacketType.Play.Server.SET_COOLDOWN, PacketType.Play.Server.REL_ENTITY_MOVE_LOOK PacketType.Play.Server.SET_COOLDOWN
); );
@Test @Test
@ -544,9 +544,9 @@ public class PacketContainerTest {
try { try {
PacketContainer constructed = new PacketContainer(type); PacketContainer constructed = new PacketContainer(type);
// if (!registered) { if (!registered) {
// fail("Expected IllegalArgumentException(Packet " + type + " not registered)"); fail("Expected IllegalArgumentException(Packet " + type + " not registered)");
// } }
// Initialize default values // Initialize default values
constructed.getModifier().writeDefaults(); constructed.getModifier().writeDefaults();