Bukkit。聊天中的颜色支持(&)?和用户名onplayerjoin不起作用?

时间:2015-06-25 08:16:51

标签: java bukkit

我有两个问题。

问题1

我想在聊天中获得像必需品这样的颜色,但我不知道该怎么做,我试过这个:

@EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent chatevent){
        chatevent.getMessage().replaceAll("&", "§");
        for (String word : chatevent.getMessage().split(" ")){
            if(SysMng.getConfig().getStringList("badwords").contains(word)){
                if (!chatevent.getPlayer().hasPermission("bypassbadwords")){
                chatevent.setCancelled(true);
                chatevent.getPlayer().sendMessage(ChatColor.RED + "Dont use dirty or swear words!");
             }
          }
        }
     }

这一行:chatevent.getMessage()。replaceAll(“&”,“§”);但它不起作用。如何在聊天中获得颜色支持?

问题1更新 好的,这就是我所做的:

public void onPlayerChat(AsyncPlayerChatEvent chatevent){
        for (String word : chatevent.getMessage().split(" ")){
            word.replaceAll("&", "§");
            if(SysMng.getConfig().getStringList("badwords").contains(word)){
                if (!chatevent.getPlayer().hasPermission("bypassbadwords")){
                chatevent.setCancelled(true);
                chatevent.getPlayer().sendMessage(ChatColor.RED + "Dont use dirty or swear words!");
             }
          }
        }
     }

但它仍然无效。我怎么能修复它聊天颜色soo会起作用?我知道你不需要告诉我的字符串。我是游戏开发人员,我知道这些简单的东西。

2问题已修复

另一个问题是我希望onplayerjoin事件在服务器所有者加入时宣布,如果名称匹配并且它确实有效但现在它不是我做错了吗?控制台说该名称不能为空。怎么了?这是事件:

@EventHandler
public void onPlayerJoin(PlayerJoinEvent joinevent){
    Player getplayer = joinevent.getPlayer();
    getplayer.sendMessage(ChatColor.AQUA + "Hey " + getplayer.getName() + "! Welcome to the Ultimate Prison server!");
    // Spawning player in spawn location
    if(SysMng.getspawnsdata().getConfigurationSection("spawn") == null){
        getplayer.sendMessage(ChatColor.RED + "Spawn is not set!. Report this problem to owner INSTANTLY!");
    }
    World w = Bukkit.getServer().getWorld(SysMng.getspawnsdata().getString("spawn.world"));
    double x = SysMng.getspawnsdata().getDouble("spawn.x");
    double y = SysMng.getspawnsdata().getDouble("spawn.y");
    double z = SysMng.getspawnsdata().getDouble("spawn.z");
    getplayer.teleport(new Location(w, x, y, z));
    // ----------------------------------------------------------------
    if(getplayer.getName() == "Herobrine112211"){
        Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.GOLD + "Server Creator Herobrine112211 has joined the game!!!!!!!!!!");
    }
}

行if(getplayer.getName()==“Herobrine112211”){是我认为的问题。我确实尝试将其更改为完全相同的名称,但仍然是相同的错误。我该如何解决?

问题2修复

if(getplayer.getName().equalsIgnoreCase("Herobrine112211")){

我知道它应该是1个问题,但我不想发布2个问题,它在1中更好。

感谢。如果你需要更多的东西告诉我,我总是在这里阅读答案。

问题1 “托马斯”

喜欢这个吗?

@EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent chatevent){
        for (String word : chatevent.getMessage().split(" ")){
            word = word.replaceAll("&", "§");
            if(SysMng.getConfig().getStringList("badwords").contains(word)){
                if (!chatevent.getPlayer().hasPermission("bypassbadwords")){
                chatevent.setCancelled(true);
                chatevent.getPlayer().sendMessage(ChatColor.RED + "Dont use dirty or swear words!");
             }
          }
        }
     }

仍然无效。

1 个答案:

答案 0 :(得分:2)

字符串是不可变的。你不能在原地改变它们。调用replaceAll()会返回一个新字符串。如果您想用新单词替换旧单词,则需要执行以下操作:

word = word.replaceAll("&", "§");