Skip to content

Commit 8272ecb

Browse files
committed
Fix some more Mojang mappings
1 parent d13ce24 commit 8272ecb

File tree

6 files changed

+87
-47
lines changed

6 files changed

+87
-47
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
import java.util.UUID;
1515
import java.util.function.BiConsumer;
1616

17-
import org.apache.commons.lang.WordUtils;
18-
import org.bukkit.Bukkit;
19-
2017
import com.comphenix.protocol.PacketTypeLookup.ClassLookup;
2118
import com.comphenix.protocol.events.ConnectionSide;
2219
import com.comphenix.protocol.injector.packet.PacketRegistry;
2320
import com.comphenix.protocol.scheduler.UniversalRunnable;
2421
import com.comphenix.protocol.utility.MinecraftReflection;
2522
import com.comphenix.protocol.utility.MinecraftVersion;
23+
2624
import com.google.common.base.Preconditions;
2725
import com.google.common.collect.ComparisonChain;
2826
import com.google.common.collect.Iterables;
27+
import org.apache.commons.lang.WordUtils;
28+
import org.bukkit.Bukkit;
2929
/**
3030
* Represents the type of a packet in a specific protocol.
3131
* <p>
@@ -477,8 +477,8 @@ public static class Client extends PacketTypeEnum {
477477
public static final PacketType UPDATE_SIGN = new PacketType(PROTOCOL, SENDER, 0x35, "SignUpdate", "UpdateSign", "CPacketUpdateSign");
478478
public static final PacketType ARM_ANIMATION = new PacketType(PROTOCOL, SENDER, 0x36, "Swing", "ArmAnimation", "CPacketAnimation");
479479
public static final PacketType SPECTATE = new PacketType(PROTOCOL, SENDER, 0x37, "TeleportToEntity", "Spectate", "CPacketSpectate");
480-
public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn", "UseItem", "CPacketPlayerTryUseItemOnBlock");
481-
public static final PacketType BLOCK_PLACE = new PacketType(PROTOCOL, SENDER, 0x39, "BlockPlace", "CPacketPlayerTryUseItem");
480+
public static final PacketType USE_ITEM_ON = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn", "BlockPlace", "CPacketPlayerTryUseItemOnBlock");
481+
public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x39, "UseItem", "CPacketPlayerTryUseItem");
482482

483483
/**
484484
* @deprecated Removed in 1.17
@@ -498,6 +498,12 @@ public static class Client extends PacketTypeEnum {
498498
@Deprecated
499499
public static final PacketType CHAT_PREVIEW = new PacketType(PROTOCOL, SENDER, 253, "ChatPreview");
500500

501+
/**
502+
* @deprecated Renamed to USE_ITEM_ON
503+
*/
504+
@Deprecated
505+
public static final PacketType BLOCK_PLACE = USE_ITEM_ON.clone();
506+
501507
private static final Client INSTANCE = new Client();
502508

503509
// Prevent accidental construction

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ public static String combine(String packageName, String className) {
6464
* @param clazz - type of class.
6565
*/
6666
public void setPackageClass(String className, Class<?> clazz) {
67-
if (clazz != null) {
68-
this.cache.put(className, Optional.of(clazz));
69-
} else {
70-
this.cache.remove(className);
67+
Optional<Class<?>> previous = cache.get(className);
68+
if (previous != null && previous.isPresent()) {
69+
throw new IllegalStateException("Tried to redefine class " + className);
7170
}
71+
72+
cache.put(className, Optional.ofNullable(clazz));
73+
}
74+
75+
public void removePackageClass(String className) {
76+
cache.remove(className);
7277
}
7378

7479
private Optional<Class<?>> resolveClass(String className) {

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public static Class<?> getEntityPlayerClass() {
514514
.getMethodByName("getHandle");
515515

516516
// EntityPlayer is the return type
517-
return setMinecraftClass("EntityPlayer", getHandle.getReturnType());
517+
return setMinecraftClass("server.level.EntityPlayer", getHandle.getReturnType());
518518
} catch (IllegalArgumentException e1) {
519519
throw new RuntimeException("Could not find EntityPlayer class.", e1);
520520
}
@@ -584,7 +584,7 @@ public static Class<?> getNmsWorldClass() {
584584
try {
585585
return getMinecraftClass("world.level.World", "world.level.Level", "World");
586586
} catch (RuntimeException e) {
587-
return setMinecraftClass("World", getWorldServerClass().getSuperclass());
587+
return setMinecraftClass("world.level.World", getWorldServerClass().getSuperclass());
588588
}
589589
}
590590

@@ -644,7 +644,7 @@ public static boolean isBundlePacket(Class<?> packetClass) {
644644
}
645645

646646
public static boolean isBundleDelimiter(Class<?> packetClass) {
647-
Class<?> bundleDelimiterClass = getBundleDelimiterClass().orElse(null);
647+
Class<?> bundleDelimiterClass = getBundleDelimiterClass().orElse(null);
648648
return bundleDelimiterClass != null && (packetClass.equals(bundleDelimiterClass) || bundleDelimiterClass.isAssignableFrom(packetClass));
649649
}
650650

@@ -727,10 +727,10 @@ public static Class<?> getMinecraftServerClass() {
727727
return getMinecraftClass("server.MinecraftServer", "MinecraftServer");
728728
} catch (RuntimeException e) {
729729
// Reset cache and try again
730-
setMinecraftClass("MinecraftServer", null);
730+
resetCacheForNMSClass("server.MinecraftServer");
731731

732732
useFallbackServer();
733-
return getMinecraftClass("MinecraftServer");
733+
return getMinecraftClass("server.MinecraftServer");
734734
}
735735
}
736736

@@ -762,10 +762,10 @@ public static Class<?> getPlayerListClass() {
762762
return getMinecraftClass("server.players.PlayerList", "PlayerList");
763763
} catch (RuntimeException e) {
764764
// Reset cache and try again
765-
setMinecraftClass("PlayerList", null);
765+
resetCacheForNMSClass("server.players.PlayerList");
766766

767767
useFallbackServer();
768-
return getMinecraftClass("PlayerList");
768+
return getMinecraftClass("server.players.PlayerList");
769769
}
770770
}
771771

@@ -797,7 +797,7 @@ public static Class<?> getItemStackClass() {
797797
return getMinecraftClass("world.item.ItemStack", "ItemStack");
798798
} catch (RuntimeException e) {
799799
// Use the handle reference
800-
return setMinecraftClass("ItemStack", FuzzyReflection.fromClass(getCraftItemStackClass(), true)
800+
return setMinecraftClass("world.item.ItemStack", FuzzyReflection.fromClass(getCraftItemStackClass(), true)
801801
.getFieldByName("handle")
802802
.getType());
803803
}
@@ -1185,13 +1185,15 @@ public static Class<?> getCraftMessageClass() {
11851185
public static Class<?> getPlayerInfoDataClass() {
11861186
try {
11871187
return getMinecraftClass(
1188-
"network.protocol.game.PacketPlayOutPlayerInfo$PlayerInfoData",
1189-
"network.protocol.game.ClientboundPlayerInfoPacket$PlayerUpdate",
1190-
"PacketPlayOutPlayerInfo$PlayerInfoData", "PlayerInfoData");
1191-
} catch (Exception ex) {
1192-
// todo: ClientboundPlayerInfoUpdatePacket$b, maybe get this via field type
1188+
"network.protocol.game.ClientboundPlayerInfoUpdatePacket$Entry",
1189+
"network.protocol.game.PacketPlayOutPlayerInfo$PlayerInfoData",
1190+
"network.protocol.game.ClientboundPlayerInfoPacket$PlayerUpdate",
1191+
"PacketPlayOutPlayerInfo$PlayerInfoData",
1192+
"PlayerInfoData"
1193+
);
1194+
} catch (Exception ignored) {
11931195
return setMinecraftClass(
1194-
"network.protocol.game.PacketPlayOutPlayerInfo$PlayerInfoData",
1196+
"network.protocol.game.ClientboundPlayerInfoUpdatePacket$Entry",
11951197
PacketType.Play.Server.PLAYER_INFO.getPacketClass().getClasses()[0]);
11961198
}
11971199
}
@@ -1512,16 +1514,22 @@ public static Optional<Class<?>> getOptionalNMS(String className, String... alia
15121514
* Retrieves a nullable NMS (net.minecraft.server) class. We will attempt to
15131515
* look up the class and its aliases, but will return null if none is found.
15141516
*
1515-
* @deprecated - Use getOptionalNMS where possible
15161517
* @param className NMS class name
15171518
* @param aliases Potential aliases
15181519
* @return The class, or null if not found
15191520
*/
1520-
@Deprecated
15211521
public static Class<?> getNullableNMS(String className, String... aliases) {
15221522
return getOptionalNMS(className, aliases).orElse(null);
15231523
}
15241524

1525+
private static void resetCacheForNMSClass(String className) {
1526+
if (minecraftPackage == null) {
1527+
minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource());
1528+
}
1529+
1530+
minecraftPackage.removePackageClass(className);
1531+
}
1532+
15251533
/**
15261534
* Set the class object for the specific Minecraft class.
15271535
*
@@ -1838,7 +1846,7 @@ public static Class<?> getHolderClass() {
18381846
}
18391847

18401848
public static Class<?> getCraftServer() {
1841-
return getCraftBukkitClass("CraftServer");
1849+
return getCraftBukkitClass("CraftServer");
18421850
}
18431851

18441852
public static Class<?> getHolderLookupProviderClass() {
@@ -1850,29 +1858,29 @@ public static Class<?> getRegistryAccessClass() {
18501858
}
18511859

18521860
public static Class<?> getProtocolInfoClass() {
1853-
return getMinecraftClass("network.ProtocolInfo");
1861+
return getMinecraftClass("network.ProtocolInfo");
18541862
}
18551863

18561864
public static Class<?> getProtocolInfoUnboundClass() {
1857-
return getMinecraftClass("network.ProtocolInfo$a" /* Spigot Mappings */, "network.ProtocolInfo$Unbound" /* Mojang Mappings */);
1865+
return getMinecraftClass("network.ProtocolInfo$a" /* Spigot Mappings */, "network.ProtocolInfo$Unbound" /* Mojang Mappings */);
18581866
}
18591867

18601868
public static Class<?> getPacketFlowClass() {
1861-
return getMinecraftClass("network.protocol.EnumProtocolDirection" /* Spigot Mappings */, "network.protocol.PacketFlow" /* Mojang Mappings */);
1869+
return getMinecraftClass("network.protocol.EnumProtocolDirection" /* Spigot Mappings */, "network.protocol.PacketFlow" /* Mojang Mappings */);
18621870
}
18631871

18641872
public static Class<?> getStreamCodecClass() {
1865-
return getMinecraftClass("network.codec.StreamCodec");
1873+
return getMinecraftClass("network.codec.StreamCodec");
18661874
}
18671875

18681876
public static Optional<Class<?>> getRegistryFriendlyByteBufClass() {
1869-
return getOptionalNMS("network.RegistryFriendlyByteBuf");
1877+
return getOptionalNMS("network.RegistryFriendlyByteBuf");
18701878
}
18711879

18721880
public static boolean isMojangMapped() {
18731881
if (isMojangMapped == null) {
1874-
String craftServerName = Bukkit.getServer().getClass().getName();
1875-
isMojangMapped = !craftServerName.contains(".v") && !craftServerName.contains("_R");
1882+
String nmsWorldName = getWorldServerClass().getName();
1883+
isMojangMapped = nmsWorldName.contains("ServerLevel");
18761884
}
18771885

18781886
return isMojangMapped;

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.comphenix.protocol.utility;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.DataInputStream;
5+
import java.io.DataOutputStream;
6+
import java.io.IOException;
7+
import java.lang.reflect.Modifier;
8+
import java.util.Base64;
9+
310
import com.comphenix.protocol.injector.netty.NettyByteBufAdapter;
411
import com.comphenix.protocol.reflect.FuzzyReflection;
512
import com.comphenix.protocol.reflect.accessors.Accessors;
@@ -8,14 +15,10 @@
815
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
916
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
1017
import com.comphenix.protocol.wrappers.nbt.NbtType;
18+
1119
import io.netty.buffer.ByteBuf;
1220
import io.netty.buffer.Unpooled;
1321
import io.netty.util.ReferenceCountUtil;
14-
import java.io.ByteArrayOutputStream;
15-
import java.io.DataInputStream;
16-
import java.io.DataOutputStream;
17-
import java.io.IOException;
18-
import java.util.Base64;
1922
import org.bukkit.inventory.ItemStack;
2023

2124
/**
@@ -148,9 +151,14 @@ public NbtCompound deserializeCompound(DataInputStream input) {
148151
*/
149152
public void serializeString(DataOutputStream output, String text) {
150153
if (WRITE_STRING_METHOD == null) {
151-
WRITE_STRING_METHOD = Accessors.getMethodAccessor(FuzzyReflection
152-
.fromClass(MinecraftReflection.getPacketDataSerializerClass(), true)
153-
.getMethodByParameters("writeString", String.class));
154+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(MinecraftReflection.getPacketDataSerializerClass(), false);
155+
WRITE_STRING_METHOD = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract.newBuilder()
156+
.parameterExactType(String.class)
157+
.parameterCount(1)
158+
.returnDerivedOf(ByteBuf.class)
159+
.requireModifier(Modifier.PUBLIC)
160+
.banModifier(Modifier.STATIC)
161+
.build()));
154162
}
155163

156164
ByteBuf buf = NettyByteBufAdapter.packetWriter(output);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,11 @@ private static void initialize() {
737737
CHAT_TYPE_CLASS = getEnum(PacketType.Play.Server.CHAT.getPacketClass(), 0);
738738
ENTITY_POSE_CLASS = MinecraftReflection.getNullableNMS("world.entity.EntityPose", "world.entity.Pose", "EntityPose");
739739
DISPLAY_SLOT_CLASS = MinecraftReflection.getNullableNMS("world.scores.DisplaySlot");
740-
RENDER_TYPE_CLASS = MinecraftReflection.getNullableNMS("world.scores.criteria.IScoreboardCriteria$EnumScoreboardHealthDisplay", "IScoreboardCriteria$EnumScoreboardHealthDisplay");
740+
741+
RENDER_TYPE_CLASS = MinecraftReflection.getNullableNMS(
742+
"world.scores.criteria.ObjectiveCriteria$RenderType",
743+
"world.scores.criteria.IScoreboardCriteria$EnumScoreboardHealthDisplay",
744+
"IScoreboardCriteria$EnumScoreboardHealthDisplay");
741745
CHAT_FORMATTING_CLASS = MinecraftReflection.getNullableNMS("ChatFormatting", "EnumChatFormat");
742746

743747
associate(PROTOCOL_CLASS, Protocol.class, getProtocolConverter());

src/main/java/com/comphenix/protocol/wrappers/nbt/io/NbtBinarySerializer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.comphenix.protocol.wrappers.nbt.io;
22

3+
import java.io.DataInput;
4+
import java.io.DataOutput;
5+
import java.lang.reflect.Method;
6+
37
import com.comphenix.protocol.reflect.FieldAccessException;
48
import com.comphenix.protocol.reflect.FuzzyReflection;
59
import com.comphenix.protocol.reflect.accessors.Accessors;
@@ -12,9 +16,6 @@
1216
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
1317
import com.comphenix.protocol.wrappers.nbt.NbtList;
1418
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
15-
import java.io.DataInput;
16-
import java.io.DataOutput;
17-
import java.lang.reflect.Method;
1819

1920
public class NbtBinarySerializer {
2021

@@ -153,15 +154,23 @@ private static class LoadMethodConfigPhaseUpdate implements CodecMethod {
153154
public LoadMethodConfigPhaseUpdate() {
154155
// there are now two methods with the same signature: readAnyTag/readUnnamedTag & writeAnyTag/writeUnnamedTag
155156
// we can only find the correct method here by using the method name... thanks Mojang
157+
String readNbtMethodName = MinecraftReflection.isMojangMapped()
158+
? "readAnyTag"
159+
: "b";
160+
161+
String writeNbtMethodName = MinecraftReflection.isMojangMapped()
162+
? "writeAnyTag"
163+
: "a";
164+
156165
Method readNbtMethod = getUtilityClass().getMethod(FuzzyMethodContract.newBuilder()
157-
.nameExact("b")
166+
.nameExact(readNbtMethodName)
158167
.returnTypeExact(MinecraftReflection.getNBTBaseClass())
159168
.parameterExactArray(DataInput.class, this.readLimitClass)
160169
.build());
161170
this.readNbt = Accessors.getMethodAccessor(readNbtMethod);
162171

163172
Method writeNbtMethod = getUtilityClass().getMethod(FuzzyMethodContract.newBuilder()
164-
.nameExact("a")
173+
.nameExact(writeNbtMethodName)
165174
.parameterExactArray(MinecraftReflection.getNBTBaseClass(), DataOutput.class)
166175
.build());
167176
this.writeNbt = Accessors.getMethodAccessor(writeNbtMethod);

0 commit comments

Comments
 (0)