Solved Set messages on custom.yml

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

  1. Offline

    Ahrigumi

    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. Online

    timtower Ninja on the waves Moderator

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

    Ahrigumi

    Oh, right , its a NullPointerException.

    and messagesConfig is...

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

    timtower Ninja on the waves 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

    Ahrigumi

    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. Online

    timtower Ninja on the waves 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

    Ahrigumi

    @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

    Ahrigumi

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

    timtower Ninja on the waves 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.
     

Share This Page