1.8.8 Tab Pots

Discussion in 'Spigot Plugin Development' started by UtilsByteMedia, Feb 1, 2020.

  1. Hello I am trying to make the hypixel skyblock tab, like how you get pots and it shows up in tab with the timer of how much you have left, I am trying to do that, but I can't get it too work, it does the too fast and it isn't in sync, any help please! Here is my code.

    My Utils Class:
    Code (Java):
    package net.ashton.hypixelskyblock.Utils;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;

    public class Utils {

        public static String color(final String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }

        public static String getActiveEffects(final Player player) {
            final StringBuilder builder = new StringBuilder("");
            if (player.getActivePotionEffects().isEmpty()) {
                builder.append(color("        &7No effects active. Drink potions or splash\nthem on the ground to buff yourself!"));
                return builder.toString();
            }
            for (final PotionEffect effect : player.getActivePotionEffects()) {
                final int sec = effect.getDuration() % 60;
                final int min = effect.getDuration() % 1000 / 60;
                final String name = String.valueOf(effect.getType().getName().substring(0, 1).toUpperCase()) + effect.getType().getName().substring(1);
                builder.append(color("&8" + name + " " + (effect.getAmplifier() + 1) + " &f" + min + ":" + sec + "\n"));
            }
            return builder.toString();
        }
    }
     
    Here is my TestTab Class:

    Code (Java):
    package net.ashton.hypixelskyblock;

    import net.minecraft.server.v1_8_R3.IChatBaseComponent;
    import net.minecraft.server.v1_8_R3.Packet;
    import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerListHeaderFooter;
    import net.ashton.hypixelskyblock.Utils.Utils;
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import java.lang.reflect.Field;
    import java.util.HashMap;

    public class TestTab implements Listener {
        private HypixelSkyblock main;
        private HashMap<Player, String> map;

        public TestTab(HypixelSkyblock main) {
            this.main = main;
            this.map = new HashMap<Player, String>();
            Bukkit.getPluginManager().registerEvents((Listener)this, (Plugin)main);
        }
        @EventHandler
        public void onJoin(final PlayerJoinEvent e) {
            new BukkitRunnable() {
                public void run() {
                    TestTab.this.sendTablist(e.getPlayer(), "&bYou are playing on &e&lMC.HYPIXEL.NET\n&7date &8mini44C\n", "\n&a&lACTIVE EFFECTS\n" + Utils.getActiveEffects(e.getPlayer()) + "\n \n&aRanks, Boosters, and MORE! &c&lSTORE.HYPIXEL.NET");
                }
            }.runTaskTimer((Plugin)this.main, 1L, 1L); //TODO WAS 20L
        }

        private void sendTablist(final Player p, String title, String subTitle) {
            if (title == null) {
                title = "";
            }
            if (subTitle == null) {
                subTitle = "";
            }
            final IChatBaseComponent tabTitle = IChatBaseComponent.ChatSerializer.a("{\"text\":\"" + Utils.color(title) + "\"}");
            final IChatBaseComponent tabSubTitle = IChatBaseComponent.ChatSerializer.a("{\"text\":\"" + Utils.color(subTitle) + "\"}");
            final PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(tabTitle);
            try {
                final Field field = packet.getClass().getDeclaredField("b");
                field.setAccessible(true);
                field.set(packet, tabSubTitle);
            }
            catch (Exception e) {
                e.printStackTrace();
                return;
            }
            finally {
                ((CraftPlayer)p).getHandle().playerConnection.sendPacket((Packet)packet);
            }
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket((Packet)packet);
        }
    }