尝试将配置文件中的文本替换为广播,并获得 'Type mismatch: cannot convert from int to String'

Trying to replace text from config file to broadcast, and getting 'Type mismatch: cannot convert from int to String'

当尝试制作一个广播代码行来读取它将从配置发送的消息时,我需要在一行中替换多个内容,但我收到了标题中描述的错误。

我试过将字符串更改为整数,但它在我的其他部分出现了错误。请帮助我修复,查看下面的代码并调试错误。谢谢!

if(plugin.getConfig().getBoolean("enable_global_death")) {
                        String bc = Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("global_death")));
                          bc = bc.replace("<killer>", killer.getName());        
                          bc = bc.replace("<player>", p.getName());
                          bc = bc.replace("<kill_weapon>", (CharSequence) killer.getItemInHand());

                        return;

我希望输出没有任何错误,我希望它能替换配置中的内容,例如杀手的名字。

你没有说出是哪一行导致抛出异常。但我假设它是最后 bc = bc.replace(…) 行并且 killer.getItemInHand() returns 是一个数字(我猜是 int)。如果我的假设是正确的,那么以下内容应该有所帮助:

方法 String.replace(…) 接受两个 CharSequence 类型的参数。但是如果 killer.getItemInHand() returns 一个数字,你不能直接将它转换为 CharSequence。 Java不知道该怎么做。您必须明确地将数字转换为 CharSequence(或子类型,例如 String):

bc.replace("<kill_weapon>", String.valueOf(killer.getItemInHand()));

您必须先替换字符串才能广播您的消息:

if(plugin.getConfig().getBoolean("enable_global_death")) {
    String bc = Utils.chat(plugin.getConfig().getString("global_death"));
    bc = bc.replace("<killer>", killer.getName());        
    bc = bc.replace("<player>", p.getName());
    bc = bc.replace("<kill_weapon>", killer.getItemInHand().getType().toString());
    Bukkit.broadcastMessage(bc);
    return;
}