Solved config.set(string, string);

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

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

    Hex_27

    Code:java
    1. plugin.reas.set(playername + ".pyro.lava-immunity", false);
    2. plugin.reas.set(playername + ".pyro.ignition", false);
    3. plugin.reas.set(playername + ".pyro.pyromania", false);
    4. plugin.reas.set(playername + ".pyro.netherrack-bomb", false);
    5. plugin.reas.set(playername + ".pyro.aspect-of-the-flame", false);
    6. plugin.reas.set(playername + ".weaponsmaster.parry", false);
    7. plugin.reas.set(playername + ".weaponsmaster.reflect", false);
    8. plugin.reas.set(playername + ".weaponsmaster.dodge", false);
    9. plugin.reas.set(playername + ".weaponsmaster.weapons-forger", false);
    10. plugin.reas.set(playername + ".weaponsmaster.blade-waltz", false);
    11. plugin.reas.set(playername + ".mage.unholy-smite", false);
    12. plugin.reas.set(playername + ".mage.slow-down", false);
    13. plugin.reas.set(playername + ".mage.the-summoning", false);
    14. plugin.reas.set(playername + ".mage.harvest", false);
    15. plugin.reas.set(playername + ".mage.potion-master", false);
    16. plugin.reas.set(playername + ".ranger.gotta-go-fast", false);
    17. plugin.reas.set(playername + ".ranger.noxius-venom", false);
    18. plugin.reas.set(playername + ".ranger.dusk-mist", false);
    19. plugin.reas.set(playername + ".ranger.unseen-threat", false);
    20. plugin.reas.set(playername + ".ranger.food-cleanse", false);
    21. plugin.reas.set(playername + ".juggernaut.unstoppable", false);
    22. plugin.reas.set(playername + ".juggernaut.iron-skin", false);
    23. plugin.reas.set(playername + ".juggernaut.environmental-immunity", false);
    24. plugin.reas.set(playername + ".juggernaut.will-power", false);
    25. plugin.reas.set(playername + ".juggernaut.perseverance", false);
    26. plugin.saveResearch();

    I set this code, but only this appeared in the config(the player name is Hex_27):

    Hex_27: 0

    (This isn't the full code, because the code itself is actually kind of long.)
    plugin.saveResearch saves the config.
     
  2. Offline

    iMurder

    Would like more code, so we can have more of an idea what you're doing.
     
  3. Offline

    Hex_27

    iMurder

    Its a pretty big plugin and there's literally 8 or 9 class files. What do you need to see? (I put the saveResearch() method and stuff related to the research.yml)
    Code:java
    1. public File research = new File("plugins/Skills/research.yml");
    2. public FileConfiguration reas = YamlConfiguration.loadConfiguration(this.research);
    3.  
    4.  
    5. public void saveResearch() {
    6. try {
    7. this.config.save(this.research);
    8. this.config = YamlConfiguration.loadConfiguration(this.research);
    9. } catch (IOException e) {
    10. e.printStackTrace();
    11. }
    12. }
     
  4. Offline

    mrCookieSlime

    Hex_27
    We need to see it because otherwise we cannot help you.
    And because the error is most likely in another place.
    Please post your full code using Pastebin or similiar.
     
  5. Offline

    AoH_Ruthless

    • We don't know the issue
      • You can't expect us to fix all your problems for you. A thorough explanation of your issue and what you have tried is necessary. You have already given us some code; go the extra step and tell us what's actually wrong with it.
    • That code is really, really ugly to look at. You could easily shorten it and make it more efficient and readable. Additionally, 8 or 9 class files doesn't necessarily constitute a complex plugin. It's not the number of lines, packages, or classes that defines the complexity, it is the contents of those classes.
     
  6. Offline

    Hex_27

    mrCookieSlime
    AoH_Ruthless
    I'm making an RPG plugin that allows players to choose a skill tpe of their own: weapons master, pyro, mage, ranger, juggernaut. I have 1 main class, which handles the yml files, commands and some methods, 5 different files, 1 for each skill type and its contents, one manager class for when a player joins to generate data in the ymls, and finally, a class to manage a level-up system. Which one do you guys need to see?
     
  7. Offline

    mrCookieSlime

    Hex_27
    The main class of course.
     
  8. Offline

    Hex_27

    mrCookieSlime
    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.  
    21. public void saveLevels() {
    22. try {
    23. this.config.save(this.file);
    24. this.config = YamlConfiguration.loadConfiguration(this.file);
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. }
    29.  
    30.  
    31. public File anotherfile = new File("plugins/Skills/skills.yml");
    32. public FileConfiguration skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    33.  
    34. public void saveSkills() {
    35. try {
    36. this.skill.save(this.anotherfile);
    37. this.skill = YamlConfiguration.loadConfiguration(this.anotherfile);
    38. } catch (IOException e) {
    39. e.printStackTrace();
    40. }
    41. }
    42.  
    43.  
    44.  
    45. public File exp = new File("plugins/Skills/experience.yml");
    46. public FileConfiguration xp = YamlConfiguration.loadConfiguration(this.exp);
    47.  
    48. public void saveXp() {
    49. try {
    50. this.xp.save(this.exp);
    51. this.xp = YamlConfiguration.loadConfiguration(this.exp);
    52. } catch (IOException e) {
    53. e.printStackTrace();
    54. }
    55. }
    56.  
    57.  
    58.  
    59. public File research = new File("plugins/Skills/research.yml");
    60. public FileConfiguration reas = YamlConfiguration.loadConfiguration(this.research);
    61.  
    62. public void saveResearch() {
    63. try {
    64. this.config.save(this.research);
    65. this.config = YamlConfiguration.loadConfiguration(this.research);
    66. } catch (IOException e) {
    67. e.printStackTrace();
    68. }
    69. }
    70.  
    71.  
    72.  
    73.  
    74.  
    75.  
    76.  
    77. public boolean hasSkill(Player p) { // I recommend UUID instead of player name storage
    78. if(skill.getInt(p.getName()) != 0){
    79. return true;
    80. }
    81. return false;
    82. }
    83.  
    84. public int getSkill(Player p) {
    85. return skill.getInt(p.getName());
    86. }
    87.  
    88. public void setSkill(Player p, int skillnum) {
    89. skill.set(p.getName(), skillnum);
    90. saveSkills(); // Optional, if you want to apply changes straight after setting it, you could also do this on disable.
    91. }
    92.  
    93. public void onEnable(){
    94. getConfig().options().copyDefaults(true);
    95. saveDefaultConfig();
    96. config.options().copyDefaults(false);
    97. saveLevels();
    98. skill.options().copyDefaults(false);
    99. saveSkills();
    100. xp.options().copyDefaults(false);
    101. saveXp();
    102. reas.options().copyDefaults(false);
    103. saveResearch();
    104. PluginManager manager = Bukkit.getServer().getPluginManager();
    105. manager.registerEvents(new LeaveManager(this), this);
    106. manager.registerEvents(new LevelManager(this), this);
    107. manager.registerEvents(new WeaponsMaster(this), this);
    108. manager.registerEvents(new Ranger(this), this);
    109. manager.registerEvents(new Mage(this), this);
    110. manager.registerEvents(new Pyro(this), this);
    111. manager.registerEvents(new Juggernaut(this), this);
    112. }
    113.  
    114. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    115.  
    116. if(cmd.getName().equalsIgnoreCase("skill")){
    117. if(sender instanceof Player){
    118. Player p = (Player) sender;
    119. if(args.length != 0){
    120. if(args[0].equalsIgnoreCase("select")){
    121. if(args.length == 2){
    122. //Weaponsmaster = 1
    123. //Ranger = 2
    124. //Mage = 3
    125. //Pyro = 4
    126. //Juggernaut = 5
    127. if(!hasSkill(p)){
    128. if(args[1].equalsIgnoreCase("pyro")){
    129. setSkill(p, 4);
    130. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    131. }else if(args[1].equalsIgnoreCase("juggernaut")){
    132. setSkill(p, 5);
    133. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    134. }else if(args[1].equalsIgnoreCase("ranger")){
    135. setSkill(p, 2);
    136. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    137. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    138. setSkill(p, 1);
    139. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    140. }else if(args[1].equalsIgnoreCase("mage")){
    141. setSkill(p, 3);
    142. p.sendMessage(ChatColor.BLUE + "You are now a " + args[1]);
    143. }else{
    144. p.sendMessage(ChatColor.RED + "That skill doesn't exist! Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    145. }
    146. }else{
    147. p.sendMessage(ChatColor.RED + "You already have a skill!");
    148. }
    149.  
    150. }else{
    151. p.sendMessage(ChatColor.RED + "Usage: /skill select [classname]");
    152. }
    153. }else if(args[0].equalsIgnoreCase("list")){
    154. p.sendMessage(ChatColor.BLUE + "=======Skill List=======");
    155. p.sendMessage(ChatColor.BLUE + "WeaponsMaster");
    156. p.sendMessage(ChatColor.BLUE + "Ranger");
    157. p.sendMessage(ChatColor.BLUE + "Mage");
    158. p.sendMessage(ChatColor.BLUE + "Pyro");
    159. p.sendMessage(ChatColor.BLUE + "Juggernaut");
    160. p.sendMessage(ChatColor.BLUE + "Do /skill info [skill name] for more info");
    161. }else if(args[0].equalsIgnoreCase("info")){
    162. if(args.length == 2){
    163. if(args[1].equalsIgnoreCase("pyro")){
    164. p.sendMessage(ChatColor.GREEN + "============================================");
    165. 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.");
    166. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the chance");
    167. p.sendMessage(ChatColor.RED + "And just in case you didn't figure it out, Pyros aren't strong against their own kind.");
    168. p.sendMessage(ChatColor.GREEN + "============================================");
    169. }else if(args[1].equalsIgnoreCase("juggernaut")){
    170. p.sendMessage(ChatColor.GREEN + "============================================");
    171. p.sendMessage(ChatColor.BLUE + "Juggernauts are thick and very tough. Will reduce all incoming damage by an amount.");
    172. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn reduces the damage you can take");
    173. p.sendMessage(ChatColor.RED + "Juggernauts can only do up to 6 damage at a time.");
    174. p.sendMessage(ChatColor.GREEN + "============================================");
    175. }else if(args[1].equalsIgnoreCase("ranger")){
    176. p.sendMessage(ChatColor.GREEN + "============================================");
    177. p.sendMessage(ChatColor.BLUE + "A Ranger's sprint is incomparable by any other class. Melee attacks poison your target.");
    178. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases poison duration.");
    179. p.sendMessage(ChatColor.RED + "Rangers take more damage from the environment.");
    180. p.sendMessage(ChatColor.GREEN + "============================================");
    181. }else if(args[1].equalsIgnoreCase("weaponsmaster")){
    182. p.sendMessage(ChatColor.GREEN + "============================================");
    183. p.sendMessage(ChatColor.BLUE + "Weapons Masters are ruthless and skillful, effectively decimating all that dares to challenge him. Attacks deal bonus damage.");
    184. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the bonus damage");
    185. p.sendMessage(ChatColor.RED + "Weapons Masters don't fare well against fire.");
    186. p.sendMessage(ChatColor.GREEN + "============================================");
    187. }else if(args[1].equalsIgnoreCase("mage")){
    188. p.sendMessage(ChatColor.GREEN + "============================================");
    189. p.sendMessage(ChatColor.BLUE + "Mages are strong in the art of magic and dispatch their enemies with a nice shiny staff. When fighting with hoes deal bonus true damage and and heal for an amount. (true damage ignores target's armor)");
    190. p.sendMessage(ChatColor.DARK_PURPLE + "Each level you earn increases the bonus true damage and every 5 levels increase the heal");
    191. p.sendMessage(ChatColor.RED + "Mages deal less damage when armed with normal weapons, excluding bows.");
    192. p.sendMessage(ChatColor.GREEN + "============================================");
    193. }else{
    194. p.sendMessage(ChatColor.RED + "Invalid skill. Valid skills: weaponsmaster, ranger, mage, pyro, juggernaut");
    195. }
    196. }else{
    197. p.sendMessage(ChatColor.RED + "Usage: /skill info [skillname]");
    198. }
    199. }else if(args[0].equalsIgnoreCase("level")){
    200. if(!hasSkill(p)){
    201. p.sendMessage(ChatColor.RED + "You don't have a skill type! Choose one with /skill select");
    202. }else{
    203. //Weaponsmaster = 1
    204. //Ranger = 2
    205. //Mage = 3
    206. //Pyro = 4
    207. //Juggernaut = 5
    208. if(getSkill(p) == 1){
    209. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Weapons Master");
    210. }else if(getSkill(p) == 2){
    211. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Ranger");
    212. }else if(getSkill(p) == 3){
    213. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Mage");
    214. }else if(getSkill(p) == 4){
    215. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Pyro");
    216. }else if(getSkill(p) == 5){
    217. p.sendMessage(ChatColor.BLUE + "You are a level " + this.config.getInt(p.getName()) + " Juggernaut");
    218. }
    219.  
    220. }
    221. }else if(args[0].equalsIgnoreCase("train")){
    222.  
    223. }else{
    224. p.sendMessage(ChatColor.BLUE + "====Skill Commands====");
    225. p.sendMessage(ChatColor.BLUE + "/skill select " + ChatColor.RED + "Choose a skill, if you don't have one");
    226. p.sendMessage(ChatColor.BLUE + "/skill list " + ChatColor.RED + "List of all skill types");
    227. p.sendMessage(ChatColor.BLUE + "/skill info " + ChatColor.RED + "Shows information about one skill type");
    228. p.sendMessage(ChatColor.BLUE + "/skill level " + ChatColor.RED + "Shows your skill level, if you have a skill type");
    229.  
    230. }
    231. }else if(args.length == 0){
    232. p.sendMessage(ChatColor.BLUE + "====Skill Commands====");
    233. p.sendMessage(ChatColor.BLUE + "/skill select " + ChatColor.RED + "Choose a skill, if you don't have one");
    234. p.sendMessage(ChatColor.BLUE + "/skill list " + ChatColor.RED + "List of all skill types");
    235. p.sendMessage(ChatColor.BLUE + "/skill info " + ChatColor.RED + "Shows information about one skill type");
    236. p.sendMessage(ChatColor.BLUE + "/skill level " + ChatColor.RED + "Shows your skill level, if you have a skill type");
    237. }
    238. }else{
    239. sender.sendMessage("ERROR: Only players can use /skill");
    240. }
    241.  
    242. }
    243. return false;
    244.  
    245. }
    246. }
    247.  
     
  9. Offline

    mrCookieSlime

    Hex_27
    There is your mistake:

    Code:java
    1. public void saveResearch() {
    2. try {
    3. this.config.save(this.research);
    4. this.config = YamlConfiguration.loadConfiguration(this.research);
    5. } catch (IOException e) {
    6. e.printStackTrace();
    7. }
    8. }


    You are saving all your Skills to the Research.yml instead of saving the actual Researches.
     
  10. Offline

    Hex_27

    mrCookieSlime
    So....I'm supposed to replace this.research with this.reas?
     
  11. Offline

    mrCookieSlime

    Hex_27
    ... Re-read the part I posted. You will notice that you are using this.config.
    Now look up what this.config represents.
     
    Hex_27 likes this.
  12. Offline

    Hex_27

    mrCookieSlime
    God. I keep making mistakes with these files. Anyways, thanks! It finally works! I'm so full of careless mistakes. And I keep posting on this forums because I fail to see them so many times. First its that eventhandler thing and now this. Thanks again! :)
     
  13. Offline

    mrCookieSlime

    Hex_27
    No problem. Please mark this Thread as Filled then.

    And everyone makes mistakes... especially in the beginning. Nobody is perfect.
     
Thread Status:
Not open for further replies.

Share This Page