Solved Set messages on custom.yml

Discussion in 'Plugin Development' started by user_91277742, Jun 12, 2019.

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

    user_91277742

    Hi there!, so im trying to set messages as default in a custom.yml .
    I did a ConfigManager class but in the moment to load , i have a null when i set the message like
    Code:
    messagesConfig.set("prefix", "&6[test]");
    
    What is wrong?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi What is messagesConfig ?
    And you have a null, as in NullpointerException or a null when you get the value again?
     
  3. Offline

    user_91277742

    Oh, right , its a NullPointerException.

    and messagesConfig is...

    Edit: Ok i saw my error hahahaha , thanks @timtower
     
  4. Offline

    timtower Administrator Administrator Moderator

    Please post the full class
     
  5. Offline

    KarimAKL

    @Ahrigumi If you want defaults in a file, you can include it in the jar and then use
    Code:Java
    1. Plugin.saveResource(String path, boolean replace)

    That way you can just write the stuff in the file and it'll include it when you create it using that method.
     
  6. Offline

    user_91277742

    Code:
    public class ConfigManager {
    
        private OakwoodHorses plugin = OakwoodHorses.getPlugin(OakwoodHorses.class);
    
        public FileConfiguration mensajesConfig;
        public File mensajesFile;
    
        public void setup() {
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
            }
    
            mensajesFile = new File(plugin.getDataFolder(), "messages.yml");
    
            if (!mensajesFile.exists()) {
                try {
                    mensajesFile.createNewFile();
                } catch (IOException e) {
                    Bukkit.getServer().getConsoleSender()
                            .sendMessage(ChatColor.RED + "[OakwoodHorses] Could not create the messages.yml file");
                }
            }
           
            mensajesConfig = YamlConfiguration.loadConfiguration(mensajesFile);
           
            mensajesConfig.set("prefix", "&6[OakwoodHorses]");
            mensajesConfig.addDefault("config-reloaded", "&aThe config and the messages were reloaded!");
            mensajesConfig.addDefault("educated", "&aCongrats, you educated this horse! , to claim use /testtest");
            mensajesConfig.addDefault("tamed", "&bThis horse is tamed , use: &c /oh claim <name> &b to claim it!");
            mensajesConfig.addDefault("no-tamed", "&cThis horse is not tamed!");
            mensajesConfig.addDefault("no-horse", "&cYou cant use this command in this animal");
            mensajesConfig.addDefault("set-name", "&bYou claimed this horse with the name %name%");
            mensajesConfig.addDefault("new-owner", "The new owner of this horse is %player%");
            mensajesConfig.addDefault("feed-horse", "&byou fed your horse");
            mensajesConfig.addDefault("toggle-stats-on", "toggle on");
            mensajesConfig.addDefault("toggle-stats-off", "toggle off"); 
           
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[OakwoodHorses] messages.yml file has been created");
        }
    
    }
    
     
  7. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi Can you please use a constructor to initialize it?
    Then there is less chance to use it before all values are initialized.
    Now you can make an instance, use menajesConfig before you call the setup method.
    Not to mention that the plugin variable might be null if you call it on the wrong moment.
     
  8. Offline

    user_91277742

    @timtower Im not really sure about what do you mean with the constructor . I mean.. im initializing this class on my main class
     
  9. Offline

    KarimAKL

    @Ahrigumi Make a constructor for your ConfigManager class and call the setup method in it.
     
  10. Offline

    user_91277742

    Im sorry, but i dont understand ,why i need to do a constructor in that class :/
     
  11. Offline

    timtower Administrator Administrator Moderator

    To get rid of the unneeded setup method that can be forgotten.
    Why not use a constructor? Now you are using a static method that can give you an object before the onEnable is called.
     
Thread Status:
Not open for further replies.

Share This Page