0

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.

Reaper123
  • 109
  • 4
  • 8
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Thomas Weller Jun 25 '15 at 08:24
  • The first question is a duplicate of http://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning, meaning that you should assign the result of `chatevent.getMessage().replaceAll("&", "§");` to a variable – Thomas Weller Jun 25 '15 at 08:26
  • This is a string i need colors not a string. – Reaper123 Jun 25 '15 at 08:54
  • 1
    Your second question should have been an separate post. Your first problem looks to be solved by @Thomas – BillRobertson42 Jun 25 '15 at 15:46
  • 1
    Like @Bill says, StackOverflow isn't the same as normal forums. For every separate question you have, you should make a new question, and not post them both on the same question. You may want to check out the [help center](http://stackoverflow.com/help) or the [site tour](http://stackoverflow.com/tour) – Jojodmo Jun 25 '15 at 21:30

1 Answers1

2

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("&", "§");
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • LIke that? check the question at the top i cannot past code in the comment section. – Reaper123 Jun 25 '15 at 15:58
  • This answer is based on the code you posted in PROBLEM 1 UPDATE. Any more updates? – Thomas Weller Jun 26 '15 at 06:05
  • That is the problem it does not work. What i am doing wrong? i am talking about problem 1. At the top i posted a update but that does not work. – Reaper123 Jun 26 '15 at 11:50
  • @Reaper123 Technically it's ok now with the update you have posted. Can you clarify what "It does not work" means? What is the input to the method, what is the expected output? We have no idea what bad word is your input and what bad words are in your bad word list. You need to provide more information than just the fact "it does not work". – Thomas Weller Jun 26 '15 at 12:06
  • badwords is a chat checker if it see that in minecraft chat there is a word that is in the config.yml list it will show a message dont use bad words soo you can ignore that. Now by the it does not work i mean when i go in the game and type &6asdas in the chat it does not change the & like it should do. Thats why it does not work. – Reaper123 Jun 26 '15 at 13:34
  • @Reaper123: so in your config file you have "§6asdas" as a bad word? – Thomas Weller Jun 29 '15 at 07:32