player.playNote() not working even with noteblock

Discussion in 'Plugin Development' started by blablubbabc, Dec 12, 2012.

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



    I got stuck at the following, using craftbukkit 1.4.5 R0.2 :
    What I want achieve is:
    Let each player hear a different melody made ou of simple notes.
    I thought of using the player.sendNote(location, instrument, note).
    I know that I need a noteblock at the location for that. So, to play the melody (different notes over time) I thought of sending the player a fake noteblock some blocks above him, and send again the actual block afterwards. But: It didn't work :(

    Here is a snippet of the code of one of my first tries, where I send the fake noteblock and play a note after a short delay there:

    import org.bukkit.Instrument;
        import org.bukkit.Location;
        import org.bukkit.Material;
        import org.bukkit.Note;
        import org.bukkit.block.Block;
        import org.bukkit.entity.Player;
        import org.bukkit.plugin.Plugin;
        public class Ton {
            public final Instrument instrument;
            public final Note note;
            public final long delay;
            public Ton(Instrument instrument, Note note, long delay) {
                this.instrument = instrument;
                this.note = note;
                this.delay = delay+1;
            public void play(final Plugin plugin, final Player p) {
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        // in the final version i will check if the player is still online and stuff..
                        final Location loc = p.getLocation().add(0, 5, 0);
                        final Block block = loc.getBlock();
                        p.sendBlockChange(loc, Material.NOTE_BLOCK, (byte) 0);
                        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                                p.playNote(loc, instrument, note);
                                // I also tried without the following line.. So i do not remove the noteblock at the time the note is/should be played..
                                p.sendBlockChange(loc, block.getType(), block.getData());
                        }, 1);
                }, delay-1);
    I even tried it with more delays, to be sure the noteblock IS there at the time of the note being played (noteblock is displayed on my client, so block-change-sending works) .
    I also tried with player.sendSound(Sound.NOTE_PIANO, ..) or so, but couldn't find a good way to translate the note-ids in correct sound pitch .

    Also I tried it with "real" setting of the noteblock on the server, and then playing the note by casting the block-state to noteblock and then playing the note from the noteblock object directly, which worked.
    But I would prefer it with fake blocks and I definetly need different melodies for each player..

    Maybe I missed something?

  2. Offline


    I can't help but notice that your class doesn't extend JavaPlugin. Is this your main class?

    Another thing that comes to mind is that notes are sent from server-side rather than client-side, which means it doesn't matter what block the player thinks is there, you would need to actually change the block. I'm sure there's a simpler way than doing that, though.
  3. Offline


    You could try using playSound instead, does not need any nearby noteblock.
  4. Offline


    Nope, it's not my main class. My object is generated fine, i just need to get my play()-method working..
    I was sure it was client sided, because I found lots of threads there people told, that they send fake packets to players because of that..
    But I actual have no idea.
    I wanted to do it the same way, but got stuck here..

    I know, but I wasn't able to "convert" my notes to the right pitch or something. If this is even possible..
    Do you know how I play the different notes with playSound() ?
  5. Offline


    Change the Sound.<other sound> and/or the pitch
  6. Offline


    @fireblast: My question was, how to change the pitch to get the right note. Lets say i have the note "D". What would I have to set the pitch to, to get exactly this note ?
  7. Offline


  8. Offline


    I made some experiments with that:
    I only get different tones from pitch value from 0.5 - 2
    0.5 and below seems to be F#3 and 2 and more to be F#5, which is good, but for example the tone f4 (id 11) is somewhere between 0.952 and 0.953 I guess ..

    So to play a melodie, this would be a lot of work to find the right values for the pitch.. and even with a good value it wouldn't be perfect..
    Is there some way to directly "calculate" from tone id to pitch ?

    But at least the noteblock issue would be gone. Right now I think I will use this with experimental pitches, if I find no better solution :/

    Thank you so far for your ideas :)

    Okay, here is my final result of my experiments:
    Note: | _0_| _1_ | _2_ | _3_ | _4_ | _5_ | _6_ | _7_ | .8. | _.9._| .10. | _11_| 12 |
    Pitch: | 0.5 | 0.53 | 0.56 | 0.6 | 0.63 | 0.67 | 0.7 | 0.76 | 0.8 | 0.84 | 0.9 | 0.94 | 1 |

    Note: | _13_ | .14. | .15. | _16_| _17_| .18. | .19. | 20 | .21. |. 22. | _23_| 24 |
    Pitch: | 1,06 | 1,12 | 1,18 | 1,26 | 1,34 | 1,42 | 1,5 | 1,6 | 1,68 | 1,78 | 1,88 | 2 |

    Edit: no idea how to make a good-looking table..

    If there is no quick and easy way to get directly from id to pitch, I will use these values..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 30, 2016
  9. Offline


    Thank you for listing up all these! [diamond] for you sir :D
  10. Offline


    whitehooder: yeay :)

    I am using this table now. Unfortunly the recieving of the melodies at the client is sometimes a bit laggy. So sometimes the delays between the notes differs a bit. But I guess I can't change anything there..
    Or could this be a problem with scheduling sync delayed tasks being somehow a bit unprecise ?

    Edit: I tried my melodie with redstone+noteblocks, and even there sometimes the melodie was a bit laggy (only slightly). So, i guess thats the way it is..
  11. Offline


    there is always a few milliseconds/nanoseconds between them probably, but not something you really should notice. Why not scheduling one repeating task?
  12. Offline


    It's more "laggy" if the server sends new chunks at the same time somehow.. I guess it's because of the task being sync..
    Sometimes I can't identify my melodie because of that :( If I play my melodie several times in row, it gets better with each time, somehow.

    I schedule a delayed sync task for each note, because each note as a different delay, which creates the melody..

    Hm, maybe I will try it again with 1 repeating task with a delay of 2 ticks (which is the minimum delay between notes), which counts the "repeats" and plays the right note, if it reaches the right delay for the note..
    Maybe that's then "more synchronized" , compared to the different tasks, if this is possible..

    Edit: one more observation: if i test it with a friend, he recieves the same "laggy" melody like me. So it's definetly a server-side issue, taht the notes are sent differently from time to time..
  13. blablubbabc Your lag issues might me... lag issues. Did you check your TPS (ticks per second, should always be 20, all other is server lag) rate? There are some plugins out for doing this.

    //EDIT: If your TPS rate is lower then 20 you need either less players, less plugins or better hardware.
  14. Offline


    V10lator hardware shouldn't be the problem. I can't recognize any lag besides these small differencies between the delays. tps is always at 20. players during my testing: 2 or alone, but the server can handle lot more players

    If something is lagging, then it's my client :/
    But as my friend recieved the same "lag-modified" melody version (at least I think so, he was able to "sing" the melody the same way I heard it), I guess it might be something else, server sided..
  15. Network lag maybe? If you have the hardware for it test on localhost (and make sure that TPS rate is alwas 20 as well as you don't have client lag).
  16. Offline


    V10lator hm.. works fine on my local machine.. It seems to work on my server better now, after I put some more delay (10 ticks more) between a teleport, which goes right 2 ticks before the melody starts, and the melody.
    Sounds great so far :)
  17. blablubbabc So it was network lag. Right after the teleport the server sends a lot of chunk packages to the client, making network lag even more worse. ;)
Thread Status:
Not open for further replies.

Share This Page