Config saving problem?

Discussion in 'Plugin Development' started by MrGriefer_HGTech, Jun 18, 2015.

Thread Status:
Not open for further replies.
  1. Hello Bukkit Dev!

    So I am working on a plugin and I want players when they type /createarena [ArenaName] it will save the arena name on the config file which is called arena.yml. I tried to that but it wont save nor send me message telling me that the arena was created, there are no console errors. Here is my code:

    Code:
    package me.MrGriefer_.SAS.Commands;
    
    import me.MrGriefer_.SAS.Arena;
    import me.MrGriefer_.SAS.ArenaCfg;
    import me.MrGriefer_.SAS.ArenaManager;
    import me.MrGriefer_.SAS.CommandInfo;
    import me.MrGriefer_.SAS.GameCommand;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    
    @CommandInfo(description = "Create an arena.", usage = "[Arena]", aliases = { "createarena" }, op = true)
    public class CreateArena extends GameCommand {
       
        private Arena arena;
       
        ArenaCfg arenaConfig = ArenaCfg.getInstance();
       
        public void onCommand(Player p, String[] args) {
            if (args.length == 0) {
                arena.prefix(p, "You must specify a name for the arena.");
                return;
            }
           
            String name = args[0];
           
            if (ArenaManager.getInstance().getArena(name) != null) {
                arena.prefix(p, "That Arena already Exists!");
                return;
            }
           
            arenaConfig.getArenas().addDefault(name, name);
            arenaConfig.getArenas().addDefault(name + ".Max-Players", Integer.valueOf(6));
            arenaConfig.getArenas().addDefault(name + ".Auto-Start-Players", Integer.valueOf(8));
            arenaConfig.getArenas().addDefault(name + ".End-Time", Integer.valueOf(500));
            Arena arena = new Arena(name);
            ArenaManager.addArena(arena);
            ArenaManager.addToList(arena);
            arenaConfig.saveArenas();
            arena.prefix(p, "You have created the Arena: " + ChatColor.RED + arena.getName());
        }
    }
    arenaConfig.getArenas: It's just a file named arenas.yml, Here:
    Code:
    package me.MrGriefer_.SAS;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    
    public class ArenaCfg {
    
        File afile;
        FileConfiguration arenas;
       
        static ArenaCfg plugin = new ArenaCfg();
       
        public static ArenaCfg getInstance() {
            return plugin;
        }
       
        public void setup(Plugin p) {
            if (!p.getDataFolder().exists()) {
                p.getDataFolder().mkdir();
            }
            afile = new File(p.getDataFolder(), "arenas.yml");
            if (!afile.exists()) {
                try {
                    afile.createNewFile();
                } catch (IOException e) {
                    p.getLogger().severe("[COD: SAS] Could not create the arenas.yml!");
                }
            }
        }
       
        public FileConfiguration getArenas() {
            return arenas;
        }
       
        public void saveArenas() {
            try {
                arenas.save(afile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe("[COD: SAS] Could not save arenas.yml!");
            }
        }
       
        public void reloadArenas() {
            arenas = YamlConfiguration.loadConfiguration(afile);
        }
       
    }
    
    If you look up there is: arenaConfig.getArenas().addDefault(); <--- I think its not working? Maybe?

    If you want me to put the rest of the code tell me.

    Thanks for reading and helping!

    ~ Sorry for my bad English!
     
  2. The method is
    onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    And the class shold extend JavaPlugin.
    I don't know which outdated version of bukkit you're using or where you learned this non working code but do something against it
     
  3. Offline

    Drkmaster83

    The rest of the code is probably gonna be necessary. To me, it seems like your FileConfiguration 'arenas' would be null.

    @FisheyLP Not in particular. If the class extended JavaPlugin, that would be the case, yes... You have much to learn, grasshopper. His class extends GameCommand, an unknown (at least, at the moment) external class, which could have its own implementation for handling commands.
     
  4. @Drkmaster83 I think you are correct about the FileConfiguration would be null, but how can I make it work?

    Also here is the GameCommand class:
    Code:
    package me.MrGriefer_.SAS;
    
    import org.bukkit.entity.Player;
    
    public abstract class GameCommand {
    
        public abstract void onCommand(Player p, String[] args);
    }
     
  5. Anyone? :rolleyes:

    bump

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

    NickenatorDev

    In your setup method in your ArenaCfg class simply put:
    Code:
    areans = p.getConfig();
    
    arenas.options().copyDefaults(true);
    Also make sure that you have the following in your onEnable method in your main class:
    Code:
    ArenaCfg.getInstance().setup(this);
     
Thread Status:
Not open for further replies.

Share This Page