[Tutorial] Creating a messages.yml file

Discussion in 'Resources' started by slayr288, Jun 19, 2013.

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

    slayr288

    Hello, I'm going to be showing you how to create a messages.yml files and refer to the messages in your plugin.

    ---

    First, you need to check if the messages.yml file exist, and if not, to create it. Add this in your onEnable() method.
    Code:
                File f = new File(getDataFolder()+File.separator+"messages.yml");
                if (!f.exists()) {
                    try {
                        f.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


    Now that we have our empty messages.yml file, we need to populate it. Add this method in your main class, or in a separate class if you know how to call it.
    Code:
        private void setMessage(String name, String message) {
            File f = new File(getDataFolder()+File.separator+"messages.yml");
            FileConfiguration config = YamlConfiguration.loadConfiguration(f);
            if (!config.isSet(name)) {
                config.set(name, message);
                try {
                    config.save(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    With that method, we can easily create messages in the messages.yml to be set by the user. Here is an example that would be put in the onEnable() method.
    Code:
                File f = new File(getDataFolder()+File.separator+"messages.yml");
                if (!f.exists()) {
                    try {
                        f.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                setMessage("noPermission", "&cYou do not have permission.");


    Since we want to have speedy access to these messages, lets put them in a hashmap.
    First, create the hashmap in your main class.
    Code:
        public static HashMap<String, String> messageData = new HashMap<String, String>();
    Then, we will iterate through the messages.yml file and add all of the messages to the hashmap.
    Here is the finished example onEnable() method.
    Code:
        @Override
        public void onEnable() {
           
            File f = new File(getDataFolder()+File.separator+"messages.yml");
            if (!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            setMessage("noPermission", "&cYou do not have permission.");
           
            FileConfiguration config = YamlConfiguration.loadConfiguration(f);
            for (String message : config.getConfigurationSection("").getKeys(false)) {
                messageData.put(message, config.getString(message));
            }
           
            getLogger().info("Example plugin has been enabled!");
           
        }


    Finally, you can now get the message from any class whenever you need.
    Here is an example in the main class:
    Code:
                        if (!player.hasPermission("your.permission")) {
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("noPermission")));
                            //Do whatever
                        }
    And an example outside of the main class:
    Code:
                        if (!player.hasPermission("your.permission")) {
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', yourMainClass.messageData.get("noPermission")));
                            //Do whatever
                        }
     
  2. Offline

    chasechocolate

Thread Status:
Not open for further replies.

Share This Page