How can i create a messages.yml file?

Discussion in 'Plugin Development' started by EyesPlay, Sep 5, 2017.

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

    AdamDev

    @EyesPlay
    So, now back to the Player data. So your config would start out as something like this:
    Code:
    Players: {}
    Right?

    Then when a player joins it adds them to the config:
    Code:
    Players: {
         AdamDev:
    }
    Then it gives them the map.
    Am I right?
     
  2. Offline

    EyesPlay

    no... is little different... the plugin put all the players data in a different-per-player file...

    but... the problem is... i can't access to the messages.yml strings from other classes...
     
  3. Offline

    AdamDev

    @EyesPlay
    Then I suggest going back to my other post with the link to multiple configs. It will take time getting used to.
     
  4. Offline

    EyesPlay

    (my pc is broken... sorry for double post but it stop working...)
     
    Last edited: Sep 13, 2017
  5. Offline

    EyesPlay

    @AdamDev ehi! after many attempts I understand that the method used in your post is not good...
    i've this in the onEnable...
    Code:
            messages= new File("plugin/TwoStep/messages.yml");
            messagesConfig= YamlConfiguration.loadConfiguration(messages);
            messagesConfig.options().copyDefaults(true);
    how can i access to the messages from the other classes?
     
  6. Offline

    Caderape2

    @EyesPlay put the FileConfiguration as a field and pass your instance of your mainclass as parameter in the constructor of others classes
     
  7. Offline

    EyesPlay

    @Caderape2 how can i do this? i am new to develope and i'm a noob... sorry
     
  8. Offline

    Caderape2

    @EyesPlay
    Code:
    public class MainClass extends JavaPlugin{
    
        private int amount;
        private Test test;
      
    
        public void onEnable()
        {
            amount = 5;
          
            test = new Test(this);
          
            test.broadCastAmount();
        }
    
    
        public int getAmount() {
            return amount;
        }
    }
    
    Code:
    public class Test {
    
        private MainClass plugin;
      
        public Test(MainClass plugin)
        {
            this.plugin = plugin;
        }
      
      
        public void broadCastAmount()
        {
            Bukkit.broadcastMessage(plugin.getAmount()+"");
        }
    }
    
    If you still don't understand, do some research on google about instances and constructors.
     
  9. Offline

    AdamDev

    @Caderape2
    Nice stuff but there's a problem. He doesn't want a class to be his messages. He wants a different "config.yml".

    @EyesPlay
    I don't like doing this, but I guess I have to. Heres some code that worked for me with the link to multiple config files:

    Code:
    package me.hype.testing.createfile;
    
    import java.io.File;
    import java.io.IOException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class YamlHandler
    {
       static File createTempFile(String filename)                                                                
       {
    
           File c = new File(Core.getInstance().getDataFolder().getAbsolutePath());
           c.mkdir();
           File f= new File(Core.getInstance().getDataFolder().getAbsolutePath() + File.separator + filename);
    
           if(!f.exists())
           {
               try {
                   f.createNewFile();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }
           else
           {
               f.delete();
               try {
                   f.createNewFile();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }
           return f;
       }
       public static File createFile(String filename)                                                                  
       {
    
           File c = new File(Core.getInstance().getDataFolder().getAbsolutePath());
           c.mkdir();
           File f= new File(Core.getInstance().getDataFolder().getAbsolutePath() + File.separator + filename);
    
           if(!f.exists())
           {
               try {
                   f.createNewFile();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }
           return f;
       }
       public static FileConfiguration createYamlFile(File f)
       {
           FileConfiguration fc= YamlConfiguration.loadConfiguration(f);
    
           return fc;
       }
    
       public static void saveYamlFile(FileConfiguration c,File f)
       {
           try {
               c.save(f);
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
    
       }
    
    }
    That's the YamlHandler.

    Now to use it you would do something like:
    Code:
    package me.hype.testing.createfile;
    
    import java.io.File;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin implements Listener {
     
        private static Core plugin;
     
        public void onEnable() {
            plugin = this;
            File name = YamlHandler.createFile("name.yml");
            File name2 = YamlHandler.createFile("name2.yml");
            FileConfiguration nameconfig = YamlHandler.createYamlFile(name);
            FileConfiguration nameconfig2 = YamlHandler.createYamlFile(name2);
            nameconfig.createSection("Strings");
            nameconfig2.createSection("Strings2");
            YamlHandler.saveYamlFile(nameconfig, name);
            YamlHandler.saveYamlFile(nameconfig2, name2);
        }
        public void onDisable() {
            plugin = null;
        }
     
        public static Core getInstance() {
            return plugin;
        }
     
     
    }
    
    In your onEnable().
     
    Last edited by a moderator: Sep 24, 2017
  10. Offline

    EyesPlay

    @AdamDev that code is working! it creates the messages.yml file but... how can i access to this from the other classes? i can't
     
  11. Offline

    AdamDev

    @EyesPlay
    Look what I used to create the sections in the config:
    If you are using this to add / get stuff you would do:
    Code:
    // This is getting the mapmessage1 string from the config
    messages.getString("message.mapmessage1");
    messages.getString("message.mapmessage2");
    messages.getString("message.mapmessage3");
    // This is adding a mapmessage4 to the config
    message.set("message.mapmessage4", "blah");
    
    If this doesn't solve your problem then I don't know what will.
     
Thread Status:
Not open for further replies.

Share This Page