Class ServerApi

java.lang.Object
de.mrjulsen.dragnsounds.api.ServerApi

public final class ServerApi extends Object
Contains useful methods to play and manipulate custom sounds on the server side. For client-side sound management see ClientApi.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static de.mrjulsen.mcdragonlib.data.StatusResult
    Deletes the sound at the given location.
    static de.mrjulsen.mcdragonlib.data.StatusResult
    Deletes the sound at the given location.
    getAllSoundFiles(net.minecraft.world.level.Level level, Collection<IFilter<SoundFile>> filters)
    Get a list of all sound files.
    static Map<String,String>
    Reads all metadata from the sound file.
    static void
    getPlaybackData(long soundId, net.minecraft.server.level.ServerPlayer[] players, SoundGetDataCallback.ISoundPlaybackData callback)
    Gets the sound playback data for each player for the given sound id.
    Get a SoundFile from the server at the given location.
    Get a list of all sound files at the given location.
    static void
    isSoundPlaying(long soundId, net.minecraft.server.level.ServerPlayer player, Consumer<Boolean> callback)
    Checks if the sound is playing for the player.
    static long
    playSound(SoundFile file, PlaybackConfig playback, net.minecraft.server.level.ServerPlayer[] player, SoundPlayingCallback.ISoundCreatedCallback soundCallback)
    Play a custom sound to all selected players.
    static void
    seek(long soundId, int ticks, net.minecraft.server.level.ServerPlayer[] players)
    Starts playing the sound at the given number of ticks.
    static void
    seekAllInstances(SoundFile file, int ticks, net.minecraft.server.level.ServerPlayer[] players)
    Starts playing the sound at the given number of ticks.
    static net.minecraft.server.level.ServerPlayer[]
    selectPlayers(net.minecraft.world.entity.Entity vehicle)
    A simplified method to get all player passengers for a specific entity.
    static net.minecraft.server.level.ServerPlayer[]
    selectPlayers(net.minecraft.world.level.Level level, IPlaybackArea playbackArea)
    A simplified method to get a list of players for specific area definition settings.
    static void
    setCone(long soundId, net.minecraft.world.phys.Vec3 direction, float angleA, float angleB, float outerGain, net.minecraft.server.level.ServerPlayer[] players)
    Define the cone in which the sound can be heard.
    static void
    setConeAllInstances(SoundFile file, net.minecraft.world.phys.Vec3 direction, float angleA, float angleB, float outerGain, net.minecraft.server.level.ServerPlayer[] players)
    Define the cone in which the sound can be heard.
    static void
    setDoppler(long soundId, float dopplerValue, net.minecraft.world.phys.Vec3 velocity, net.minecraft.server.level.ServerPlayer[] players)
    Enable the doppler effect for the given sound.
    static void
    setDopplerAllInstances(SoundFile file, float dopplerValue, net.minecraft.world.phys.Vec3 velocity, net.minecraft.server.level.ServerPlayer[] players)
    Enable the doppler effect for the given sound.
    static void
    setPosition(long soundId, net.minecraft.world.phys.Vec3 pos, net.minecraft.server.level.ServerPlayer[] players)
    Changes to position of the sound source.
    static void
    setPositionAllInstances(SoundFile file, net.minecraft.world.phys.Vec3 pos, net.minecraft.server.level.ServerPlayer[] players)
    Changes to position of the sound source.
    static void
    setSoundPaused(long soundId, boolean pause, net.minecraft.server.level.ServerPlayer[] players)
    Changes the state of the sound to paused or playing.
    static void
    setSoundPausedAllInstances(SoundFile file, boolean pause, net.minecraft.server.level.ServerPlayer[] players)
    Changes the state of the sound to paused or playing.
    static void
    setVolumeAndPitch(long soundId, float volume, float pitch, int attenuationDistance, net.minecraft.server.level.ServerPlayer[] players)
    Changes the volume, pitch and attenuationDistance of the sound.
    static void
    setVolumeAndPitchAllInstances(SoundFile file, float volume, float pitch, int attenuationDistance, net.minecraft.server.level.ServerPlayer[] players)
    Changes the volume, pitch and attenuationDistance of the sound.
    static void
    stopAllCustomSounds(net.minecraft.server.level.ServerPlayer[] players)
    Stops the currently playing sound.
    static void
    stopAllSoundInstances(SoundFile file, net.minecraft.server.level.ServerPlayer[] players)
    Stops all playing instances of the given sound file.
    static void
    stopSound(long soundId, net.minecraft.server.level.ServerPlayer[] players)
    Stops the selected custom sound for all players.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ServerApi

      public ServerApi()
  • Method Details

    • playSound

      public static long playSound(SoundFile file, PlaybackConfig playback, net.minecraft.server.level.ServerPlayer[] player, SoundPlayingCallback.ISoundCreatedCallback soundCallback)
      Play a custom sound to all selected players.
      Parameters:
      file - The sound file to play.
      playback - The playback settings.
      player - The players that should hear that sound.
      soundCallback - This callback will be called multiple times and once for EVERY single player in the player-Array. The callback is called when the client received the first data packet, when the client started playing the sound and when the playback of the client has been stopped. Since this may be different between all players you have to check for the player too. Also don't rely on a STOP notification, because the server may not receive one in some edge cases. Use player.isAlive() and other methods to check if the player is still online.
      Returns:
      The id of the sound.
      Impl Note:
      Please note that the sound cannot be manipulated directly after executing this method as the sound data must first be send to the client(s). Use the callback to make sure that the client(s) have started playing before using the sound.
    • setDoppler

      public static void setDoppler(long soundId, float dopplerValue, net.minecraft.world.phys.Vec3 velocity, net.minecraft.server.level.ServerPlayer[] players)
      Enable the doppler effect for the given sound.
      Parameters:
      soundId - The id of the cusotm sound.
      dopplerValue - The doppler factor.
      velocity - The velocity of the sound source.
      players - The affected players.
    • setCone

      public static void setCone(long soundId, net.minecraft.world.phys.Vec3 direction, float angleA, float angleB, float outerGain, net.minecraft.server.level.ServerPlayer[] players)
      Define the cone in which the sound can be heard. Nothing happens if the given sound does not exist.
      Parameters:
      soundId - The id of the cusotm sound.
      direction - The doppler factor.
      angleA - The first angle of the cone.
      angleB - The second angle of the cone.
      outerGain - The gain at the outside of the cone.
    • setVolumeAndPitch

      public static void setVolumeAndPitch(long soundId, float volume, float pitch, int attenuationDistance, net.minecraft.server.level.ServerPlayer[] players)
      Changes the volume, pitch and attenuationDistance of the sound.
      Parameters:
      soundId - The if of the custom sound.
      volume - The new volume value. Use -1 to keep the current value.
      pitch - The new pitch value. Use -1 to keep the current value.
      attenuationDistance - The attenuationDistance. Use -1 to keep the current value.
      players - The affected players.
    • setPosition

      public static void setPosition(long soundId, net.minecraft.world.phys.Vec3 pos, net.minecraft.server.level.ServerPlayer[] players)
      Changes to position of the sound source.
      Parameters:
      soundId - The id of the custom sound.
      pos - The new position of the sound source in the world.
      players - The affected players.
    • seek

      public static void seek(long soundId, int ticks, net.minecraft.server.level.ServerPlayer[] players)
      Starts playing the sound at the given number of ticks.
      Parameters:
      soundId - The id of the custom sound.
      ticks - The amount of time in ticks to skip.
      players - The affected players.
    • setSoundPaused

      public static void setSoundPaused(long soundId, boolean pause, net.minecraft.server.level.ServerPlayer[] players)
      Changes the state of the sound to paused or playing.
      Parameters:
      soundId - The if of the custom sound.
      pause - Paused or playing.
      players - The affected players.
    • setDopplerAllInstances

      public static void setDopplerAllInstances(SoundFile file, float dopplerValue, net.minecraft.world.phys.Vec3 velocity, net.minecraft.server.level.ServerPlayer[] players)
      Enable the doppler effect for the given sound.
      Parameters:
      file - The custom sound file.
      dopplerValue - The doppler factor.
      velocity - The velocity of the sound source.
      players - The affected players.
    • setConeAllInstances

      public static void setConeAllInstances(SoundFile file, net.minecraft.world.phys.Vec3 direction, float angleA, float angleB, float outerGain, net.minecraft.server.level.ServerPlayer[] players)
      Define the cone in which the sound can be heard. Nothing happens if the given sound does not exist.
      Parameters:
      file - The custom sound file.
      direction - The doppler factor.
      angleA - The first angle of the cone.
      angleB - The second angle of the cone.
      outerGain - The gain at the outside of the cone.
    • setVolumeAndPitchAllInstances

      public static void setVolumeAndPitchAllInstances(SoundFile file, float volume, float pitch, int attenuationDistance, net.minecraft.server.level.ServerPlayer[] players)
      Changes the volume, pitch and attenuationDistance of the sound.
      Parameters:
      file - The custom sound file.
      volume - The new volume value. Use -1 to keep the current value.
      pitch - The new pitch value. Use -1 to keep the current value.
      attenuationDistance - The attenuationDistance. Use -1 to keep the current value.
      players - The affected players.
    • setPositionAllInstances

      public static void setPositionAllInstances(SoundFile file, net.minecraft.world.phys.Vec3 pos, net.minecraft.server.level.ServerPlayer[] players)
      Changes to position of the sound source.
      Parameters:
      file - The custom sound file.
      pos - The new position of the sound source in the world.
      players - The affected players.
    • seekAllInstances

      public static void seekAllInstances(SoundFile file, int ticks, net.minecraft.server.level.ServerPlayer[] players)
      Starts playing the sound at the given number of ticks.
      Parameters:
      file - The custom sound file.
      ticks - The amount of time in ticks to skip.
      players - The affected players.
    • getPlaybackData

      public static void getPlaybackData(long soundId, net.minecraft.server.level.ServerPlayer[] players, SoundGetDataCallback.ISoundPlaybackData callback)
      Gets the sound playback data for each player for the given sound id.
      Parameters:
      soundId - The sound id to get the data from.
      players - The affected players.
      callback - Called for each player once and contains all the playback data.
    • setSoundPausedAllInstances

      public static void setSoundPausedAllInstances(SoundFile file, boolean pause, net.minecraft.server.level.ServerPlayer[] players)
      Changes the state of the sound to paused or playing.
      Parameters:
      file - The custom sound file.
      pause - Paused or playing.
      players - The affected players.
    • stopSound

      public static void stopSound(long soundId, net.minecraft.server.level.ServerPlayer[] players)
      Stops the selected custom sound for all players.
      Parameters:
      soundId - The id of the custom sound.
      players - The players for whom the sound should be stopped.
    • stopAllSoundInstances

      public static void stopAllSoundInstances(SoundFile file, net.minecraft.server.level.ServerPlayer[] players)
      Stops all playing instances of the given sound file.
      Parameters:
      file - The file of the sound.
      players - The players for whom the sound should be stopped.
    • stopAllCustomSounds

      public static void stopAllCustomSounds(net.minecraft.server.level.ServerPlayer[] players)
      Stops the currently playing sound. This method will also send a notification to the server.
      Parameters:
      players - The players for whom the sound should be stopped.
    • isSoundPlaying

      public static void isSoundPlaying(long soundId, net.minecraft.server.level.ServerPlayer player, Consumer<Boolean> callback)
      Checks if the sound is playing for the player.
      Parameters:
      soundId - The id of the custom sound.
      player - The affected player.
      callback - Contains the response of the client.
    • selectPlayers

      public static net.minecraft.server.level.ServerPlayer[] selectPlayers(net.minecraft.world.level.Level level, IPlaybackArea playbackArea)
      A simplified method to get a list of players for specific area definition settings.
      Parameters:
      level - The current world level.
      playbackArea - The area definition settings.
      Returns:
      A list of players that match the given criteria.
    • selectPlayers

      public static net.minecraft.server.level.ServerPlayer[] selectPlayers(net.minecraft.world.entity.Entity vehicle)
      A simplified method to get all player passengers for a specific entity.
      Parameters:
      vehicle - The vehicle to get the players from.
      Returns:
      A list of players that are riding that vehicle.
    • getAllSoundFiles

      public static Optional<SoundFile[]> getAllSoundFiles(net.minecraft.world.level.Level level, Collection<IFilter<SoundFile>> filters)
      Get a list of all sound files.
      Parameters:
      level - The world level to get the sounds from.
      filters - Filters for the result.
      Returns:
      The sound files.
      See Also:
      Impl Note:
      All used custom filters MUST be registered in the FilterRegistry first!
    • getSoundFilesAt

      public static Optional<SoundFile[]> getSoundFilesAt(SoundLocation location, Collection<IFilter<SoundFile>> filters)
      Get a list of all sound files at the given location.
      Parameters:
      location - The location to get the sounds from. Enter null as a root directory to get all sound files.
      filters - Filters for the result.
      Returns:
      The sound files.
      See Also:
      Impl Note:
      All used custom filters MUST be registered in the FilterRegistry first!
    • getSoundFile

      public static Optional<SoundFile> getSoundFile(SoundLocation location, String id)
      Get a SoundFile from the server at the given location.
      Parameters:
      location - The location where the sound is saved at.
      id - The id of the custom sound.
      Returns:
      The sound file.
    • deleteSound

      public static de.mrjulsen.mcdragonlib.data.StatusResult deleteSound(SoundLocation location, String id)
      Deletes the sound at the given location.
      Parameters:
      location - The location where the sound is saved at.
      id - The id of the custom sound.
      Returns:
      A status notification.
    • deleteSound

      public static de.mrjulsen.mcdragonlib.data.StatusResult deleteSound(SoundFile file)
      Deletes the sound at the given location.
      Parameters:
      file - The sound file to delete.
      Returns:
      A status notification.
    • getFileMetadata

      public static Map<String,String> getFileMetadata(SoundFile file)
      Reads all metadata from the sound file.
      Parameters:
      file - The custom sound file.
      Returns:
      All metadata found in the audio file.