Minecraft(bukkit)插件,向用户发送可点击的链接 [英] Minecraft (bukkit) plugins, send user a clickable link

查看:919
本文介绍了Minecraft(bukkit)插件,向用户发送可点击的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发bukkit插件,我需要向用户发送URL,我需要让用户点击链接,以便可以在用户的​​浏览器中打开它.

Im developing a bukkit plugin and I need to send a URL to the user, I need to have the link clickable by the user so it can be opened in the users's browser.

我尝试使用HTML和其他类型的标签,但没有任何效果. 我还搜索了Bukkit Java库,除了为文本输出着色外没有找到其他东西.

I tried using HTML and other types of tags but nothing worked. I also searched Bukkit Java library and did not find anything other than colouring the text output.

推荐答案

要将可点击的链接发送给客户端,您需要向其发送原始的json消息,有多种方法可以做到这一点:

To send a clickable link to a client you need to send a raw json message to him, there are different methods to do this:

使用Server.dispatchCommand(<your sender>,<Your command String>);,您可以让控制台执行命令,而我们要执行命令/tellraw <user> {text:"Click.",clickEvent:{action:open_url,value:"http://stackoverflow.com/q/34635271"}}.可以通过以下代码完成此操作:

Using Server.dispatchCommand(<your sender>,<Your command String>); you can let the console execute a command, we want to execute the command /tellraw <user> {text:"Click.",clickEvent:{action:open_url,value:"http://stackoverflow.com/q/34635271"}}. This can be done in code as follows:

public void sendMessage(Player player, String message, String url) {
    Bukkit.getServer().dispatchCommand(
        Bukkit.getConsoleSender(),
        "/tellraw " + player.getName() + 
        " {text:\"" + message + "\",clickEvent:{action:open_url,value:\"" +
        url + "\"}}");
}

使用本地的Craftbukkit方法

我们可以调用Bukkit的一些不安全的方法直接发送消息,为此,我们首先需要将播放器转换为CraftPlayer,然后获取EntityPlayer,最后在播放器的playerConnection上调用sendPacket.

Using native craftbukkit methods

We can call some unsafe methods of Bukkit to send a message directly, to do this, we first need to cast our player to a CraftPlayer, then get a EntityPlayer and finally invoke sendPacket on the playerConnection of the player.

基于: Gamecube762的JsonMessages.java

public static PacketPlayOutChat createPacketPlayOutChat(String s){return new PacketPlayOutChat(ChatSerializer.a(s));}

public static void SendJsonMessage(Player p, String s){( (CraftPlayer)p ).getHandle().playerConnection.sendPacket( createPacketPlayOutChat(s) );}

public void sendMessage(Player player, String message, String url) {
    SendJsonMessage(player,
        "{text:\"" + message + "\",clickEvent:{action:open_url,value:\"" +
        url + "\"}}");
}

为bukkit使用库

很多人以前都遇到过这个问题,并写了一个图书馆来解决这个麻烦.

Using a libary for bukkit

There are a lot of people who faced this problem before and wrote a library to solve the hassle.

可以通过简单的Google搜索 bukkit发送json消息来找到这些内容.

These can be found by a simple google search for bukkit send json message.

以上方法假定您的代码在方法的调用控制下,如果传递给该方法的代码/数据由不受信任的来源提供,则它们可以转出json字符串并添加您没有使用的json标签预计.您应该验证或转义传入的不可信数据.

The above methods assume YOUR code is under control of the calling of the methods, if the code/data passed to the method is provided by untrusted sources, they can escape out of the json string and add json tags you didn't expect. You should either validate or escape incoming untrusted data.

Minecraftforums:

Minecraftforums: 1.8 - Raw JSON Text Examples (for /tellraw, /title, books, signs)

这篇关于Minecraft(bukkit)插件,向用户发送可点击的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆