Replicating /playsound with Bukkit

Discussion in 'Plugin Development' started by Cryptite, Mar 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Cryptite

    I have custom (added, not replaced) sounds in a resource pack I've made and I'm able to play the sounds within with /playsound. However, i'm unable nor know how to access these sounds within bukkit's api. No combo of the (deprecated) string based playSound class seems to work. Is this possible or has anyone else been able to do it? Thanks in advance!

    -Crypt
     
  2. Offline

    Noxyro

    Custom sound is NOT possible with Bukkit API. Period.
    (Only with resource packs and/or replacement of existing sounds)
     
  3. Offline

    Bammerbom

    Noxyro Cryptite
    He means: He has a resource pack with custom sounds inside. How to play that sounds.
    I have no idea, anyone else?
     
    Locercus likes this.
  4. Offline

    d3v1n302418

    Jhtzb Look at @Noxyro's post. It clearly says.
    . Its not possible, you'd need NMS.
     
  5. Offline

    Cryptite

    Is this an API problem that may one day be resolved, or is not in the cards that this may one day exist in the API?
     
  6. Offline

    Darq

    Not in the Bukkit API, but without a doubt in the "official API" whenever that comes out.
     
  7. Offline

    Cryptite

    I'd like to return to this thread to report that this IS possible.

    Short and sweet, if you have a properly made resource pack with the json configured correctly, playSound() will work on added sounds that are not replacements of original ones. Ensure your player is not possibly null and I believe this'll work just fine!

    A Proper sounds.json file like below:

    Code:
    {
      "SoundFileName": {
        "category": "player",
        "sounds": [
          "SOUNDFILE"
        ]
      }
    }
    with the following:
    Code:java
    1. player.playSound(player.getLocation(), "SoundFileName", 1, 1);
    will work.
     
  8. Offline

    willeb96

    That is very nice, I wouldn't dream that something like that would actually work.
    Bookmarking this for future reference :)
     
  9. Offline

    Garris0n

    The way you made that text small was rather misleading.
     
    Locercus likes this.
  10. Offline

    Cryptite

    Because the String version of playSound only exists with Player and doesn't exist in the world format, I've gone ahead and written a little custom static method that will replicate the world based version and play the custom sound for nearby entities as well. Fortunately, because of the location requirement of playSound, volume/direction/etc are all handled and the implementation is very simple. Please feel free to use:

    Code:java
    1. public static void playCustomSound(Player p, String sound) {
    2. //Get all nearby entities within 15 blocks
    3. for (Entity near : p.getNearbyEntities(15, 15, 15)) {
    4. if (!(near instanceof Player)) continue;
    5.  
    6. ((Player) near).playSound(p.getLocation(), sound, 1, 1);
    7. }
    8.  
    9. //Also play for the player
    10. p.playSound(p.getLocation(), sound, 1, 1);
    11. }


    Hope this helps!
     
  11. Offline

    Garris0n

    Cryptite
    1. What if I want to play a sound that's not next to a player.
    2. What if I want to play a sound with a volume loud enough to be heard farther than your getNearbyEntities() allows for?
     
  12. Offline

    Cryptite

    Garris0n
    I suppose two different options, one, a world-based sound method and another the player one.

    Here's a world-based one:

    Code:java
    1. public static void playWorldCustomSound(Location l, String sound, int radius) {
    2. for (Entity e : l.getWorld().getEntities()) {
    3. if (!(e instanceof Player)) continue;
    4. Player player = (Player) e;
    5.  
    6. double distance = player.getLocation().distance(l);
    7. if (distance <= radius) {
    8. player.playSound(l, sound, 1, 1);
    9. }
    10. }
    11. }


    As for volume, I'm less inclined to worry about volume and would probably rather just use a distance based approach. Since these are custom sounds, you have control over volume. If you did want a radius based playSound from a player location, here's one:

    Code:java
    1. public static void playCustomSound(Player p, String sound, int radius) {
    2. //Get all nearby entities within 15 blocks
    3. for (Entity near : p.getNearbyEntities(radius, radius, radius)) {
    4. if (!(near instanceof Player)) continue;
    5.  
    6. ((Player) near).playSound(p.getLocation(), sound, 1, 1);
    7. }
    8.  
    9. //Also play for the player
    10. p.playSound(p.getLocation(), sound, 1, 1);
    11. }
     
Thread Status:
Not open for further replies.

Share This Page