Last active
August 7, 2016 13:49
-
-
Save justisr/170860810374d8519f18 to your computer and use it in GitHub Desktop.
Send messages to the player's action bar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.justisroot.actionutil; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
public abstract class ActionBar { | |
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 sendActionBar(Player p, String msg) { | |
try { | |
String version = getServerVersion(); | |
String nmsClass = (!(version.startsWith("v1_7_R")) ? "IChatBaseComponent$" : "") + "ChatSerializer"; | |
Object serializer = getNmsClass(nmsClass).getMethod("a", new Class[]{String.class}).invoke(null, "{'text': '" + msg + "'}"); | |
Object packet = getNmsClass("PacketPlayOutChat").getConstructor(new Class[]{getNmsClass("IChatBaseComponent"), Byte.TYPE}).newInstance(serializer, (byte) 2); | |
Object handle = p.getClass().getMethod("getHandle", new Class[0]).invoke(p); | |
Object playerConnection = handle.getClass().getField("playerConnection").get(handle); | |
playerConnection.getClass().getMethod("sendPacket", new Class[]{getNmsClass("Packet")}).invoke(playerConnection, packet); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment