How to create and use a custom config

Discussion in 'Plugin Development' started by gabrielmaennl555, Jan 27, 2015.

Thread Status:
Not open for further replies.
  1. Hello, Im trying to make a config file where it will hold users and their stats,
    Example:

    Code:
    Players.yml
    
    (in players.yml)
    
    (Player Username):
    Coins: (amount)
    etc.
    I know how to create a default config, but not custom ones.
    So how would I do something like this? Thanks!
     
  2. Offline

    Konato_K

  3. Offline

    teej107

    That sounds like a superb Google search query!
     
    Datdenkikniet likes this.
  4. Offline

    gal0511Dev

    @gabrielmaennl555
    Spoonfeeding but...
    Code:
    public class Main extends JavaPlugin implements Listener{
      
        static File coinsFile;
        static FileConfiguration coins;
      
        public void onEnable() {
          
            coinsFile = new File(getDataFolder(), "coins.yml");
            coins = YamlConfiguration.loadConfiguration(coinsFile);
            coins.options().copyDefaults(true);
            saveCoins();
        }
      
        public static void saveCoins(){
            try {
            coins.save(coinsFile);
            } catch (Exception e) {
            }
            }
      
        public static Integer getCoins(Player p){
            return coins.getInt(p.getName(), 0);
        }
      
        public static void addCoins(int amount, Player p){
            int add = coins.getInt(p.getName(), 0 + amount);
            coins.set(p.getName(), add);
            saveCoins();
        }
      
        public static void removeCoins(int amount, Player p){
            int add = coins.getInt(p.getName(), amount - 0);
            coins.set(p.getName(), add);
            saveCoins();
        }
      
        @EventHandler
        public void PlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
          
            if(!(coins.contains(p.getName()))){
                coins.set(p.getName(), 0);
                saveCoins();
            }
        }
     
  5. Offline

    teej107

    Spoonfed code is one of the lowest quality of codes. Your code has needless static and a Java Naming Convention error.
     
    Konato_K, Datdenkikniet and TGRHavoc like this.
  6. Offline

    TGRHavoc

    That indentation to though.

    If you're going to spoon-feed code at least indent it correctly and comment it out so that the dumb-asses people copying it knows what it going on.
     
  7. Code:java
    1.  
    2. //Ill show ya how to create the file and load his config
    3. //First we create a file
    4. File file;
    5. //This will be the config
    6. FileConfiguration config;
    7. //For me the best way if you wanna set something default is create a "Empty File" in the default package called something like "myfile.yml"
    8. public void createFile() {
    9. //Now we have to assign a path to the file
    10. file = new File(getDataFolder()+File.separator+"myfile.yml");
    11. //Now check if the file exist
    12. if(!file.exists() {
    13. //If the file don't exist we create a new one
    14. saveResource("myfile.yml", false);
    15. }
    16. //To load the config we have to assign the File to the FileConfiguration
    17. config = YamlConfiguration.loadConfiguration(file);
    18. //Now we can easy get the config with
    19. config.set(), config.getString() ....
    20. config == getConfig();
    21. }
    22. //If you're doing this in another class you have to use the instance from the main class
    23.  

    I know my english is not good, but i tried to explain it the best what i can.
     
    Last edited: Jan 28, 2015
  8. Offline

    gal0511Dev

    @teej107 wow your a bit mad but i use the static to to access the coins from other plugins...
     
  9. Offline

    mythbusterma

    @gal0511Dev

    That's even worse. Learn what an "accessor method" is. "static" is not a catch all access modifier, that's not at all what it's meant for, so stop using it like that.
     
    Konato_K, teej107 and TGRHavoc like this.
  10. Offline

    teej107

    Other plugins? Or do you mean other classes? Static isn't an access everywhere modifier. That is what public is.
     
    Konato_K likes this.
  11. Offline

    _Filip

    teej107 likes this.
  12. Offline

    teej107

  13. Offline

    Konato_K

    @gal0511Dev Are you trying to tell me I can't access other plugin data without static? D: Heck I must be hacking into java because I can :/
     
    CraftCreeper6 likes this.
  14. Offline

    CraftCreeper6

    @gal0511Dev
    What!?!? There are no more instances of classes!?!?
     
  15. Offline

    1Rogue

    I don't understand either of these posts. @gal0511Dev didn't say static is required for plugin access, and static doesn't mean instance-less.
     
    gal0511Dev likes this.
  16. Offline

    Konato_K


    @1Rogue He said he uses static to access it from other plugins
     
  17. Offline

    1Rogue

    That seems vastly different than:
     
  18. Offline

    ColonelHedgehog

  19. Offline

    Konato_K

    @1Rogue Seeing that's the only excuse he said for static I assumed he meant that static is necessary to access the data from other plugins.
     
  20. Offline

    mythbusterma

    No, static doesn't mean instance-less, unless you're galodev, in which case, it does. Look at the god awful code he posted.
     
Thread Status:
Not open for further replies.

Share This Page