9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-06 15:52:03 +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 { try {
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt(); int id = buf.readVarInt();
Key soundId = buf.readKey(); if (id == 0) {
Float range = null; Key soundId = buf.readKey();
if (buf.readBoolean()) { Float range = null;
range = buf.readFloat(); if (buf.readBoolean()) {
} range = buf.readFloat();
int source = buf.readVarInt(); }
int x = buf.readInt(); int source = buf.readVarInt();
int y = buf.readInt(); int x = buf.readInt();
int z = buf.readInt(); int y = buf.readInt();
float volume = buf.readFloat(); int z = buf.readInt();
float pitch = buf.readFloat(); float volume = buf.readFloat();
long seed = buf.readLong(); float pitch = buf.readFloat();
Key mapped = BukkitBlockManager.instance().replaceSoundIfExist(soundId); long seed = buf.readLong();
if (mapped != null) { Key mapped = BukkitBlockManager.instance().replaceSoundIfExist(soundId);
event.setChanged(true); if (mapped != null) {
buf.clear(); event.setChanged(true);
buf.writeVarInt(event.packetID()); buf.clear();
buf.writeVarInt(id); buf.writeVarInt(event.packetID());
buf.writeKey(mapped); buf.writeVarInt(id);
if (range != null) { buf.writeKey(mapped);
buf.writeBoolean(true); if (range != null) {
buf.writeFloat(range); buf.writeBoolean(true);
} else { buf.writeFloat(range);
buf.writeBoolean(false); } 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) { } catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundSoundPacket", 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 ahocorasick_version=0.6.3
snake_yaml_version=2.4 snake_yaml_version=2.4
anti_grief_version=0.13 anti_grief_version=0.13
nms_helper_version=0.50 nms_helper_version=0.51
# Ignite Dependencies # Ignite Dependencies
mixinextras_version=0.4.1 mixinextras_version=0.4.1
mixin_version=0.15.2+mixin.0.8.7 mixin_version=0.15.2+mixin.0.8.7