Solved Get value from custom config.

Discussion in 'Plugin Development' started by Doubtstand, Sep 14, 2015.

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

    Doubtstand

    Hello everyone,

    I succesfully made a method that creates a playerfile whenever a player joins (And the file doesn't already exist), but my problem right now is that I don't know how to get the value's out of my custom files.
    Can anyone explain how I can get these value's?

    Create Playerfile method:

    Code:java
    1.  
    2. public void firstJoin(PlayerJoinEvent e){
    3.  
    4. Player player = e.getPlayer();
    5.  
    6. FileConfiguration playerfile = null;
    7. File file = new File("plugins"+File.separator+plugin.getDescription().getName()+File.separator+"users"+File.separator+e.getPlayer().getUniqueId()+".yml");
    8.  
    9. if(!file.exists()){
    10. try {
    11. file.createNewFile();
    12. } catch (IOException e1) {
    13. e1.printStackTrace();
    14. }
    15.  
    16. playerfile = YamlConfiguration.loadConfiguration(file);
    17.  
    18. playerfile.set("Player.Name", player.getName());
    19. playerfile.set("Player.Rank", "member");
    20.  
    21. try {
    22. playerfile.save(file);
    23. } catch (IOException e1) {
    24. e1.printStackTrace();
    25. }
    26. }
    27. }
    28.  


    Class where I need the value's:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerChat(AsyncPlayerChatEvent e){
    4.  
    5. FileConfiguration playerfile = null;
    6. File file = new File("plugins"+File.separator+plugin.getDescription().getName()+File.separator+"users"+File.separator+e.getPlayer().getUniqueId()+".yml");
    7.  
    8. playerfile = YamlConfiguration.loadConfiguration(file);
    9.  
    10. if(???????){
    11. e.setFormat(ChatColor.YELLOW + "[Member] " + ChatColor.GRAY + e.getPlayer().getName() + ChatColor.RED + " » " + ChatColor.GRAY + e.getMessage());
    12. } else{
    13. e.getPlayer().sendMessage("It still doesn't work.");
    14. }
    15. }
    16.  


    Hope someone can help,
    Sten.
     
  2. Offline

    FabeGabeMC

    @Doubtstand Retrieve the values just as you would with a normal config.
    Code:
    FileConfiguration config = getConfig();
    would work the same as
    Code:
    FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    since the plugin config is a FileConfiguration.
    EDIT: Source
     
  3. Offline

    Oxyorum

    @Doubtstand Use one of the various getters that FileConfiguration class offers for retrieving the data. Just find the right method and pass the desired path to it as an argument. For strings, you want FileConfiguration#getString(path), and its similar for other objects.

    Link for you: Configuration API
     
  4. Offline

    Doubtstand

    When using the following code, it still gives this error: http://pastebin.com/QtUxbFSt
    It gives an error on line 25: File file = new File(etc)

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerChat(AsyncPlayerChatEvent e){
    4.  
    5. File file = new File("plugins"+File.separator+plugin.getDescription().getName()+File.separator+"users"+File.separator+e.getPlayer().getUniqueId()+".yml");
    6. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    7.  
    8. if(config.getString("Player.Name") == "member"){
    9. e.setFormat(ChatColor.YELLOW + "[Member] " + ChatColor.GRAY + e.getPlayer().getName() + ChatColor.RED + " » " + ChatColor.GRAY + e.getMessage());
    10. } else{
    11. e.getPlayer().sendMessage("It still doesn't work.");
    12. }
    13. }
    14.  


    EDIT: It also doesn't say the else message: it still doesn't work.
     
  5. Offline

    Oxyorum

    @Doubtstand
    1. Check if the file exists before using it, check that the plugin is not null, check that the directory your are writing the file to exists.
    2. You do not compare strings with ==. Use the equals or equalsIgnoreCase methods instead.
     
  6. Offline

    Zombie_Striker

    @Doubtstand
    1. Load the file once for the player, not Every Single Time the player sends a message.
    2. Do tell me (AND have your plugin print out ) what the file's path will be?
     
  7. Offline

    Doubtstand

    Plugins folder > Datafolder > users > <uuid>.yml
     
  8. Offline

    Zombie_Striker

    @Doubtstand
    And what is the string IT gives...

    ps, you get the datafolder by doing JavaPlugin.getDataFodler();
     
  9. Offline

    Doubtstand

    The file will exist, because it is generated when a player joins with !file.exist.

    It will return member in the default case ( sorry if im unclear, my english isn't that great ).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  10. Offline

    Zombie_Striker

    @Doubtstand
    Please post EXACTLY what it prints out. This is what you should use to actually see EXACTLY what the string is
    Code:
    System.out.println(THAT STRING)
     
  11. Offline

    Oxyorum

    @Doubtstand Better to be safe than sorry. Also, are you sure that the plugin variable is not null?
     
  12. Offline

    Doubtstand

    I found the problem, it was the simple thing that I was using plugin.getDescription().getName() to get the datafolder instead of plugin.getDataFolder(). Thanks you guys for your help anyways ;d
     
Thread Status:
Not open for further replies.

Share This Page