Can't update config file

Discussion in 'Plugin Development' started by wh1ld, Jul 13, 2014.

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

    wh1ld

    Having trouble with this, keeps saying 0 zombies and not updating after I kill one? Also not getting a /mobmine/config.yml file when running the server.

    Code:
    Code:java
    1. package me.wh1ld.mobmine;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.entity.Zombie;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class MobMine extends JavaPlugin implements Listener {
    16.  
    17. public void OnDisable() {
    18. saveConfig();
    19. }
    20.  
    21. public void OnEnable() {
    22. this.getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. }
    25.  
    26. @EventHandler(priority = EventPriority.HIGHEST)
    27. public void KillZombie(EntityDeathEvent e) {
    28.  
    29. //Killed Entity
    30. Entity deadEntity = e.getEntity();
    31. //Who killed the Entity
    32. Entity sender = e.getEntity().getKiller();
    33.  
    34. if (sender instanceof Player && deadEntity instanceof Zombie) {
    35.  
    36. Player player = (Player) sender;
    37.  
    38. int zkillcount = getConfig().getInt("zombieskilled");
    39. int zkillworth = getConfig().getInt("zombieskilledworth");
    40. getConfig().set("zombieskilled", zkillcount + 1);
    41. player.sendMessage(ChatColor.DARK_GREEN + "Zombie Killed! " + zkillworth + ChatColor.GOLD + "Added to your balance.");
    42. }
    43. }
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    46. Player player = (Player) sender;
    47. if(commandLabel.equalsIgnoreCase("zombiemine")) {
    48. player.sendMessage(ChatColor.GOLD + "" + this.getConfig().getInt("zombieskilled") + ChatColor.DARK_GREEN + " Zombies" + ChatColor.GOLD + " killed in total.");
    49. }
    50. return false;
    51. }
    52.  
    53. }
    54.  
     
  2. Offline

    TheMcScavenger

    At the moment you're only saving the configuration file when you're unloading the plugin. This is bad for two reasons:
    • You have to save it after modifying it, because otherwise you're not getting the proper return when you're requesting it.
    • When you modify the config YAML, and then reload the server, it'll override your modifications with the original file, meaning you lose your modifications.
    FIX:
    • Remove the saveConfig() from the onDisable()
    • Add the saveConfig() below the getConfig().set();
     
  3. Offline

    fireblast709

    wh1ld onEnable and on disable, not OnEnable or OnDisable. Also you haven't registered your events. Moreover you didn't check if sender instanceof Player, which would throw an error if the console would execute the command. Lastly rather use cmd.getName() for alias support
     
  4. Offline

    xTigerRebornx

    TheMcScavenger wh1ld
    Untrue, config values are stored in memory, saving it everytime its modified is just unneeded io. Should save it every-so-often, like every 30min to prevent data loss.
    That is why most plugins implement a 'reload' feature for their configs, to "reload" the data stored in memory.
     
    TheSpherret likes this.
  5. Offline

    wh1ld

    Okay, I've done as you said. New code:

    Code:java
    1. package me.wh1ld.mobmine;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.entity.Zombie;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class MobMine extends JavaPlugin implements Listener {
    16.  
    17. public void onDisable() {
    18. //Disabled
    19. }
    20.  
    21. public void onEnable() {
    22. this.getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. }
    25.  
    26. @EventHandler(priority = EventPriority.HIGHEST)
    27. public void KillZombie(EntityDeathEvent e) {
    28.  
    29. //Killed Entity
    30. Entity deadEntity = e.getEntity();
    31. //Who killed the Entity
    32. Entity sender = e.getEntity().getKiller();
    33.  
    34. if (sender instanceof Player && deadEntity instanceof Zombie) {
    35.  
    36. Player player = (Player) sender;
    37.  
    38. int zkillcount = getConfig().getInt("zombieskilled");
    39. int zkillworth = getConfig().getInt("zombieskilledworth");
    40. getConfig().set("zombieskilled", zkillcount + 1);
    41. saveConfig();
    42. player.sendMessage(ChatColor.DARK_GREEN + "Zombie Killed! " + zkillworth + ChatColor.GOLD + "Added to your balance.");
    43. }
    44. }
    45.  
    46. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    47. Player player = (Player) sender;
    48. if(commandLabel.equalsIgnoreCase("zombiemine")) {
    49. player.sendMessage(ChatColor.GOLD + "" + this.getConfig().getInt("zombieskilled") + ChatColor.DARK_GREEN + " Zombies" + ChatColor.GOLD + " killed in total.");
    50. }
    51. return false;
    52. }
    53.  
    54. }


    Thing is, it's now creating the MobMine/config.yml (Which I'm so fucking happy about) however it's not updating the killcount every time I kill a zombie?

    I haven't registered my events? I'm unsure what you mean by this? :p And when I come to check if sender instanceof player isn't that this bit?
    if (sender instanceof Player && deadEntity instanceof Zombie) {
     
  6. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page