From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Sat, 19 Jun 2021 22:55:10 -0400 Subject: [PATCH] Add SoundEvent diff --git a/src/main/java/me/lexikiq/event/sound/EntitySoundEvent.java b/src/main/java/me/lexikiq/event/sound/EntitySoundEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..af2a2e87c8e266e908a9a72df1e469842cb7f19d --- /dev/null +++ b/src/main/java/me/lexikiq/event/sound/EntitySoundEvent.java @@ -0,0 +1,49 @@ +package me.lexikiq.event.sound; + +import org.apache.commons.lang.Validate; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.HumanEntity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Called when an entity sound is sent to a player. Cancelling this event will prevent the packet from sending. + *

+ * This type of sound represents one that will follow the {@link #origin} entity. + */ +public class EntitySoundEvent extends NamedSoundEvent { + private @NotNull Entity origin; + + public EntitySoundEvent(@Nullable HumanEntity receiver, @NotNull Entity origin, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + super(receiver, sound, category, volume, pitch); + Validate.notNull(origin, "origin cannot be null"); + this.origin = origin; + } + + /** + * Gets the entity that the sound is originating from + * @return originating entity + */ + public @NotNull Entity getOrigin() { + return origin; + } + + /** + * Sets the entity that the sound will originate from + * @param origin originating entity + */ + public void setOrigin(@NotNull Entity origin) { + Validate.notNull(origin, "origin cannot be null"); + if (!this.origin.getWorld().equals(origin.getWorld())) + throw new IllegalArgumentException("Entity must be in same world as originating location"); + this.origin = origin; + } + + @Override + public @org.jetbrains.annotations.NotNull World getWorld() { + return origin.getWorld(); + } +} diff --git a/src/main/java/me/lexikiq/event/sound/LocationCustomSoundEvent.java b/src/main/java/me/lexikiq/event/sound/LocationCustomSoundEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..f007bc326bea341735e7313789f5d110af5ac4ca --- /dev/null +++ b/src/main/java/me/lexikiq/event/sound/LocationCustomSoundEvent.java @@ -0,0 +1,121 @@ +package me.lexikiq.event.sound; + +import me.lexikiq.HasLocation; +import org.apache.commons.lang.Validate; +import org.bukkit.Keyed; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.SoundCategory; +import org.bukkit.World; +import org.bukkit.entity.HumanEntity; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Called when a sound not available in {@link org.bukkit.Sound} is sent to a player from a {@link Location}. Cancelling this event will prevent the packet from sending. + */ +public class LocationCustomSoundEvent extends SoundEvent implements Keyed, HasLocation { + private @NotNull Vector vector; + private @NotNull NamespacedKey key; + private @NotNull final World world; + + public LocationCustomSoundEvent(@Nullable HumanEntity receiver, @NotNull World world, @NotNull Vector vector, @NotNull NamespacedKey key, @NotNull SoundCategory category, float volume, float pitch) { + super(receiver, category, volume, pitch); + Validate.notNull(world, "world cannot be null"); + Validate.notNull(vector, "vector cannot be null"); + Validate.notNull(key, "key cannot be null"); + this.world = world; + this.vector = vector; + this.key = key; + } + + public LocationCustomSoundEvent(@Nullable HumanEntity receiver, @NotNull Location location, @NotNull NamespacedKey key, @NotNull SoundCategory category, float volume, float pitch) { + this(receiver, checkNotNull(location, "location").getWorld(), location.toVector(), key, category, volume, pitch); + } + + public LocationCustomSoundEvent(@NotNull HumanEntity receiver, @NotNull Vector vector, @NotNull NamespacedKey key, @NotNull SoundCategory category, float volume, float pitch) { + this(checkNotNull(receiver, "receiver"), receiver.getWorld(), vector, key, category, volume, pitch); + } + + public LocationCustomSoundEvent(@NotNull HumanEntity receiver, @NotNull NamespacedKey key, @NotNull SoundCategory category, float volume, float pitch) { + this(checkNotNull(receiver, "receiver"), receiver.getLocation().toVector(), key, category, volume, pitch); + } + + /** + * Gets the position in the world of the sound + * @return position in the world + */ + public @NotNull Vector getVector() { + return vector; + } + + /** + * Gets the resource pack key of the sound + * @return asset key + */ + public @NotNull NamespacedKey getKey() { + return key; + } + + @Override + public @NotNull World getWorld() { + return world; + } + + /** + * Gets the location of the sound being played + * @return copy of sound location + * @see #getVector() + * @see #setVector(Vector) + */ + public @NotNull Location getLocation() { + return new Location(world, vector.getX(), vector.getY(), vector.getZ()); + } + + /** + * Sets the position in the world of the sound + * @param vector position in the world + */ + public void setVector(@NotNull Vector vector) { + Validate.notNull(vector, "vector cannot be null"); + this.vector = vector; + } + + /** + * Sets the resource pack key of the sound + * @param key asset key + */ + public void setKey(@NotNull NamespacedKey key) { + Validate.notNull(key, "key cannot be null"); + this.key = key; + } + + // boilerplate + + private boolean cancelled; + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + private static final org.bukkit.event.HandlerList handlers = new org.bukkit.event.HandlerList(); + + @Override + public @NotNull org.bukkit.event.HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static org.bukkit.event.HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/me/lexikiq/event/sound/LocationNamedSoundEvent.java b/src/main/java/me/lexikiq/event/sound/LocationNamedSoundEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..81ba14fea8d04347a0fd3931b27188028c5ac859 --- /dev/null +++ b/src/main/java/me/lexikiq/event/sound/LocationNamedSoundEvent.java @@ -0,0 +1,72 @@ +package me.lexikiq.event.sound; + +import me.lexikiq.HasLocation; +import org.apache.commons.lang.Validate; +import org.bukkit.Location; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.World; +import org.bukkit.entity.HumanEntity; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Called when a sound available in {@link Sound} is sent to a player from a {@link Location}. Cancelling this event will prevent the packet from sending. + */ +public class LocationNamedSoundEvent extends NamedSoundEvent implements HasLocation { + private @NotNull Vector vector; + private @NotNull final World world; + + public LocationNamedSoundEvent(@Nullable HumanEntity receiver, @NotNull World world, @NotNull Vector vector, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + super(receiver, sound, category, volume, pitch); + this.world = checkNotNull(world, "world"); + this.vector = checkNotNull(vector, "vector"); + } + + public LocationNamedSoundEvent(@Nullable HumanEntity receiver, @NotNull Location location, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + this(receiver, checkNotNull(location, "location").getWorld(), location.toVector(), sound, category, volume, pitch); + } + + public LocationNamedSoundEvent(@NotNull HumanEntity receiver, @NotNull Vector vector, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + this(checkNotNull(receiver, "receiver"), receiver.getWorld(), vector, sound, category, volume, pitch); + } + + public LocationNamedSoundEvent(@NotNull HumanEntity receiver, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + this(checkNotNull(receiver, "receiver"), receiver.getLocation().toVector(), sound, category, volume, pitch); + } + + /** + * Gets the position in the world of the sound + * @return position in the world + */ + public @NotNull Vector getVector() { + return vector; + } + + @Override + public @NotNull World getWorld() { + return world; + } + + /** + * Gets the location of the sound being played + * @return copy of sound location + * @see #getVector() + * @see #setVector(Vector) + */ + public @NotNull Location getLocation() { + return new Location(world, vector.getX(), vector.getY(), vector.getZ()); + } + + /** + * Sets the position in the world of the sound + * @param vector position in the world + */ + public void setVector(@NotNull Vector vector) { + Validate.notNull(vector, "vector cannot be null"); + this.vector = vector; + } +} diff --git a/src/main/java/me/lexikiq/event/sound/NamedSoundEvent.java b/src/main/java/me/lexikiq/event/sound/NamedSoundEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..8623e03aa5ca094629bf6a20669ea8c5086d938c --- /dev/null +++ b/src/main/java/me/lexikiq/event/sound/NamedSoundEvent.java @@ -0,0 +1,64 @@ +package me.lexikiq.event.sound; + +import org.apache.commons.lang.Validate; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.entity.HumanEntity; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Called when a sound available in {@link Sound} is sent to a player. Cancelling this event will prevent the packet from sending. + */ +public abstract class NamedSoundEvent extends SoundEvent { + private @NotNull Sound sound; + + public NamedSoundEvent(@Nullable HumanEntity receiver, @NotNull Sound sound, @NotNull SoundCategory category, float volume, float pitch) { + super(receiver, category, volume, pitch); + Validate.notNull(sound, "sound cannot be null"); + this.sound = sound; + } + + /** + * Gets the sound being played + * @return sound to play + */ + public @NotNull Sound getSound() { + return sound; + } + + /** + * Sets the sound yo played + * @param sound sound to play + */ + public void setSound(@NotNull Sound sound) { + Validate.notNull(sound, "sound cannot be null"); + this.sound = sound; + } + + // boilerplate + + private boolean cancelled; + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + private static final org.bukkit.event.HandlerList handlers = new org.bukkit.event.HandlerList(); + + @Override + public @NotNull org.bukkit.event.HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static org.bukkit.event.HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/me/lexikiq/event/sound/SoundEvent.java b/src/main/java/me/lexikiq/event/sound/SoundEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..5cc39ee9ae52a52bebde46236162f1260b96b3a0 --- /dev/null +++ b/src/main/java/me/lexikiq/event/sound/SoundEvent.java @@ -0,0 +1,97 @@ +package me.lexikiq.event.sound; + +import me.lexikiq.OptionalHumanEntity; +import org.apache.commons.lang.Validate; +import org.bukkit.SoundCategory; +import org.bukkit.World; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Called when a sound is sent to a player. Cancelling this event will prevent the packet from sending. + */ +public abstract class SoundEvent extends Event implements Cancellable, OptionalHumanEntity { + private @Nullable final HumanEntity receiver; + private @NotNull SoundCategory category; + private float volume; + private float pitch; + + public SoundEvent(@Nullable HumanEntity receiver, @NotNull SoundCategory category, float volume, float pitch) { + super(true); + Validate.notNull(category, "category cannot be null"); + this.receiver = receiver; + this.category = category; + this.volume = volume; + this.pitch = pitch; + } + + /** + * Gets the player that this sound will be sent to. May be null if the sound is being played to several players. + * @return player receiving the sound + */ + @Override + public @Nullable HumanEntity getPlayer() { + return receiver; + } + + /** + * Gets the category of sounds this will be played with. + * @return category of sounds + */ + public @NotNull SoundCategory getCategory() { + return category; + } + + /** + * Gets the volume of the sound to be played. Should be non-negative. + * @return sound volume + */ + public float getVolume() { + return volume; + } + + /** + * Gets the pitch of the sound to be played. Should be within the range [0,2]. + * @return sound pitch + */ + public float getPitch() { + return pitch; + } + + /** + * Sets the category of sounds this will be played with. + * @param category category of sounds + */ + public void setCategory(@NotNull SoundCategory category) { + Validate.notNull(category, "category cannot be null"); + this.category = category; + } + + /** + * Sets the volume of the sound to be played. Must be non-negative. + * @param volume sound volume + */ + public void setVolume(float volume) { + Validate.isTrue(volume >= 0, "volume should be non-negative"); + this.volume = volume; + } + + /** + * Sets the pitch of the sound to be played. Must be within the range [0,2]. + * @param pitch sound pitch + */ + public void setPitch(float pitch) { + Validate.isTrue(pitch >= 0 && pitch <= 2, "pitch should be within range [0,2]"); + this.pitch = pitch; + } + + /** + * Gets the world in which the sound is being played + * @return sound's world + */ + @NotNull + public abstract World getWorld(); +}