mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-25 18:09:27 +00:00
refactor(network): 使用1.21+的内置方法自动获取包id
This commit is contained in:
@@ -116,10 +116,8 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
}
|
||||
|
||||
private PacketIds setupPacketIds() {
|
||||
if (VersionHelper.isVersionNewerThan1_21_5()) {
|
||||
return new PacketIds1_21_5();
|
||||
} else if (VersionHelper.isVersionNewerThan1_21_2()) {
|
||||
return new PacketIds1_21_2();
|
||||
if (VersionHelper.isVersionNewerThan1_21()) {
|
||||
return new PacketIds1_21();
|
||||
} else if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
return new PacketIds1_20_5();
|
||||
} else if (VersionHelper.isVersionNewerThan1_20_3()) {
|
||||
|
||||
@@ -2,30 +2,30 @@ package net.momirealms.craftengine.bukkit.plugin.network.impl;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;
|
||||
|
||||
public class PacketIds1_21_5 implements PacketIds {
|
||||
public class PacketIds1_21 implements PacketIds {
|
||||
|
||||
@Override
|
||||
public int clientboundBlockUpdatePacket() {
|
||||
return 8;
|
||||
return PacketIdsFind.getClientboundPackets("minecraft:block_update");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundSectionBlocksUpdatePacket() {
|
||||
return 72;
|
||||
return PacketIdsFind.getClientboundPackets("minecraft:section_blocks_update");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundLevelParticlesPacket() {
|
||||
return 40;
|
||||
return PacketIdsFind.getClientboundPackets("minecraft:level_particles");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundLevelEventPacket() {
|
||||
return 39;
|
||||
return PacketIdsFind.getClientboundPackets("minecraft:level_event");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundAddEntityPacket() {
|
||||
return 1;
|
||||
return PacketIdsFind.getClientboundPackets("minecraft:add_entity");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.momirealms.craftengine.bukkit.plugin.network.impl;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;
|
||||
|
||||
@Deprecated
|
||||
public class PacketIds1_21_2 implements PacketIds {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.impl;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PacketIdsFind {
|
||||
private static final Map<String, Map<String, Integer>> gamePacketIds = new HashMap<>();
|
||||
|
||||
static {
|
||||
try {
|
||||
Object packetReport = Reflections.constructor$PacketReport.newInstance((Object) null);
|
||||
JsonElement jsonElement = (JsonElement) Reflections.method$PacketReport$serializePackets.invoke(packetReport);
|
||||
var play = jsonElement.getAsJsonObject().get("play");
|
||||
for (var entry : play.getAsJsonObject().entrySet()) {
|
||||
Map<String, Integer> ids = new HashMap<>();
|
||||
gamePacketIds.put(entry.getKey(), ids);
|
||||
for (var entry2 : entry.getValue().getAsJsonObject().entrySet()) {
|
||||
ids.put(entry2.getKey(), entry2.getValue().getAsJsonObject().get("protocol_id").getAsInt());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to get packets", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getClientboundPackets(String packetName) {
|
||||
return gamePacketIds.get("clientbound").get(packetName);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.util;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gson.JsonElement;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
@@ -6123,4 +6124,20 @@ public class Reflections {
|
||||
clazz$EntityLookup, clazz$Entity, int.class
|
||||
)
|
||||
);
|
||||
|
||||
// 1.21+
|
||||
public static final Class<?> clazz$PacketReport =
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("data.info.PacketReport")
|
||||
);
|
||||
|
||||
// 1.21+
|
||||
public static final Constructor<?> constructor$PacketReport = Optional.ofNullable(clazz$PacketReport)
|
||||
.map(it -> ReflectionUtils.getConstructor(it, 0))
|
||||
.orElse(null);
|
||||
|
||||
// 1.21+
|
||||
public static final Method method$PacketReport$serializePackets = Optional.ofNullable(clazz$PacketReport)
|
||||
.map(it -> ReflectionUtils.getDeclaredMethod(it, JsonElement.class))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user