Skip to content

Commit 9ca7c91

Browse files
committed
Simplify packet registry
1 parent 466354c commit 9ca7c91

File tree

12 files changed

+310
-530
lines changed

12 files changed

+310
-530
lines changed

src/main/java/com/comphenix/protocol/PacketType.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public static class Server extends PacketTypeEnum {
187187
public static final PacketType UPDATE_HEALTH = new PacketType(PROTOCOL, SENDER, 0x52, "UpdateHealth", "SPacketUpdateHealth");
188188
public static final PacketType SCOREBOARD_OBJECTIVE = new PacketType(PROTOCOL, SENDER, 0x53, "ScoreboardObjective", "SPacketScoreboardObjective");
189189
public static final PacketType MOUNT = new PacketType(PROTOCOL, SENDER, 0x54, "Mount", "SPacketSetPassengers");
190-
public static final PacketType SCOREBOARD_TEAM = new PacketType(PROTOCOL, SENDER, 0x55, "ScoreboardTeam$b", "ScoreboardTeam", "SPacketTeams");
190+
public static final PacketType SCOREBOARD_TEAM = new PacketType(PROTOCOL, SENDER, 0x55, "ScoreboardTeam", "SPacketTeams");
191191
public static final PacketType SCOREBOARD_SCORE = new PacketType(PROTOCOL, SENDER, 0x56, "ScoreboardScore", "SPacketUpdateScore");
192192
public static final PacketType SET_SUBTITLE_TEXT = new PacketType(PROTOCOL, SENDER, 0x57, "SetSubtitleText");
193193
public static final PacketType UPDATE_TIME = new PacketType(PROTOCOL, SENDER, 0x58, "UpdateTime", "SPacketTimeUpdate");
@@ -647,7 +647,7 @@ public String getMojangName() {
647647
/**
648648
* Protocol version of all the current IDs.
649649
*/
650-
private static final MinecraftVersion PROTOCOL_VERSION = MinecraftVersion.NETHER_UPDATE;
650+
private static final MinecraftVersion PROTOCOL_VERSION = MinecraftVersion.CAVES_CLIFFS_1;
651651

652652
private final Protocol protocol;
653653
private final Sender sender;
@@ -935,7 +935,9 @@ public static Collection<PacketType> fromName(String name) {
935935
* Determine if a given class represents a packet class.
936936
* @param packetClass - the class to lookup.
937937
* @return TRUE if this is a packet class, FALSE otherwise.
938+
* @deprecated Doesn't really have a purpose
938939
*/
940+
@Deprecated
939941
public static boolean hasClass(Class<?> packetClass) {
940942
return PacketRegistry.getPacketType(packetClass) != null;
941943
}
@@ -1090,11 +1092,7 @@ public List<String> getClassNames() {
10901092
* @return The packet class, or NULL if not found.
10911093
*/
10921094
public Class<?> getPacketClass() {
1093-
try {
1094-
return PacketRegistry.getPacketClassFromType(this);
1095-
} catch (Exception e) {
1096-
return null;
1097-
}
1095+
return PacketRegistry.tryGetPacketClass(this).orElse(null);
10981096
}
10991097

11001098
// Only used by Enum processor

src/main/java/com/comphenix/protocol/PacketTypeEnum.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,6 @@ public boolean registerMember(PacketType instance, String name) {
110110
public boolean hasMember(PacketType member) {
111111
return members.contains(member);
112112
}
113-
114-
/**
115-
* Retrieve a member by name,
116-
* @param name - name of member to retrieve.
117-
* @return The member, or NULL if not found.
118-
* @deprecated Don't use this
119-
*/
120-
@Deprecated
121-
public PacketType valueOf(String name) {
122-
for (PacketType member : members) {
123-
if (member.name().equals(name))
124-
return member;
125-
}
126-
127-
return null;
128-
}
129113

130114
/**
131115
* Retrieve every registered member.

src/main/java/com/comphenix/protocol/concurrency/PacketTypeSet.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public PacketTypeSet(Collection<? extends PacketType> values) {
3232
* @param type - the type to add.
3333
*/
3434
public synchronized void addType(PacketType type) {
35-
Class<?> packetClass = getPacketClass(type);
35+
Class<?> packetClass = type.getPacketClass();
3636
types.add(Preconditions.checkNotNull(type, "type cannot be NULL."));
3737

3838
if (packetClass != null) {
39-
classes.add(getPacketClass(type));
39+
classes.add(type.getPacketClass());
4040
}
4141
}
4242

@@ -55,11 +55,11 @@ public synchronized void addAll(Iterable<? extends PacketType> types) {
5555
* @param type - the type to remove.
5656
*/
5757
public synchronized void removeType(PacketType type) {
58-
Class<?> packetClass = getPacketClass(type);
58+
Class<?> packetClass = type.getPacketClass();
5959
types.remove(Preconditions.checkNotNull(type, "type cannot be NULL."));
6060

6161
if (packetClass != null) {
62-
classes.remove(getPacketClass(type));
62+
classes.remove(packetClass);
6363
}
6464
}
6565

@@ -72,16 +72,7 @@ public synchronized void removeAll(Iterable<? extends PacketType> types) {
7272
removeType(type);
7373
}
7474
}
75-
76-
/**
77-
* Retrieve the packet class associated with a particular type.
78-
* @param type - the packet type.
79-
* @return The associated packet type.
80-
*/
81-
protected Class<?> getPacketClass(PacketType type) {
82-
return PacketRegistry.getPacketClassFromType(type);
83-
}
84-
75+
8576
/**
8677
* Determine if the given packet type exists in the set.
8778
* @param type - the type to find.

src/main/java/com/comphenix/protocol/injector/ListenerInvoker.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ public interface ListenerInvoker {
3939
*/
4040
void invokePacketSending(PacketEvent event);
4141

42-
/**
43-
* Retrieve the associated ID of a packet.
44-
* @param packet - the packet.
45-
* @return The packet ID.
46-
*/
47-
@Deprecated
48-
int getPacketID(Object packet);
49-
5042
/**
5143
* Retrieve the associated type of a packet.
5244
* @param packet - the packet.

src/main/java/com/comphenix/protocol/injector/PacketConstructor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Constructor;
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.util.List;
23+
import java.util.Optional;
2324

2425
import com.comphenix.protocol.PacketType;
2526
import com.comphenix.protocol.error.RethrowErrorReporter;
@@ -146,13 +147,11 @@ public PacketConstructor withPacket(PacketType type, Object[] values) {
146147
types[i] = Object.class;
147148
}
148149
}
149-
Class<?> packetType = PacketRegistry.getPacketClassFromType(type, true);
150-
151-
if (packetType == null)
152-
throw new IllegalArgumentException("Could not find a packet by the type " + type);
153-
150+
151+
Class<?> packetClass = PacketRegistry.getPacketClassFromType(type);
152+
154153
// Find the correct constructor
155-
for (Constructor<?> constructor : packetType.getConstructors()) {
154+
for (Constructor<?> constructor : packetClass.getConstructors()) {
156155
Class<?>[] params = constructor.getParameterTypes();
157156

158157
if (isCompatible(types, params)) {

src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -979,12 +979,6 @@ private int getPhaseLoginCount() {
979979
return phaseLoginCount.get();
980980
}
981981

982-
@Override
983-
@Deprecated
984-
public int getPacketID(Object packet) {
985-
return PacketRegistry.getPacketID(packet.getClass());
986-
}
987-
988982
@Override
989983
public PacketType getPacketType(Object packet) {
990984
if (packet == null)

src/main/java/com/comphenix/protocol/injector/StructureCache.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ public static Object newPacket(Class<?> clazz) {
9292
* @return Created packet.
9393
*/
9494
public static Object newPacket(PacketType type) {
95-
Class<?> clazz = PacketRegistry.getPacketClassFromType(type, true);
96-
if (clazz != null) {
97-
return newPacket(clazz);
98-
}
99-
throw new IllegalArgumentException("Cannot find associated packet class: " + type);
95+
return newPacket(PacketRegistry.getPacketClassFromType(type));
10096
}
10197

10298
/**
@@ -146,7 +142,7 @@ public static StructureModifier<Object> getStructure(final PacketType type, bool
146142
if (result == null) {
147143
// Use the vanilla class definition
148144
final StructureModifier<Object> value = new StructureModifier<>(
149-
PacketRegistry.getPacketClassFromType(type, true), MinecraftReflection.getPacketClass(), true);
145+
PacketRegistry.getPacketClassFromType(type), MinecraftReflection.getPacketClass(), true);
150146

151147
result = structureModifiers.putIfAbsent(type, value);
152148

0 commit comments

Comments
 (0)