Solved Nms help for ActionBar

Discussion in 'Spigot Plugin Development' started by Greenman1805, May 23, 2016.

  1. For 1.8 I used this code to send an ActionBar message:
    Code (Text):
                                Object icbc = getNmsClass("IChatBaseComponent$ChatSerializer").getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{'text': '" + message + "'}" });
                                Object ppoc = getNmsClass("PacketPlayOutChat").getConstructor(new Class[] { getNmsClass("IChatBaseComponent"), Byte.TYPE }).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 });
     
    But this doesn't work in 1.9. Can someone help me to get a working method for 1.9?
     
  2. What's not working? Is it throwing any errors? Is it just not sending? It would also be useful to see your whole class to see your other methods. Also, what is the point of getting the byte value of a byte?
     
  3. Choco

    Moderator

    Doesn't look like the OP wrote that code. Looks decompiled to me
    Code (Java):
    /* 1. new Class[]{getNmsClass("IChatBaseComponent")}
    * 2. new Class[]{String.class}
    */
    etc. There are a few more of those. This is clearly decompiled :p
     
    • Agree Agree x 2
  4. Code (Text):
        public void sendAction(Player p, String msg) {
            try {
                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.TYPE }).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 | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        public static Class<?> getNmsClass(String nmsClassName) throws ClassNotFoundException {
            return Class.forName("net.minecraft.server." + Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + "." + nmsClassName);
        }
    Here is the whole code.

    And thats the error:
    Code (Text):
    [22:30:02 WARN]: Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3
    [22:30:02 WARN]:        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
    [22:30:02 WARN]:        at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
    [22:30:02 WARN]:        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:488)
    [22:30:02 WARN]:        at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
    [22:30:02 WARN]:        at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:666)
    [22:30:02 WARN]:        at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642)
    [22:30:02 WARN]:        at com.google.gson.internal.Streams.parse(Streams.java:44)
    [22:30:02 WARN]:        ... 17 more
     
  5. It was a tutorial from the internet because I've never used nms code before.
     
  6. Choco

    Moderator

    Lol well the tutorial you learned it from used decompiled code. There's no way that is not decompiled

    Regardless
    " malformed JSON at line 1 column 3"
     
    • Agree Agree x 1
  7. Sorry, but I've never done something with nms. How can I fix this issue? :D
     
  8. are you trying to send a message from above hotbar?
     
  9. Code (Text):
    package com.lookcook.particlesgui;

    import java.lang.reflect.InvocationTargetException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    public class ActionBar {
        public Plugin plugin;

        public ActionBar(Plugin plugin) {
            this.plugin = plugin;
        }

        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, Plugin plugin, boolean colors) {
            try {
                if (ActionBar.getServerVersion().equalsIgnoreCase("v1_9_R1") || ActionBar.getServerVersion().equalsIgnoreCase("v1_9_R2")) {
                    Object icbc = ActionBar.getNmsClass("ChatComponentText").getConstructor(String.class).newInstance(ChatColor.translateAlternateColorCodes((char)'&', (String)msg));
                    Object ppoc = ActionBar.getNmsClass("PacketPlayOutChat").getConstructor(ActionBar.getNmsClass("IChatBaseComponent"), Byte.TYPE).newInstance(icbc, Byte.valueOf((byte) 2));
                    Object nmsp = p.getClass().getMethod("getHandle", new Class[0]).invoke((Object)p, new Object[0]);
                    Object pcon = nmsp.getClass().getField("playerConnection").get(nmsp);
                    pcon.getClass().getMethod("sendPacket", ActionBar.getNmsClass("Packet")).invoke(pcon, ppoc);
                } else if (ActionBar.getServerVersion().equalsIgnoreCase("v1_8_R2") || ActionBar.getServerVersion().equalsIgnoreCase("v1_8_R3")) {
                    Object icbc = ActionBar.getNmsClass("IChatBaseComponent$ChatSerializer").getMethod("a", String.class).invoke(null, "{'text': '" + msg + "'}");
                    Object ppoc = ActionBar.getNmsClass("PacketPlayOutChat").getConstructor(ActionBar.getNmsClass("IChatBaseComponent"), Byte.TYPE).newInstance(icbc, Byte.valueOf((byte) 2));
                    Object nmsp = p.getClass().getMethod("getHandle", new Class[0]).invoke((Object)p, new Object[0]);
                    Object pcon = nmsp.getClass().getField("playerConnection").get(nmsp);
                    pcon.getClass().getMethod("sendPacket", ActionBar.getNmsClass("Packet")).invoke(pcon, ppoc);
                } else {
                    Object icbc = ActionBar.getNmsClass("ChatSerializer").getMethod("a", String.class).invoke(null, "{'text': '" + msg + "'}");
                    Object ppoc = ActionBar.getNmsClass("PacketPlayOutChat").getConstructor(ActionBar.getNmsClass("IChatBaseComponent"), Byte.TYPE).newInstance(icbc, Byte.valueOf((byte) 2));
                    Object nmsp = p.getClass().getMethod("getHandle", new Class[0]).invoke((Object)p, new Object[0]);
                    Object pcon = nmsp.getClass().getField("playerConnection").get(nmsp);
                    pcon.getClass().getMethod("sendPacket", ActionBar.getNmsClass("Packet")).invoke(pcon, ppoc);
                }
            }
            catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchFieldException | NoSuchMethodException | SecurityException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
     
    Could use that class :)
     
    • Winner Winner x 1
  10. Thanks! I will try this :)
     
  11. MiniDigger

    Supporter

    • Agree Agree x 1
  12. Choco

    Moderator

  13. MiniDigger

    Supporter

    well, why would someone use a bukkit/craftbukkit without spigot/spigot-server nowadays? I don't understand the sepratation anywas and talked to md5 and tod about that but they want to keep the seperation....
     
    • Agree Agree x 1
  14. Choco

    Moderator

    Oh definitely agreed. Most people should be phased away from Bukkit servers now. Spigot servers just recently took over statistic-wise. :p I suppose using Spigot only API methods may encourage people to switch over to Spigot more