plugin.yml not saving on restart/reload

Discussion in 'Plugin Development' started by maxecito, Apr 15, 2015.

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

    maxecito

    Hey guys!
    Simple problem: my config doesnt save on restart. ._.
    Code:
    package at.skyblock.main;
    
    import java.io.File;
    
    import org.bukkit.Location;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener {
    
       public void onEnable() {
         this.getConfig().set("RPG.villagerspawn", "false");
         PluginManager pm = getServer().getPluginManager();
         pm.registerEvents(new Events(this), this);
         pm.registerEvents(this, this);
         String fileName = getDataFolder()
             + System.getProperty("file.separator") + "config.yml";
         File file = new File(fileName);
         if (!file.exists()) {
           loadConfiguration();
         }
    
       }
    
       public void onDisable() {
         saveConfig();
       }
    
       public void loadConfiguration() {
    
         final FileConfiguration config = this.getConfig();
         config.options().copyDefaults(true);
         saveDefaultConfig();
    
       }
    
       @EventHandler
       public void onPlayerJoin(PlayerJoinEvent event) {
         Player p = event.getPlayer();
         p.sendMessage("Playing as: "
             + getConfig().getString("RPG.folk." + p.getName()));
    
         if (getConfig().getBoolean("RPG.villagerspawn") == false) {
           getConfig().set("RPG.villagerspawn", true);
           Location loc = p.getLocation();
    
           Villager starter_villager_elf = loc.getWorld().spawn(loc,
               Villager.class);
    
           /**
            * loc.setX(-620); loc.setY(4); loc.setZ(-1181);
            **/
    
           starter_villager_elf.setProfession(Villager.Profession.LIBRARIAN);
           starter_villager_elf.setCustomName("§a§lGaladriel");
           starter_villager_elf.setCustomNameVisible(true);
           starter_villager_elf.setAdult();
           starter_villager_elf.setAgeLock(true);
    
           starter_villager_elf.addPotionEffect(new PotionEffect(
               PotionEffectType.SLOW, 100000000, 100));
           this.getConfig().set("RPG.quests.villager.starter_villager_elf",
               starter_villager_elf.getUniqueId());
         }
    
         p.sendMessage(this.getConfig().getString(
             "RPG.quests.villager.starter_villager_elf"));
         if (!p.hasPlayedBefore()) {
           p.openInventory(at.skyblock.main.Inventory.class_selector);
           this.getConfig().addDefault("RPG.folk." + p.getName(), "");
           this.getConfig().addDefault("RPG.folk.class" + p.getName(), "");
    
         }
       }
    
    }
    
     
  2. Invisible

    nverdier

    @maxecito Instead of adding a default, use FileConfiguration#set(String path, Object value)
     
  3. Offline

    maxecito

    @nverdier , Im sorry, but Im not even close to understanding what you would do.
    set the object for every path? I dont even need all at the start... Can you please give me an example?
     
  4. Invisible

    nverdier

    @maxecito Everywhere you have 'this.getConfig().addDefault' use '.set' instead of '.addDefault'.
     
    undeaD_D likes this.
  5. Offline

    undeaD_D

    Code:
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener {
    
        private FileConfiguration config;
    
        public void onEnable() {
            config = getConfig();
            config.set("RPG.villagerspawn","false");
            config.options().copyDefaults(true);
            saveConfig();
      
            getServer().getPluginManager().registerEvents(new Events(this), this);
            getServer().getPluginManager().registerEvents(this, this);
        }
      
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
      
            if(!config.getBoolean("RPG.villagerspawn")) {
                config.set("RPG.villagerspawn",true);
              
                // loc.setX(-620);loc.setY(4);loc.setZ(-1181);
              
                Villager starter_villager_elf = p.getLocation().getWorld().spawn(p.getLocation(), Villager.class);
                    starter_villager_elf.setProfession(Villager.Profession.LIBRARIAN);
                    starter_villager_elf.setCustomName("§a§lGaladriel");
                    starter_villager_elf.setCustomNameVisible(true);
                    starter_villager_elf.setAdult();
                    starter_villager_elf.setAgeLock(true);
      
                    starter_villager_elf.addPotionEffect(new PotionEffect( PotionEffectType.SLOW, 100000000, 100));
      
                config.set("RPG.quests.villager.starter_villager_elf", starter_villager_elf.getUniqueId().toString());
            }
      
            p.sendMessage(this.getConfig().getString("RPG.quests.villager.starter_villager_elf"));
          
            if(!p.hasPlayedBefore()){
                p.openInventory(at.skyblock.main.Inventory.class_selector);
                config.set("RPG.folk." + p.getName(), "");
                config.set("RPG.folk.class" + p.getName(), "");
            }
          
            saveConfig();
        }
      
    }
    made some changes ... seems to work now ;)

    ps: .getUniqueID().toString() may be what you need :p
     
  6. Invisible

    nverdier

Thread Status:
Not open for further replies.

Share This Page