Skip to content

Instantly share code, notes, and snippets.

@Goblom
Last active August 29, 2015 14:07
Show Gist options
  • Save Goblom/f3a74829f0fedbde83af to your computer and use it in GitHub Desktop.
Save Goblom/f3a74829f0fedbde83af to your computer and use it in GitHub Desktop.
package net.theemeraldisle.core.utils;
import net.minecraft.server.v1_7_R4.ChatSerializer;
import net.minecraft.server.v1_7_R4.IChatBaseComponent;
import net.minecraft.server.v1_7_R4.PacketPlayOutChat;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.json.simple.JSONObject;
/**
*
* @author Goblom
*/
public class TitleUtil {
private static final int MIN_VERSION = 47;
public static void send(Player player, String msg) {
msg = color(msg);
JSONObject json = new JSONObject();
json.put("text", msg);
IChatBaseComponent chat = ChatSerializer.a(json.toJSONString());
PacketPlayOutChat bar = new PacketPlayOutChat(chat, 2);
if (isViable(player)) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(bar);
}
}
public static void send(String msg) {
for (Player player : Bukkit.getOnlinePlayers()) {
send(player, msg);
}
}
private static String color(String ms) {
return ChatColor.translateAlternateColorCodes('&', ms);
}
private static int getVersion(Player player) {
CraftPlayer cP = (CraftPlayer) player;
return cP.getHandle().playerConnection.networkManager.getVersion();
}
private static boolean isViable(Player player) {
return getVersion(player) >= MIN_VERSION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment