Reaper123
Reaper123

Reputation: 109

Bukkit. Color support in chat (&) ? and Username onplayerjoin does not work?

I have 2 problems.

PROBLEM 1

I want to get colors like essentials have in the chat but i dont know how to do that i tried this:

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

this line: chatevent.getMessage().replaceAll("&", "§"); but it does not work. How can i get color support then in chat?

PROBLEM 1 UPDATE Ok soo this is what i did:

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

But it still does not work. How can i fix it soo in chat colors would work? i know about strings you dont need to tell me about that. I am a game developer i know these simple stuff.

2 PROBLEM IS FIXED

And another problem is i want onplayerjoin event to announce when server owner join if the name match and it did work but now it does not what i am doing wrong? the console say that the name cannot be null. What is wrong? here is the event:

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

The line if(getplayer.getName() == "Herobrine112211"){ is the problem i think. I did try changing it to exact the same name but still the same error. How can i fix it?

PROBLEM 2 FIX

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

I know it should be 1 question but i dont want to post 2 questions its better that its in 1.

Thanks. I am always here reading answers if you need something more tell me.

Problem 1 "Thomas"

Like this?

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

Still does not work.

Upvotes: 0

Views: 332

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59430

Strings are immutable. You can't change them inplace. Calling replaceAll() returns a new string. If you want to replace the old word by the new word, you need to do:

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

Upvotes: 2

Related Questions