Solved ActionBar

Discussion in 'Spigot Plugin Development' started by Maxiceks, Sep 12, 2019.

  1. Hello, I want to make a ActionBar in my plugin but it´s not working.
    Can you please help me?
    And dont tell me to teach Java...

    Here is the code:
    BarEvent.java
    Code (Java):

    private final Main pl;

        public BarEvent(Main pl) {
            this.pl = pl;
        }

        public void join(PlayerJoinEvent e) {
            if (pl.getConfig().getBoolean("bar.enable")) {
                Player p = e.getPlayer();
                String label = ChatColor.translateAlternateColorCodes('&', pl.getConfig().getString("bar.text"));
               
               
               
                BarPrepair.sendAction(p, label, pl, true);
               
               
            }
        }



    BarPrepair.java
    Code (Java):
    public static Class<?> getNmsClass(String nmsClassName) throws ClassNotFoundException {
            return Class.forName("net.minecraft.server."
                    + Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + "."
                    + nmsClassName);
        }

        public static String getServerVersion() {
            return Bukkit.getServer().getClass().getPackage().getName().substring(23);
        }

        public static void sendAction(Player p, String msg, Main plugin, boolean colors) {

            try {
                if (getServerVersion().equalsIgnoreCase("v1_8_R2") || getServerVersion().equalsIgnoreCase("v1_8_R3")) {

                    Object icbc = getNmsClass("IChatBaseComponent$ChatSerializer").getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{'text': '" + msg + "'}" });
                    Object ppoc = getNmsClass("PacketPlayOutChat").getConstructor(new Class[] { getNmsClass("IChatBaseComponent"), byte.class }).newInstance(new Object[] { icbc, Byte.valueOf((byte) 2) });
                    Object nmsp = p.getClass().getMethod("getHandle", new Class[0]).invoke(p, new Object[0]);
                    Object pcon = nmsp.getClass().getField("playerConnection").get(nmsp);
                    pcon.getClass().getMethod("sendPacket", new Class[] { getNmsClass("Packet") }).invoke(pcon,new Object[] { ppoc });

                } else {

                    Object icbc = getNmsClass("ChatSerializer").getMethod("a", new Class[] { String.class }).invoke(null,new Object[] { "{'text': '" + msg + "'}" });
                    Object ppoc = getNmsClass("PacketPlayOutChat").getConstructor(new Class[] { getNmsClass("IChatBaseComponent"), byte.class }).newInstance(new Object[] { icbc, Byte.valueOf((byte) 2) });
                    Object nmsp = p.getClass().getMethod("getHandle", new Class[0]).invoke(p, new Object[0]);
                    Object pcon = nmsp.getClass().getField("playerConnection").get(nmsp);
                    pcon.getClass().getMethod("sendPacket", new Class[] { getNmsClass("Packet") }).invoke(pcon,new Object[] { ppoc });

                }

            } catch (IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
     
  2. Which spigot version do you use?
    Try
    Code (Java):

    Player#spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(msg));
     
     
  3. Ok, I will try it please wait
     
  4. It say when i use Player#spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(msg));
    Create a variable spigot
    and when i use p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(label));
    The method sendMessage(BaseComponent) in the Player.Spigot is not acciable for argguments (ChatMessageType, TextComponent)
     
  5. This feature is only supported from version 1.9+
     
  6. And im using version 1.8.8
     
  7. If you want your event listener's method to be called when a player joins, you'll need to add @EventHandler before the method.
     
  8. Oh my god, im idiot i will test it
     
  9. Oh my god its working thank you very much i dont know what to say
    I will tag this thread at solved
    Thank you everymuch