Help config resets ever reboot

Discussion in 'Spigot Plugin Development' started by Ayresy, Jun 1, 2015.

  1. Code (Text):
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
        String uuid = p.getUniqueId().toString();
        if(!getConfig().contains("Players."  + uuid + ".kills"));
        getConfig().set("Players." + uuid + ".kills", 0);
        String kUUID = p.getUniqueId().toString();
        int kills = getConfig().getInt("Players." + kUUID + ".kills");
        p.setDisplayName(ChatColor.GOLD +"[" + ChatColor.RED + kills + ChatColor.GOLD + "] "+ ChatColor.RED + p.getName() + ChatColor.WHITE);
        saveConfig();
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e){
        Player p = e.getEntity();
        if(p.getKiller() instanceof Player){
            Player k = p.getKiller();
            String uuid = p.getUniqueId().toString();
            String kUUID = k.getUniqueId().toString();
            int kills = getConfig().getInt("Players." + kUUID + ".kills");
            int kills2 = getConfig().getInt("Players." + uuid + ".kills");
            getConfig().set("Players." + kUUID + ".kills", kills++);
            saveConfig();
           
             if(e.getEntity() instanceof Player){
                  if(e.getEntity().getKiller() instanceof Player){
                      if(e.getEntity().getKiller() != null){
                    ItemStack stack = k.getItemInHand();
                      if(stack.getType() == Material.AIR){            
                       e.setDeathMessage("§6[§c" + kills2 + "§6]" + "§c" + p.getName() + " §ewas slain by "  + "§6[§c" +
                        kills + "§6]" + "§c" + k.getName() + " §eusing their §cFist§e!");
                       saveConfig();
                      }else{
                         String item = "";
                         if((stack.hasItemMeta()) && stack.getItemMeta().hasDisplayName()){
                             item = stack.getItemMeta().getDisplayName();
                             e.setDeathMessage("§6[§c" + kills2 + "§6]" + "§c" + p.getName() + " §ewas slain by "  + "§6[§c" +
                                        kills + "§6]" + "§c" + k.getName() + " §eusing §c" + item + "§e!");
                             saveConfig();
                         }else
                            {
                              String[] stringArray = stack.getType().toString().split("_");
                              for (int i = 0; i < stringArray.length; i++) {
                                item = item + stringArray[i].charAt(0) + stringArray[i].substring(1).toLowerCase() + (i == stringArray.length - 1 ?
                                        "" :  "");
                                e.setDeathMessage("§6[§c" + kills2 + "§6]" + "§c" + p.getName() + " §ewas slain by "  + "§6[§c" +
                                        kills + "§6]" + "§c" + k.getName() + " §eusing §c" + item + "§e");
                                saveConfig();
                               
    every time i reboot the server the config resets itself
     
  2. Inkzzz

    Resource Staff

    Can we see your onEnable / onDisable methods?
     
  3. Code (Text):
    public void onEnable() {
            getLogger().info("has been enabled!");
            getCommand();
            getListeners();
            saveDefaultConfig();
            saveConfig();
    sure
     
  4. Inkzzz

    Resource Staff

    remove the saveConfig()
     
  5. Inkzzz

    Resource Staff

    and you want getConfig().options().copyDefaults(true); above saveDefaultConfig();
     
  6. Think it's fixed now, thanks !
     
  7. Inkzzz

    Resource Staff

    No problem!
     
  8. Still doesn't work, resets every time i reboot the server
     
  9. Is your event listener in your main class or not?
     
  10. yes it is
     
  11. Haha, I might have found your problem ;D

    Code (Text):
    if(!getConfig().contains("Players."  + uuid + ".kills"));
        getConfig().set("Players." + uuid + ".kills", 0);
    `

    You are closing the if statement with ;
    before running the set function.

    So the kills will always be set to 0 ;D
     
    #11 SubSide, Jun 2, 2015
    Last edited: Jun 2, 2015
  12. of damnn i didn't even see that xD hahah thanks
     
    #12 Ayresy, Jun 2, 2015
    Last edited: Jun 2, 2015