Skip to content

Commit 03d7be1

Browse files
authored
Update for 1.20.2 (#2501)
1 parent f0401ac commit 03d7be1

32 files changed

+890
-326
lines changed

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ repositories {
3434

3535
dependencies {
3636
implementation 'net.bytebuddy:byte-buddy:1.14.3'
37-
compileOnly 'org.spigotmc:spigot-api:1.20-R0.1-SNAPSHOT'
38-
compileOnly 'org.spigotmc:spigot:1.20-R0.1-SNAPSHOT'
37+
compileOnly 'org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT'
38+
compileOnly 'org.spigotmc:spigot:1.20.2-R0.1-SNAPSHOT'
3939
compileOnly 'io.netty:netty-all:4.0.23.Final'
4040
compileOnly 'net.kyori:adventure-text-serializer-gson:4.13.0'
4141
compileOnly 'com.googlecode.json-simple:json-simple:1.1.1'
@@ -44,9 +44,9 @@ dependencies {
4444
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
4545
testImplementation 'org.mockito:mockito-core:4.11.0'
4646
testImplementation 'org.mockito:mockito-inline:4.11.0'
47-
testImplementation 'io.netty:netty-common:4.1.77.Final'
48-
testImplementation 'io.netty:netty-transport:4.1.77.Final'
49-
testImplementation 'org.spigotmc:spigot:1.20-R0.1-SNAPSHOT'
47+
testImplementation 'io.netty:netty-common:4.1.97.Final'
48+
testImplementation 'io.netty:netty-transport:4.1.97.Final'
49+
testImplementation 'org.spigotmc:spigot:1.20.2-R0.1-SNAPSHOT'
5050
testImplementation 'net.kyori:adventure-text-serializer-gson:4.13.0'
5151
testImplementation 'net.kyori:adventure-text-serializer-plain:4.13.1'
5252
}

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

Lines changed: 220 additions & 158 deletions
Large diffs are not rendered by default.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public static class ClassLookup {
6060
public final Map<String, PacketType> STATUS_SERVER = new ConcurrentHashMap<>();
6161
public final Map<String, PacketType> LOGIN_CLIENT = new ConcurrentHashMap<>();
6262
public final Map<String, PacketType> LOGIN_SERVER = new ConcurrentHashMap<>();
63-
63+
public final Map<String, PacketType> CONFIGURATION_CLIENT = new ConcurrentHashMap<>();
64+
public final Map<String, PacketType> CONFIGURATION_SERVER = new ConcurrentHashMap<>();
65+
6466
/**
6567
* Retrieve the correct integer map for a specific protocol and sender.
6668
* @param protocol - the protocol.
@@ -77,6 +79,8 @@ public Map<String, PacketType> getMap(Protocol protocol, Sender sender) {
7779
return sender == Sender.CLIENT ? STATUS_CLIENT : STATUS_SERVER;
7880
case LOGIN:
7981
return sender == Sender.CLIENT ? LOGIN_CLIENT : LOGIN_SERVER;
82+
case CONFIGURATION:
83+
return sender == Sender.CLIENT ? CONFIGURATION_CLIENT : CONFIGURATION_SERVER;
8084
default:
8185
throw new IllegalArgumentException("Unable to find protocol " + protocol);
8286
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public class ProtocolLibrary {
3737
/**
3838
* The maximum version ProtocolLib has been tested with.
3939
*/
40-
public static final String MAXIMUM_MINECRAFT_VERSION = "1.20.1";
40+
public static final String MAXIMUM_MINECRAFT_VERSION = "1.20.2";
4141

4242
/**
43-
* The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.20.1) was released.
43+
* The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.20.2) was released.
4444
*/
45-
public static final String MINECRAFT_LAST_RELEASE_DATE = "2023-06-12";
45+
public static final String MINECRAFT_LAST_RELEASE_DATE = "2023-09-21";
4646

4747
/**
4848
* Plugins that are currently incompatible with ProtocolLib.

src/main/java/com/comphenix/protocol/events/AbstractStructure.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,16 @@ public StructureModifier<MinecraftKey> getMinecraftKeys() {
867867
MinecraftKey.getConverter());
868868
}
869869

870+
/**
871+
* Retrieve a read/write structure for custom packet payloads (available since Minecraft 1.20.2).
872+
* @return A modifier for CustomPacketPayloads fields.
873+
*/
874+
public StructureModifier<CustomPacketPayloadWrapper> getCustomPacketPayloads() {
875+
return structureModifier.withType(
876+
CustomPacketPayloadWrapper.getCustomPacketPayloadClass(),
877+
CustomPacketPayloadWrapper.getConverter());
878+
}
879+
870880
/**
871881
* Retrieve a read/write structure for dimension IDs in 1.13.1+
872882
* @return A modifier for dimension IDs

src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.bukkit.entity.Player;
4141
import org.bukkit.profile.PlayerProfile;
4242
import org.jetbrains.annotations.NotNull;
43+
import org.jetbrains.annotations.Nullable;
4344

4445
import java.io.IOException;
4546
import java.io.ObjectInputStream;
@@ -48,6 +49,9 @@
4849
import java.lang.reflect.Constructor;
4950
import java.lang.reflect.InvocationTargetException;
5051
import java.lang.reflect.Method;
52+
import java.time.Duration;
53+
import java.time.Instant;
54+
import java.util.Date;
5155
import java.util.Map;
5256
import java.util.UUID;
5357
import java.util.concurrent.ConcurrentHashMap;
@@ -240,6 +244,24 @@ public boolean isBanned() {
240244
return banned;
241245
}
242246

247+
@Nullable
248+
public BanEntry<PlayerProfile> ban(@Nullable String s, @Nullable Date date, @Nullable String s1) {
249+
setBanned(true);
250+
return null;
251+
}
252+
253+
@Nullable
254+
public BanEntry<PlayerProfile> ban(@Nullable String s, @Nullable Instant instant, @Nullable String s1) {
255+
setBanned(true);
256+
return null;
257+
}
258+
259+
@Nullable
260+
public BanEntry<PlayerProfile> ban(@Nullable String s, @Nullable Duration duration, @Nullable String s1) {
261+
setBanned(true);
262+
return null;
263+
}
264+
243265
public void setBanned(boolean banned) {
244266
this.banned = banned;
245267
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ public static boolean tryInitTrickDataSerializer() {
179179
DynamicType.Builder<?> baseBuilder = ByteBuddyFactory.getInstance()
180180
.createSubclass(MinecraftReflection.getPacketDataSerializerClass())
181181
.name(MinecraftMethods.class.getPackage().getName() + ".ProtocolLibTricksNmsDataSerializerBase")
182-
.method(ElementMatchers.returns(MinecraftReflection.getNBTCompoundClass())
183-
.and(ElementMatchers.takesArguments(MinecraftReflection.getNBTReadLimiterClass())))
182+
.method(ElementMatchers.takesArguments(MinecraftReflection.getNBTReadLimiterClass())
183+
.and(ElementMatchers.returns(ElementMatchers.isSubTypeOf(MinecraftReflection.getNBTBaseClass()))))
184184
.intercept(FixedValue.value(compound))
185185
.method(ElementMatchers.returns(MinecraftReflection.getIChatBaseComponentClass()))
186186
.intercept(FixedValue.value(textCompound))

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import com.comphenix.protocol.ProtocolLogger;
2828
import com.comphenix.protocol.reflect.FuzzyReflection;
2929
import com.comphenix.protocol.reflect.StructureModifier;
30+
import com.comphenix.protocol.reflect.fuzzy.FuzzyClassContract;
3031
import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract;
32+
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
3133
import com.comphenix.protocol.utility.MinecraftReflection;
3234
import com.comphenix.protocol.utility.MinecraftVersion;
3335

@@ -158,8 +160,10 @@ private static synchronized Register createNewRegister() {
158160
final Map<Object, Map<Class<?>, Integer>> clientMaps = new LinkedHashMap<>();
159161

160162
Register result = new Register();
163+
161164
Field mainMapField = null;
162165
Field packetMapField = null;
166+
Field holderClassField = null; // only 1.20.2+
163167

164168
// Iterate through the protocols
165169
for (Object protocol : protocols) {
@@ -184,7 +188,26 @@ private static synchronized Register createNewRegister() {
184188
for (Map.Entry<Object, Object> entry : directionMap.entrySet()) {
185189
Object holder = entry.getValue();
186190
if (packetMapField == null) {
187-
FuzzyReflection fuzzy = FuzzyReflection.fromClass(holder.getClass(), true);
191+
Class<?> packetHolderClass = holder.getClass();
192+
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
193+
FuzzyReflection holderFuzzy = FuzzyReflection.fromClass(packetHolderClass, true);
194+
holderClassField = holderFuzzy.getField(FuzzyFieldContract.newBuilder()
195+
.banModifier(Modifier.STATIC)
196+
.requireModifier(Modifier.FINAL)
197+
.typeMatches(FuzzyClassContract.newBuilder()
198+
.method(FuzzyMethodContract.newBuilder()
199+
.returnTypeExact(MinecraftReflection.getPacketClass())
200+
.parameterCount(2)
201+
.parameterExactType(int.class, 0)
202+
.parameterExactType(MinecraftReflection.getPacketDataSerializerClass(), 1)
203+
.build())
204+
.build())
205+
.build());
206+
holderClassField.setAccessible(true);
207+
packetHolderClass = holderClassField.getType();
208+
}
209+
210+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(packetHolderClass, true);
188211
packetMapField = fuzzy.getField(FuzzyFieldContract.newBuilder()
189212
.banModifier(Modifier.STATIC)
190213
.requireModifier(Modifier.FINAL)
@@ -193,10 +216,18 @@ private static synchronized Register createNewRegister() {
193216
packetMapField.setAccessible(true);
194217
}
195218

196-
Map<Class<?>, Integer> packetMap;
219+
Object holderInstance = holder;
220+
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
221+
try {
222+
holderInstance = holderClassField.get(holder);
223+
} catch (ReflectiveOperationException ex) {
224+
throw new RuntimeException("Failed to access packet map", ex);
225+
}
226+
}
197227

228+
Map<Class<?>, Integer> packetMap;
198229
try {
199-
packetMap = (Map<Class<?>, Integer>) packetMapField.get(holder);
230+
packetMap = (Map<Class<?>, Integer>) packetMapField.get(holderInstance);
200231
} catch (ReflectiveOperationException ex) {
201232
throw new RuntimeException("Failed to access packet map", ex);
202233
}

src/main/java/com/comphenix/protocol/utility/MinecraftFields.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.comphenix.protocol.utility;
22

33
import com.comphenix.protocol.injector.BukkitUnwrapper;
4+
import com.comphenix.protocol.reflect.FuzzyReflection;
45
import com.comphenix.protocol.reflect.accessors.Accessors;
56
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
67
import org.bukkit.entity.Player;
@@ -33,7 +34,13 @@ public static Object getNetworkManager(Player player) {
3334
if (NETWORK_ACCESSOR == null) {
3435
Class<?> networkClass = MinecraftReflection.getNetworkManagerClass();
3536
Class<?> connectionClass = MinecraftReflection.getPlayerConnectionClass();
36-
NETWORK_ACCESSOR = Accessors.getFieldAccessor(connectionClass, networkClass, true);
37+
NETWORK_ACCESSOR = FuzzyReflection.fromClass(connectionClass, true)
38+
.getDeclaredFields(Object.class)
39+
.stream()
40+
.filter(field -> field.getType().equals(networkClass))
41+
.findFirst()
42+
.map(Accessors::getFieldAccessor)
43+
.orElseThrow(() -> new IllegalArgumentException("Unable to find the NetworkManager field in PlayerConnection"));
3744
}
3845

3946
// Retrieve the network manager

src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private static Constructor<?> setupProxyConstructor() {
171171
.method(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class)))
172172
.intercept(MethodDelegation.to(new Object() {
173173
@RuntimeType
174-
public Object delegate(@SuperCall Callable<?> zuper, @Origin Method method) throws Exception {
174+
public Object delegate(@SuperCall(nullIfImpossible = true) Callable<?> zuper, @Origin Method method) throws Exception {
175175
if (method.getName().contains("read")) {
176176
throw new ReadMethodException();
177177
}
@@ -203,7 +203,8 @@ private static void initializePacket() {
203203
}
204204

205205
// constructs a new decorated serializer
206-
Object decoratedSerializer = decoratedDataSerializerAccessor.invoke(Unpooled.EMPTY_BUFFER);
206+
Object serializerBacking = decoratedDataSerializerAccessor.invoke(Unpooled.EMPTY_BUFFER);
207+
Object decoratedSerializer = decoratedDataSerializerAccessor.invoke(serializerBacking);
207208

208209
// find all methods which might be the read or write methods
209210
List<Method> candidates = FuzzyReflection

0 commit comments

Comments
 (0)