9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 09:59:20 +00:00

refactor(network): 重构实体相关的网络包处理

This commit is contained in:
jhqwqmc
2025-04-07 18:17:17 +08:00
parent 74743ee25b
commit 1e8b7e0607
10 changed files with 101 additions and 33 deletions

View File

@@ -141,7 +141,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
registerNMSPacketConsumer(PacketConsumers.LOGIN, Reflections.clazz$ClientboundLoginPacket);
registerNMSPacketConsumer(PacketConsumers.RESPAWN, Reflections.clazz$ClientboundRespawnPacket);
registerNMSPacketConsumer(PacketConsumers.INTERACT_ENTITY, Reflections.clazz$ServerboundInteractPacket);
registerNMSPacketConsumer(PacketConsumers.REMOVE_ENTITY, Reflections.clazz$ClientboundRemoveEntitiesPacket);
// registerNMSPacketConsumer(PacketConsumers.REMOVE_ENTITY, Reflections.clazz$ClientboundRemoveEntitiesPacket);
registerNMSPacketConsumer(PacketConsumers.SYNC_ENTITY_POSITION, Reflections.clazz$ClientboundEntityPositionSyncPacket);
registerNMSPacketConsumer(PacketConsumers.MOVE_ENTITY, Reflections.clazz$ClientboundMoveEntityPacket$Pos);
registerNMSPacketConsumer(PacketConsumers.PICK_ITEM_FROM_ENTITY, Reflections.clazz$ServerboundPickItemFromEntityPacket);
@@ -157,7 +157,8 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
registerByteBufPacketConsumer(PacketConsumers.LEVEL_PARTICLE, this.packetIds.clientboundLevelParticlesPacket());
registerByteBufPacketConsumer(PacketConsumers.LEVEL_EVENT, this.packetIds.clientboundLevelEventPacket());
registerByteBufPacketConsumer(PacketConsumers.OPEN_SCREEN, this.packetIds.clientboundOpenScreenPacket());
// registerByteBufPacketConsumer(PacketConsumers.SYNC_ENTITY_POSITION, this.packetIds.clientboundEntityPositionSyncPacket());
registerByteBufPacketConsumer(PacketConsumers.REMOVE_ENTITY, this.packetIds.clientboundRemoveEntitiesPacket());
registerByteBufPacketConsumer(PacketConsumers.ADD_ENTITY_BYTEBUFFER, this.packetIds.clientboundAddEntityPacket());
}
public static BukkitNetworkManager instance() {
@@ -483,7 +484,9 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
}
if (byteBuf.isReadable()) {
list.add(byteBuf.retain());
}
}/* else {
list.add(io.netty.buffer.Unpooled.EMPTY_BUFFER);
}*/
}
private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) {

View File

@@ -652,18 +652,54 @@ public class PacketConsumers {
Reflections.field$ServerPlayer$connection.get(FastNMS.INSTANCE.method$CraftPlayer$getHandle(player)), FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack));
}
// TODO USE bytebuffer
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> ADD_ENTITY_BYTEBUFFER = (user, event) -> {
try {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
UUID uuid = buf.readUUID();
int type = buf.readVarInt();
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
byte xRot = buf.readByte();
byte yRot = buf.readByte();
byte yHeadRot = buf.readByte();
int data = buf.readVarInt();
int xa = buf.readShort();
int ya = buf.readShort();
int za = buf.readShort();
// Falling blocks
if (type == Reflections.instance$EntityType$FALLING_BLOCK$registryId) {
int remapped = remap(data);
if (remapped != data) {
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
buf.writeUUID(uuid);
buf.writeVarInt(type);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeByte(xRot);
buf.writeByte(yRot);
buf.writeByte(yHeadRot);
buf.writeVarInt(remapped);
buf.writeShort(xa);
buf.writeShort(ya);
buf.writeShort(za);
}
} else if (type == Reflections.instance$EntityType$BLOCK_DISPLAY$registryId) {
user.entityView().put(id, Reflections.instance$EntityType$BLOCK_DISPLAY);
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundAddEntityPacket", e);
}
};
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> ADD_ENTITY = (user, event, packet) -> {
try {
Object entityType = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$type(packet);
// Falling blocks
if (entityType == Reflections.instance$EntityType$FALLING_BLOCK) {
int data = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$data(packet);
int remapped = remap(data);
if (remapped != data) {
Reflections.field$ClientboundAddEntityPacket$data.set(packet, remapped);
}
} else if (entityType == Reflections.instance$EntityType$ITEM_DISPLAY) {
if (entityType == Reflections.instance$EntityType$ITEM_DISPLAY) {
// Furniture
int entityId = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$entityId(packet);
LoadedFurniture furniture = BukkitFurnitureManager.instance().loadedFurnitureByRealEntityId(entityId);
@@ -681,9 +717,6 @@ public class PacketConsumers {
if (furniture != null) {
event.setCancelled(true);
}
} else if (entityType == Reflections.instance$EntityType$BLOCK_DISPLAY) {
int entityId = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$entityId(packet);
user.entityView().put(entityId, entityType);
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundAddEntityPacket", e);
@@ -691,21 +724,9 @@ public class PacketConsumers {
};
// 1.21.3+
// TODO USE bytebuffer
// public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> OPEN_SCREEN = (user, event) -> {
// try {
// FriendlyByteBuf buf = event.getBuffer();
// int entityId = buf.readVarInt();
// if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) {
// event.setCancelled(true); // 这里炸了导致无法实现
// }
// } catch (Exception e) {
// CraftEngine.instance().logger().warn("Failed to handle ClientboundEntityPositionSyncPacket", e);
// }
// };
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> SYNC_ENTITY_POSITION = (user, event, packet) -> {
try {
int entityId = (int) Reflections.field$ClientboundEntityPositionSyncPacket$id.get(packet);
int entityId = FastNMS.INSTANCE.method$ClientboundEntityPositionSyncPacket$id(packet);
if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) {
event.setCancelled(true);
}
@@ -714,7 +735,6 @@ public class PacketConsumers {
}
};
// TODO USE bytebuffer
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> MOVE_ENTITY = (user, event, packet) -> {
try {
int entityId = (int) Reflections.field$ClientboundMoveEntityPacket$entityId.get(packet);
@@ -726,10 +746,10 @@ public class PacketConsumers {
}
};
// TODO USE bytebuffer
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> REMOVE_ENTITY = (user, event, packet) -> {
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> REMOVE_ENTITY = (user, event) -> {
try {
IntList intList = FastNMS.INSTANCE.field$ClientboundRemoveEntitiesPacket$entityIds(packet);
FriendlyByteBuf buf = event.getBuffer();
IntList intList = buf.readIntIdList();
for (int i = 0, size = intList.size(); i < size; i++) {
List<Integer> entities = user.furnitureView().remove(intList.getInt(i));
if (entities == null) continue;

View File

@@ -15,4 +15,6 @@ public interface PacketIds {
int clientboundOpenScreenPacket();
int clientboundEntityPositionSyncPacket();
int clientboundRemoveEntitiesPacket();
}

View File

@@ -38,4 +38,9 @@ public class PacketIds1_20 implements PacketIds {
public int clientboundEntityPositionSyncPacket() {
return -1;
}
@Override
public int clientboundRemoveEntitiesPacket() {
return 62;
}
}

View File

@@ -38,4 +38,9 @@ public class PacketIds1_20_2 implements PacketIds {
public int clientboundEntityPositionSyncPacket() {
return -1;
}
@Override
public int clientboundRemoveEntitiesPacket() {
return 64;
}
}

View File

@@ -38,4 +38,9 @@ public class PacketIds1_20_3 implements PacketIds {
public int clientboundEntityPositionSyncPacket() {
return -1;
}
@Override
public int clientboundRemoveEntitiesPacket() {
return 64;
}
}

View File

@@ -38,4 +38,9 @@ public class PacketIds1_20_5 implements PacketIds {
public int clientboundEntityPositionSyncPacket() {
return -1;
}
@Override
public int clientboundRemoveEntitiesPacket() {
return 66;
}
}

View File

@@ -38,4 +38,9 @@ public class PacketIds1_21 implements PacketIds {
public int clientboundEntityPositionSyncPacket() {
return PacketIdFinder.clientboundByName("minecraft:entity_position_sync");
}
@Override
public int clientboundRemoveEntitiesPacket() {
return PacketIdFinder.clientboundByName("minecraft:remove_entities");
}
}

View File

@@ -6264,4 +6264,22 @@ public class Reflections {
public static final Field field$ClientInformation$particleStatus = Optional.ofNullable(clazz$ClientInformation)
.map(it -> ReflectionUtils.getDeclaredField(it, 8))
.orElse(null);
public static final Method method$Registry$getId = requireNonNull(
ReflectionUtils.getMethod(clazz$Registry, int.class, Object.class)
);
public static final int instance$EntityType$FALLING_BLOCK$registryId;
public static final int instance$EntityType$BLOCK_DISPLAY$registryId;
static {
try {
Object blockDisplay = method$ResourceLocation$fromNamespaceAndPath.invoke(null, "minecraft", "block_display");
instance$EntityType$BLOCK_DISPLAY$registryId = (int) Reflections.method$Registry$getId.invoke(Reflections.instance$BuiltInRegistries$ENTITY_TYPE, blockDisplay);
Object fallingBlock = method$ResourceLocation$fromNamespaceAndPath.invoke(null, "minecraft", "falling_block");
instance$EntityType$FALLING_BLOCK$registryId = (int) Reflections.method$Registry$getId.invoke(Reflections.instance$BuiltInRegistries$ENTITY_TYPE, fallingBlock);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -51,7 +51,7 @@ byte_buddy_version=1.17.5
ahocorasick_version=0.6.3
snake_yaml_version=2.4
anti_grief_version=0.13
nms_helper_version=0.43
nms_helper_version=0.44
# Ignite Dependencies
mixinextras_version=0.4.1
mixin_version=0.15.2+mixin.0.8.7