messages.yml not reload from disk

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

Thread Status:
Not open for further replies.
  1. 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. Online

    timtower Administrator Administrator Moderator

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

    timtower Administrator Administrator 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. Ok, I'll remove all
    What do you mean?
     
  6. Online

    timtower Administrator Administrator Moderator

    Every static variable initialization gets called before the onEnable, plugin might not even be loaded yet.
     
  7. And the "static" can give problem when I reload it from config? I mean using a plugin command not a /reload command
     
  8. Online

    timtower Administrator Administrator Moderator

    Yes, it can cause issues when used like this.
     
  9. 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. Online

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

    timtower Administrator Administrator Moderator

    Again: code order.
     
  13. Online

    timtower Administrator Administrator Moderator

    @Cri16228 You need to modify more for it to work.
    Add a constructor to the MessageFile class that takes the main class.
     
  14. Like:
    Code:
    public MessagesFile(Main main){
           
        }
     
  15. Online

    timtower Administrator Administrator Moderator

  16. And after?
     
  17. Online

    timtower Administrator Administrator Moderator

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

Share This Page