Resource Playing Records via PacketPlayOutWorldEvent

Discussion in 'Spigot Plugin Development' started by Kakalavala, Feb 9, 2019.

  1. Not 100% sure if this is the best means of playing records to players (regardless of where they are in the world), but it was a bit of a pain to put it together as the IDs for records and sounds changed overtime (and https://wiki.vg/Protocol says to use the record ID, which isn't an Integer anymore).

    Firstly, how I send the packet:
    Code (Java):
    final Location loc = player.getLocation();
    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutWorldEvent(1010, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), id, false));
    It should be noted that 1010 is directly from https://wiki.vg/Protocol#Effect (Play record), and id is the ID of the record being played.

    The IDs

    I wrote the IDs into an enum:
    (It should be noted that there's more sounds playable via their IDs here, not just records.)
    Code (Java):
    DISC_13(774),
    CAT(775),
    BLOCKS(776),
    CHIRP(777),
    FAR(778),
    MALL(779),
    MELLOHI(780),
    STAL(781),
    STRAD(782),
    WARD(783),
    DISC_11(784),
    WAIT(785),
    STOP(0);
    With the Integers being their ID.

    It is stated on https://wiki.vg/Protocol#Effect that any ID other than a music ID will stop the current sound.

    I wrote this because I couldn't find any (updated) info about what to do seeing how records no longer have integer IDs.
    If you find this useful, neato. If there's better ways of doing this, and I overlooked something horrifically, please do say.
    (Side note: idk where to actually post this nor the tag to use, I took a guess tbh and put it here with the Resource tag.)
     
    #1 Kakalavala, Feb 9, 2019
    Last edited: Feb 12, 2019 at 4:30 PM
    • Informative Informative x 1
  2. I've been struggling with the Jukebox in general - playing it via its own methods is very hit and miss, not to mention the messing about with the disc being ejected and the fact that sending a null or air block to end the song just doesn't work (as the docs say it should), so I'm going to try this approach myself, and I really appreciate you posting this.

    Quick follow up question though - any idea if you start at a position within the disc or can you just send the record at the start

    So for example, my jukebox runs on a distance - if you're x blocks away, I want it to stop, if you come back within range, it should be playing again. I could just have it start the song over, but for multiple people that'll pop it out of sync, but more importantly, once the next song starts, could cut it off for certain players. So if we're playing chirps and are 45 seconds in, player leaves, comes back at 72 seconds in.. do you know a way I can play chirps starting at 72 seconds?

    Alternately, I may just use the STOP to end songs as that is where I have the most trouble. Please feel free to PM if I'm being annoyingly vague.

    Thanks!
     
    • Like Like x 1
  3. I don't believe there's any way to pick a position to start playing a record, and I know you can't find out what position a record is in either. You could use https://minecraft.gamepedia.com/Music_Disc#Discs to get the times of all discs, and just measure the time elapsed yourself. Thanks for actually replying to this too, didn't think anyone noticed it.

    EDIT: I did reread https://wiki.vg/Protocol and found this:
    But I myself can't figure it out.
     
    • Like Like x 1
  4. Thanks, I have the times already, so know when to click to the next song.. just kind of a pain that if someone leaves and then comes back, I have to start it over and then when it's time, abruptly end the song to click to the next one.

    I was reading that same bit about starting at a specific location and just assumed it meant location in the world.

    Regardless, I appreciate you posting your approach, even with the issue I'm having, it still blows my original code out the water.
     
    • Like Like x 1
  5. My plugin just is pretty simple and allows the player to toggle music on/off, and I wanted to do the same thing (have it start where it left off); however, I ended up just sending them a message saying "Music will resume after the current song is finished" and not restarting the record.
     
  6. Not a bad approach - mine is a looping Jukebox, so better to just start over and then end early I think. Shouldn't happen TOO often I'd hope, but I'll need to make it fairly clear in the instructions. There is the chance someone will skip the song mid-way through anyway, so really isn't any different to that.
     
  7. It'd honestly be nice if it were possible to select a timestamp in the record, as well as a means of detecting if a record was finished, but oh well.
     
  8. I’m curious but can’t the above NMS code be avoided by using the World.playSound(...) and Player.playSound(...) methods?
     
  9. I've run into issues stopping the sounds from playing in that manner - If you know a way I can stop the record playing instantly, then I'd love to avoid NMS
     
  10. Ah my bad, didn’t read the full post & responses carefully.
     
  11. Quick update - this non-NMS approach does work, but does have a couple of drawbacks.

    1) You don't get the alert saying what disc is now playing if you just use playSound
    2) The volume fades out the further you get from the location where you started the sound.. this actually could be a plus for some people, not so much for what I'm looking for

    The positives here of course are the lack of NMS, better volume control (if you're into that) and control over pitch.

    What would be the real gold standard for me would be if I could continue the song after log-off/log-in if it is still currently playing. So if a 2 minute song is on and someone disconnect/reconnects before the 2 minutes are up, then have it continue, not restart (or worse, dead silence)

    I tried both player.playSound and world.playSound, both stopped if i logged off and back in.
     
    • Informative Informative x 1

Share This Page