Custom Chat System (With Commands)

Discussion in 'Plugin Development' started by caHarkness, Mar 26, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    caHarkness

    I am trying very hard to implement a custom chat system on my mod, but it seems that it doesn't want to execute anything I check for.


    Code:
        public void onPlayerCommandPreprocess(PlayerChatEvent i) {
            String[] message = i.getMessage().split(" ");
            String command = message[0].trim().substring(1).toLowerCase();
    
            if (command == "test") {
                i.getPlayer().getServer().broadcastMessage("Success!");
            }
        }
    
        public void onPlayerChat(PlayerChatEvent i) {
            i.getPlayer().getServer().broadcastMessage(ChatColor.GREEN + i.getPlayer().getName() + ChatColor.WHITE + ": " + i.getMessage());
            if (!i.getMessage().startsWith("/")) {
                i.setCancelled(true);
            }
        }
    I did register the event listeners, and the custom CHAT part works just fine, but the commands to not want to come through. I said "/test" in game and the success message did not show up, however, when I used this:

    Code:
        public void onPlayerCommandPreprocess(PlayerChatEvent i) {
            String[] message = i.getMessage().split(" ");
            String command = message[0].trim().substring(1).toLowerCase();
    
            i.getPlayer().getServer().broadcastMessage(message[0]);
            i.getPlayer().getServer().broadcastMessage(message[1]);
            i.getPlayer().getServer().broadcastMessage(message[2]);
        }
        
    ..It showed my input correctly. I don't know why it's not working, when clearly, it should.
     
  2. Offline

    Edward Hand

    You cannot use the logical equality operator (==) for equating strings in Java.
    Use instead:
    Code:
    if(string1.equals(string2))
    or
    Code:
    if(string1.equalsIgnoreCase(string2))
     
  3. Offline

    caHarkness

    Wow, do I feel silly! I'm fairly new to Java anyways, I know C# better.
    I'll test it out, then get back to you later. Thanks for the input!
     
Thread Status:
Not open for further replies.

Share This Page