Skip to content

Commit 90a386b

Browse files
authored
Fix some 1.21.5 related issues (unit tests) (#3432)
1 parent a80762f commit 90a386b

File tree

11 files changed

+90
-35
lines changed

11 files changed

+90
-35
lines changed

build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ dependencies {
5151
compileOnly("org.spigotmc:spigot-api:${mcVersion}-R0.1-SNAPSHOT")
5252
compileOnly("org.spigotmc:spigot:${mcVersion}-R0.1-SNAPSHOT:remapped-mojang")
5353
compileOnly("io.netty:netty-all:4.0.23.Final")
54-
compileOnly("net.kyori:adventure-text-serializer-gson:4.19.0")
54+
/*
55+
* TODO(fix): once you update kyori:adventure-text-serializer-gson please uncomment the TODO in
56+
* com.comphenix.protocol.wrappers.WrappedComponentStyleTest if the following issue got fixed:
57+
* https://github.com/KyoriPowered/adventure/issues/1194
58+
*/
59+
compileOnly("net.kyori:adventure-text-serializer-gson:4.21.0")
5560
compileOnly("com.googlecode.json-simple:json-simple:1.1.1")
5661

5762
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.0")

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ public class ProtocolLibrary {
3535
/**
3636
* The maximum version ProtocolLib has been tested with.
3737
*/
38-
// TODO(fix): once all tests pass again change this to latest version
39-
public static final String MAXIMUM_MINECRAFT_VERSION = "1.21.4";
38+
public static final String MAXIMUM_MINECRAFT_VERSION = "1.21.5";
4039

4140
/**
42-
* The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.21.4) was released.
41+
* The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.21.5) was released.
4342
*/
44-
public static final String MINECRAFT_LAST_RELEASE_DATE = "2024-12-03";
43+
public static final String MINECRAFT_LAST_RELEASE_DATE = "2025-03-25";
4544

4645
private static Plugin plugin;
4746
private static ProtocolConfig config;

src/main/java/com/comphenix/protocol/injector/packet/internal/ProtocolRegistry_1_20_5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void fillRegister(Register register) {
9494
}
9595

9696
PacketType packetType = PacketType.fromCurrent(packetProtocol, packetSender, id, packetClass);
97-
// TODO(fix): validate that every codec pass in and expects FriendlyByteBuf
97+
// TODO(fix): validate that every codec expects FriendlyByteBuf
9898
register.registerPacket(packetType, packetClass, packetSender, entry.serializer());
9999
}
100100
} catch (Exception e) {

src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ public enum ItemSlot {
366366
LEGS,
367367
CHEST,
368368
HEAD,
369-
BODY
369+
BODY,
370+
SADDLE
370371
}
371372

372373
public enum Hand {

src/main/java/com/comphenix/protocol/wrappers/nbt/TileEntityAccessor.java

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
package com.comphenix.protocol.wrappers.nbt;
22

3+
import java.io.IOException;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Modifier;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.bukkit.block.BlockState;
10+
311
import com.comphenix.protocol.injector.BukkitUnwrapper;
412
import com.comphenix.protocol.reflect.FuzzyReflection;
513
import com.comphenix.protocol.reflect.accessors.Accessors;
614
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
715
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
816
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
9-
import com.comphenix.protocol.utility.ByteBuddyFactory;
10-
import com.comphenix.protocol.utility.MinecraftMethods;
1117
import com.comphenix.protocol.utility.MinecraftReflection;
18+
import com.comphenix.protocol.utility.MinecraftRegistryAccess;
1219
import com.comphenix.protocol.utility.MinecraftVersion;
13-
import java.io.IOException;
14-
import java.lang.reflect.Constructor;
15-
import java.lang.reflect.Modifier;
16-
import java.util.HashMap;
17-
import java.util.Map;
18-
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
19-
import net.bytebuddy.implementation.InvocationHandlerAdapter;
20+
2021
import net.bytebuddy.jar.asm.ClassReader;
2122
import net.bytebuddy.jar.asm.ClassVisitor;
2223
import net.bytebuddy.jar.asm.MethodVisitor;
2324
import net.bytebuddy.jar.asm.Opcodes;
2425
import net.bytebuddy.jar.asm.Type;
25-
import net.bytebuddy.matcher.ElementMatchers;
26-
import org.bukkit.block.BlockState;
2726

2827
/**
2928
* Manipulate tile entities.
@@ -34,6 +33,7 @@ class TileEntityAccessor<T extends BlockState> {
3433

3534
private static final boolean BLOCK_DATA_INCL = MinecraftVersion.NETHER_UPDATE.atOrAbove()
3635
&& !MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
36+
private static final boolean USE_HOLDER_LOOKUP = MinecraftVersion.v1_21_5.atOrAbove();
3737

3838
/**
3939
* Token indicating that the given block state doesn't contain any tile entities.
@@ -104,11 +104,33 @@ public static <T extends BlockState> TileEntityAccessor<T> getAccessor(T state)
104104
}
105105

106106
void findMethods(Class<?> type, T state) {
107-
if (BLOCK_DATA_INCL) {
108-
Class<?> tileEntityClass = MinecraftReflection.getTileEntityClass();
109-
Class<?> iBlockData = MinecraftReflection.getIBlockDataClass();
110-
Class<?> nbtCompound = MinecraftReflection.getNBTCompoundClass();
107+
Class<?> tileEntityClass = MinecraftReflection.getTileEntityClass();
108+
Class<?> nbtCompound = MinecraftReflection.getNBTCompoundClass();
109+
110+
if (USE_HOLDER_LOOKUP) {
111+
Class<?> holderLookup = MinecraftReflection.getHolderLookupProviderClass();
112+
113+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(tileEntityClass, false);
114+
// should be equiv. to `CompoundTag saveWithFullMetadata(HolderLookup.Provider)`
115+
writeCompound = Accessors.getMethodAccessor(fuzzy.getMethod(
116+
FuzzyMethodContract.newBuilder()
117+
.banModifier(Modifier.STATIC)
118+
.requireModifier(Modifier.FINAL)
119+
.returnTypeExact(nbtCompound)
120+
.parameterExactArray(holderLookup)
121+
.build()));
111122

123+
// should be equiv. to `void loadWithComponents(CompoundTag, HolderLookup.Provider)`
124+
readCompound = Accessors.getMethodAccessor(fuzzy.getMethod(
125+
FuzzyMethodContract.newBuilder()
126+
.banModifier(Modifier.STATIC)
127+
.requireModifier(Modifier.FINAL)
128+
.returnTypeVoid()
129+
.parameterExactArray(nbtCompound, holderLookup)
130+
.build()));
131+
} else if (BLOCK_DATA_INCL) {
132+
Class<?> iBlockData = MinecraftReflection.getIBlockDataClass();
133+
112134
FuzzyReflection fuzzy = FuzzyReflection.fromClass(tileEntityClass, false);
113135
writeCompound = Accessors.getMethodAccessor(fuzzy.getMethod(
114136
FuzzyMethodContract.newBuilder()
@@ -214,12 +236,17 @@ public void visitEnd() {
214236
* @return The compound.
215237
*/
216238
public NbtCompound readBlockState(T state) {
217-
NbtCompound output = NbtFactory.ofCompound("");
218239
Object tileEntity = tileEntityField.get(state);
219240

220241
// Write the block state to the output compound
221-
writeCompound.invoke(tileEntity, NbtFactory.fromBase(output).getHandle());
222-
return output;
242+
if (USE_HOLDER_LOOKUP) {
243+
Object tag = writeCompound.invoke(tileEntity, MinecraftRegistryAccess.get());
244+
return new WrappedCompound(tag);
245+
} else {
246+
NbtCompound output = NbtFactory.ofCompound("");
247+
writeCompound.invoke(tileEntity, NbtFactory.fromBase(output).getHandle());
248+
return output;
249+
}
223250
}
224251

225252
/**
@@ -232,7 +259,9 @@ public void writeBlockState(T state, NbtCompound compound) {
232259
Object tileEntity = tileEntityField.get(state);
233260

234261
// Ensure the block state is set to the compound
235-
if (BLOCK_DATA_INCL) {
262+
if (USE_HOLDER_LOOKUP) {
263+
readCompound.invoke(tileEntity, NbtFactory.fromBase(compound).getHandle(), MinecraftRegistryAccess.get());
264+
} else if (BLOCK_DATA_INCL) {
236265
Object blockData = BukkitUnwrapper.getInstance().unwrapItem(state);
237266
readCompound.invoke(tileEntity, blockData, NbtFactory.fromBase(compound).getHandle());
238267
} else {

src/test/java/com/comphenix/protocol/PacketTypeTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1920
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import java.util.ArrayList;
2223
import java.util.List;
24+
import java.util.Set;
2325
import java.util.regex.Matcher;
2426
import java.util.regex.Pattern;
2527

@@ -252,6 +254,16 @@ public void testFindCurrent() {
252254
assertEquals(type, roundTrip);
253255
}
254256
}
257+
258+
@Test
259+
public void testPacketCodec() {
260+
Set<PacketType> notSerializable = Set.of(PacketType.Play.Server.BUNDLE);
261+
for (PacketType type : PacketType.values()) {
262+
if (!notSerializable.contains(type) && type.isSupported()) {
263+
assertNotNull(PacketRegistry.getStreamCodec(type.getPacketClass()), "missing serializer for " + type);
264+
}
265+
}
266+
}
255267

256268
@Test
257269
public void testLoginStart() {

src/test/java/com/comphenix/protocol/events/PacketContainerTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void testGetIntegerArrays() {
235235

236236
@Test
237237
public void testGetItemModifier() {
238-
PacketContainer windowClick = new PacketContainer(PacketType.Play.Client.WINDOW_CLICK);
238+
PacketContainer windowClick = new PacketContainer(PacketType.Play.Server.SET_CURSOR_ITEM);
239239

240240
ItemStack item = this.itemWithData();
241241

@@ -282,8 +282,6 @@ public void testGetNbtModifier() {
282282
NbtCompound compound = NbtFactory.ofCompound("test");
283283
compound.put("test", "name");
284284
compound.put(NbtFactory.ofList("ages", 1, 2, 3));
285-
// TODO(fix): the type field got replace by `identifyRawElementType()` which is invoked
286-
// on write to determine the type
287285

288286
updateTileEntity.getNbtModifier().write(0, compound);
289287

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.comphenix.protocol.utility;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class MinecraftRegistryAccessTest {
7+
8+
@Test
9+
public void testGet() {
10+
assertNotNull(MinecraftRegistryAccess.get());
11+
}
12+
}

src/test/java/com/comphenix/protocol/utility/MinecraftVersionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void testComparison() {
4848

4949
@Test
5050
void testCurrent() {
51-
assertEquals(MinecraftVersion.v1_21_4, MinecraftVersion.getCurrentVersion());
51+
assertEquals(MinecraftVersion.v1_21_5, MinecraftVersion.getCurrentVersion());
5252
}
5353

5454
@Test

src/test/java/com/comphenix/protocol/wrappers/AutoWrapperTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ private AutoWrapper<WrappedAdvancementDisplay> displayWrapper() {
9595
.field(0, BukkitConverters.getWrappedChatComponentConverter())
9696
.field(1, BukkitConverters.getWrappedChatComponentConverter())
9797
.field(2, BukkitConverters.getItemStackConverter())
98-
.field(3, Converters.optional(MinecraftKey.getConverter()))
99-
// TODO(fix): we would technically need a new Converter for ClientAsset here as it replace the MinecraftKey in DisplayInfo
98+
.field(3, Converters.optional(Converters.passthrough(ClientAsset.class)))
10099
.field(4, EnumWrappers.getGenericConverter(
101100
getMinecraftClass("advancements.AdvancementType", "advancements.AdvancementFrameType", "advancements.FrameType"),
102101
WrappedFrameType.class)

0 commit comments

Comments
 (0)