Custom config not saving properly?

Discussion in 'Plugin Development' started by shohouku, Sep 10, 2013.

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

    shohouku

    Code:
    public class quest extends JavaPlugin implements Listener {
        final YamlConfiguration config2 = new YamlConfiguration();
        public Logger logger = Logger.getLogger("Minecraft");
        @Override
        public void onEnable() {
         
            try {
                config2.save("plugins/aozquest1/complete.yml");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    Is anything wrong??

    Because if I do this code:

    Code:
    config2.set(p.getName(), "waiting");
    It won't save my name .

    Whats wrong??? o.o
     
  2. Offline

    Lolmewn

    Create the file first.
     
  3. Offline

    shohouku


    So moving this:
    Code:
        final YamlConfiguration config2 = new YamlConfiguration();
    Into onEnable??

    But if I move the code anywhere else.. I cannot use this code:

    config2.set(p.getName(), "waiting");
     
  4. Offline

    Lolmewn

    shohouku no, just do
    File f = new File(this.getDataFolder(), "customFile.yml");
    if(!f.exists())
    f.createNewFile() ;
     
  5. Offline

    shohouku


    Uhm.. it still doesn't work :confused:
     
  6. Offline

    Lolmewn

  7. Offline

    shohouku


    It's a bit messy... but eh :\

    Code:
    package plugin;
     
     
     
     
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class quest extends JavaPlugin implements Listener {
        public YamlConfiguration config2 = new YamlConfiguration();
       
        Cooldowns cooldowns = new Cooldowns();
        public static quest plugin;
        public static IconMenu im;
        public static IconMenu im1;
        public static IconMenu im2;
        public static IconMenu im3;
        public static IconMenu im4;
        public static IconMenu im5;
        public static quest instance;
        public Logger logger = Logger.getLogger("Minecraft");
        @Override
        public void onEnable() {
     
            File config = new File(this.getDataFolder(), "config.yml");
            if (!config.exists()) {
                this.saveDefaultConfig();  //Tests if config exists. If it does it saves it. If not, it proceeds with generating it
            }     
            try {
                config2.save("plugins/aozquest1/complete.yml");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            instance = this;
            getServer().getPluginManager().registerEvents(this, this);
       
     
            im = new IconMenu("Obtain Flowers", 9, new IconMenu.OptionClickEventHandler() {
     
                @Override
                public void onOptionClick(final IconMenu.OptionClickEvent event) {
                    Player p = event.getPlayer();
                    event.setWillClose(false);
         
                    if (event.getName().equals("Obtain Quest")) {
                        if(!getConfig().contains(p.getName())) {
                            getConfig().set(p.getName(), "Flower");
                            saveConfig();
                            p.sendMessage("Get 10 yellow flowers for Mrs Hare (Obtain 25 EXP Points");
                            p.closeInventory();
                   
                            }
                            else {
                                p.sendMessage("You are already doing this quest!");
                            }
                    }
                    if (event.getName().equals("Complete")) {
    if(getConfig().contains(p.getName())) {
        ItemStack item = new ItemStack(Material.YELLOW_FLOWER, 10);
        if(p.getInventory().contains(Material.YELLOW_FLOWER, 10)){
            p.getInventory().removeItem(item);
           
            p.sendMessage("You have completed the quest Obtain 10 flowers! (Reward: 25 EXP Points");
     
            p.giveExp(10);
            getConfig().set(p.getName(), null);
            config2.set(p.getName(), "waiting");
     
        }else{
        p.sendMessage("You have not collected 10 yellow flowers for Mrs Hare yet!");
        p.closeInventory(); 
                            }
                      }else {
                                  p.sendMessage("You are currently not doing this quest!");
                                  p.closeInventory();   
                        }
                      }
                    if (event.getName().equals("Abandon")) {
                        if(!getConfig().contains(p.getName())) {           
                                        p.sendMessage("You are currently not doing this quest!");
                                        p.closeInventory();
                                                }
                        if(getConfig().contains(p.getName())) {
                            getConfig().set(p.getName(), null);
                            p.sendMessage("Successfully left.");
                            p.closeInventory();
                                    }
                                          }
                }
            }, this)
            .setOption(0, new ItemStack(Material.PAPER, 1), "Obtain Quest", "Get the following quest")
            .setOption(1, new ItemStack(Material.BOOK, 1), "Complete", "Press Complete when you have finished the quest.")
            .setOption(2, new ItemStack(Material.BOOK, 1), "Abandon", "Abandon the current quest.");
     
        }
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
          Player p = e.getPlayer();
          Block b = e.getClickedBlock();
       
       
          if(b.getState() instanceof Sign){
                Sign sign = (Sign) b.getState();
                if(sign.getLine(0).equals("[ObtainFlowers]")){
                    if(!config2.contains(p.getName())) {
                        im.open(p);
                    } else {
                       
                          if (Cooldowns.tryCooldown(p, "MagicHand", 15000)) {
                              config2.set(p.getName(), null);
                 
                         
                          } else {
                              // Cooldown hasn't expired yet
                              p.sendMessage("You have " + (Cooldowns.getCooldown(p, "MagicHand") / 1000) + " seconds left until doing the daily quest again!");
                          }
       
     
            }
                }
               
     
         
      }
        }
    }
     
     
       
     
       
     
    
     
  8. Offline

    Lolmewn

    shohouku You're only creating config.yml, not complete.yml. It's the same idea as for config.yml, but then with complete instead of config ;)
     
  9. Offline

    shohouku

    I am creating complete.yml xD

    Code:
     try {
                config2.save("plugins/aozquest1/complete.yml");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    The yml is in the folder but it doesn't save? ...
     
  10. Offline

    Lolmewn

    shohouku No you're not. You should create the file seperatly.
     
  11. Offline

    shohouku


    But if I use your method...

    I cannot use f.save and such :\

    I don't get ittttttttttttt zzz..
     
  12. Offline

    Lolmewn

    shohouku
    Just open the file, then use config2.save(f);
     
  13. Offline

    ShadowDog007

    shohouku

    You need to save the config after you change it.

    You can either save it every time you make a change, or you could save it in the onDisable() method.

    Also 'saveDefaultConfig()' doesn't copy config.yml if it already exists. So you don't need to check it yourself.
     
  14. Offline

    shohouku


    Thank you so much!!! <3
     
Thread Status:
Not open for further replies.

Share This Page