Get a CUSTOM config from another class

Discussion in 'Plugin Development' started by megasaad44, Aug 7, 2014.

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

    megasaad44

    I have a playerdatabase folder and when a player joins, the plugin creates a yml for that player. How would I get the config of that player with this from another class?
    Code:java
    1. public class PlayerDatabase implements Listener {
    2.  
    3. static HashMap<UUID, YamlConfiguration> yamlplayers = new HashMap<UUID, YamlConfiguration>();
    4.  
    5. @EventHandler
    6. public void onJoin(PlayerJoinEvent event) {
    7. File players = new File(Main.plugin.getDataFolder(), "players");
    8. players.mkdir();
    9. File playerfile = new File(players, event.getPlayer().getUniqueId() + ".yml");
    10. try {
    11. playerfile.createNewFile();
    12. } catch (IOException e) {
    13. Main.plugin.getLogger().severe("Failed to make a new player file. Sorry. :(");
    14. }
    15. YamlConfiguration config = YamlConfiguration.loadConfiguration(playerfile);
    16. yamlplayers.put(event.getPlayer().getUniqueId(), config);
    17. }
    18. }
     
  2. Offline

    xAstraah

    megasaad44,
    Code:java
    1. private PlayerDatabase pdb;
    2.  
    3. public ClassName(PlayerDatabase pdb) {
    4. this.pdb = pdb;
    5. }

    That will give you access to that class from any other class you should know what to do from there.
     
  3. Offline

    IkBenHarm

    megasaad44
    i recommend you make a "config" class and just do it like this:

    Code:java
    1. static File money = new File("plugins/HKingdoms/money.yml");
    2. static YamlConfiguration m = YamlConfiguration.loadConfiguration(money);
    3.  
    4. public void createConfig() {
    5. if (!money.exists()) {
    6. try {
    7. money.createNewFile();
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. }
    13.  
    14. public static YamlConfiguration getBankConfig(){
    15. return m;
    16. }
    17.  
    18. public static void saveConfig() {
    19. try {
    20. m.save(money);
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }


    from now on you can simply use (in this case) BankConfig.getBankConfig().SomeOtherMethod()
     
  4. Offline

    fireblast709

  5. Offline

    IkBenHarm

    fireblast709
    What do you mean by abuse? :3 I've never been to programming school or something so..

    Just found this was an easy way which worked :p
     
  6. Offline

    megasaad44

    IkBenHarm
    What is "m"?
    I doubt your thing supports multiple player database thingamabob ymls for each player. I can't say for sure though.
    fireblast709
    Any better solutions that can work on my thing?
     
  7. Offline

    fireblast709

    IkBenHarm static does not exist for convenience, and carries a lot of risks
    megasaad44 I would create a managing class (with a 'database file' per player, and methods to manipulate this data), create a Map in the main class that maps UUID to fhis managing class, and create a getter to get this instance from the main class. The rest is passing the instance of the main class like xAstraah mentioned.
     
  8. Offline

    IkBenHarm

    megasaad44
    ah right, I did not notice the per player config
     
  9. Offline

    megasaad44

    fireblast709 I don't think i understand. Could you maybe write a class that can get and set and save and all that and link to the player database? Would be nice. Because I've asked everyone i know and they are all stumped on this.
    Maybe stuff like p.getConfig.set(...). and p.getConfig.save etc... This is harder than it seems to be.
     
  10. Offline

    fireblast709

    megasaad44 like a YamlConfiguration object per player, and a file per player.
     
    hintss likes this.
Thread Status:
Not open for further replies.

Share This Page