Replace multiple strings in a Config String with multiple outcomes.

Discussion in 'Plugin Development' started by gamerguy115, Jun 30, 2015.

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

    gamerguy115

    Hello, I am trying to make a CustomStaffChat plugin that allows staff to chat without players seeing it.

    I am trying to make it so an admin could customize the way the staff chat works, but I have ran into an issue.
    I want players to have a config like this:
    Code:
    message-format: "Prefix >> [PLAYERNAME]: {MESSAGE}"
    I am not sure how to replace two things in a certain line of code. I have tried doing .replaceAll().replaceAll(), but I had no luck. How could I do this?
     
  2. Offline

    teej107

    @gamerguy115 Please post your code so we can work from there.
     
  3. Moved to Plugin Development
     
  4. You've tried something like:
    Code:
    String formattedMessage = config.getString("message-format").replace("[PLAYERNAME]", player.getName()).replace("{MESSAGE}", message);
    
    Anyway, just a speculation but I think it's because you're using replaceAll and not replace. replaceAll uses regex whereas replace doesn't (I think). "[PLAYERNAME]" seems like a regex thing as it is enclosed in "[]"'s so that may be why it doesn't work.
     
  5. Offline

    gamerguy115

    You use .replaceAll() for RegEx's.

    @teej107
    Code:
    package me.CodeDotJar.CustomStaffChat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
      
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("staffchat")) {
                if(sender instanceof Player) {
                    Player p = (Player)sender;
                    if(p.hasPermission("staffchat.chat")) {
                        if(args.length == 0) {
                            p.sendMessage(ChatColor.RED + "Usage: /staffchat <message>");                      
                        } if (args.length != 0) {
                            StringBuilder x = new StringBuilder();
    
                            for(int i = 0; i < args.length; i++ ) {
                                x.append(args[i] + " ");
                            }
                            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("message-format").replaceAll("%player%", p.getName())));
                        }
                    }
                } else {
                    sender.sendMessage("You're not a player!");
                }
            }
          
            return false;
        }
      
    }
    
    EDIT: I was going to make a permission after the message in the Bukkit broadcast, just to let you know. :p
     
    Last edited by a moderator: Jun 30, 2015
  6. 1. You forgot to return true after the last else statement in onCommand.
    2. Again, use replace() and not replaceAll().
    3. It would be:
    Code:
    this.getConfig().getString("message-format").replace("%player%", p.getName()).replace("%message%", x.toString().trim())
    
    'aight thanks for clarifying. I said this in my post but I didn't know if I was fully correct.
    <Editted by bwfcwalshy: The king double posted! :eek:
    Merged posts>
     
    Last edited by a moderator: Jun 30, 2015
Thread Status:
Not open for further replies.

Share This Page