将arraylist项分隔为常规关键字组

pw9qyyiw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(321)

我在minecraft中为插件编码启动了java。现在我尝试添加角色名作为前缀,并在聊天中显示它。

ArrayList<String> userRoles = new ArrayList<String>();

    if(player.hasPermission("chat.rank.admin")){
        userRoles.add("[Admin]");
    } if(player.hasPermission("chat.rank.vip")) {
        userRoles.add("[VIP]");
    }

    event.setFormat(userRoles<>(1) + " " + player.getDisplayName() + "§7: " + msg);
// In this line, the expected output is "[Admin] user: msg" or both [Admin] [VIP] user: msg"
// But it gives "([Admin],[VIP]) user: msg" 
// I'm sure it has a simple solution but as I said, I'm new here. thanks from now
3df52oht

3df52oht1#

似乎您正在尝试创建一个只存储一个值的列表。
您可能需要尝试创建一个函数,以获取您的外部玩家的排名名称 PlayerChatEvent 听众。
下面是演示代码:

public String getPlayerRankName(Player p){
        if (p.hasPermission("chat.rank.admin"))
            return "[Admin]";
        else if (p.hasPermission("chat.rank.vip"))
            return "[VIP]";
        else 
            return "";
    }

在你的 PlayerChatEvent 事件监听器,在聊天线路中调用此函数:

event.setFormat(getPlayerRankName(event.getPlayer()) + " " + player.getDisplayName() + "§7: " + msg);

相关问题