Skip to content

Commit 05ffeb8

Browse files
committed
Restore compatibility with 1.8
1.8.8 is still one of the most popular server versions. As a result, many servers were not receiving important fixes and new APIs. I was able to accomplish this with minimal bloat, making it worth it. Upon release, 4.1.0 will become the recommended version for 1.8 thru the current Spigot build.
1 parent 6c982a8 commit 05ffeb8

File tree

8 files changed

+369
-89
lines changed

8 files changed

+369
-89
lines changed

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

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Note that vanilla Minecraft reuses packet IDs per protocol (ping, game, login) and IDs are subject to change, so they are not reliable.
3232
* @author Kristian
3333
*/
34-
public class PacketType implements Serializable, Comparable<PacketType> {
34+
public class PacketType implements Serializable, Cloneable, Comparable<PacketType> {
3535
// Increment whenever the type changes
3636
private static final long serialVersionUID = 1L;
3737

@@ -182,29 +182,53 @@ public static class Server extends ObjectEnum<PacketType> {
182182
public static final PacketType UPDATE_ATTRIBUTES = new PacketType(PROTOCOL, SENDER, 0x4B, 0x20, "UpdateAttributes");
183183
public static final PacketType ENTITY_EFFECT = new PacketType(PROTOCOL, SENDER, 0x4C, 0x1D, "EntityEffect");
184184

185+
// ---- Removed in 1.9
186+
187+
/**
188+
* @deprecated Removed in 1.9
189+
*/
190+
@Deprecated
191+
public static final PacketType MAP_CHUNK_BULK = new PacketType(PROTOCOL, SENDER, -1, -1, "MapChunkBulk").deprecated();
192+
193+
/**
194+
* @deprecated Removed in 1.9
195+
*/
196+
@Deprecated
197+
public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, -1, -1, "SetCompression").deprecated();
198+
199+
/**
200+
* @deprecated Removed in 1.9
201+
*/
202+
@Deprecated
203+
public static final PacketType UPDATE_ENTITY_NBT = new PacketType(PROTOCOL, SENDER, -1, -1, "UpdateEntityNBT").deprecated();
204+
205+
// ----- Renamed packets
206+
185207
/**
186-
* @deprecated Replaced by {@link WINDOW_DATA}
208+
* @deprecated Renamed to {@link WINDOW_DATA}
187209
*/
188210
@Deprecated
189-
public static final PacketType CRAFT_PROGRESS_BAR = WINDOW_DATA;
211+
public static final PacketType CRAFT_PROGRESS_BAR = WINDOW_DATA.deprecated();
190212

191213
/**
192-
* @deprecated Replaced by {@link REL_ENTITY_MOVE_LOOK}
214+
* @deprecated Renamed to {@link REL_ENTITY_MOVE_LOOK}
193215
*/
194216
@Deprecated
195-
public static final PacketType ENTITY_MOVE_LOOK = REL_ENTITY_MOVE_LOOK;
217+
public static final PacketType ENTITY_MOVE_LOOK = REL_ENTITY_MOVE_LOOK.deprecated();
196218

197219
/**
198-
* @deprecated Replaced by {@link STATISTIC}
220+
* @deprecated Renamed to {@link STATISTIC}
199221
*/
200222
@Deprecated
201-
public static final PacketType STATISTICS = STATISTIC;
223+
public static final PacketType STATISTICS = STATISTIC.deprecated();
224+
225+
// ----- Replaced in 1.9.4
202226

203227
/**
204228
* @deprecated Replaced by {@link TILE_ENTITY_DATA}
205229
*/
206230
@Deprecated
207-
public static final PacketType UPDATE_SIGN = TILE_ENTITY_DATA;
231+
public static final PacketType UPDATE_SIGN = TILE_ENTITY_DATA.deprecated();
208232

209233
// The instance must
210234
private final static Server INSTANCE = new Server();
@@ -297,7 +321,7 @@ public static class Server extends ObjectEnum<PacketType> {
297321
* @deprecated Replaced by {@link SERVER_INFO}
298322
*/
299323
@Deprecated
300-
public static final PacketType OUT_SERVER_INFO = SERVER_INFO;
324+
public static final PacketType OUT_SERVER_INFO = SERVER_INFO.deprecated();
301325

302326
private final static Server INSTANCE = new Server();
303327

@@ -562,6 +586,7 @@ public String getPacketName() {
562586

563587
private boolean forceAsync;
564588
private boolean dynamic;
589+
private boolean deprecated;
565590

566591
/**
567592
* Retrieve the current packet/legacy lookup.
@@ -1043,6 +1068,16 @@ public boolean forceAsync() {
10431068
return forceAsync;
10441069
}
10451070

1071+
private PacketType deprecated() {
1072+
PacketType ret = clone();
1073+
ret.deprecated = true;
1074+
return ret;
1075+
}
1076+
1077+
public boolean isDeprecated() {
1078+
return deprecated;
1079+
}
1080+
10461081
@Override
10471082
public int hashCode() {
10481083
return Objects.hashCode(protocol, sender, currentId, legacyId);
@@ -1078,8 +1113,17 @@ public String toString() {
10781113
Class<?> clazz = getPacketClass();
10791114

10801115
if (clazz == null)
1081-
return "UNREGISTERED[" + protocol + ", " + sender + ", " + currentId + ", legacy: " + legacyId + ", classNames: " + Arrays.toString(classNames) + "]";
1116+
return name() + "[" + protocol + ", " + sender + ", " + currentId + ", legacy: " + legacyId + ", classNames: " + Arrays.toString(classNames) + " (unregistered)]";
10821117
else
10831118
return clazz.getSimpleName() + "[" + currentId + ", legacy: " + legacyId + "]";
10841119
}
1120+
1121+
@Override
1122+
public PacketType clone() {
1123+
try {
1124+
return (PacketType) super.clone();
1125+
} catch (CloneNotSupportedException ex) {
1126+
throw new Error("This shouldn't happen", ex);
1127+
}
1128+
}
10851129
}

modules/API/src/main/java/com/comphenix/protocol/ProtocolLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ProtocolLibrary {
3434
/**
3535
* The minimum version ProtocolLib has been tested with.
3636
*/
37-
public static final String MINIMUM_MINECRAFT_VERSION = "1.9";
37+
public static final String MINIMUM_MINECRAFT_VERSION = "1.8";
3838

3939
/**
4040
* The maximum version ProtocolLib has been tested with.

modules/API/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.comphenix.protocol.injector.packet;
1919

2020
import java.util.Collections;
21+
import java.util.HashSet;
2122
import java.util.Map;
2223
import java.util.Map.Entry;
2324
import java.util.Set;
@@ -28,6 +29,7 @@
2829
import com.comphenix.protocol.injector.netty.NettyProtocolRegistry;
2930
import com.comphenix.protocol.injector.netty.ProtocolRegistry;
3031
import com.comphenix.protocol.reflect.FieldAccessException;
32+
import com.comphenix.protocol.utility.MinecraftReflection;
3133
import com.google.common.base.Function;
3234
import com.google.common.collect.Maps;
3335
import com.google.common.collect.Sets;
@@ -154,11 +156,10 @@ public static Map<Integer, Class> getPreviousPackets() {
154156
*/
155157
@Deprecated
156158
public static Set<Integer> getServerPackets() throws FieldAccessException {
157-
initialize();
158-
159159
if (LEGACY_SERVER_PACKETS == null) {
160-
LEGACY_SERVER_PACKETS = toLegacy(NETTY.getServerPackets());
160+
LEGACY_SERVER_PACKETS = toLegacy(getServerPacketTypes());
161161
}
162+
162163
return LEGACY_SERVER_PACKETS;
163164
}
164165

@@ -168,9 +169,18 @@ public static Set<Integer> getServerPackets() throws FieldAccessException {
168169
*/
169170
public static Set<PacketType> getServerPacketTypes() {
170171
initialize();
171-
172172
NETTY.synchronize();
173-
return NETTY.getServerPackets();
173+
174+
Set<PacketType> types = new HashSet<>();
175+
176+
// Filter out unsupported packets
177+
for (PacketType type : NETTY.getServerPackets()) {
178+
if (!type.isDeprecated()) {
179+
types.add(type);
180+
}
181+
}
182+
183+
return types;
174184
}
175185

176186
/**
@@ -182,11 +192,10 @@ public static Set<PacketType> getServerPacketTypes() {
182192
*/
183193
@Deprecated
184194
public static Set<Integer> getClientPackets() throws FieldAccessException {
185-
initialize();
186-
187195
if (LEGACY_CLIENT_PACKETS == null) {
188-
LEGACY_CLIENT_PACKETS = toLegacy(NETTY.getClientPackets());
196+
LEGACY_CLIENT_PACKETS = toLegacy(getClientPacketTypes());
189197
}
198+
190199
return LEGACY_CLIENT_PACKETS;
191200
}
192201

@@ -196,9 +205,18 @@ public static Set<Integer> getClientPackets() throws FieldAccessException {
196205
*/
197206
public static Set<PacketType> getClientPacketTypes() {
198207
initialize();
199-
200208
NETTY.synchronize();
201-
return NETTY.getClientPackets();
209+
210+
Set<PacketType> types = new HashSet<>();
211+
212+
// Filter out unsupported packets
213+
for (PacketType type : NETTY.getClientPackets()) {
214+
if (!type.isDeprecated()) {
215+
types.add(type);
216+
}
217+
}
218+
219+
return types;
202220
}
203221

204222
/**
@@ -269,7 +287,24 @@ public static Class getPacketClassFromType(PacketType type) {
269287
*/
270288
public static Class getPacketClassFromType(PacketType type, boolean forceVanilla) {
271289
initialize();
272-
return NETTY.getPacketTypeLookup().get(type);
290+
291+
// Try the lookup first
292+
Class<?> clazz = NETTY.getPacketTypeLookup().get(type);
293+
if (clazz != null) {
294+
return clazz;
295+
}
296+
297+
// Then try looking up the class names
298+
for (String name : type.getClassNames()) {
299+
try {
300+
clazz = MinecraftReflection.getMinecraftClass(name);
301+
break;
302+
} catch (Exception ex) {
303+
}
304+
}
305+
306+
// TODO Cache the result?
307+
return clazz;
273308
}
274309

275310
/**

modules/API/src/main/java/com/comphenix/protocol/reflect/ObjectEnum.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ObjectEnum(Class<T> fieldType) {
5252
@SuppressWarnings("unchecked")
5353
protected void registerAll(Class<T> fieldType) {
5454
try {
55-
// Register every int field
55+
// Register every non-deprecated field
5656
for (Field entry : this.getClass().getFields()) {
5757
if (Modifier.isStatic(entry.getModifiers()) && fieldType.isAssignableFrom(entry.getType())) {
5858
T value = (T) entry.get(null);
@@ -63,7 +63,6 @@ protected void registerAll(Class<T> fieldType) {
6363
registerMember(value, entry.getName());
6464
}
6565
}
66-
6766
} catch (IllegalArgumentException e) {
6867
e.printStackTrace();
6968
} catch (IllegalAccessException e) {

modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public class MinecraftReflection {
152152

153153
// Whether or not we are using netty
154154
private static Boolean cachedNetty;
155+
private static Boolean cachedWatcherObject;
155156

156157
private MinecraftReflection() {
157158
// No need to make this constructable.
@@ -1313,16 +1314,23 @@ public static Class<?> getDataWatcherItemClass() {
13131314
}
13141315

13151316
public static Class<?> getDataWatcherObjectClass() {
1316-
// TODO Implement a fallback
1317-
return getMinecraftClass("DataWatcherObject");
1317+
try {
1318+
return getMinecraftClass("DataWatcherObject");
1319+
} catch (RuntimeException ex) {
1320+
return null;
1321+
}
13181322
}
13191323

13201324
public static boolean watcherObjectExists() {
1321-
try {
1322-
return getDataWatcherObjectClass() != null;
1323-
} catch (RuntimeException e) {
1324-
return false;
1325+
if (cachedWatcherObject == null) {
1326+
try {
1327+
return cachedWatcherObject = getDataWatcherObjectClass() != null;
1328+
} catch (Throwable ex) {
1329+
return cachedWatcherObject = false;
1330+
}
13251331
}
1332+
1333+
return cachedWatcherObject;
13261334
}
13271335

13281336
public static Class<?> getDataWatcherSerializerClass() {

0 commit comments

Comments
 (0)