[Solved]Playing jukebox music on 1.8 player.playEffect vs packet ?

Discussion in 'Spigot Plugin Development' started by WinterGuardian, Mar 5, 2015.

  1. Hi everyone, i'm currently programming a mini-game plugin involving music. When a player join the game, i play him an record effect on the mini-game's spawn and when he leaves, i want to remove the music. Let me show you my code:

    Join:
    Code (Text):
    p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, Material.RECORD_12);
         
    musicTasks.put(p, Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    {
        @Override
        public void run()
        {
            p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, Material.RECORD_12);
            ActionBarUtil.clear(p);
        }
    }, 4760, 4760));
    Leave:
    Code (Text):

    try
    {
        p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, null);
    }
    catch(Exception e)
    {
        LogUtil.error("Error while stopping record.", e);
    }
    if(musicTasks.containsKey(p))
    {
        Bukkit.getScheduler().cancelTask(musicTasks.get(p));
        musicTasks.remove(p);
    }
    The main problem with this is the music isn't stopping. I tried many different usages of p.playEffect found on google:

    p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, null); //internal error
    p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, Material.AIR); //internal error
    p.playEffect(IceRun.getSettings().getSpawn().getLocation(), Effect.RECORD_PLAY, 0); //deprecated

    Internal Error got with null and Material.AIR:
    java.lang.IllegalArgumentException: Wrong kind of data for this effect!
    at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer.playEffect(CraftPlayer.java:334) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
    at ice.IceRun.leave(IceRun.java:129) [IceRun.jar:?]
    at ice.command.LeaveSubCommand.onSubCommand(LeaveSubCommand.java:58) [IceRun.jar:?]
    at core.command.CommandSplitter.onCommand(CommandSplitter.java:50) [Core.jar:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]

    I can use what i want to stop the music, it doesn't work. I also tried to start the music with the ID and stop it with ID without any success. I decompiled a bukkit plugin named "JukeboxRegion" and the plugin uses the ID metho. So, i'm now pretty tired of player.playEffect (i think it's just a spigot bug or something like that) and i would like to use packets. Unfortunatly, every posts i found on google were saying packets are useless and people should use play.playEffect.

    So, do you know the properly method to stop player.playEffect method
    or do you know how to play and stop record music with packets.

    Thanks for answering and sorry for my language, i'm not english.
     
  2. Try something like this to play the record:
    Code (Text):
    public void playRecord(Player player, Material record) {
            player.playEffect(player.getLocation(), Effect.RECORD_PLAY, record);
        }
     
  3. @WinterGuardian the last (with data being set to anything but a valid record) should work fine according to the client. If your only issue is that it's deprecated, well, there are plenty of methods deprecated without a valid substitute :p.
     
    • Informative Informative x 1
  4. @DarkSeraphim The problems isn't that it's deprecated, it's that it isn't working. The first two method are making an internal error but the third silently fail... But if you said i can send anything, i'll try to send something else than 0.

    EDIT: I tested it with the last spigot on 1.8.3 with ID 0 and 1, both not working.

    EDIT2: Oh i found something, it reset the song to his beginning when i try to stop it. (With any ID) And the teleportation after the play effect doesn't influence it.
     
    #4 WinterGuardian, Mar 5, 2015
    Last edited: Mar 5, 2015
  5. Anybody knows what packet is used to send jukebox music ? Please anyone, that would help me a lot.
     
  6. I saw something on bukkit like this:

    Code (Text):
    Player player = (Player) sender;
                PacketPlayOutNamedSoundEffect p = new PacketPlayOutNamedSoundEffect(args[1], 0, 0, 0, Float.MAX_VALUE, 1);
                ((CraftPlayer)player).getHandle().playerConnection.sendPacket(p);
     
  7. That's about sounds, not jukebox records. We can't control jukebox volume and pitch so i don't think they made the same packet for sound and records. Thank you for your efforts but that's not what I was looking for.
     
  8. O wait, I think I remember. This is NOT a packet though, but it works.

    If you place a jukebox somewhere, you can cast it to Jukebox jb = (Jukebox) somelocation.getBlock().getState();

    And then you can use something like jb.play(Material.RECORD_HERE);

    I know you are looking for a packet, but if you cannot end up finding one, this is probably the closest you can get to one.
     
    • Optimistic Optimistic x 2
    • Useful Useful x 1
  9. There's Effect.RECORD_PLAY in combination with Player/World#playEffect(Location, Effect, T data[, int radius]). The packet equal would be PacketPlayOutWorldEvent.
     
    • Useful Useful x 1
  10. @WinterGuardian Try it without the try-catch clause in the Leave method.
     
  11. @Cracksn0w This is what i did at first but it have no effect except to skip all off the code after the exception and completly bug the plugin.

    @DarkSeraphim Thanks ! :)
     
  12. It's working !
    Code (Text):
    public class RecordUtil
    {
        private RecordUtil(){ }
       
        @SuppressWarnings("deprecation")
        public static void playRecord(Player p, Location loc, Material record)
        {
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(new PacketPlayOutWorldEvent(1005, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), record.getId(), false));
        }
       
        public static void stopRecord(Player p, Location loc)
        {
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(new PacketPlayOutWorldEvent(1005, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), 0, false));
        }
    }
     
    It wasn't working at first but i changed loc.getX() for loc.getBlockX() for every coordinates. Thanks for your help ! :D
     
    • Useful Useful x 3