2015-06-25 21 views
0

我有2個問題。Bukkit。聊天中的顏色支持(&)?和用戶名onplayerjoin不起作用?

問題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個UPDATE 好洙這是我做過什麼:

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!"); 
      } 
      } 
     } 
    } 

但它仍然無法正常工作。我如何解決它在聊天顏色洙會工作?我知道你不需要告訴我的字符串。我是一名遊戲開發者,我知道這些簡單的東西。

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!!!!!!!!!!"); 
    } 
} 

如果(getplayer.getName()== 「Herobrine112211」){是我認爲這個問題的線路。我曾嘗試將它改爲確切的名稱,但仍然是相同的錯誤。我該如何解決它?

問題2 FIX

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!"); 
      } 
      } 
     } 
    } 

仍然不起作用。

+0

可能重複http://stackoverflow.com/questions/513832/如何做我比較字符串在Java) –

+0

第一個問題是重複http://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning,這意味着您應該將'chatevent.getMessage()。replaceAll(「&」,「§」);'的結果賦值給變量 –

+0

這是一個字符串,我需要顏色而不是字符串。 – Reaper123

回答

2

字符串是不可變的。你不能在原地改變它們。調用replaceAll()會返回一個新的字符串。如果你想通過這個新詞來代替舊的話,你需要做的:

word = word.replaceAll("&", "§"); 
的[?我如何在Java中比較字符串(
+0

像這樣?檢查頂部的問題,我不能在評論部分中過去代碼。 – Reaper123

+0

此答案基於您在問題1更新中發佈的代碼。還有更新嗎? –

+0

這是它不起作用的問題。我做錯了什麼?我正在談論問題1.在頂部,我發佈了一個更新,但不起作用。 – Reaper123