Solved Replacing variable from config

Discussion in 'Spigot Plugin Development' started by Impasta1000, Jun 25, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys, I am having some problems with replacing variables taken from the config, in this case being the Prefix. The problem is that %prefix% does not get replaced.

    This is where the output is generated, both debug messages output as they should.
    Code (Text):
    if (args[0].equalsIgnoreCase("arenas")) {
                        configManager.loadConfig(ConfigFile.LOCALE);
                        FileConfiguration config = configManager.getConfig(ConfigFile.LOCALE);
                        rApi.sendColouredMessage(player, "DEBUG > NO-PERMISSION: " + config.getString("Messages.no-permission"));
                        rApi.sendColouredMessage(player, "DEBUG > PREFIX: " + config.getString("Configuration.Prefix"));
                        if (!checkPerm(player, "XKits.Arena.List")) {
                            rApi.sendColouredMessage(player, plugin.getMessages().get("NO-PERMISSION")); <== Problem on this line, %prefix% does not get replaced.
                            return true;
                        }
                        arenaManager.listArenaLobbies(player);
                    }
    plugin.getMessages().get("NO-PERMISSION") gets the message from a HashMap defined.
    Code (Text):
        private HashMap<String, String> messages = new HashMap<String, String>();
        public HashMap<String, String> getMessages() {
            return messages;
        }

        private void loadLocalization() {
            messages.put("NO-PERMISSION", localeManager.getLocaleMessage("Messages.no-permission"));
        }  <== This is in the onEnable()
     
    These are the methods being called.
    Code (Text):
    public String getLocaleMessage(String name) {
            configManager.loadConfig(ConfigFile.LOCALE);
            FileConfiguration config = configManager.getConfig(ConfigFile.LOCALE);
            String caption = config.getString(name);
            replacePlaceholders(caption);
            caption = ChatColor.translateAlternateColorCodes('&', caption);
            return caption;
        }

        public String replacePlaceholders(String string) {
            configManager.loadConfig(ConfigFile.LOCALE);
            FileConfiguration config = configManager.getConfig(ConfigFile.LOCALE);
            string = string.replaceAll("%prefix%", config.getString("Configuration.Prefix"));
            return string;
        }
    Locale.yml
    Code (Text):
    Configuration:
    # Prefix is usually displayed in front of messages
    # Placeholder: %prefix%
        Prefix: "&c&l(!)"

    Messages:
     
        no-permission: "%prefix% &cYou have insufficient permission!"
     
    #1 Impasta1000, Jun 25, 2016
    Last edited: Jun 26, 2016
  2. config.set("Configuration.Prefix", "Meow");
    Then save.
     
  3. I don't see why there is a need to do this as the "locale.yml" I posted came from the plugins folder in the server files.
     
  4. Problem solved.

    If anyone needs help on how I did it, feel free to PM me.
     
  5. Why not just post how ;)
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  6. Alright then, I reduced the code to this and now it works...
    Code (Text):
    public String getLocaleMessage(String name) {
            configManager.loadConfig(ConfigFile.LOCALE);
            FileConfiguration config = configManager.getConfig(ConfigFile.LOCALE);
           
            name = replacePlaceholders(config.getString(name));
           
            return name;
        }

        public String replacePlaceholders(String string) {
            configManager.loadConfig(ConfigFile.LOCALE);
            FileConfiguration config = configManager.getConfig(ConfigFile.LOCALE);
           
            string = string.replace("%prefix%", config.getString("Configuration.Prefix"));
            return string;
        }
     
    • Optimistic Optimistic x 1
Thread Status:
Not open for further replies.