mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-28 03:19:14 +00:00
feat(network): 补充聊天命令拦截
This commit is contained in:
@@ -128,6 +128,8 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
|
||||
registerNMSPacketConsumer(PacketConsumers.CHAT, Reflections.clazz$ServerboundChatPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.RENAME_ITEM, Reflections.clazz$ServerboundRenameItemPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.SIGN_UPDATE, Reflections.clazz$ServerboundSignUpdatePacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.COMMAND_SIGNED, Reflections.clazz$ServerboundChatCommandSignedPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.COMMAND, Reflections.clazz$ServerboundChatCommandPacket);
|
||||
registerByteBufPacketConsumer(PacketConsumers.SECTION_BLOCK_UPDATE, this.packetIds.clientboundSectionBlocksUpdatePacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.BLOCK_UPDATE, this.packetIds.clientboundBlockUpdatePacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.LEVEL_PARTICLE, this.packetIds.clientboundLevelParticlesPacket());
|
||||
|
||||
@@ -732,6 +732,50 @@ public class PacketConsumers {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> COMMAND_SIGNED = (user, event, packet) -> {
|
||||
try {
|
||||
String command = (String) Reflections.field$ServerboundChatCommandSignedPacket$command.get(packet);
|
||||
ImageManager manager = CraftEngine.instance().imageManager();
|
||||
if (!manager.isDefaultFontInUse()) return;
|
||||
runIfContainsIllegalCharacter(command, manager, (s) -> {
|
||||
event.setCancelled(true);
|
||||
try {
|
||||
Object newPacket = Reflections.constructor$ServerboundChatCommandSignedPacket.newInstance(
|
||||
s,
|
||||
Reflections.field$ServerboundChatCommandSignedPacket$timeStamp.get(packet),
|
||||
Reflections.field$ServerboundChatCommandSignedPacket$salt.get(packet),
|
||||
Reflections.field$ServerboundChatCommandSignedPacket$argumentSignatures.get(packet),
|
||||
Reflections.field$ServerboundChatCommandSignedPacket$lastSeenMessages.get(packet)
|
||||
);
|
||||
user.receivePacket(newPacket);
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to create replaced chat command signed packet", e);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ServerboundChatCommandSignedPacket", e);
|
||||
}
|
||||
};
|
||||
|
||||
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> COMMAND = (user, event, packet) -> {
|
||||
try {
|
||||
String command = (String) Reflections.field$SServerboundChatCommandPacket$command.get(packet);
|
||||
ImageManager manager = CraftEngine.instance().imageManager();
|
||||
if (!manager.isDefaultFontInUse()) return;
|
||||
runIfContainsIllegalCharacter(command, manager, (s) -> {
|
||||
event.setCancelled(true);
|
||||
try {
|
||||
Object newPacket = Reflections.constructor$ServerboundChatCommandPacket.newInstance(s);
|
||||
user.receivePacket(newPacket);
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to create replaced chat command packet", e);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ServerboundChatCommandPacket", e);
|
||||
}
|
||||
};
|
||||
|
||||
private static void runIfContainsIllegalCharacter(String string, ImageManager manager, Consumer<String> callback) {
|
||||
char[] chars = string.toCharArray();
|
||||
int[] codepoints = CharacterUtils.charsToCodePoints(chars);
|
||||
|
||||
@@ -5201,4 +5201,68 @@ public class Reflections {
|
||||
clazz$ServerboundSignUpdatePacket, String[].class, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$ArgumentSignatures = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("commands.arguments.ArgumentSignatures")
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$ServerboundChatCommandSignedPacket = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ServerboundChatCommandSignedPacket"),
|
||||
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ServerboundChatCommandPacket")
|
||||
)
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundChatCommandSignedPacket$command = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundChatCommandSignedPacket, String.class, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundChatCommandSignedPacket$timeStamp = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundChatCommandSignedPacket, Instant.class, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundChatCommandSignedPacket$salt = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundChatCommandSignedPacket, long.class, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundChatCommandSignedPacket$argumentSignatures = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundChatCommandSignedPacket, clazz$ArgumentSignatures, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundChatCommandSignedPacket$lastSeenMessages = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundChatCommandSignedPacket, clazz$LastSeenMessages$Update, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Constructor<?> constructor$ServerboundChatCommandSignedPacket = requireNonNull(
|
||||
ReflectionUtils.getConstructor(
|
||||
clazz$ServerboundChatCommandSignedPacket, String.class, Instant.class, long.class, clazz$ArgumentSignatures, clazz$LastSeenMessages$Update
|
||||
)
|
||||
);
|
||||
|
||||
// 1.20.5+
|
||||
public static final Class<?> clazz$ServerboundChatCommandPacket = VersionHelper.isVersionNewerThan1_20_5()
|
||||
? ReflectionUtils.getClazz(BukkitReflectionUtils.assembleMCClass("network.protocol.game.ServerboundChatCommandPacket"))
|
||||
: null;
|
||||
|
||||
// 1.20.5+
|
||||
public static final Field field$SServerboundChatCommandPacket$command = Optional.ofNullable(clazz$ServerboundChatCommandPacket)
|
||||
.map(it -> ReflectionUtils.getDeclaredField(it, String.class, 0))
|
||||
.orElse(null);
|
||||
|
||||
// 1.20.5+
|
||||
public static final Constructor<?> constructor$ServerboundChatCommandPacket = Optional.ofNullable(clazz$ServerboundChatCommandPacket)
|
||||
.map(it -> ReflectionUtils.getConstructor(it, String.class))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user