Per Player Yaml Config Not Saving

Discussion in 'Plugin Development' started by GaminForDummys, Aug 20, 2013.

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

    GaminForDummys

    Hey guys, I'm trying to code ymls for each player. I get everything else working but they don't save. I can't figure out what I'm doing wrong. No error as well.

    Main Class:
    Code:java
    1. package gaminfordummys;
    2.  
    3. import gaminfordummys.cmd.*;
    4. import gaminfordummys.util.Economy;
    5. import gaminfordummys.util.PlayerFile;
    6.  
    7. import java.io.File;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.Material;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.ShapedRecipe;
    15. import org.bukkit.plugin.PluginManager;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class GaminForDummys extends JavaPlugin {
    19.  
    20. static GaminForDummys plugin;
    21.  
    22. final Logger log = Logger.getLogger("Minecraft");
    23.  
    24. public Economy eco = new Economy(this);
    25.  
    26. @Override
    27. public void onEnable() {
    28.  
    29. File pluginDir = new File("plugins/GaminForDummys/");
    30. if(!pluginDir.exists()){
    31. pluginDir.mkdir();
    32. log.info("[GaminForDummys] No Plugin Folder Found, Making A New One.");
    33. }
    34.  
    35. File usersDir = new File("plugins/GaminForDummys/UserData/");
    36. if(!usersDir.exists()){
    37. usersDir.mkdir();
    38. log.info("[GaminForDummys] No UserData Folder Found, Making A New One.");
    39. }
    40.  
    41. Economy.balance.clear();
    42.  
    43. getCommand("GM").setExecutor(new GM());
    44. getCommand("Money").setExecutor(new Money());
    45. getCommand("Spawn").setExecutor(new Spawn());
    46. //getCommand("Vanish").setExecutor(new Vanish());
    47. //getCommand("Jumpto").setExecutor(new Jumpto());
    48. getCommand("Lag").setExecutor(new Lag());
    49. getCommand("Zone").setExecutor(new Zone());
    50. getCommand("MSG").setExecutor(new MSG());
    51. //getCommand("INVSee").setExecutor(new INVSee());
    52. //getCommand("Chat").setExecutor(new Chat());
    53. //getCommand("Stats").setExecutor(new Stats());
    54. //getCommand("Handto").setExecutor(new Handto());
    55. //getCommand("Clan").setExecutor(new Clan());
    56.  
    57. PluginManager pm = this.getServer().getPluginManager();
    58. pm.registerEvents(new evts(), this);
    59.  
    60. final ShapedRecipe saddle = new ShapedRecipe(new ItemStack(Material.SADDLE, 1));
    61. saddle.shape("A A", "AAA", " B ");
    62. saddle.setIngredient('A', Material.LEATHER);
    63. saddle.setIngredient('B', Material.IRON_INGOT);
    64. getServer().addRecipe(saddle);
    65.  
    66. final ShapedRecipe ironhorse = new ShapedRecipe(new ItemStack(417, 1));
    67. ironhorse.shape(" A ", "A A", " ");
    68. ironhorse.setIngredient('A', Material.IRON_BLOCK);
    69. getServer().addRecipe(ironhorse);
    70.  
    71. final ShapedRecipe goldhorse = new ShapedRecipe(new ItemStack(418, 1));
    72. goldhorse.shape(" A ", "A A", " ");
    73. goldhorse.setIngredient('A', Material.GOLD_BLOCK);
    74. getServer().addRecipe(goldhorse);
    75.  
    76. final ShapedRecipe diamondhorse = new ShapedRecipe(new ItemStack(419, 1));
    77. diamondhorse.shape(" A ", "A A", " ");
    78. diamondhorse.setIngredient('A', Material.DIAMOND_BLOCK);
    79. getServer().addRecipe(diamondhorse);
    80.  
    81. log.info("[GaminForDummys] v0.1 Enabled");
    82.  
    83. }
    84.  
    85. @Override
    86. public void onDisable() {
    87. log.info("[GaminForDummys] v0.1 Disabled");
    88. for(Player p : Bukkit.getOnlinePlayers()) {
    89. getPlayerYaml(p.getName()).save();
    90. }
    91. }
    92.  
    93. public static PlayerFile getPlayerYaml(String PlayerName)
    94. {
    95. return new PlayerFile("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    96. }
    97.  
    98. public static PlayerFile getOfflinePlayerYaml(String PlayerName)
    99. {
    100. return new PlayerFile("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    101. }
    102. }
    103.  


    Economy Class:
    Code:java
    1. package gaminfordummys.util;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import gaminfordummys.GaminForDummys;
    6.  
    7. public class Economy
    8. {
    9. private static int startingBalance = 0;
    10.  
    11. public static HashMap<String, Integer> balance = new HashMap<String, Integer>();
    12.  
    13. static GaminForDummys plugin;
    14. public Economy(GaminForDummys _plugin) {
    15. plugin = _plugin;
    16. }
    17.  
    18. public static boolean createAccount(String PlayerName)
    19. {
    20. if (!balance.containsKey(PlayerName)) {
    21. balance.put(PlayerName, startingBalance);
    22. GaminForDummys.getPlayerYaml(PlayerName).set("Money", startingBalance);
    23. GaminForDummys.getPlayerYaml(PlayerName).save();
    24. return true;
    25. }
    26. return false;
    27. }
    28.  
    29. public static boolean hasAccount(String PlayerName) {
    30. return balance.containsKey(PlayerName);
    31. }
    32.  
    33. public static int getBalance(String PlayerName) {
    34. if (balance.containsKey(PlayerName)) {
    35. return ((Integer)balance.get(PlayerName)).intValue();
    36. }
    37. return 0;
    38. }
    39.  
    40. public static boolean addBalance(String PlayerName, int Amount) {
    41. if (balance.containsKey(PlayerName)) {
    42. int result = ((Integer)balance.get(PlayerName)).intValue() + Amount;
    43. balance.put(PlayerName, Integer.valueOf(result));
    44. GaminForDummys.getPlayerYaml(PlayerName).set("Money", Integer.valueOf(result));
    45. GaminForDummys.getPlayerYaml(PlayerName).save();
    46. return true;
    47. }
    48. return false;
    49. }
    50.  
    51. public static boolean removeBalance(String PlayerName, int Amount) {
    52. if ((balance.containsKey(PlayerName)) &&
    53. (((Integer)balance.get(PlayerName)).intValue() - Amount >= 0.0D)) {
    54. int result = ((Integer)balance.get(PlayerName)).intValue() - Amount;
    55. balance.put(PlayerName, Integer.valueOf(result));
    56. GaminForDummys.getPlayerYaml(PlayerName).set("Money", Integer.valueOf(result));
    57. GaminForDummys.getPlayerYaml(PlayerName).save();
    58. return true;
    59. }
    60.  
    61. return false;
    62. }
    63.  
    64. public static boolean hasBalance(String PlayerName, int Amount) {
    65. if (balance.containsKey(PlayerName)) {
    66. return ((Integer)balance.get(PlayerName)).intValue() - Amount >= 0;
    67. }
    68. return false;
    69. }
    70. }


    Events:
    Code:java
    1. PlayerFile yaml = GaminForDummys.getPlayerYaml(p.getName());
    2. if(!yaml.getfile(p.getName()).exists()) {
    3. yaml.set("Money", 0);
    4. yaml.set("Bounty", 0);
    5. yaml.set("Nickname", p.getName());
    6. yaml.set("Kills", 0);
    7. yaml.set("Deaths", 0);
    8. yaml.save();
    9. }
    10.  
    11. if(!Economy.hasAccount(p.getName())) {
    12. Economy.createAccount(p.getName());
    13. }else{
    14. int money = GaminForDummys.getPlayerYaml(p.getName()).getInteger("Money");
    15. Economy.balance.put(p.getName(), money);
    16. }
    17. }


    PlayerFile Class:
    Code:java
    1. package gaminfordummys.util;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.YamlConfiguration;
    7. import org.bukkit.configuration.file.YamlConfigurationOptions;
    8.  
    9. public class PlayerFile {
    10.  
    11. private File file = null;
    12.  
    13. private YamlConfiguration yaml = new YamlConfiguration();
    14.  
    15. public PlayerFile(File file) {
    16. this.file = file;
    17. if (!file.exists()) {
    18. try {
    19. file.createNewFile();
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. this.load();
    25. }
    26.  
    27. public PlayerFile(String path) {
    28. this.file = new File(path);
    29. if (!file.exists()) {
    30. try {
    31. file.createNewFile();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. this.load();
    37. }
    38.  
    39. private void load() {
    40. try {
    41. this.yaml.load(this.file);
    42. } catch (Exception e) {
    43. e.printStackTrace();
    44. }
    45. }
    46.  
    47. /**
    48. * Save the Yaml's data to the file passed in the constructor.
    49. */
    50. public void save() {
    51. try {
    52. this.yaml.save(this.file);
    53. } catch (Exception e) {
    54. e.printStackTrace();
    55. }
    56. }
    57.  
    58. public void delete() {
    59. try {
    60. this.file.delete();
    61. } catch (Exception e) {
    62. e.printStackTrace();
    63. }
    64. }
    65.  
    66. public final File getfile(String PlayerName) {
    67. return this.file;
    68. }
    69.  
    70. /**
    71. * Get an Integer from the given path.
    72. *
    73. * @param s
    74. * Path to the Integer.
    75. * @return Integer at given path.
    76. */
    77. public int getInteger(String s) {
    78. return this.yaml.getInt(s);
    79. }
    80.  
    81. /**
    82. * Save, then load the Yaml file. **Warning** Very Unstable.
    83. */
    84. public void reload() {
    85. this.save();
    86. this.load();
    87. }
    88.  
    89. /**
    90. * Get a String from the path defined.
    91. *
    92. * @param s
    93. * Path to the String.
    94. * @return String at given path.
    95. */
    96. public String getString(String s) {
    97. return this.yaml.getString(s);
    98. }
    99.  
    100. /**
    101. * Gets an Object at the given path.
    102. *
    103. * @param s
    104. * Path to given Object.
    105. * @return An Object at the given Path.
    106. */
    107. public Object get(String s) {
    108. return this.yaml.get(s);
    109. }
    110.  
    111. /**
    112. * Gets a boolean at the given path.
    113. *
    114. * @param s
    115. * Path to the boolean.
    116. * @return Boolean at the given path.
    117. */
    118. public boolean getBoolean(String s) {
    119. return this.yaml.getBoolean(s);
    120. }
    121.  
    122. /**
    123. * If the given path has no variable, it will be given a variable.
    124. *
    125. * @param s
    126. * Path to look for.
    127. * @param o
    128. * Variable to be assigned if not existing.
    129. */
    130. public void add(String s, Object o) {
    131. if (!this.contains(s)) {
    132. this.set(s, o);
    133. }
    134.  
    135. }
    136.  
    137. /**
    138. * Adds a String to a List of Strings.
    139. *
    140. * @param s
    141. * Path to given String List.
    142. * @param o
    143. * String to add to the String List.
    144. */
    145. public void addToStringList(String s, String o) {
    146. this.yaml.getStringList(s).add(o);
    147. }
    148.  
    149. /**
    150. * Removes a String to a List of Strings.
    151. *
    152. * @param s
    153. * Path to given String List.
    154. * @param o
    155. * String to remove from the String List.
    156. */
    157. public void removeFromStringList(String s, String o) {
    158. this.yaml.getStringList(s).remove(o);
    159. }
    160.  
    161. /**
    162. * Looks for a String List at given Path.
    163. *
    164. * @param s
    165. * Path to String List.
    166. * @return String List at given Path.
    167. */
    168. public java.util.List<String> getStringList(String s) {
    169. return this.yaml.getStringList(s);
    170. }
    171.  
    172. /**
    173. * Adds an Integer to a List of Integers.
    174. *
    175. * @param s
    176. * Path to given Integer List.
    177. * @param o
    178. * Integer to add to the Integer List.
    179. */
    180. public void addToIntegerList(String s, int o) {
    181. this.yaml.getIntegerList(s).add(o);
    182. }
    183.  
    184. /**
    185. * Removes an Integer to a List of Integers.
    186. *
    187. * @param s
    188. * Path to given Integer List.
    189. * @param o
    190. * Integer to remove to the Integer List.
    191. */
    192. public void removeFromIntegerList(String s, int o) {
    193. this.yaml.getIntegerList(s).remove(o);
    194. }
    195.  
    196. /**
    197. * Looks for a Integer List at given Path.
    198. *
    199. * @param s
    200. * Path to Integer List.
    201. * @return Integer List at given Path.
    202. */
    203. public java.util.List<Integer> getIntegerList(String s) {
    204. return this.yaml.getIntegerList(s);
    205. }
    206.  
    207. /**
    208. * Creates a new String List at given Path.
    209. *
    210. * @param s
    211. * Path to create String List at.
    212. * @param list
    213. * List to add.
    214. */
    215. public void createNewStringList(String s, java.util.List<String> list) {
    216. this.yaml.set(s, list);
    217. }
    218.  
    219. /**
    220. * Creates a new Integer List at given Path.
    221. *
    222. * @param s
    223. * Path to create Integer List at.
    224. * @param list
    225. * List to add.
    226. */
    227. public void createNewIntegerList(String s, java.util.List<Integer> list) {
    228. this.yaml.set(s, list);
    229. }
    230.  
    231. /**
    232. * **Untested/Unstable** Attempts to remove a variable at the given Path.
    233. *
    234. * @param s
    235. * Path to given variable needing removal.
    236. */
    237. public void remove(String s) {
    238. this.set(s, null);
    239. }
    240.  
    241. /**
    242. * Returns true if the given Path has a value.
    243. *
    244. * @param s
    245. * Path to value.
    246. * @return True if the given Path has a value.
    247. */
    248. public boolean contains(String s) {
    249. return this.yaml.contains(s);
    250. }
    251.  
    252. /**
    253. * Gets a double at the given Path.
    254. *
    255. * @param s
    256. * Path to double.
    257. * @return Double at given Path.
    258. */
    259. public double getDouble(String s) {
    260. return this.yaml.getDouble(s);
    261. }
    262.  
    263. /**
    264. * Sets a Object to the given Path.
    265. *
    266. * @param s
    267. * Path to variable being assigned.
    268. * @param o
    269. * Variable being assigned.
    270. */
    271. public void set(String s, Object o) {
    272. this.yaml.set(s, o);
    273. }
    274.  
    275. /**
    276. * Increases an Integer by 1.
    277. *
    278. * @param s
    279. * Path to Integer being incremented.
    280. */
    281. public void increment(String s) {
    282. this.yaml.set(s, this.getInteger(s) + 1);
    283. }
    284.  
    285. /**
    286. * Decreases an Integer by 1.
    287. *
    288. * @param s
    289. * Path to Integer being decremented.
    290. */
    291. public void decrement(String s) {
    292. this.yaml.set(s, this.getInteger(s) - 1);
    293. }
    294.  
    295. /**
    296. * Increases an Integer by i.
    297. *
    298. * @param s
    299. * Path to Integer being incremented.
    300. */
    301. public void increment(String s, int i) {
    302. this.yaml.set(s, this.getInteger(s) + i);
    303. }
    304.  
    305. /**
    306. * Decreases an Integer by 1.
    307. *
    308. * @param s
    309. * Path to Integer being decremented.
    310. */
    311. public void decrement(String s, int i) {
    312. this.yaml.set(s, this.getInteger(s) - i);
    313. }
    314.  
    315. /**
    316. * Returns the YamlConfiguration's Options.
    317. *
    318. * @return YamlConfiguration's Options.
    319. */
    320. public YamlConfigurationOptions options() {
    321. return this.yaml.options();
    322. }
    323.  
    324. }
     
  2. Offline

    Mattredsox

    If I remember correctly, if you save a YML onDisable it revokes all changes made to it.
     
Thread Status:
Not open for further replies.

Share This Page