Solved Update the real time in tablist?

Discussion in 'Spigot Plugin Development' started by MrIvanPlays, Jun 16, 2018.

  1. Tried with runnables, Intellij is giving me this error:
    ehmmm.png
    What to do?
     
  2. JanTuck

    Supporter

  3. Where i need to add the finals ??
     
  4. Code (Text):
    private void sendTablist(final String header, final String footer, Player p){
     
  5. Now this s*it happens:
    now this.png
     
  6. JanTuck

    Supporter

    He doesn’t java. He can’t set the values again.
     
  7. Could you pls paste the code so i can try it also in my editor?
     
  8. Okay:

    Code (Java):
        public void sendTablist(final String header, final String footer, Player p) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
            Date date = new Date();
            header = header.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()));
            footer = footer.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()));
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
                @Override
                public void run() {
                    header = header.replace("%time%", time.format(date));
                    footer = footer.replace("%time%", time.format(date));
                }
            }, 20, 20);
     
    • Like Like x 1
  9. Maybe ur whole code?
    I think this is the whole code (taked from ur other post), so try this:
    Code (Java):
        public void sendTablist(String header, String footer, Player p) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
            Date date = new Date();
            Bukkit.getScheduler().scheduleSyncRepeatingTask(main.main, new BukkitRunnable() {
                @Override
                public void run() {
                   
                    String head = header.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                            .replace("%max%", String.valueOf(Bukkit.getMaxPlayers())).replace("%time%", time.format(date));
                    String foot = footer.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                            .replace("%max%", String.valueOf(Bukkit.getMaxPlayers())).replace("%time%", time.format(date));
                   
                    PlayerConnection con = ((CraftPlayer)p).getHandle().playerConnection;
                    IChatBaseComponent tfooter = IChatBaseComponent.ChatSerializer.a(("{\"text\": \"" + foot + "\"}"));
                    IChatBaseComponent theader = IChatBaseComponent.ChatSerializer.a(("{\"text\": \"" + head + "\"}"));
                    PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(theader);
                    try{
                        try{
                            Field f = packet.getClass().getDeclaredField("b");
                            f.setAccessible(true);
                            f.set(packet, tfooter);
                        }catch(Exception e){
                            e.printStackTrace();
                            con.sendPacket(packet);
                        }
                    }finally{
                        con.sendPacket(packet);
                    }
                   
                }
            }, 0, 20);

        }
     
  10. Ehm:

    Code (Java):
        public void sendTablist(final String header, final String footer, Player p) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
            Date date = new Date();
            header = header.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()));
            footer = footer.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size())).replace("%player%", p.getName()).replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()));
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
                @Override
                public void run() {
                    header = header.replace("%time%", time.format(date));
                    footer = footer.replace("%time%", time.format(date));
                }
            }, 20, 20);
            PlayerConnection con = ((CraftPlayer)p).getHandle().playerConnection;
            IChatBaseComponent tfooter = IChatBaseComponent.ChatSerializer.a(("{\"text\": \"" + footer + "\"}"));
            IChatBaseComponent theader = IChatBaseComponent.ChatSerializer.a(("{\"text\": \"" + header + "\"}"));
            PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(theader);
            try{
                try{
                    Field f = packet.getClass().getDeclaredField("b");
                    f.setAccessible(true);
                    f.set(packet, tfooter);
                }catch(Exception e){
                    e.printStackTrace();
                    con.sendPacket(packet);
                }
            }finally{
                con.sendPacket(packet);
            }
        }
     
    • Like Like x 1
  11. Do you tried my code?
     
  12. Ill try it now
     
    • Like Like x 1
  13. Still not updating the time
     
    • Like Like x 1
  14. Another basic Java 101 question. :/

    Code (Java):
    public class HeaderFooterUpdater extends BukkitRunnable {
        private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        private static SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");

        private String header;
        private String footer;
        private Player player;

        public HeaderFooterUpdater(String header, String footer, Player player) {
            this.header = header.replace("%player%", player.getName());
            this.footer = footer.replace("%player%", player.getName());
            this.player = player;
        }

        @Override
        public void run() {
            Date date = new Date();

            String formattedHeader = header.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size()))
                    .replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()))
                    .replace("%time%", time.format(date));
            String formattedFooter = footer.replace("%online%", String.valueOf(Bukkit.getOnlinePlayers().size()))
                    .replace("%date%", sdf.format(date))
                    .replace("%max%", String.valueOf(Bukkit.getMaxPlayers()))
                    .replace("%time%", time.format(date));

            player.setPlayerListHeaderFooter(TextComponent.fromLegacyText(formattedHeader), TextComponent.fromLegacyText(formattedFooter));
        }
    }
    Code (Java):
    public class MyPlugin extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            String header = "Welcome, %player%!";
            String footer = "%online%\\%max% players online!\n%date% %time%";
            new HeaderFooterUpdater(header, footer, event.getPlayer())
                .runTaskTimer(plugin, 20, 20);
        }
    }
    That should give you the idea. It's real simple. Dont just copy and paste this either. Learn from it. It might not even compile, I wrote it on my phone..
     
  15. Thank you! I am not copying and pasting (trying to learn something)!
     
    • Like Like x 1