你可以这样做:
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" + "Choose one: " + "\",\"extra\":[{\"text\":\"" + "MOTD" + "\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + "/motd" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) <player>).getHandle().playerConnection.sendPacket(packet);
这将向他们发送一条消息,显示:
Choose one: MOTD
当用户点击MOTD时,它会以玩家身份运行命令/motd。以下是我们实际在做的一些细目:
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" + "<Ignored Message> " +
"\",\"extra\":[{\"text\":\"" + "<Message that will be clicked>" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"<Command to be run when message is clicked>" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) <player>).getHandle().playerConnection.sendPacket(packet);
以上代码将发送给播放器:
<Ignored Message> <Message that will be clicked>
当玩家点击<Message that will be clicked>时
他们会运行命令<Command to be run when a message is clicked>,因为它不以命令前缀/开头,所以会强制他们聊天<Command to be run when a message is clicked>。
不幸的是,据我所知,每条消息只能放置一个点击事件,因此您必须执行以下操作:
选择一个:
MOTD
邮件
所以你必须这样做,变量player 是播放器:
player.sendMessage("Choose one:");
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" +
"\",\"extra\":[{\"text\":\"" + "MOTD" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"/motd" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
IChatBaseComponent comp2 = ChatSerializer
.a("{\"text\":\"" +
"\",\"extra\":[{\"text\":\"" + "Mail" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"/mail" + "\"}}]}");
PacketPlayOutChat packet2 = new PacketPlayOutChat(comp2, true);
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet2);
当MOTD被点击时,/motd会被玩家运行,当Mail被点击时,/mail会被运行。
顺便说一句,您需要在构建路径中包含 craftbukkit 以及 bukkit 才能执行此操作