Solved Replace words on chat/commands

Discussion in 'Plugin Development' started by Natanio, Feb 2, 2018.

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

    Natanio

    i want to make a plugin where when someone types a message or runs a command it will be converted to lowercase and replace the phrase "tnt" with the phrase "sponge". Example: if someone runs the command: "//replacenear 10 air tnt" it will change it will run as "//replacenear 10 air sponge". Example 2: if someone says "I AM ABOUT TO CRAFT SOME TNT" it will change to "i am about to craft some sponge" and the player will be sent a message Replaced ' "TNT" with "sponge" '

    although it doesn't work

    Code:
    package Chat;
    
    import static org.bukkit.ChatColor.*;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    
    public class antiTNT implements Listener {
    
        @EventHandler
    // so when worldedit is used, instead of using tnt it will use sponge
        public void onCommandPreprocess(PlayerCommandPreprocessEvent e) {
            String cmd = e.getMessage().toLowerCase();
            Player p = e.getPlayer();
            if (cmd.toLowerCase().contains("tnt")) {
                cmd.toLowerCase().replace("tnt", "sponge");
                e.setMessage(cmd);
                p.sendMessage(RED + "Replaced \"TNT\" with \"sponge\"");
            }
        }
    
        @EventHandler
    //  if someone says "i love tnt" in chat it will change the message to "i love sponge"
        public void onChat(AsyncPlayerChatEvent e) {
    
            String newMessage = e.getMessage().toLowerCase();
            Player p = e.getPlayer();
    
            if (newMessage.toLowerCase().contains("tnt")) {
                newMessage.toLowerCase().replace("tnt", "sponge");
                e.setMessage(newMessage);
                p.sendMessage(RED + "Replaced \"TNT\" with \"sponge\"");
            }
    
        }
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Natanio Did you register the events?
    Make sure that this is handled before the other handlers.
     
  3. Offline

    Zombie_Striker

    @Natanio
    The replace method does not change the string instance, it instead creates a new instance with the change. That means "newMessage" does not get changed, and instead a new string is created, but never used. To fix this, set newMessage equal to this new string when you try to replace the word.
     
  4. Offline

    Natanio

    @Zombie_Striker
    sorry i'm a coding noob. Can you give me the line of code?

    just figured out how to do it

    Code:
    package Chat;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    
    public class antiTNT implements Listener {
    
        @EventHandler
        public void onCommandPreprocess(PlayerCommandPreprocessEvent e) {
            String cmd = e.getMessage().toLowerCase().replace("tnt", "sponge");
            e.setMessage(cmd);
        }
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            String msg = e.getMessage().toLowerCase().replace("tnt", "sponge");
            e.setMessage(msg);
    
        }
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 3, 2018
  5. Offline

    Machine Maker

    Please mark this thread as Solved if your issue has been resolved.
     
Thread Status:
Not open for further replies.

Share This Page