|
47 | 47 | import java.util.Set;
|
48 | 48 | import java.util.UUID;
|
49 | 49 | import java.util.concurrent.ConcurrentHashMap;
|
| 50 | +import java.util.concurrent.ThreadLocalRandom; |
50 | 51 | import net.kyori.adventure.audience.MessageType;
|
51 | 52 | import net.kyori.adventure.identity.Identity;
|
52 | 53 | import net.kyori.adventure.nbt.BinaryTagIO;
|
@@ -425,15 +426,37 @@ static class EntitySound extends PacketFacet<Player> implements Facet.EntitySoun
|
425 | 426 | findMcClassName("world.phys.Vec3")
|
426 | 427 | );
|
427 | 428 |
|
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; |
430 | 431 | private static final MethodHandle NEW_VEC3 = findConstructor(CLASS_VEC3, double.class, double.class, double.class);
|
431 | 432 | private static final MethodHandle NEW_RESOURCE_LOCATION = findConstructor(CLASS_RESOURCE_LOCATION, String.class, String.class);
|
432 | 433 | private static final MethodHandle REGISTRY_GET_OPTIONAL = searchMethod(CLASS_REGISTRY, Modifier.PUBLIC, "getOptional", Optional.class, CLASS_RESOURCE_LOCATION);
|
433 | 434 | private static final MethodHandle SOUND_SOURCE_GET_NAME;
|
434 | 435 | private static final Object REGISTRY_SOUND_EVENT;
|
435 | 436 |
|
436 | 437 | 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 | + } |
437 | 460 | Object registrySoundEvent = null;
|
438 | 461 | MethodHandle soundSourceGetName = null;
|
439 | 462 | if (CLASS_SOUND_SOURCE != null) {
|
@@ -524,10 +547,10 @@ private Object createForEntity(final net.kyori.adventure.sound.Sound sound, fina
|
524 | 547 | final Object nameRl = NEW_RESOURCE_LOCATION.invoke(sound.name().namespace(), sound.name().value());
|
525 | 548 | final java.util.Optional<?> event = (Optional<?>) REGISTRY_GET_OPTIONAL.invoke(REGISTRY_SOUND_EVENT, nameRl);
|
526 | 549 | 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 */); |
528 | 551 | } else if (NEW_CLIENTBOUND_CUSTOM_SOUND != null && NEW_VEC3 != null) {
|
529 | 552 | 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 */); |
531 | 554 | }
|
532 | 555 | } catch (final Throwable error) {
|
533 | 556 | logError(error, "Failed to send sound tracking an entity");
|
|
0 commit comments