9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 01:49:30 +00:00

Merge pull request #98 from jhqwqmc/dev

feat(network): 优化数据包处理逻辑
This commit is contained in:
XiaoMoMi
2025-04-09 19:48:17 +08:00
committed by GitHub
6 changed files with 192 additions and 42 deletions

View File

@@ -139,7 +139,6 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
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);
registerNMSPacketConsumer(PacketConsumers.SOUND, Reflections.clazz$ClientboundSoundPacket);
registerNMSPacketConsumer(PacketConsumers.RENAME_ITEM, Reflections.clazz$ServerboundRenameItemPacket);
registerNMSPacketConsumer(PacketConsumers.SIGN_UPDATE, Reflections.clazz$ServerboundSignUpdatePacket);
registerNMSPacketConsumer(PacketConsumers.EDIT_BOOK, Reflections.clazz$ServerboundEditBookPacket);
@@ -159,6 +158,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
registerByteBufPacketConsumer(VersionHelper.isVersionNewerThan1_20_3() ? PacketConsumers.SET_OBJECTIVE_1_20_3 : PacketConsumers.SET_OBJECTIVE_1_20, this.packetIds.clientboundSetObjectivePacket());
registerByteBufPacketConsumer(PacketConsumers.REMOVE_ENTITY, this.packetIds.clientboundRemoveEntitiesPacket());
registerByteBufPacketConsumer(PacketConsumers.ADD_ENTITY_BYTEBUFFER, this.packetIds.clientboundAddEntityPacket());
registerByteBufPacketConsumer(PacketConsumers.SOUND, this.packetIds.clientboundSoundPacket());
registerByteBufPacketConsumer(PacketConsumers.SET_ENTITY_DATA, this.packetIds.clientboundSetEntityDataPacket());
}

View File

@@ -39,7 +39,6 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.RayTraceResult;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiConsumer;
@@ -241,25 +240,6 @@ public class PacketConsumers {
}
};
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> SOUND = (user, event, packet) -> {
try {
Object soundEvent = FastNMS.INSTANCE.field$ClientboundSoundPacket$soundEvent(packet);
Key soundId = Key.of(FastNMS.INSTANCE.field$SoundEvent$location(soundEvent).toString());
Key mapped = BukkitBlockManager.instance().replaceSoundIfExist(soundId);
if (mapped != null) {
event.setCancelled(true);
Object newId = FastNMS.INSTANCE.method$ResourceLocation$fromNamespaceAndPath(mapped.namespace(), mapped.value());
Object newSoundEvent = VersionHelper.isVersionNewerThan1_21_2() ?
Reflections.constructor$SoundEvent.newInstance(newId, Reflections.field$SoundEvent$fixedRange.get(soundEvent)) :
Reflections.constructor$SoundEvent.newInstance(newId, Reflections.field$SoundEvent$range.get(soundEvent), Reflections.field$SoundEvent$newSystem.get(soundEvent));
Object newSoundPacket = FastNMS.INSTANCE.fastConstructor$ClientboundSoundPacket(newSoundEvent, packet);
user.sendPacket(newSoundPacket, true);
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundSoundPacket", e);
}
};
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> TEAM_1_20_3 = (user, event) -> {
if (!Config.interceptTeam()) return;
try {
@@ -936,28 +916,107 @@ public class PacketConsumers {
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> LEVEL_PARTICLE = (user, event) -> {
try {
FriendlyByteBuf buf = event.getBuffer();
Object mcByteBuf;
Method writeMethod;
if (VersionHelper.isVersionNewerThan1_20_5()) {
mcByteBuf = Reflections.constructor$RegistryFriendlyByteBuf.newInstance(buf, Reflections.instance$registryAccess);
writeMethod = Reflections.method$ClientboundLevelParticlesPacket$write;
if (VersionHelper.isVersionNewerThan1_21_3()) {
boolean overrideLimiter = buf.readBoolean();
boolean alwaysShow = buf.readBoolean();
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
float xDist = buf.readFloat();
float yDist = buf.readFloat();
float zDist = buf.readFloat();
float maxSpeed = buf.readFloat();
int count = buf.readInt();
Object option = FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$decode(buf);
if (option == null) return;
if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return;
Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option);
int id = BlockStateUtils.blockStateToId(blockState);
int remapped = remap(id);
if (remapped == id) return;
Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option);
Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped));
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeBoolean(overrideLimiter);
buf.writeBoolean(alwaysShow);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeFloat(xDist);
buf.writeFloat(yDist);
buf.writeFloat(zDist);
buf.writeFloat(maxSpeed);
buf.writeInt(count);
FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$encode(buf, remappedOption);
} else if (VersionHelper.isVersionNewerThan1_20_5()) {
boolean overrideLimiter = buf.readBoolean();
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
float xDist = buf.readFloat();
float yDist = buf.readFloat();
float zDist = buf.readFloat();
float maxSpeed = buf.readFloat();
int count = buf.readInt();
Object option = FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$decode(buf);
if (option == null) return;
if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return;
Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option);
int id = BlockStateUtils.blockStateToId(blockState);
int remapped = remap(id);
if (remapped == id) return;
Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option);
Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped));
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeBoolean(overrideLimiter);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeFloat(xDist);
buf.writeFloat(yDist);
buf.writeFloat(zDist);
buf.writeFloat(maxSpeed);
buf.writeInt(count);
FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$encode(buf, remappedOption);
} else {
mcByteBuf = Reflections.constructor$FriendlyByteBuf.newInstance(event.getBuffer().source());
writeMethod = Reflections.method$Packet$write;
Object particleType = FastNMS.INSTANCE.method$FriendlyByteBuf$readById(buf, Reflections.instance$BuiltInRegistries$PARTICLE_TYPE);
boolean overrideLimiter = buf.readBoolean();
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
float xDist = buf.readFloat();
float yDist = buf.readFloat();
float zDist = buf.readFloat();
float maxSpeed = buf.readFloat();
int count = buf.readInt();
Object option = FastNMS.INSTANCE.method$ClientboundLevelParticlesPacket$readParticle(buf, particleType);
if (option == null) return;
if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return;
Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option);
int id = BlockStateUtils.blockStateToId(blockState);
int remapped = remap(id);
if (remapped == id) return;
Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option);
Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped));
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
FastNMS.INSTANCE.method$FriendlyByteBuf$writeId(buf, remappedOption, Reflections.instance$BuiltInRegistries$PARTICLE_TYPE);
buf.writeBoolean(overrideLimiter);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeFloat(xDist);
buf.writeFloat(yDist);
buf.writeFloat(zDist);
buf.writeFloat(maxSpeed);
buf.writeInt(count);
FastNMS.INSTANCE.method$ParticleOptions$writeToNetwork(remappedOption, buf);
}
Object packet = Reflections.constructor$ClientboundLevelParticlesPacket.newInstance(mcByteBuf);
Object option = FastNMS.INSTANCE.field$ClientboundLevelParticlesPacket$particle(packet);
if (option == null) return;
if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return;
Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option);
int id = BlockStateUtils.blockStateToId(blockState);
int remapped = remap(id);
if (remapped == id) return;
Reflections.field$BlockParticleOption$blockState.set(option, BlockStateUtils.idToBlockState(remapped));
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
writeMethod.invoke(packet, mcByteBuf);
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundLevelParticlesPacket", e);
}
@@ -1492,6 +1551,85 @@ public class PacketConsumers {
}
};
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> SOUND = (user, event) -> {
try {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
if (id == 0) {
Key soundId = buf.readKey();
Float range = null;
if (buf.readBoolean()) {
range = buf.readFloat();
}
int source = buf.readVarInt();
int x = buf.readInt();
int y = buf.readInt();
int z = buf.readInt();
float volume = buf.readFloat();
float pitch = buf.readFloat();
long seed = buf.readLong();
Key mapped = BukkitBlockManager.instance().replaceSoundIfExist(soundId);
if (mapped != null) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
buf.writeKey(mapped);
if (range != null) {
buf.writeBoolean(true);
buf.writeFloat(range);
} else {
buf.writeBoolean(false);
}
buf.writeVarInt(source);
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeFloat(volume);
buf.writeFloat(pitch);
buf.writeLong(seed);
}
} else {
Optional<Object> optionalSound = FastNMS.INSTANCE.method$BuiltInRegistries$byId(Reflections.instance$BuiltInRegistries$SOUND_EVENT, id - 1);
if (optionalSound.isEmpty()) return;
Object sound = optionalSound.get();
Key soundId = Key.of(FastNMS.INSTANCE.method$SoundEvent$location(sound));
int source = buf.readVarInt();
int x = buf.readInt();
int y = buf.readInt();
int z = buf.readInt();
float volume = buf.readFloat();
float pitch = buf.readFloat();
long seed = buf.readLong();
Key mapped = BukkitBlockManager.instance().replaceSoundIfExist(soundId);
if (mapped != null) {
Optional<Integer> mappedId = FastNMS.INSTANCE.method$BuiltInRegistries$getId(
Reflections.instance$BuiltInRegistries$SOUND_EVENT,
FastNMS.INSTANCE.method$SoundEvent$createVariableRangeEvent(KeyUtils.toResourceLocation(mapped))
);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
if (mappedId.isPresent()) {
buf.writeVarInt(mappedId.get() + 1);
} else {
buf.writeVarInt(0);
buf.writeKey(mapped);
buf.writeBoolean(false);
}
buf.writeVarInt(source);
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeFloat(volume);
buf.writeFloat(pitch);
buf.writeLong(seed);
}
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundSoundPacket", e);
}};
// we handle it on packet level to prevent it from being captured by plugins
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> RENAME_ITEM = (user, event, packet) -> {
try {

View File

@@ -35,4 +35,6 @@ public interface PacketIds {
int clientboundSetPlayerTeamPacket();
int clientboundSetObjectivePacket();
int clientboundLevelChunkWithLightPacket();
}

View File

@@ -89,4 +89,9 @@ public class PacketIds1_20 implements PacketIds {
public int clientboundSetObjectivePacket() {
return PacketIdFinder.clientboundByClazz(Reflections.clazz$ClientboundSetObjectivePacket);
}
@Override
public int clientboundLevelChunkWithLightPacket() {
return PacketIdFinder.clientboundByClazz(Reflections.clazz$ClientboundLevelChunkWithLightPacket);
}
}

View File

@@ -88,4 +88,9 @@ public class PacketIds1_20_5 implements PacketIds {
public int clientboundSetObjectivePacket() {
return PacketIdFinder.clientboundByName("minecraft:set_objective");
}
@Override
public int clientboundLevelChunkWithLightPacket() {
return PacketIdFinder.clientboundByName("minecraft:level_chunk_with_light");
}
}

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.48
nms_helper_version=0.51
# Ignite Dependencies
mixinextras_version=0.4.1
mixin_version=0.15.2+mixin.0.8.7