Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WizardlyBump17/dea00a5fbf17be70d4081bfdae907fe0 to your computer and use it in GitHub Desktop.
Save WizardlyBump17/dea00a5fbf17be70d4081bfdae907fe0 to your computer and use it in GitHub Desktop.
Send message actionbar to players (1.8 only)
package packetplayoutchat;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
public class ActionBarMessage {
public static void sendMessage(Player p, String message) {
PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + message.replace("&", "§") + "\"}"), (byte) 2);
((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
}
public static void broadcastMessage(String message) {
PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + message.replace("&", "§") + "\"}"), (byte) 2);
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
}
}
}
//Example
package packetplayoutchat;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class C implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
if(cmd.getName().equalsIgnoreCase("actionbar")) {
if(sender instanceof Player) {
Player p = (Player) sender;
if(args.length == 0) {
p.sendMessage("§c/actionbar (me, server) [args]");
} else {
if(args.length < 2) {
p.sendMessage("§c/actionbar (me, server) [args]");
} else {
if(args[0].equalsIgnoreCase("me")) {
StringBuilder sb = new StringBuilder();
for(int i = 1; i < args.length; i++) {
if(i > 1) sb.append(" ");
sb.append(args[i]);
}
ActionBarMessage.sendMessage(p, sb.toString());
p.sendMessage("§aYou sent the message to yourself!");
}
if(args[0].equalsIgnoreCase("server")) {
StringBuilder sb = new StringBuilder();
for(int i = 1; i < args.length; i++) {
if(i > 1) sb.append(" ");
sb.append(args[i]);
}
ActionBarMessage.broadcastMessage(sb.toString());
p.sendMessage("§aYou the message sent to all!");
Bukkit.getServer().broadcastMessage("§aYou received a message from " + p.getName());
}
}
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment