Any idea why this isn't working?

Discussion in 'Plugin Development' started by hankered, Jun 10, 2014.

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

    hankered

    Can't figure out why this will not work.
    Please help.
    Code:java
    1. if(args[0].equalsIgnoreCase("setspawn")){
    2. if(this.getConfig().contains("crates." + args[1])) {
    3. p.sendMessage("§3Setting spawn for: §6"+args[1]);
    4. this.getConfig().set("crates."+args[1]+".X", p.getLocation().getX());
    5. this.getConfig().set("crates."+args[1]+".Y", p.getLocation().getY());
    6. this.getConfig().set("crates."+args[1]+".Z", p.getLocation().getZ());
    7. this.getConfig().set("crates."+args[1]+".world", p.getLocation().getWorld());
    8. }else{
    9. p.sendMessage("§3Invalid crate name.");
    10. }
    11. }


    Buhmp.

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

    AmShaegar

    Well, what is not working? Any errors? Config not saving? I don't know what your problem is.
     
  3. Offline

    hankered

    AmShaegar

    No, simply nothing happens.

    I want it to modify the config, but it doesn't.
     
  4. Offline

    AmShaegar

    So your config is not saving? I am bad at mind reading ... Is that what you are looking for?

    this.saveConfig();
     
  5. Offline

    hankered


    No, there's already a save config in there.

    Ill say it more in depth.

    - I want it to modify the config
    - When I do the command correctly, it does not modify the config, it simply does nothing.
    Understand? (not in a rude way)
     
  6. Offline

    Rocoty

    Please...don't just post the relevant code. Give us some context. Post all the code.
     
  7. Offline

    hankered

    Rocoty

    Code:java
    1. package me.hankered.crates;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.enchantments.Enchantment;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class Crates extends JavaPlugin implements Listener{
    22.  
    23. private Location blockLoc = new Location( getServer().getWorld(this.getConfig().getString("crates.donorchest1.world")), this.getConfig().getInt("crates.donorchest1.X"), this.getConfig().getInt("crates.donorchest1.Y"), this.getConfig().getInt("crates.donorchest1.Z"));
    24. //private Location blockLoc1 = new Location( getServer().getWorld(this.getConfig().getString("crates.donorchest1.world")), this.getConfig().getInt("crates.donorchest.X"), this.getConfig().getInt("crates.donorchest.Y"), this.getConfig().getInt("crates.donorchest.Z"));
    25. //private Location blockLoc2 = new Location( getServer().getWorld(this.getConfig().getString("crates.donorchest1.world")), this.getConfig().getInt("crates.randomchest.X"), this.getConfig().getInt("crates.randomchest.Y"), this.getConfig().getInt("crates.randomchest.Z"));
    26.  
    27. public void broadcast(){
    28. for(Player p : Bukkit.getOnlinePlayers()){
    29. if(p.hasPermission("crates.recieve")){
    30. p.sendMessage("§3A player has opened a crate!");
    31. }
    32. }
    33. }
    34.  
    35. public void loadConfig(){
    36.  
    37. FileConfiguration config = this.getConfig();
    38.  
    39. List<String> donorchest1 = config.getStringList("crates.donorchest1.rewards");
    40. List<String> donorchest = config.getStringList("crates.donorchest.rewards");
    41. List<String> randomchest = config.getStringList("crates.randomchest.rewards");
    42. donorchest1.add("EMERALD_ORE");
    43. donorchest.add("EMERALD_ORE");
    44. randomchest.add("EMERALD_ORE");
    45.  
    46. config.addDefault("crates.donorchest1.Z", 0);
    47. config.addDefault("crates.donorchest1.Y", 50);
    48. config.addDefault("crates.donorchest1.X", 0);
    49. config.addDefault("crates.donorchest1.world", "world");
    50. config.addDefault("crates.donorchest1.keyname", "key1");
    51. config.addDefault("crates.donorchest1.rewards", donorchest1);
    52.  
    53. config.addDefault("crates.donorchest.Z", 1);
    54. config.addDefault("crates.donorchest.Y", 50);
    55. config.addDefault("crates.donorchest.X", 1);
    56. config.addDefault("crates.donorchest.world", "world");
    57. config.addDefault("crates.donorchest.keyname", "key1");
    58. config.addDefault("crates.donorchest.rewards", donorchest);
    59.  
    60. config.addDefault("crates.randomchest.Z", 2);
    61. config.addDefault("crates.randomchest.Y", 50);
    62. config.addDefault("crates.randomchest.X", 2);
    63. config.addDefault("crates.randomchest.world", "world");
    64. config.addDefault("crates.randomchest.keyname", "key1");
    65. config.addDefault("crates.randomchest.rewards", randomchest);
    66.  
    67.  
    68.  
    69. this.getConfig().options().copyDefaults(true); this.saveConfig();
    70. }
    71.  
    72. @Override
    73. public void onEnable() {
    74. Bukkit.getPluginManager().registerEvents(this, this);
    75. loadConfig();
    76. getLogger().info("Crates has been Enabled!");
    77.  
    78. }
    79.  
    80. @EventHandler
    81. public void clickChest2(PlayerInteractEvent e){
    82. Player p = e.getPlayer();
    83.  
    84. if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    85. if(e.getClickedBlock().getType().equals(Material.CHEST)){
    86. if(p.getItemInHand().hasItemMeta()){
    87. if(e.getClickedBlock().getLocation().equals(blockLoc)){
    88. if(!(p.getInventory().getItemInHand().getItemMeta().getDisplayName().contains("mega krate key"))){
    89. p.sendMessage(ChatColor.DARK_GRAY + ChatColor.ITALIC.toString() + "You do not have the correct key for this chest!");
    90. }
    91. else{
    92. for(int i=0;i<getConfig().getStringList("list").size();i++) {
    93. String s = getConfig().getStringList("list").get(i);
    94. p.getInventory().addItem(new ItemStack(Material.getMaterial(s)));
    95. p.getInventory().getItemInHand().setType(Material.AIR);
    96.  
    97. if(p.getInventory().getBoots().hasItemMeta()){
    98. p.getInventory().getBoots().addUnsafeEnchantment(Enchantment.LUCK, 100);
    99. }
    100.  
    101. }
    102. }
    103. }
    104. }
    105. }
    106. }
    107. }
    108.  
    109. @Override
    110. public boolean onCommand(CommandSender sender, Command command,
    111. String label, String[] args) {
    112.  
    113. Player p = (Player)sender;
    114. if(label.equalsIgnoreCase("crates")){
    115. if(args.length<1){
    116. p.sendMessage("§7Not enough arguements. Did you mean to do /crates help?");
    117. }else if(!(args.length<2)){
    118. if(args[0].equalsIgnoreCase("setspawn")){
    119. if(this.getConfig().contains("crates." + args[1])) {
    120. p.sendMessage("§3Setting spawn for: §6"+args[1]);
    121. this.getConfig().set("crates."+args[1]+".X", p.getLocation().getX());
    122. this.getConfig().set("crates."+args[1]+".Y", p.getLocation().getY());
    123. this.getConfig().set("crates."+args[1]+".Z", p.getLocation().getZ());
    124. this.getConfig().set("crates."+args[1]+".world", p.getLocation().getWorld());
    125. }else{
    126. p.sendMessage("§3Invalid crate name.");
    127. }
    128. }
    129. }else if(!(args.length<1)){
    130. if(!(args.length>1)){
    131. if(args[0].equalsIgnoreCase("help")){
    132. p.sendMessage("§6-Commands-");
    133. p.sendMessage("§3/crates §6setspawn §3[name]");
    134. }
    135. }
    136. }
    137. }
    138.  
    139.  
    140.  
    141. return false;
    142. }
    143.  
    144.  
    145. /
     
  8. Offline

    Rocoty

    That makes the answer crystal clear. Like AmShaegar said you should call this.saveConfig() after setting all the values.
     
    AmShaegar and RingOfStorms like this.
  9. Offline

    hankered

    Rocoty

    So you want me to save right after the

    Code:java
    1. config.addDefault("crates.donorchest.rewards", donorchest);
    2.  
    ?
     
  10. Offline

    Rocoty

    When you set values to the config using the set method like in your first snippet, the changes won't be saved to the file before you call saveConfig.

    tl;dr: No. Not THAT line.
     
Thread Status:
Not open for further replies.

Share This Page