Solved Creating an ActionBar in 1.12

Discussion in 'Spigot Plugin Development' started by knoapp, Jun 10, 2017.

  1. Hey all!

    In Minecraft version 1.11.2 and below, this bit of code worked for me to send an ActionBar to a player.

    Code (Text):
        public static void sendActionbar(Player player, String msg) {
            try {
                Constructor<?> constructor = getNMSClass("PacketPlayOutChat").getConstructor(getNMSClass("IChatBaseComponent"), byte.class);
                 
                Object icbc = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + msg + "\"}");
                Object packet = constructor.newInstance(icbc, (byte) 2);
                Object entityPlayer= player.getClass().getMethod("getHandle").invoke(player);
                Object playerConnection = entityPlayer.getClass().getField("playerConnection").get(entityPlayer);

                playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
            } catch (Exception e) {
                  e.printStackTrace();
            }
        }

        public static Class<?> getNMSClass(String name) {
            try {
                return Class.forName("net.minecraft.server." + getVersion() + "." + name);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
           
        public static String getVersion() {
            return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
        }
    But it doesn't work after the 1.12 update. I'm not looking to use an API for ActionBars, so does anyone have a simple code solution for my problem? Thanks.
     
  2. electronicboy

    IRC Staff

    Mojang replaced the byte with an actual enum, nms.ChatMessageType.GAME_INFO
     
    • Funny Funny x 1
  3. Yes. I am aware that the problem is on this line:
    Code (Text):
    Object packet = constructor.newInstance(icbc, (byte) 2);
    And part of the solution is:
    Code (Text):
    Object packet = constructor.newInstance(icbc, getNMSClass("ChatMessageType"));
    I just need that last line completed out.
     
  4. electronicboy

    IRC Staff

    • Agree Agree x 1
  5. That works! Thanks.
     
  6. Mas

    Mas

    What's wrong with the easy way?
    Code (Java):
    Player#spigot().sendMessage(ChatMessageType.ACTION_BAR, "Hello player");
     
    • Useful Useful x 1
  7. he tries to update in all the versions the action bar.
     
  8. To any code wanderers completely confused about the final code result, try this:

    Code (Text):
        public static void sendActionbar(Player player, String msg) {
            try {
                Constructor<?> constructor = getNMSClass("PacketPlayOutChat").getConstructor(getNMSClass("IChatBaseComponent"), getNMSClass("ChatMessageType"));
                   
                Object icbc = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + msg + "\"}");
                Object packet = constructor.newInstance(icbc, getNMSClass("ChatMessageType").getEnumConstants()[2]);
                Object entityPlayer= player.getClass().getMethod("getHandle").invoke(player);
                Object playerConnection = entityPlayer.getClass().getField("playerConnection").get(entityPlayer);

                playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
            } catch (Exception e) {
                  e.printStackTrace();
            }
        }

        public static Class<?> getNMSClass(String name) {
            try {
                return Class.forName("net.minecraft.server." + getVersion() + "." + name);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }
             
        public static String getVersion() {
            return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
        }
     
    • Like Like x 2
    • Winner Winner x 2
  9. This is actually works for 1.12 + right?
     
  10. Yes. As long as mojang doesn't change their stupid api again. :p
     
  11. That's true. We will see soon