Solved File change on player join event

Discussion in 'Plugin Development' started by VenamousV, Feb 3, 2014.

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

    VenamousV

    How would I go about making it so my plugin could add something into the config when a player logs in?
     
  2. Offline

    Whomp54

    Are you wanting to add the players names into the config? If not, what are you tring to do?
     
  3. Offline

    VenamousV

    Yeah, I want to add their name and the rest I can figure from that. Whomp54
     
  4. Offline

    Pizza371

    VenamousV using default config i assume?
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    //code
    getConfig().set(e.getPlayer().getName(), infos); //assuming in main class
    }

    Otherwise I dont understand what you mean?
     
  5. Offline

    Whomp54

    First, what you want to do is make a config. If you have already done so then good you can move on.

    You need to create a parent directory in the config. Ex: "Players:" (Make sure to have the colon)

    Then on to adding the players. You would want a PlayerJoinEvent so it logs the player every time they join.


    Code:java
    1. @EventHandler
    2.  
    3. public void onJoin(PlayerJoinEvent e){
    4.  
    5. Player p = (Player) e.getPlayer();
    6.  
    7. String name = p.getName();
    8.  
    9. List<String> players = getConfig().getStringList("Players");
    10. players.add(p.getName());
    11. getConfig().set("Players", players);
    12.  
    13. saveConfig();
    14.  
    15. }


    You can then see the players in the config. If you want to clear the list, do:

    Code:java
    1. getConfig().set("Players", null);


    Edit: Sorry forgot to mention, if you would like to get all the players in that list and do something to them, do:

    Code:java
    1. for(String playerstodosomethingto : getConfig().getStringList("Players")){
    2. //code here
    3. }


    I'm pretty sure this works, but someone correct me if this doesn't it has not been tested by me.
     
  6. Offline

    VenamousV

    I am using custom configs Pizza371 but it's the same thing I presume. Thank you both. Whomp54
     
  7. Offline

    Whomp54

  8. Offline

    VenamousV

    Okay well something went wrong, this is my code up to the config bit. The bit you gave me is at the bottom.
    Code:java
    1. public class PlayerFiles extends JavaPlugin{
    2. PluginDescriptionFile pdfFile = this.getDescription();
    3. public PlayerFiles plugin;
    4. public final Logger logger = Logger.getLogger("Minecraft");
    5.  
    6. //Players Config
    7. public YamlConfiguration playerCfg;
    8. public File playerCfgFile;
    9. public void reloadplayerCfg() {
    10. playerCfgFile = new File(getDataFolder(), "Players/Players.yml");
    11. playerCfg = YamlConfiguration.loadConfiguration(playerCfgFile);
    12. // Look for defaults in the jar
    13. InputStream defConfigStream = this.getResource("Players/Players.yml");
    14. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    15. playerCfg.setDefaults(defConfig);
    16. }
    17. public FileConfiguration getplayerCfgFile() {
    18. this.reloadplayerCfg();
    19. return playerCfg;
    20. }
    21. public void savePlayersDefaultCfg() {
    22. playerCfgFile = new File(getDataFolder(), "Players/Players.yml");
    23. if (!playerCfgFile.exists()) {
    24. this.plugin.saveResource("Players/Players.yml", false);
    25. }
    26. }
    27. public void saveplayerCfg() {
    28. try {
    29. playerCfg.save(playerCfgFile);
    30. }catch (IOException ex) {
    31. // will be executed if the file could not be saved
    32. ex.printStackTrace();
    33. }
    34. }
    35.  
    36. @Override
    37. public void onEnable() {
    38. PluginDescriptionFile pdfFile = this.getDescription();
    39. PluginManager pm = getServer().getPluginManager();
    40. this.logger.info("[" + pdfFile.getName() + " Version-" + pdfFile.getVersion() + "] Has been Enabled!");
    41. //Default Config
    42. getConfig().options().copyDefaults(true);
    43. saveConfig();
    44. //Players Config
    45. playerCfgFile = new File(getDataFolder(), "Players/Players.yml");
    46. playerCfg = YamlConfiguration.loadConfiguration(playerCfgFile);
    47. getplayerCfgFile().options().copyDefaults(true);
    48. saveplayerCfg();
    49. }
    50.  
    51. @Override
    52. public void onDisable(){
    53. PluginDescriptionFile pdfFile = this.getDescription();
    54. this.logger.info("[" + pdfFile.getName() + " Version-" + pdfFile.getVersion() + "] Has been disabled!");
    55. }
    56.  
    57. @EventHandler
    58. public void onJoin(PlayerJoinEvent e) {
    59. Player p = (Player) e.getPlayer();
    60.  
    61. String name = p.getName();
    62.  
    63. List<String> players = getplayerCfgFile().getStringList("Players");
    64. players.add(p.getName());
    65. getplayerCfgFile().set("Players", players);
    66. saveplayerCfg();
    67.  
    68. }

    And there are no errors in the file or the console, but after logging in I check the file and it still didn't add anything. Any suggestions? Whomp54
     
  9. Offline

    Whomp54

    VenamousV
    I think I know the problem. If you only have one class, do this in onEnable:


    pm.registerEvents(this, this);

    This registers your events so that the server can read them! ;D
     
  10. Offline

    VenamousV

    Perfect, everything works now! Whomp54
     
  11. Offline

    Whomp54


    Great! :D Hope it goes good! Have a great day!
     
    immensebuttpain likes this.
Thread Status:
Not open for further replies.

Share This Page