Skip to content

Commit cba3e84

Browse files
authored
Merge pull request #99 from Machine-Maker/fix/craftbukkit/entity-sound/1.19
fix: CraftBukkit EntitySound for 1.19+
2 parents 92962c2 + 22036c1 commit cba3e84

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/CraftBukkitFacet.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Set;
4848
import java.util.UUID;
4949
import java.util.concurrent.ConcurrentHashMap;
50+
import java.util.concurrent.ThreadLocalRandom;
5051
import net.kyori.adventure.audience.MessageType;
5152
import net.kyori.adventure.identity.Identity;
5253
import net.kyori.adventure.nbt.BinaryTagIO;
@@ -425,15 +426,37 @@ static class EntitySound extends PacketFacet<Player> implements Facet.EntitySoun
425426
findMcClassName("world.phys.Vec3")
426427
);
427428

428-
private static final MethodHandle NEW_CLIENTBOUND_ENTITY_SOUND = findConstructor(CLASS_CLIENTBOUND_ENTITY_SOUND, CLASS_SOUND_EFFECT, CLASS_SOUND_SOURCE, CLASS_NMS_ENTITY, float.class, float.class);
429-
private static final MethodHandle NEW_CLIENTBOUND_CUSTOM_SOUND = findConstructor(CLASS_CLIENTBOUND_CUSTOM_SOUND, CLASS_RESOURCE_LOCATION, CLASS_SOUND_SOURCE, CLASS_VEC3, float.class, float.class);
429+
private static final MethodHandle NEW_CLIENTBOUND_ENTITY_SOUND;
430+
private static final MethodHandle NEW_CLIENTBOUND_CUSTOM_SOUND;
430431
private static final MethodHandle NEW_VEC3 = findConstructor(CLASS_VEC3, double.class, double.class, double.class);
431432
private static final MethodHandle NEW_RESOURCE_LOCATION = findConstructor(CLASS_RESOURCE_LOCATION, String.class, String.class);
432433
private static final MethodHandle REGISTRY_GET_OPTIONAL = searchMethod(CLASS_REGISTRY, Modifier.PUBLIC, "getOptional", Optional.class, CLASS_RESOURCE_LOCATION);
433434
private static final MethodHandle SOUND_SOURCE_GET_NAME;
434435
private static final Object REGISTRY_SOUND_EVENT;
435436

436437
static {
438+
{
439+
MethodHandle entitySoundPacketConstructor = findConstructor(CLASS_CLIENTBOUND_ENTITY_SOUND, CLASS_SOUND_EFFECT, CLASS_SOUND_SOURCE, CLASS_NMS_ENTITY, float.class, float.class, long.class);
440+
if (entitySoundPacketConstructor == null) {
441+
// 1.18 (no seed)
442+
entitySoundPacketConstructor = findConstructor(CLASS_CLIENTBOUND_ENTITY_SOUND, CLASS_SOUND_EFFECT, CLASS_SOUND_SOURCE, CLASS_NMS_ENTITY, float.class, float.class);
443+
if (entitySoundPacketConstructor != null) {
444+
entitySoundPacketConstructor = dropArguments(entitySoundPacketConstructor, 5, long.class);
445+
}
446+
}
447+
NEW_CLIENTBOUND_ENTITY_SOUND = entitySoundPacketConstructor;
448+
}
449+
{
450+
MethodHandle customSoundPacketConstructor = findConstructor(CLASS_CLIENTBOUND_CUSTOM_SOUND, CLASS_RESOURCE_LOCATION, CLASS_SOUND_SOURCE, CLASS_VEC3, float.class, float.class, long.class);
451+
if (customSoundPacketConstructor == null) {
452+
// 1.18 (no seed)
453+
customSoundPacketConstructor = findConstructor(CLASS_CLIENTBOUND_CUSTOM_SOUND, CLASS_RESOURCE_LOCATION, CLASS_SOUND_SOURCE, CLASS_VEC3, float.class, float.class);
454+
if (customSoundPacketConstructor != null) {
455+
customSoundPacketConstructor = dropArguments(customSoundPacketConstructor, 5, long.class);
456+
}
457+
}
458+
NEW_CLIENTBOUND_CUSTOM_SOUND = customSoundPacketConstructor;
459+
}
437460
Object registrySoundEvent = null;
438461
MethodHandle soundSourceGetName = null;
439462
if (CLASS_SOUND_SOURCE != null) {
@@ -524,10 +547,10 @@ private Object createForEntity(final net.kyori.adventure.sound.Sound sound, fina
524547
final Object nameRl = NEW_RESOURCE_LOCATION.invoke(sound.name().namespace(), sound.name().value());
525548
final java.util.Optional<?> event = (Optional<?>) REGISTRY_GET_OPTIONAL.invoke(REGISTRY_SOUND_EVENT, nameRl);
526549
if (event.isPresent()) {
527-
return NEW_CLIENTBOUND_ENTITY_SOUND.invoke(event.get(), soundCategory, nmsEntity, sound.volume(), sound.pitch());
550+
return NEW_CLIENTBOUND_ENTITY_SOUND.invoke(event.get(), soundCategory, nmsEntity, sound.volume(), sound.pitch(), ThreadLocalRandom.current().nextLong() /* TODO replace with sound seed when 4.12.X releases */);
528551
} else if (NEW_CLIENTBOUND_CUSTOM_SOUND != null && NEW_VEC3 != null) {
529552
final Location loc = entity.getLocation();
530-
return NEW_CLIENTBOUND_CUSTOM_SOUND.invoke(nameRl, soundCategory, NEW_VEC3.invoke(loc.getX(), loc.getY(), loc.getZ()), sound.volume(), sound.pitch());
553+
return NEW_CLIENTBOUND_CUSTOM_SOUND.invoke(nameRl, soundCategory, NEW_VEC3.invoke(loc.getX(), loc.getY(), loc.getZ()), sound.volume(), sound.pitch(), ThreadLocalRandom.current().nextLong() /* TODO replace with sound seed when 4.12.X releases */);
531554
}
532555
} catch (final Throwable error) {
533556
logError(error, "Failed to send sound tracking an entity");

0 commit comments

Comments
 (0)