Skip to content

Commit f0468e0

Browse files
committed
Fix data watchers and particles
1 parent 052e7a3 commit f0468e0

File tree

6 files changed

+190
-44
lines changed

6 files changed

+190
-44
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.base.Preconditions;
2727
import com.google.common.collect.ComparisonChain;
2828
import com.google.common.collect.Iterables;
29+
2930
/**
3031
* Represents the type of a packet in a specific protocol.
3132
* <p>
@@ -477,8 +478,8 @@ public static class Client extends PacketTypeEnum {
477478
public static final PacketType UPDATE_SIGN = new PacketType(PROTOCOL, SENDER, 0x35, "SignUpdate", "UpdateSign", "CPacketUpdateSign");
478479
public static final PacketType ARM_ANIMATION = new PacketType(PROTOCOL, SENDER, 0x36, "Swing", "ArmAnimation", "CPacketAnimation");
479480
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");
481+
public static final PacketType USE_ITEM = new PacketType(PROTOCOL, SENDER, 0x38, "UseItemOn", "CPacketPlayerTryUseItemOnBlock");
482+
public static final PacketType BLOCK_PLACE = new PacketType(PROTOCOL, SENDER, 0x39, "UseItem", "BlockPlace", "CPacketPlayerTryUseItem");
482483

483484
/**
484485
* @deprecated Removed in 1.17

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,15 @@ public void onDisable() {
535535

536536
// that reloading the server might break ProtocolLib / plugins depending on it
537537
if (Util.isCurrentlyReloading()) {
538-
logger.severe("╔══════════════════════════════════════════════════════════════════╗");
539-
logger.severe("║ WARNING ║");
540-
logger.severe("║ RELOADING THE SERVER WHILE PROTOCOL LIB IS ENABLED MIGHT ║");
541-
logger.severe("║ LEAD TO UNEXPECTED ERRORS! ║");
542-
logger.severe("║ ║");
543-
logger.severe("║ Consider to cleanly restart your server if you encounter ║");
544-
logger.severe("║ any issues related to Protocol Lib before opening an issue ║");
545-
logger.severe("║ on GitHub! ║");
546-
logger.severe("╚══════════════════════════════════════════════════════════════════╝");
538+
highlyVisibleError(
539+
" WARNING ",
540+
" RELOADING THE SERVER WHILE PROTOCOLLIB IS ENABLED MIGHT ",
541+
" LEAD TO UNEXPECTED ERRORS! ",
542+
" ",
543+
" Consider cleanly restarting your server if you encounter ",
544+
" any issues related to ProtocolLib before opening an issue ",
545+
" on GitHub! "
546+
);
547547
}
548548

549549
// Clean up

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
import java.lang.reflect.Array;
2121
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.Field;
2223
import java.lang.reflect.Method;
2324
import java.lang.reflect.Modifier;
2425
import java.lang.reflect.ParameterizedType;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Optional;
30+
import java.util.function.Supplier;
2931
import java.util.logging.Level;
3032
import java.util.regex.Matcher;
3133
import java.util.regex.Pattern;
@@ -115,6 +117,8 @@ public final class MinecraftReflection {
115117
private static MethodAccessor asCraftMirror = null;
116118
private static MethodAccessor isEmpty = null;
117119

120+
private static boolean isMojangMapped = false;
121+
118122
private MinecraftReflection() {
119123
// No need to make this constructable.
120124
}
@@ -204,6 +208,9 @@ public static String getMinecraftPackage() {
204208
}
205209
}
206210

211+
// for now, we're going to say that it's Mojang mapped if the nms world was renamed to ServerLevel
212+
isMojangMapped = getNmsWorldClass().getName().contains("ServerLevel");
213+
207214
return MINECRAFT_FULL_PACKAGE;
208215
} catch (NoSuchMethodException exception) {
209216
throw new IllegalStateException("Cannot find getHandle() in CraftEntity", exception);
@@ -1109,19 +1116,37 @@ public static Class<?> getPlayerInfoDataClass() {
11091116
}
11101117
}
11111118

1119+
static Class<?> getOrInferMinecraftClass(String className, Supplier<Class<?>> supplier) {
1120+
return getOptionalNMS(className).orElseGet(() -> {
1121+
Class<?> clazz = supplier.get();
1122+
return setMinecraftClass(className, clazz);
1123+
});
1124+
}
1125+
11121126
/**
11131127
* Retrieves the entity use action class in 1.17.
11141128
*
11151129
* @return The EntityUseAction class
11161130
*/
11171131
public static Class<?> getEnumEntityUseActionClass() {
1118-
Class<?> packetClass = PacketType.Play.Client.USE_ENTITY.getPacketClass();
1119-
FuzzyReflection fuzzyReflection = FuzzyReflection.fromClass(packetClass, true);
1120-
try {
1121-
return fuzzyReflection.getFieldByType("^.*(EnumEntityUseAction)").getType();
1122-
} catch (IllegalArgumentException ignored) {
1123-
return fuzzyReflection.getFieldByType("^.*(Action)").getType();
1124-
}
1132+
return getOrInferMinecraftClass("ServerboundInteractPacket.Action", () -> {
1133+
Class<?> packetClass = PacketType.Play.Client.USE_ENTITY.getPacketClass();
1134+
FuzzyReflection fuzzyReflection = FuzzyReflection.fromClass(packetClass, true);
1135+
1136+
Field field = fuzzyReflection.getField(FuzzyFieldContract.newBuilder()
1137+
.banModifier(Modifier.STATIC)
1138+
.typeDerivedOf(Object.class)
1139+
.build());
1140+
if (field != null) {
1141+
return field.getType();
1142+
}
1143+
1144+
try {
1145+
return fuzzyReflection.getFieldByType("^.*(EnumEntityUseAction)").getType();
1146+
} catch (IllegalArgumentException ignored) {
1147+
return fuzzyReflection.getFieldByType("^.*(Action)").getType();
1148+
}
1149+
});
11251150
}
11261151

11271152
/**
@@ -1763,4 +1788,8 @@ public static Class<?> getStreamCodecClass() {
17631788
public static Optional<Class<?>> getRegistryFriendlyByteBufClass() {
17641789
return getOptionalNMS("network.RegistryFriendlyByteBuf");
17651790
}
1791+
1792+
public static boolean isMojangMapped() {
1793+
return isMojangMapped;
1794+
}
17661795
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import com.comphenix.protocol.reflect.ExactReflection;
88
import com.comphenix.protocol.reflect.FuzzyReflection;
99
import com.comphenix.protocol.reflect.accessors.Accessors;
10+
import com.comphenix.protocol.reflect.fuzzy.FuzzyMatchers;
11+
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
1012
import com.comphenix.protocol.utility.MinecraftReflection;
1113
import com.comphenix.protocol.utility.MinecraftVersion;
1214
import org.apache.commons.lang.Validate;
1315
import org.bukkit.GameMode;
1416

1517
import java.lang.reflect.Field;
18+
import java.lang.reflect.Method;
1619
import java.util.EnumSet;
1720
import java.util.HashMap;
1821
import java.util.HashSet;
@@ -549,8 +552,14 @@ private static void initialize() {
549552
// In 1.17 the hand and use action class is no longer a field in the packet
550553
if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
551554
HAND_CLASS = MinecraftReflection.getMinecraftClass("world.EnumHand", "world.InteractionHand");
552-
// class is named 'b' in the packet but class order differs in spigot and paper so we can only use the first method's return type (safest way)
553-
ENTITY_USE_ACTION_CLASS = MinecraftReflection.getEnumEntityUseActionClass().getMethods()[0].getReturnType();
555+
556+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(MinecraftReflection.getEnumEntityUseActionClass(), true);
557+
Method getType = fuzzy.getMethod(FuzzyMethodContract.newBuilder()
558+
.parameterCount(0)
559+
.returnTypeMatches(FuzzyMatchers.except(Void.class))
560+
.build());
561+
562+
ENTITY_USE_ACTION_CLASS = getType.getReturnType();
554563
} else {
555564
HAND_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 1);
556565
ENTITY_USE_ACTION_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 0);

0 commit comments

Comments
 (0)