mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Implement new sound changes
This commit is contained in:
parent
e5ca8d992e
commit
0ba27939d9
@ -3,6 +3,7 @@ package net.minestom.server.adventure;
|
|||||||
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||||
import net.kyori.adventure.bossbar.BossBar;
|
import net.kyori.adventure.bossbar.BossBar;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.sound.Sound;
|
import net.kyori.adventure.sound.Sound;
|
||||||
import net.kyori.adventure.sound.SoundStop;
|
import net.kyori.adventure.sound.SoundStop;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
@ -14,6 +15,7 @@ import net.minestom.server.network.packet.server.play.SoundEffectPacket;
|
|||||||
import net.minestom.server.network.packet.server.play.StopSoundPacket;
|
import net.minestom.server.network.packet.server.play.StopSoundPacket;
|
||||||
import net.minestom.server.registry.Registries;
|
import net.minestom.server.registry.Registries;
|
||||||
import net.minestom.server.sound.SoundEvent;
|
import net.minestom.server.sound.SoundEvent;
|
||||||
|
import net.minestom.server.utils.Position;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -44,6 +46,7 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the int value of a boss bar overlay.
|
* Gets the int value of a boss bar overlay.
|
||||||
|
*
|
||||||
* @param overlay the overlay
|
* @param overlay the overlay
|
||||||
* @return the value
|
* @return the value
|
||||||
*/
|
*/
|
||||||
@ -53,6 +56,7 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the byte value of a collection of boss bar flags.
|
* Gets the byte value of a collection of boss bar flags.
|
||||||
|
*
|
||||||
* @param flags the flags
|
* @param flags the flags
|
||||||
* @return the value
|
* @return the value
|
||||||
*/
|
*/
|
||||||
@ -66,6 +70,7 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the int value of a boss bar color.
|
* Gets the int value of a boss bar color.
|
||||||
|
*
|
||||||
* @param color the color
|
* @param color the color
|
||||||
* @return the value
|
* @return the value
|
||||||
*/
|
*/
|
||||||
@ -75,6 +80,7 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the int value of a sound source.
|
* Gets the int value of a sound source.
|
||||||
|
*
|
||||||
* @param source the source
|
* @param source the source
|
||||||
* @return the value
|
* @return the value
|
||||||
*/
|
*/
|
||||||
@ -84,6 +90,7 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the int value from a named text color.
|
* Gets the int value from a named text color.
|
||||||
|
*
|
||||||
* @param color the color
|
* @param color the color
|
||||||
* @return the int value
|
* @return the int value
|
||||||
*/
|
*/
|
||||||
@ -93,17 +100,18 @@ public class AdventurePacketConvertor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a sound packet from a sound and a location.
|
* Creates a sound packet from a sound and a location.
|
||||||
|
*
|
||||||
* @param sound the sound
|
* @param sound the sound
|
||||||
* @param x the x coordinate
|
* @param x the x coordinate
|
||||||
* @param y the y coordinate
|
* @param y the y coordinate
|
||||||
* @param z the z coordinate
|
* @param z the z coordinate
|
||||||
* @return the sound packet
|
* @return the sound packet
|
||||||
*/
|
*/
|
||||||
public static ServerPacket createSoundPacket(@NotNull Sound sound, double x, double y, double z) {
|
public static @NotNull ServerPacket createSoundPacket(@NotNull Sound sound, double x, double y, double z) {
|
||||||
SoundEvent minestomSound = Registries.getSoundEvent(sound.name());
|
final SoundEvent minestomSound = Registries.getSoundEvent(sound.name());
|
||||||
|
|
||||||
if (minestomSound == null) {
|
if (minestomSound == null) {
|
||||||
NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
final NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
||||||
packet.soundName = sound.name().asString();
|
packet.soundName = sound.name().asString();
|
||||||
packet.soundSource = sound.source();
|
packet.soundSource = sound.source();
|
||||||
packet.x = (int) x;
|
packet.x = (int) x;
|
||||||
@ -113,7 +121,7 @@ public class AdventurePacketConvertor {
|
|||||||
packet.pitch = sound.pitch();
|
packet.pitch = sound.pitch();
|
||||||
return packet;
|
return packet;
|
||||||
} else {
|
} else {
|
||||||
SoundEffectPacket packet = new SoundEffectPacket();
|
final SoundEffectPacket packet = new SoundEffectPacket();
|
||||||
packet.soundId = minestomSound.getId();
|
packet.soundId = minestomSound.getId();
|
||||||
packet.soundSource = sound.source();
|
packet.soundSource = sound.source();
|
||||||
packet.x = (int) x;
|
packet.x = (int) x;
|
||||||
@ -126,29 +134,60 @@ public class AdventurePacketConvertor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an entity sound packet from an Adventure sound.
|
* Creates a sound effect packet from a sound and an emitter.
|
||||||
|
*
|
||||||
* @param sound the sound
|
* @param sound the sound
|
||||||
* @param entity the entity the sound is coming from
|
* @param emitter the emitter, must be an {@link Entity}
|
||||||
* @return the packet
|
* @return the sound packet
|
||||||
*/
|
*/
|
||||||
public static ServerPacket createEntitySoundPacket(@NotNull Sound sound, @NotNull Entity entity) {
|
public static @NotNull ServerPacket createSoundPacket(@NotNull Sound sound, Sound.@NotNull Emitter emitter) {
|
||||||
SoundEvent soundEvent = Registries.getSoundEvent(sound.name());
|
if (emitter == Sound.Emitter.self())
|
||||||
|
throw new IllegalArgumentException("you must replace instances of Emitter.self() before calling this method");
|
||||||
|
if (!(emitter instanceof Entity))
|
||||||
|
throw new IllegalArgumentException("you can only call this method with entities");
|
||||||
|
|
||||||
if (soundEvent == null) {
|
final Entity entity = (Entity) emitter;
|
||||||
throw new IllegalArgumentException("Sound must be a valid sound event.");
|
final SoundEvent minestomSound = Registries.getSoundEvent(sound.name());
|
||||||
} else {
|
|
||||||
EntitySoundEffectPacket packet = new EntitySoundEffectPacket();
|
if (minestomSound != null) {
|
||||||
packet.soundId = soundEvent.getId();
|
final EntitySoundEffectPacket packet = new EntitySoundEffectPacket();
|
||||||
|
packet.soundId = minestomSound.getId();
|
||||||
packet.soundSource = sound.source();
|
packet.soundSource = sound.source();
|
||||||
packet.entityId = entity.getEntityId();
|
packet.entityId = entity.getEntityId();
|
||||||
packet.volume = sound.volume();
|
packet.volume = sound.volume();
|
||||||
packet.pitch = sound.pitch();
|
packet.pitch = sound.pitch();
|
||||||
return packet;
|
return packet;
|
||||||
|
} else {
|
||||||
|
final Position pos = entity.getPosition();
|
||||||
|
|
||||||
|
final NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
||||||
|
packet.soundName = sound.name().asString();
|
||||||
|
packet.soundSource = sound.source();
|
||||||
|
packet.x = (int) pos.getX();
|
||||||
|
packet.y = (int) pos.getY();
|
||||||
|
packet.z = (int) pos.getX();
|
||||||
|
packet.volume = sound.volume();
|
||||||
|
packet.pitch = sound.pitch();
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an entity sound packet from an Adventure sound.
|
||||||
|
*
|
||||||
|
* @param sound the sound
|
||||||
|
* @param entity the entity the sound is coming from
|
||||||
|
* @return the packet
|
||||||
|
* @deprecated Use {@link #createSoundPacket(Sound, Sound.Emitter)}
|
||||||
|
*/
|
||||||
|
@Deprecated(forRemoval = true)
|
||||||
|
public static ServerPacket createEntitySoundPacket(@NotNull Sound sound, @NotNull Entity entity) {
|
||||||
|
return createSoundPacket(sound, entity);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a sound stop packet from a sound stop.
|
* Creates a sound stop packet from a sound stop.
|
||||||
|
*
|
||||||
* @param stop the sound stop
|
* @param stop the sound stop
|
||||||
* @return the sound stop packet
|
* @return the sound stop packet
|
||||||
*/
|
*/
|
||||||
@ -156,14 +195,16 @@ public class AdventurePacketConvertor {
|
|||||||
StopSoundPacket packet = new StopSoundPacket();
|
StopSoundPacket packet = new StopSoundPacket();
|
||||||
packet.flags = 0x0;
|
packet.flags = 0x0;
|
||||||
|
|
||||||
if (stop.source() != null) {
|
final Sound.Source source = stop.source();
|
||||||
|
if (source != null) {
|
||||||
packet.flags |= 0x1;
|
packet.flags |= 0x1;
|
||||||
packet.source = AdventurePacketConvertor.getSoundSourceValue(stop.source());
|
packet.source = AdventurePacketConvertor.getSoundSourceValue(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stop.sound() != null) {
|
final Key sound = stop.sound();
|
||||||
|
if (sound != null) {
|
||||||
packet.flags |= 0x2;
|
packet.flags |= 0x2;
|
||||||
packet.sound = stop.sound().asString();
|
packet.sound = sound.asString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return packet;
|
return packet;
|
||||||
|
@ -90,11 +90,25 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
|||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void playSound(@NotNull Sound sound, Sound.@NotNull Emitter emitter) {
|
||||||
|
if (emitter != Sound.Emitter.self()) {
|
||||||
|
PacketUtils.sendGroupedPacket(this.getPlayers(), AdventurePacketConvertor.createSoundPacket(sound, emitter));
|
||||||
|
} else {
|
||||||
|
// if we're playing on self, we need to delegate to each audience member
|
||||||
|
for (Audience audience : this.audiences()) {
|
||||||
|
audience.playSound(sound, emitter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void stopSound(@NotNull SoundStop stop) {
|
default void stopSound(@NotNull SoundStop stop) {
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), AdventurePacketConvertor.createSoundStopPacket(stop));
|
PacketUtils.sendGroupedPacket(this.getPlayers(), AdventurePacketConvertor.createSoundStopPacket(stop));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default @NotNull Iterable<? extends Audience> audiences() {
|
default @NotNull Iterable<? extends Audience> audiences() {
|
||||||
return this.getPlayers();
|
return this.getPlayers();
|
||||||
|
@ -2,6 +2,7 @@ package net.minestom.server.entity;
|
|||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
import com.google.common.collect.Queues;
|
import com.google.common.collect.Queues;
|
||||||
|
import net.kyori.adventure.sound.Sound;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.event.HoverEvent;
|
import net.kyori.adventure.text.event.HoverEvent;
|
||||||
import net.kyori.adventure.text.event.HoverEvent.ShowEntity;
|
import net.kyori.adventure.text.event.HoverEvent.ShowEntity;
|
||||||
@ -64,7 +65,7 @@ import java.util.function.UnaryOperator;
|
|||||||
* <p>
|
* <p>
|
||||||
* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead.
|
* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead.
|
||||||
*/
|
*/
|
||||||
public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, DataContainer, PermissionHandler, HoverEventSource<ShowEntity> {
|
public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, DataContainer, PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter {
|
||||||
|
|
||||||
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
|
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
|
||||||
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
|
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
|
||||||
|
@ -836,7 +836,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playSound(@NotNull Sound sound) {
|
public void playSound(@NotNull Sound sound) {
|
||||||
playerConnection.sendPacket(AdventurePacketConvertor.createEntitySoundPacket(sound, this));
|
this.playSound(sound, this.position.getX(), this.position.getY(), this.position.getZ());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -844,6 +844,19 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
playerConnection.sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
playerConnection.sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playSound(@NotNull Sound sound, Sound.@NotNull Emitter emitter) {
|
||||||
|
final ServerPacket packet;
|
||||||
|
|
||||||
|
if (emitter == Sound.Emitter.self()) {
|
||||||
|
packet = AdventurePacketConvertor.createSoundPacket(sound, this);
|
||||||
|
} else {
|
||||||
|
packet = AdventurePacketConvertor.createSoundPacket(sound, emitter);
|
||||||
|
}
|
||||||
|
|
||||||
|
playerConnection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopSound(@NotNull SoundStop stop) {
|
public void stopSound(@NotNull SoundStop stop) {
|
||||||
playerConnection.sendPacket(AdventurePacketConvertor.createSoundStopPacket(stop));
|
playerConnection.sendPacket(AdventurePacketConvertor.createSoundStopPacket(stop));
|
||||||
|
Loading…
Reference in New Issue
Block a user