Solved Adding Lines to Configuration file

Discussion in 'Plugin Development' started by baugh70, Aug 26, 2014.

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

    baugh70

    Hello all,

    I am currently writing a plugin that has an economy aspect. The main goal of the plugin is to give players "tokens" when they kill a mob and such. But because I do not know who the players are before they join the server, I need to update the config once they join. So I am unsure as to how I would add a full line with a key and value once the configuration has been created. I currently have this, which doesn't work. I was wondering if there was any way to go about this or if I needed to create a custom config per person.

    Code:java
    1. Player player = event.getPlayer();
    2. event.getPlayer().sendMessage(player.getUniqueId().toString());
    3.  
    4. if(plugin.getConfig().contains(player.getUniqueId().toString())){
    5. plugin.getConfig().addDefault(player.getUniqueId().toString(), 15);
    6. plugin.saveConfig();
    7. plugin.getConfig().options().copyDefaults(true);
    8. }
    9. }


    Thank you all in advanced!

    - Baugh70
     
  2. Offline

    _LB

  3. Offline

    baugh70

    _LB Okay thanks. Thought I might have to do that but I wasn't sure. Thanks for your help.

    After trying to make separate configs I made this in my main class:

    Code:java
    1. package us.legioncraft.tokens;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStreamReader;
    6. import java.io.Reader;
    7. import java.io.UnsupportedEncodingException;
    8. import java.util.UUID;
    9. import java.util.logging.Level;
    10.  
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. /**
    16. *
    17. * @author TheMindWreck
    18. * @version 1.0
    19. *
    20. */
    21.  
    22. public class TokensAPI extends JavaPlugin{
    23. private FileConfiguration customConfig = null;
    24. private File customConfigFile = null;
    25.  
    26. /**
    27.   * Standard plugin startup method
    28.   * Loads config. Do not touch. Do it in your own code by using the plugin variable.
    29.   */
    30. public void onEnable(){
    31. new Token(this);
    32. new TokenPlayer(this);
    33. }
    34.  
    35. public void reloadCustomConfig(UUID uuid) {
    36. if (customConfigFile == null) {
    37. customConfigFile = new File(getDataFolder(), uuid.toString() + ".yml");
    38. }
    39. customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
    40.  
    41. // Look for defaults in the jar
    42. Reader defConfigStream;
    43. try {
    44. defConfigStream = new InputStreamReader(this.getResource(uuid.toString() + ".yml"), "UTF8");
    45. if (defConfigStream != null) {
    46. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    47. customConfig.setDefaults(defConfig);
    48. }
    49. e.printStackTrace();
    50. }
    51. }
    52.  
    53. public FileConfiguration getCustomConfig(UUID uuid) {
    54. if (customConfig == null) {
    55. reloadCustomConfig(uuid);
    56. }
    57. return customConfig;
    58. }
    59.  
    60. public void saveCustomConfig(UUID uuid) {
    61. if (customConfig == null || customConfigFile == null) {
    62. return;
    63. }
    64. try {
    65. getCustomConfig(uuid).save(customConfigFile);
    66. } catch (IOException ex) {
    67. getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
    68. }
    69. }
    70.  
    71. public void saveDefaultConfig(UUID uuid) {
    72. if (customConfigFile == null) {
    73. customConfigFile = new File(getDataFolder(), uuid.toString() + ".yml");
    74. }
    75. if (!customConfigFile.exists()) {
    76. saveResource(uuid.toString() + ".yml", false);
    77. }
    78. }
    79.  
    80. }
    81.  


    But there is something with line 44 or the line:

    Code:java
    1. defConfigStream = new InputStreamReader(this.getResource(uuid.toString() + ".yml"), "UTF8");


    The error in the plugin was this : http://pastebin.com/RPKS9Lwf

    If anyone can help me that would be great that would be fantastic.

    Thanks in advanced,
    Baugh70

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page