如何使用bukkit创建一个可执行命令的可单击链接

时间:2014-03-24 13:55:22

标签: minecraft bukkit

我正在尝试制作一个bukkit插件,我似乎无法找到任何关于此的文档,但我已经看到它完成了,如何将命令输入到用户可以点击的聊天消息中在服务器上执行命令,如“/ motd”,以URL等可点击链接的形式

if (commandLabel.equalsIgnoreCase("cmd") {
    player.sendMessage("Pick a command: " + </motd> + ", " + </mail> );
}

替换“”和“”输出如下内容:

选择一个命令:MOTDMail

并单击它们将执行命令到服务器。我该怎么做?

2 个答案:

答案 0 :(得分:4)

你可以这样做:

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执行此操作

答案 1 :(得分:0)

或者你可以简单地这样做(我自己做,你可以编辑它)

/execute @a ~ ~ ~ tellraw @p ["",{"text":"Click this to die","color":"dark_red","bold":true,"clickEvent":{"action":"run_command","value":"/kill @p"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"Kills you!"}]}}}]

run_command也可以替换为Open URL。

您也可以用任何颜色替换深红色。如果需要,可以将false替换为false替换为false,可以使用命令替换/ kill @p(如果执行Open URL,则可以替换为https://链接,show_text可以替换为Show Item,Show entity或Show Achivement 。文本和杀戮你可以用不同的东西替换(例如,显示实体)(实体替换文本)

如果你卡住我找到了一个网站!美好的一天:) http://minecraftjson.com/