Bukkit 聊天可点击按钮

Bukkit Chat Clickable Buttons

我实际上是在编写一个插件,在一个命令中它会提示一条确认消息,我想单击一个按钮(在聊天文本上)来确认和取消。我不喜欢复制和粘贴别人的代码,我也不想使用别人的类。 我正在尝试使用 TextComponents,但无法使其正常工作。这是命令代码

package myPackage;

import java.awt.TextComponent;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class InfoCommand implements CommandExecutor {

    public TextComponent TextComponent;
    @Override
    public boolean onCommand(CommandSender sender, Command cmnd, String alias, String[] args) {
        if (!(sender instanceof Player)) {
            return false;
        }
        Player player = (Player) sender;
        player.sendMessage(new String[] {
                            "Confirmation message.",
                            "Do you want to confirm?"});
        TextComponent message = new TextComponent ("Yes");
        message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/command"));
        return true;
    }            
}

它给我带来了 3 个错误:

TextComponent(String) is not public in TextComponent; cannot be accesed from outside package(在我定义 TextComponent 的那一行)

Cannot find symbol(在 ClickEvent 行中)

Package ClickEvent does not exist(在 ClickEvent 行中)

如何解决错误?有更简单的方法来制作可点击按钮(没有其他 类 或 copying/pasting)?

您有 2 个问题。

  1. 您缺少依赖项。将以下依赖项添加到您的 pom.xml:
<dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.12.2-R0.1-SNAPSHOT</version><!--change this value depending on the version-->
        <type>jar</type>
        <scope>provided</scope>
</dependency>
  1. 您在 InfoCommand class 中导入了 java.awt.TextComponent,这是错误的。所以删除那个导入并添加这些:
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;

我不推荐使用当前内置在 spigot 中的系统。您应该使用 Kyori/adventure。它好多了,paper 很快就会在本地实现它。

使用adventure的解决方案:

Component.text("My Clickable Text").color(NamedTextColor.GREEN).clickEvent(new ClickEvent(<paramaters>)