Solved PlayerName.yml Getting

Discussion in 'Plugin Development' started by BurnerDiamond, Jan 28, 2015.

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

    BurnerDiamond

    How would I get the yml with the players name?

    How would I get the yml with the players name and set ints in there?

    I know how to do it with regular config.yml but not this one.

    Here is my code:

    Code:java
    1.  
    2. package me.bd.events;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6.  
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerLoginEvent;
    10.  
    11. public class CreateFileLoginEvent implements Listener {
    12.  
    13. @EventHandler
    14. public void onJoin(PlayerLoginEvent e){
    15. File f = new File("plugins/KitPVP/" + e.getPlayer().getName() + ".yml");
    16. try {
    17. f.createNewFile();
    18. } catch (IOException e1) {
    19. e1.printStackTrace();
    20. }
    21. }
    22.  
    23. }
    24. and the Main:
    25.  
    26. Code (text):
    27. package me.bd.kitpvp;
    28.  
    29. import me.bd.events.CreateFileLoginEvent;
    30. import me.bd.events.LoginEvent;
    31. import me.bd.events.RespawnEvent;
    32. import me.bd.kits.Menu;
    33. import me.bd.tank.Smash;
    34. import me.bd.tank.Tank;
    35.  
    36. import org.bukkit.plugin.java.JavaPlugin;
    37.  
    38. public class Main extends JavaPlugin {
    39.  
    40. public void onEnable() {
    41.  
    42. getServer().getPluginManager().registerEvents(new Menu(), this);
    43. getServer().getPluginManager().registerEvents(new Tank(), this);
    44. getServer().getPluginManager().registerEvents(new Menu(), this);
    45. getServer().getPluginManager().registerEvents(new LoginEvent(), this);
    46. getServer().getPluginManager().registerEvents(new RespawnEvent(), this);
    47. getServer().getPluginManager().registerEvents(new CreateFileLoginEvent(), this);
    48. getServer().getPluginManager().registerEvents(new Smash(), this);
    49.  
    50. }
     
    Last edited by a moderator: Jan 28, 2015
  2. 1) use [syntax =java] [/syntax ]
    2) getDataFolder()+File.separator+player.getUniqueId()+".yml";
     
  3. Offline

    BurnerDiamond

    I'm not exactly sure on how to do this.

    I've tried everything but failed.

    The point is to make an onLoginEvent and then make a config for it which I can save and add things too.

    I have no idea on how to do this.
    Code:java
    1.  
    2. @EventHandler
    3. public void onJoin(PlayerLoginEvent e) {
    4. if(new File("plugins" + File.separator + "KitPVP" + File.separator + e.getPlayer().getName() + ".yml").isFile()) {
    5.  
    6. }else{
    7.  
    8. File ConfigYml = new File("plugins" + File.separator + "KitPVP" + File.separator + e.getPlayer().getName() + ".yml");
    9. FileConfiguration rConfig = YamlConfiguration.loadConfiguration(ConfigYml);
    10.  
    11. try {
    12. rConfig.save(ConfigYml);
    13. } catch (IOException e1) {
    14.  
    15. }
    16. }
    17. }
    18. }


    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 28, 2015
  4. Offline

    Experminator

    @BurnerDiamond To create a file in the plugin directory use (You are near the solution):
    Code:
     File f = new File(getDataFolder(), e.getPlayer().getName() + ".yml");
    
            try {
                f.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
    That creates a file in the plugin directory, with name: BurnerDiamond.yml. (If it's in another class then the main class, make a Plugin instance method. Like getPlugin().)

    To check if a file exists:
    Code:
    File f = new File(getDataFolder(), "filename.yml");
    
    if(f.exists()){
        // If it exists, it return true, else it return false.
    }
    To load configuration to a file:
    Code:
    FileConfiguration config = null; // This is not practical.
    File file = new File(getDataFolder(), "filename.yml");
    
    config = YamlConiguration.loadConfiguration(file);
    Note: All code above is not Exception-proof.
    Load configuration: config can give a NullPointerException.

    Be secure at that to add some if and/or try&catch statements.


    I hope this is enough information for this subject.
    If not, you can always tag me for help!
     
    Last edited: Jan 28, 2015
  5. Offline

    BurnerDiamond

    @Experminator

    I've spent hours!!!!!!

    I've got a couple questions.

    1. How would I make a getPlugin instance?

    2. How would I get the configuration from in class to another?

    THANKS SO MUCH,
     
    Experminator likes this.
  6. Offline

    Experminator

    @BurnerDiamond Plugin instance method is something like this:
    Code:
    public static Plugin getPlugin(){
        return Bukkit.getPluginManager().getPlugin("NameOfPlugin");
    }
    Note: This must be in the Main class, it must be static, and 'NameOfPlugin' must changed to the plugin name (Like your Project Name).

    Other way is to use (This can give a NullPointerException, when it's not intialized):
    Code:
    private static Plugin p;
    
    public static Plugin getPlugin(){
        return p;
    }
    @BurnerDiamond Do you want the Plugin default configuration method or a written method your own (That takes more work)?

    With default configuration method:
    Code:
    public static FileConfiguration getConfiguration(){
        return getConfig();
    }
    Note 1: The name of the method may not be getConfig() because it's already exists in JavaPlugin.
    Note 2: The method must be in the Main class.

    I think this is enough for you. And i don't want to write the other method :p

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 28, 2015
  7. Offline

    timtower Administrator Administrator Moderator

    @Experminator Please don't advertise static stuff, it is bad practice to use.
    Use constructors instead
     
    Experminator and bwfcwalshy like this.
  8. Offline

    FerusGrim

    To expand onto @timtower's statement:

    Code:java
    1. public class SomePlugin {
    2.  
    3. @Override
    4. public void onEnable() {
    5. new SomeOtherClass(this).someMethod();
    6. // Note - You may wish to store 'SomeOtherClass'; this is just an example.
    7. }
    8. }
    9.  
    10. public class SomeOtherClass {
    11.  
    12. private final SomePlugin plugin;
    13.  
    14. public SomeOtherClass(SomePlugin plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. public void someMethod() {
    19. for (Player player : this.plugin.getServer().getOnlinePlayers()) {
    20. player.getInventory().addItem(new ItemStack(Material.COAL));
    21. player.sendMessage("You've been naughty~!");
    22. }
    23. }
    24. }
    25.  
     
    timtower likes this.
  9. Offline

    Experminator

    @timtower It works, but i know it's bad practice.
     
  10. Offline

    FerusGrim

    Then why suggest it?
     
    timtower likes this.
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    Experminator

    @FerusGrim Because it's a little idea to solve their problem.
    In this case is the priority to give functionality, and not to solve bugs.

    But if you don't agree with it. I leaving it out.
    Then is my help useless. (That's not really bad for me.)
     
  13. Offline

    timtower Administrator Administrator Moderator

    @Experminator Better to learn once good then to learn bad and need to learn again.
     
    TGRHavoc, Experminator and FerusGrim like this.
  14. Offline

    Experminator

  15. Offline

    BurnerDiamond

    @Experminator

    Is this supposed to work?

    Main:

    Code:
    package me.bd.kitpvp;
    
    import me.bd.events.LoginEvent;
    import me.bd.events.RespawnEvent;
    import me.bd.kits.Menu;
    import me.bd.tank.Smash;
    import me.bd.tank.Tank;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
        public static Main plugin;
      
        public void onEnable() {
          
            getServer().getPluginManager().registerEvents(new Menu(), this);
            getServer().getPluginManager().registerEvents(new Tank(), this);
            getServer().getPluginManager().registerEvents(new LoginEvent(), this);
            getServer().getPluginManager().registerEvents(new RespawnEvent(), this);
            getServer().getPluginManager().registerEvents(new Smash(), this);
      
            }
      
        public void onDisable() {
          
        }
      
        public static Plugin getPlugin() {
            return Bukkit.getPluginManager().getPlugin("KitPVP");
      
        }
    
    }
    
    Login Event:

    Code:
    package me.bd.events;
    
    import java.io.File;
    import java.io.IOException;
    
    import me.bd.kitpvp.Main;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class ConfigLoginEvent implements Listener {
       
        public void onPlayerLogin(PlayerLoginEvent e) {
           
            File f = new File(Main.getPlugin().getDataFolder(), e.getPlayer().getUniqueId() + ".yml");
           
            if(f.exists()) {
                return;
                }
            try {
                f.createNewFile(); }
            catch (IOException e1) {
                e1.printStackTrace();
               
            }
           
            @SuppressWarnings("unused")
            FileConfiguration newConfig = null;
            File file = new File(Main.getPlugin().getDataFolder(), "filename.yml");
            newConfig = YamlConfiguration.loadConfiguration(file);
           
        }
    
    }
    
     
  16. Offline

    Experminator

    @BurnerDiamond
    You must change save instead loading i think?
    Then you must not call 'loadConfiguration()'.
    You must use:
    Code:
    newConfig.save(f);
    The CODE should work, but it's better to use this:
    Code:
     if(!f.exists()) {
          try {
                f.createNewFile();
          } catch (IOException e1) {
                e1.printStackTrace();
          }
      }
    ----------------------------------------
    Even is it better to add this in a Try & Catch (Not required, but IF configuration loading fails, it throws a NullPointerException):
    Code:
     @SuppressWarnings("unused")
            FileConfiguration newConfig = null;
            File file = new File(Main.getPlugin().getDataFolder(), "filename.yml");
            newConfig = YamlConfiguration.loadConfiguration(file);
    And you know you can only get access of 'newConfig' in the PlayerLoginEvent?

    If you make a variable called 'newConfig' outside the event, you can get access everywhere (In the class).
    And the you can instantiating the variable in the event (Like you already did.)

    ----------------------------------------
    Maybe you don't know it, but you have already 'newConfig' instantiating (But on the wrong place).

    This is instantiating of a variable:
    Code:
    newConfig = YamlConfiguration.loadConfiguration(file);
     
    Last edited: Jan 28, 2015
  17. Offline

    BurnerDiamond

  18. Offline

    Experminator

  19. Offline

    BurnerDiamond

    @Experminator

    It's the same as the one above!

     
  20. Offline

    Experminator

    @BurnerDiamond Wait... Then you don't have changed anything .... And i given instructions for that...
     
  21. Offline

    BurnerDiamond

    @Experminator

    Where would I place this?

    Code:
    FileConfiguration newConfig = null;
            File file = new File(Main.getPlugin().getDataFolder(), "filename.yml");
            try {
                newConfig.save(f);
            } catch (IOException e1) {
               
                e1.printStackTrace();
            }
    Thanks for the help your giving me!
     
  22. Offline

    SuperOriginal

    You never added the @EventHandler notation
     
    Experminator likes this.
  23. Offline

    Experminator

  24. Offline

    BurnerDiamond

  25. Offline

    Experminator

    @BurnerDiamond
    Code:
     File file = new File(Main.getPlugin().getDataFolder(), "filename.yml");
            try {
                newConfig.save(f);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
    That must be at the place you want to save it. I believe at the bottom of PlayerLoginEvent.
     
  26. Offline

    BurnerDiamond

    @Experminator

    This is what I did.

    Code:
    package me.bd.events;
    
    import java.io.File;
    import java.io.IOException;
    
    import me.bd.kitpvp.Main;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class ConfigLoginEvent implements Listener {
      
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent e) {
          
            File f = new File(Main.getPlugin().getDataFolder(), e.getPlayer().getUniqueId() + ".yml");
          
            if(f.exists()) {
                return;
                }
            try {
                f.createNewFile(); }
            catch (IOException e1) {
                e1.printStackTrace();
              
            }
          
            FileConfiguration newConfig = null;
            File file = new File(Main.getPlugin().getDataFolder(), "filename.yml");
            try {
                newConfig.save(f);
            } catch (IOException e1) {
              
                e1.printStackTrace();
            }
          
        }
    
    }
    
     
  27. Offline

    Experminator

    @BurnerDiamond Use this (Sorry, it's spoonfeed):
    Code:
    public class SomeClass implements Listener {
      
        FileConfiguration config;
        File cFile;
    
        @EventHandler
        public void onLogin(PlayerLoginEvent e){
            if(cFile == null){ // Initialize when not instantiated.
                cFile = new File(Main.getPlugin().getDataFolder(), "config.yml");
            }
    
           // Do Stuff With Config. Like adding.
          config.set("users", e.getPlayer().getUniqueId());
         // This add a player's uuid to the config section: users.
    
           config.save(cFile); // Save Config to cFile.
       }
    }
     
  28. Offline

    BurnerDiamond

    @Experminator

    I don't use spoon feed. I'm going to study this code until I understand it!

    @Experminator

    But this is for a config.yml.

    How would I get it from a PlayerName.yml?

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

    TGRHavoc

    You'd replace "config.yml" with "Player#getName() + ".yml" "

    cFile = new File(Main.getPlugin().getDataFolder(), "config.yml");<<<
     
    Experminator likes this.
  30. Offline

    BurnerDiamond

    And what would i place in my main file?
     
Thread Status:
Not open for further replies.

Share This Page