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

fix(network): 改进声音事件处理

This commit is contained in:
jhqwqmc
2025-04-09 18:11:27 +08:00
parent 01ecfbe331
commit d2b65d4f7e
2 changed files with 70 additions and 32 deletions

View File

@@ -1555,38 +1555,76 @@ public class PacketConsumers {
try {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
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);
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);
}
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);

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