Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created January 1, 2014 03:36
Show Gist options
  • Save aadnk/8204789 to your computer and use it in GitHub Desktop.
Save aadnk/8204789 to your computer and use it in GitHub Desktop.
Send a raw tell command to a player through ProtocolLib.
package com.comphenix.example;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
public class SendRawTellPacket extends JavaPlugin implements Listener {
// Look at this example for the rest of the syntax:
// https://gist.github.com/thinkofdeath/e882ce057ed83bac0a1c
private static final String MESSAGE =
"{" +
"\"text\": \"Hello world\"," +
"\"clickEvent\": {" +
"\"action\":\"run_command\", " + "" +
"\"value\": \"/time set day\"" +
"}" +
"}";
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
PacketContainer chat = new PacketContainer(PacketType.Play.Server.CHAT);
chat.getChatComponents().write(0, WrappedChatComponent.fromJson(MESSAGE));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, chat);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment