messages.yml not reload from disk

Discussion in 'Plugin Development' started by Cri16228, Mar 13, 2018.

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

    Cri16228

    I use this code to reload the messages.yml from config, but if I do the command to reload it the code don't reload anything from disk
    Code:
    public final static void reloadMessagesFile() {
            if (messagesFile == null) {
                messagesFile = new File(pl.getDataFolder(), "messages.yml");
            }
            messages = YamlConfiguration.loadConfiguration(messagesFile);
    
            InputStream messagesStream = pl.getResource("messages.yml");
            if (messagesStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(messagesStream);
                messages.setDefaults(defConfig);
            }
        }
    But if I use this code to reload a gui It's work any help?
     
  2. Offline

    timtower Ninja on the waves Moderator

  3. Offline

    Cri16228

    Code:
    public class MessagesFile {
    
        public static FileConfiguration messages = null;
        public static File messagesFile = null;
    
        static Plugin pl = Bukkit.getPluginManager().getPlugin("TestPlugin");
    
        public static void createMessagesFile() {
            messagesFile = new File(pl.getDataFolder(), "messages.yml");
            if (!messagesFile.exists()) {
                messagesFile.getParentFile().mkdirs();
                pl.saveResource("messages.yml", false);
            }
            messages = new YamlConfiguration();
            try {
                messages.load(messagesFile);
            } catch (IOException | InvalidConfigurationException e) {
                System.out.println("Errore: " + e);
            }
        }
    
        public static void deleteMessagesFile() {
            messagesFile = new File(pl.getDataFolder(), "messages.yml");
            if (messagesFile.exists()) {
                messagesFile.delete();
            }
        }
    
        public static void saveMessagesFile() {
            try {
                messages.save(messagesFile);
            } catch (Exception e) {
            }
        }
    
        public final static void reloadMessagesFile() {
            if (messagesFile == null) {
                messagesFile = new File(pl.getDataFolder(), "messages.yml");
            }
            messages = YamlConfiguration.loadConfiguration(messagesFile);
    
            InputStream messagesStream = pl.getResource("messages.yml");
            if (messagesStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(messagesStream);
                messages.setDefaults(defConfig);
            }
        }
    
        public static FileConfiguration getMessages() {
    
            return messages;
        }
    }
    Q: Why you use
    Code:
    static Plugin pl = Bukkit.getPluginManager().getPlugin("TestPlugin");
    ?
    A: Because
    Code:
    Main.getInstance().getDataFolder()
    not work!
     
  4. Offline

    timtower Ninja on the waves Moderator

    @Cri16228 Remove all static.
    Use it as an instance that needs to be created in the onEnable.
    Call order is important, the getPlugin is high likely to give the wrong value as well.
     
  5. Offline

    Cri16228

    Ok, I'll remove all
    What do you mean?
     
  6. Offline

    timtower Ninja on the waves Moderator

    Every static variable initialization gets called before the onEnable, plugin might not even be loaded yet.
     
  7. Offline

    Cri16228

    And the "static" can give problem when I reload it from config? I mean using a plugin command not a /reload command
     
  8. Offline

    timtower Ninja on the waves Moderator

    Yes, it can cause issues when used like this.
     
  9. Offline

    Cri16228

    And why " Main.getInstance().getDataFolder() " not work?
    In my Main:
    Code:
    public static Main instance;
    
    public void onEnable()
      {
        DisableFly();
        MessagesFile.createMessagesFile();
        MessagesFile.saveMessagesFile();
        RegisterEvents();
        RegisterCommands();
        instance = this;
        this.messagesfl = new MessagesFile();
      }
     
      public static Main getInstance() {
        return instance;
      }
     
  10. Offline

    timtower Ninja on the waves Moderator

    @Cri16228 You never need static, please don't use it and use constructors instead.
    And what isn't working about it? You don't give anything to work with, might be called way before the onEnable gets called.
     
  11. Offline

    Cri16228

    It gives me an error during the onEnable on "MessagesFile.createMessagesFile();" if I use " Main.getInstance().getDataFolder() "
    P.s. I don't have removed yet the "static" because now I can't
     
  12. Offline

    timtower Ninja on the waves Moderator

    Again: code order.
     
  13. Offline

    Cri16228

    upload_2018-3-13_21-54-0.png
     
  14. Offline

    timtower Ninja on the waves Moderator

    @Cri16228 You need to modify more for it to work.
    Add a constructor to the MessageFile class that takes the main class.
     
  15. Offline

    Cri16228

    Like:
    Code:
    public MessagesFile(Main main){
           
        }
     
  16. Offline

    timtower Ninja on the waves Moderator

  17. Offline

    Cri16228

    And after?
     
  18. Offline

    timtower Ninja on the waves Moderator

    @Cri16228 Then you set all variables and you load the file.
     
Thread Status:
Not open for further replies.

Share This Page