Config help

Discussion in 'Plugin Development' started by Hex_27, Nov 17, 2014.

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

    Hex_27

    I'm trying to save the "skill-level" of players into a config file, so when a player joins, a new line is created, for example when Balloonman joins, then this would be added into the config file:

    BalloonMan: 0

    Then, I can fetch BalloonMan's value anywhere and change it anytime. How do I do this?
     
  2. Offline

    pookeythekid

    This is a method in the config API:
    Code:java
    1. getConfig().set("config.path.with.indents.separated.by.dots", value);

    So in this case, the config path (which is the term for the location of your "BalloonMan: 0" in your config hierarchy) would simply be "BalloonMan", and your value will be the number 0.

    Edit: Oh, and this is very important.
    Code:java
    1. saveConfig();

    You must do this every time you finish editing a config file.
     
  3. Offline

    Hex_27

    pookeythekid Thanks! It helped a lot. But is there a way to check if that path already exists? Right now I'm using p.hasPlayedBefore(), but I worry that players who have played the server before would not have this config created for them.
     
  4. Offline

    pookeythekid

  5. Offline

    Hex_27

    pookeythekid
    This is my code. Somehow, when a new player joins, nothing happens.

    Code:java
    1. package me.leothepro555.random;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7.  
    8. public class LeaveManager implements Listener{
    9.  
    10. public Main plugin;
    11.  
    12. public LeaveManager(Main plugin) {
    13. this.plugin = plugin;
    14. }
    15. @EventHandler
    16. public void onPlayerJoin(PlayerJoinEvent event){
    17. Player p = event.getPlayer();
    18. if(plugin.config.contains(p.getName())&&
    19. plugin.skill.contains(p.getName())){
    20. String playername = event.getPlayer().getName();
    21. plugin.config.set(playername, 0);
    22. plugin.saveLevels();
    23. plugin.skill.set(playername, "none");
    24. plugin.saveSkills();
    25. }
    26. }
    27.  
    28. }
    29.  
     
  6. Offline

    pookeythekid

    Hex_27
    Unless this is what your plugin.saveLevels() and/or your plugin.saveSkills() does?
     
  7. Offline

    Hex_27

    pookeythekid
    It kind of works when I set it to if(!p.hasPlayedBefore()). I guess I'll try it again
     
  8. Offline

    pookeythekid

    Hex_27 Could I see your main class?
     
  9. Offline

    Hex_27

    pookeythekid sure
    Code:java
    1. package me.leothepro555.random;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.HashSet;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.configuration.file.FileConfiguration;
    13. import org.bukkit.configuration.file.YamlConfiguration;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener{
    19. HashSet<UUID> weaponsmasters = new HashSet<UUID>();
    20. HashSet<UUID> rangers = new HashSet<UUID>();
    21. HashSet<UUID> mages = new HashSet<UUID>();
    22. HashSet<UUID> pyros = new HashSet<UUID>();
    23. HashSet<UUID> juggernauts = new HashSet<UUID>();
    24.  
    25. public File file = new File("plugins/Skills/levels.yml");
    26. public FileConfiguration config = YamlConfiguration.loadConfiguration(this.file);
    27. public File anotherfile = new File("plugins/Skills/skills.yml");
    28. public FileConfiguration skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    29.  
    30. public void saveLevels() {
    31. try {
    32. this.config.save(this.file);
    33. this.config = YamlConfiguration.loadConfiguration(this.file);
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. public void saveSkills() {
    40. try {
    41. this.config.save(this.anotherfile);
    42. this.config = YamlConfiguration.loadConfiguration(this.anotherfile);
    43. } catch (IOException e) {
    44. e.printStackTrace();
    45. }
    46. }
    47.  
    48. public void onEnable(){
    49. getConfig().options().copyDefaults(true);
    50. saveDefaultConfig();
    51. config.options().copyDefaults(true);
    52. saveLevels();
    53. skill.options().copyDefaults(true);
    54. saveSkills();
    55.  
    56. Bukkit.getServer().getPluginManager().registerEvents(new LeaveManager(this), this);
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    60.  
    61. if(cmd.getName().equalsIgnoreCase("skill")){
    62. if(sender instanceof Player){
    63. Player p = (Player) sender;
    64. if(args.length != 0){
    65. if(args[0].equalsIgnoreCase("select")){
    66. if(args.length == 2){
    67.  
    68. if(this.skill.getString(p.getName()) == "none"){
    69. if(args[1].equalsIgnoreCase("pyro")){
    70. skill.set(p.getName(), "pyro");
    71. saveSkills();
    72. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    73. }else if(args[1].equalsIgnoreCase("juggernaut")){
    74. skill.set(p.getName(), "juggernaut");
    75. saveSkills();
    76. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    77. }else if(args[1].equalsIgnoreCase("ranger")){
    78. skill.set(p.getName(), "ranger");
    79. saveSkills();
    80. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    81. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    82. skill.set(p.getName(), "weaponsmaster");
    83. saveSkills();
    84. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    85. }else if(args[1].equalsIgnoreCase("mage")){
    86. skill.set(p.getName(), "mage");
    87. saveSkills();
    88. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    89. }else{
    90. p.sendMessage(ChatColor.RED + "That skill doesn't exist! Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    91. }
    92. }else{
    93. p.sendMessage(ChatColor.RED + "You already have a skill!");
    94. }
    95.  
    96. }else{
    97. p.sendMessage(ChatColor.RED + "Usage: /skill select [classname]");
    98. }
    99. }else if(args[0].equalsIgnoreCase("list")){
    100. p.sendMessage(ChatColor.BLUE + "=======Skill List=======");
    101. p.sendMessage(ChatColor.BLUE + "WeaponsMaster");
    102. p.sendMessage(ChatColor.BLUE + "Ranger");
    103. p.sendMessage(ChatColor.BLUE + "Mage");
    104. p.sendMessage(ChatColor.BLUE + "Pyro");
    105. p.sendMessage(ChatColor.BLUE + "Juggernaut");
    106. p.sendMessage(ChatColor.BLUE + "Do /skill info [skill name] for more info");
    107. }else if(args[0].equalsIgnoreCase("info")){
    108. if(args.length == 2){
    109. if(args[1].equalsIgnoreCase("pyro")){
    110. p.sendMessage(ChatColor.GREEN + "============================================");
    111. p.sendMessage(ChatColor.BLUE + "Pyros are immune to fire, and take significantly less damage from lava. When Pyros hit their target, they have a chance of their target catching fire");
    112. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the chance");
    113. p.sendMessage(ChatColor.RED + "Pyros are weak and slow when in water.");
    114. p.sendMessage(ChatColor.GREEN + "============================================");
    115. }else if(args[1].equalsIgnoreCase("juggernaut")){
    116. p.sendMessage(ChatColor.GREEN + "============================================");
    117. p.sendMessage(ChatColor.BLUE + "Juggernauts are thick and very tough. Will reduce all incoming damage by an amount.");
    118. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the amount of hearts you have");
    119. p.sendMessage(ChatColor.RED + "Juggernauts are weighed down by any armor other than leather");
    120. p.sendMessage(ChatColor.GREEN + "============================================");
    121. }else if(args[1].equalsIgnoreCase("ranger")){
    122. p.sendMessage(ChatColor.GREEN + "============================================");
    123. p.sendMessage(ChatColor.BLUE + "Rangers are quick, stealthy and specialize in light swords and bows. When armed with a bow, you gain speed. When armed with an iron sword or wooden sword, each hit gves you a small chance to become invisible.");
    124. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases that chance.");
    125. p.sendMessage(ChatColor.RED + "Rangers are weighed down by diamond tools and armor");
    126. p.sendMessage(ChatColor.GREEN + "============================================");
    127. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    128. p.sendMessage(ChatColor.GREEN + "============================================");
    129. p.sendMessage(ChatColor.BLUE + "Weapons Masters are ruthless and skillful, effectively decimating all that dares to challenge him. Attacks cause a slow and some bonus damage based on a percentage of the damage you dealt.");
    130. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the bonus damage percentage.");
    131. p.sendMessage(ChatColor.RED + "Weapons masters deal less damage to nether mobs and endermen.");
    132. p.sendMessage(ChatColor.GREEN + "============================================");
    133. }else if(args[1].equalsIgnoreCase("mage")){
    134. p.sendMessage(ChatColor.GREEN + "============================================");
    135. p.sendMessage(ChatColor.BLUE + "Mages are strong in the art of magic and dispatch their enemies with concoctions and a nice shiny staff. When fighting with hoes, slow enemies and and have a chance to heal for an amount.");
    136. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the chance you will be healed and the heal amount(Up to an extent)");
    137. p.sendMessage(ChatColor.RED + "Mages deal less damage when armed with normal weapons, excluding bows.");
    138. p.sendMessage(ChatColor.GREEN + "============================================");
    139. }else{
    140. p.sendMessage(ChatColor.RED + "Invalid skill. Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    141. }
    142. }else{
    143. p.sendMessage(ChatColor.RED + "Usage: /skill info [skillname]");
    144. }
    145. }else if(args[0].equalsIgnoreCase("level")){
    146. if(this.skill.getString(p.getName()) == "none"){
    147. p.sendMessage(ChatColor.RED + "You don't have a skill type! Choose one with /skill select");
    148. }else{
    149. if(this.skill.getString(p.getName()) == "weaponsmaster"){
    150. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Weapons Master");
    151. }else if(this.skill.getString(p.getName()) == "ranger"){
    152. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Ranger");
    153. }else if(this.skill.getString(p.getName()) == "mage"){
    154. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Mage");
    155. }else if(this.skill.getString(p.getName()) == "pyro"){
    156. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Pyro");
    157. }else if(this.skill.getString(p.getName()) == "juggernaut"){
    158. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Juggernaut");
    159. }
    160.  
    161. }
    162. }else {
    163. p.sendMessage(ChatColor.BLUE + "====Skill Commands====");
    164. p.sendMessage(ChatColor.BLUE + "/skill select " + ChatColor.RED + "Choose a skill, if you don't have one");
    165. p.sendMessage(ChatColor.BLUE + "/skill list " + ChatColor.RED + "List of all skill types");
    166. p.sendMessage(ChatColor.BLUE + "/skill info " + ChatColor.RED + "Shows information about one skill type");
    167. p.sendMessage(ChatColor.BLUE + "/skill level " + ChatColor.RED + "Shows your skill level, if you have a skill type");
    168.  
    169. }
    170. }
    171. }else{
    172. sender.sendMessage("ERROR: Only players can use /skill");
    173. }
    174.  
    175. }
    176. return false;
    177.  
    178. }
    179. }
    180.  
     
  10. Offline

    pookeythekid

    Hex_27 Well I can't find anything wrong with it... There is this one method that I always used, before I found out about config.contains(path), which I actually haven't had the chance to implement yet. I did this:
    Code:java
    1. if (config.getString(path) == null /*or != null*/)

    I hear it's a pretty bad way to do it, but it's never failed me yet.
     
  11. Offline

    Hex_27

    pookeythekid
    There's another problem now... I found out that doing /skill select [Classname] doesn't actually set the value for Playername.
     
  12. Offline

    pookeythekid

    Hex_27 Huh? I didn't know that command existed in your plugin. What is it supposed to do if it worked?
     
  13. Offline

    Hex_27

    pookeythekid The command is in the Main.java
    It is supposed to set the PlayerName's value to a specific integer.
    I also changed my main a little, so here it is now:
    Code:java
    1. package me.leothepro555.random;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.PluginManager;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Main extends JavaPlugin implements Listener{
    17.  
    18. public File file = new File("plugins/Skills/levels.yml");
    19. public FileConfiguration config = YamlConfiguration.loadConfiguration(this.file);
    20. public File anotherfile = new File("plugins/Skills/skills.yml");
    21. public FileConfiguration skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    22.  
    23. public void saveLevels() {
    24. try {
    25. this.config.save(this.file);
    26. this.config = YamlConfiguration.loadConfiguration(this.file);
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31.  
    32. public void saveSkills() {
    33. try {
    34. this.config.save(this.anotherfile);
    35. this.config = YamlConfiguration.loadConfiguration(this.anotherfile);
    36. } catch (IOException e) {
    37. e.printStackTrace();
    38. }
    39. }
    40.  
    41. public void onEnable(){
    42. getConfig().options().copyDefaults(true);
    43. saveDefaultConfig();
    44. config.options().copyDefaults(false);
    45. saveLevels();
    46. skill.options().copyDefaults(false);
    47. saveSkills();
    48. PluginManager manager = Bukkit.getServer().getPluginManager();
    49. manager.registerEvents(new LeaveManager(this), this);
    50. manager.registerEvents(new WeaponsMaster(this), this);
    51. manager.registerEvents(new Ranger(this), this);
    52. manager.registerEvents(new Mage(this), this);
    53. manager.registerEvents(new Pyro(this), this);
    54. manager.registerEvents(new Juggernaut(this), this);
    55. }
    56.  
    57. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    58.  
    59. if(cmd.getName().equalsIgnoreCase("skill")){
    60. if(sender instanceof Player){
    61. Player p = (Player) sender;
    62. if(args.length != 0){
    63. if(args[0].equalsIgnoreCase("select")){
    64. if(args.length == 2){
    65. //Weaponsmaster = 1
    66. //Ranger = 2
    67. //Mage = 3
    68. //Pyro = 4
    69. //Juggernaut = 5
    70. if(this.skill.getInt(p.getName()) == 0){
    71. if(args[1].equalsIgnoreCase("pyro")){
    72. skill.set(p.getName(), 4);
    73. saveSkills();
    74. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    75. }else if(args[1].equalsIgnoreCase("juggernaut")){
    76. skill.set(p.getName(), 5);
    77. saveSkills();
    78. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    79. }else if(args[1].equalsIgnoreCase("ranger")){
    80. skill.set(p.getName(), 2);
    81. saveSkills();
    82. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    83. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    84. skill.set(p.getName(), 1);
    85. saveSkills();
    86. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    87. }else if(args[1].equalsIgnoreCase("mage")){
    88. skill.set(p.getName(), 3);
    89. saveSkills();
    90. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    91. }else{
    92. p.sendMessage(ChatColor.RED + "That skill doesn't exist! Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    93. }
    94. }else{
    95. p.sendMessage(ChatColor.RED + "You already have a skill!");
    96. }
    97.  
    98. }else{
    99. p.sendMessage(ChatColor.RED + "Usage: /skill select [classname]");
    100. }
    101. }else if(args[0].equalsIgnoreCase("list")){
    102. p.sendMessage(ChatColor.BLUE + "=======Skill List=======");
    103. p.sendMessage(ChatColor.BLUE + "WeaponsMaster");
    104. p.sendMessage(ChatColor.BLUE + "Ranger");
    105. p.sendMessage(ChatColor.BLUE + "Mage");
    106. p.sendMessage(ChatColor.BLUE + "Pyro");
    107. p.sendMessage(ChatColor.BLUE + "Juggernaut");
    108. p.sendMessage(ChatColor.BLUE + "Do /skill info [skill name] for more info");
    109. }else if(args[0].equalsIgnoreCase("info")){
    110. if(args.length == 2){
    111. if(args[1].equalsIgnoreCase("pyro")){
    112. p.sendMessage(ChatColor.GREEN + "============================================");
    113. p.sendMessage(ChatColor.BLUE + "Pyros are immune to fire, and take significantly less damage from lava. When Pyros hit their target, they have a chance of their target catching fire");
    114. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the chance");
    115. p.sendMessage(ChatColor.RED + "Pyros are weak and slow when in water.");
    116. p.sendMessage(ChatColor.GREEN + "============================================");
    117. }else if(args[1].equalsIgnoreCase("juggernaut")){
    118. p.sendMessage(ChatColor.GREEN + "============================================");
    119. p.sendMessage(ChatColor.BLUE + "Juggernauts are thick and very tough. Will reduce all incoming damage by an amount.");
    120. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the amount of hearts you have");
    121. p.sendMessage(ChatColor.RED + "Juggernauts can only do up to 4 damage at a time.");
    122. p.sendMessage(ChatColor.GREEN + "============================================");
    123. }else if(args[1].equalsIgnoreCase("ranger")){
    124. p.sendMessage(ChatColor.GREEN + "============================================");
    125. p.sendMessage(ChatColor.BLUE + "Rangers are quick, stealthy and specialize in light swords and bows. When armed with a bow, you gain speed. When armed with an iron sword or wooden sword, each hit gves you a small chance to become invisible.");
    126. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases that chance.");
    127. p.sendMessage(ChatColor.RED + "Rangers are weighed down by diamond tools and armor");
    128. p.sendMessage(ChatColor.GREEN + "============================================");
    129. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    130. p.sendMessage(ChatColor.GREEN + "============================================");
    131. p.sendMessage(ChatColor.BLUE + "Weapons Masters are ruthless and skillful, effectively decimating all that dares to challenge him. Attacks cause a slow and some bonus damage based on a percentage of the damage you dealt.");
    132. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the bonus damage percentage.");
    133. p.sendMessage(ChatColor.RED + "Weapons masters deal less damage to nether mobs and endermen.");
    134. p.sendMessage(ChatColor.GREEN + "============================================");
    135. }else if(args[1].equalsIgnoreCase("mage")){
    136. p.sendMessage(ChatColor.GREEN + "============================================");
    137. p.sendMessage(ChatColor.BLUE + "Mages are strong in the art of magic and dispatch their enemies with concoctions and a nice shiny staff. When fighting with hoes, slow enemies and and have a chance to heal for an amount.");
    138. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the chance you will be healed and the heal amount(Up to an extent)");
    139. p.sendMessage(ChatColor.RED + "Mages deal less damage when armed with normal weapons, excluding bows.");
    140. p.sendMessage(ChatColor.GREEN + "============================================");
    141. }else{
    142. p.sendMessage(ChatColor.RED + "Invalid skill. Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    143. }
    144. }else{
    145. p.sendMessage(ChatColor.RED + "Usage: /skill info [skillname]");
    146. }
    147. }else if(args[0].equalsIgnoreCase("level")){
    148. if(this.skill.getInt(p.getName()) == 0){
    149. p.sendMessage(ChatColor.RED + "You don't have a skill type! Choose one with /skill select");
    150. }else{
    151. //Weaponsmaster = 1
    152. //Ranger = 2
    153. //Mage = 3
    154. //Pyro = 4
    155. //Juggernaut = 5
    156. if(this.skill.getInt(p.getName()) == 1){
    157. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Weapons Master");
    158. }else if(this.skill.getInt(p.getName()) == 2){
    159. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Ranger");
    160. }else if(this.skill.getInt(p.getName()) == 3){
    161. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Mage");
    162. }else if(this.skill.getInt(p.getName()) == 4){
    163. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Pyro");
    164. }else if(this.skill.getInt(p.getName()) == 5){
    165. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Juggernaut");
    166. }
    167.  
    168. }
    169. }else {
    170. p.sendMessage(ChatColor.BLUE + "====Skill Commands====");
    171. p.sendMessage(ChatColor.BLUE + "/skill select " + ChatColor.RED + "Choose a skill, if you don't have one");
    172. p.sendMessage(ChatColor.BLUE + "/skill list " + ChatColor.RED + "List of all skill types");
    173. p.sendMessage(ChatColor.BLUE + "/skill info " + ChatColor.RED + "Shows information about one skill type");
    174. p.sendMessage(ChatColor.BLUE + "/skill level " + ChatColor.RED + "Shows your skill level, if you have a skill type");
    175.  
    176. }
    177. }else if(args.length == 0){
    178. p.sendMessage(ChatColor.BLUE + "====Skill Commands====");
    179. p.sendMessage(ChatColor.BLUE + "/skill select " + ChatColor.RED + "Choose a skill, if you don't have one");
    180. p.sendMessage(ChatColor.BLUE + "/skill list " + ChatColor.RED + "List of all skill types");
    181. p.sendMessage(ChatColor.BLUE + "/skill info " + ChatColor.RED + "Shows information about one skill type");
    182. p.sendMessage(ChatColor.BLUE + "/skill level " + ChatColor.RED + "Shows your skill level, if you have a skill type");
    183. }
    184. }else{
    185. sender.sendMessage("ERROR: Only players can use /skill");
    186. }
    187.  
    188. }
    189. return false;
    190.  
    191. }
    192. }
    193.  
     
  14. Offline

    pookeythekid

    Hex_27
    Code:java
    1. if(this.skill.getInt(p.getName()) == 0){

    Doesn't the player not exist in the skill file at this point?
     
  15. Offline

    Hex_27

    pookeythekid
    It would plant the variable there, set it to "0", but the /skill select does not change the variable's setting. For example when Hex_27 joins the server, the plugin would check if that variable exists, then it would set it to "0". But when I do /skill select Mage , which should set it to this:
    Hex_27: 3
    But it remains as:
    Hex_27: 0
    The only way to actually change it now is by going to the config and altering it manually.
     
  16. Offline

    lenis0012

    i would NOT store all player data in a YAML file.
    It will totally lag your server out.

    Either use 1 file per user, or store it using SQL or FlatFile
     
  17. Offline

    Hex_27

    lenis0012
    I'm new to this config stuff. I have no idea what an sql file or a flat file is. So right now, I assume you're recommending me to generate a new PLAYERNAME.yml each time a new player joins..... right?
     
  18. Offline

    mythbusterma


    Where's your tests to back this data up? I, for one, feel SQL would be much slower with response times ~10-80 ms (and is non-determinant) whereas a YAML file, at least I would guess, would be around 1ms. There's no issue with storing the data in a YAML file. Also, define "flat file," as that can refer to almost anything that isn't a relational database (and even some relational databases, and including YAML).

    Hex_27

    Don't bother with SQL unless you need the power, portability, or scalability of a relational database (99/100 plugins don't). It's much more difficult to use and generally more difficult for a server owner to set up too, for what generally amounts to a performance loss.
     
  19. Offline

    Hex_27

  20. Offline

    lenis0012

    go try and store 10,000 entries in 1 file.
    I tried it, 2s lag spikes :)

    Good thing is that sql and flatfile can both be handles and loaded in a async task.
    Im assuming you did it all wrong if you really get 10-80ms load time on the main thread.

    YAML is better for configuration, it is really slow compared to other storage types.

    Hex_27

    Now lets actualy give you your answer:
    First of all, saveDefaults it not needed as your configuration files are most likely empty at the beginning, nor is your save method on enable.

    Use the following methdo to check if datat exists for a player:
    Code:java
    1. public boolean hasSkill(UUID player) { // I recommend UUID instead of player name storage
    2. return skill.contains(player.toString());
    3. }

    Getting the data:
    Code:java
    1. public int getSkill(UUID player) {
    2. return skill.getInt(player.toString());
    3. }

    Setting the data at any point:
    Code:java
    1. public void setSkill(UUID player, int skill) {
    2. skill.set(player.toString(), skill);
    3. saveSkills(); // Optional, if you want to apply changes straight after setting it, you could also do this on disable.
    4. }
     
  21. Offline

    Hex_27

    lenis0012
    For setSkill() theres an error in eclipse that says "Cannot invoke set(UUID, int) on the primitive type int"

    EDIT: Nevermind, turns out the plugin was confusing the skill config with the setSkill()'s skill

    I FINALLY LET IT WORK! Again, it was another dumb mistake that I didn't find for days.
    Old code:
    Code:java
    1. public File file = new File("plugins/Skills/levels.yml");
    2. public FileConfiguration config = YamlConfiguration.loadConfiguration(this.file);
    3. public File anotherfile = new File("plugins/Skills/skills.yml");
    4. public FileConfiguration skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    5.  
    6. public void saveLevels() {
    7. try {
    8. this.config.save(this.file);
    9. this.config = YamlConfiguration.loadConfiguration(this.file);
    10. } catch (IOException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14.  
    15. public void saveSkills() {
    16. try {
    17. //HERE
    18. this.config.save(this.anotherfile);
    19. this.config = YamlConfiguration.loadConfiguration(this.anotherfile);
    20. //UP HERE
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }


    New code:
    Code:java
    1. public File file = new File("plugins/Skills/levels.yml");
    2. public FileConfiguration config = YamlConfiguration.loadConfiguration(this.file);
    3. public File anotherfile = new File("plugins/Skills/skills.yml");
    4. public FileConfiguration skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    5.  
    6. public void saveLevels() {
    7. try {
    8. this.config.save(this.file);
    9. this.config = YamlConfiguration.loadConfiguration(this.file);
    10. } catch (IOException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14.  
    15. public void saveSkills() {
    16. try {
    17. //CORRECTED VERSION
    18. this.skill.save(this.anotherfile);
    19. this.skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. }


    I'm starting to feel a bit sorry for wasting so much time from you guys. Thanks for trying though.

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

    pookeythekid

    Hex_27 Aahaha. I love the feeling of stupidity when I find those errors in my code. xD
    Glad you worked it out.
     
  23. Offline

    mythbusterma

    lenis0012

    It's not 10-80 ms on the main thread....that's the amount of time it takes to receive data (on average) from an SQL server. Of course when I do SQL I do it asynchronously.

    So then I challenge you, why doesn't PEX or GroupManager lag servers terribly? They have >10,000 entries stored in a YAML file and seem to have no issues loading them.

    YAML, in most cases that don't require the size or relational database features, is usually faster than the other options.

    Also, you still haven't defined "flat file," because that can literally refer to anything that isn't a relational database.
     
  24. Offline

    lenis0012

    Caching.

    YAML is really slow.
    JSON is much faster
    ObjectOutputStreams are even faster, but cannot be modified by a text editor.

    SQL is slower, but has nice query support, and can be loaded in PHP very easily.
    NoSQL is a faster variant of MySQL SQLite, i use this oen for my own private plugins.

    If you do not agree, please let me know what i am saying wrong
     
  25. Offline

    mythbusterma

    lenis0012

    Yes, SnakeYAML caches quite well.

    JSON is sometimes faster, but also less readable.

    NoSQL refers to any database system that behaves similar to SQL, but doesn't use relational databases for storage, things like Mongo.
     
Thread Status:
Not open for further replies.

Share This Page