Bug updating config? Need quickly!

Discussion in 'Plugin Development' started by sooon_mitch, Feb 8, 2014.

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

    sooon_mitch

    I am a more experienced coder but there is a bug I am having trouble fixing and cant find any threads or anything about it. It may be simple of something I am over looking. I want to update my my int (iall) to equal a set amount in the config as shown:
    Code:
    //configHandler
     
        private Main plugin;
     
        private int iall = 0;
     
        public configHandler(Main plugin){
              this.plugin = plugin;
        }
     
        public void setArenaHills(String arenaname, Player player)
        {
            iall = plugin.cfg.getInt("arenas."+arenaname+".hills.amount");
           
            Bukkit.broadcastMessage("Amount of hill spawns: "+iall);
            Bukkit.broadcastMessage("Actual config: " + plugin.cfg.getInt("arenas."+arenaname+".hills.amount"));
            Bukkit.broadcastMessage("Name: "+arenaname);
            Bukkit.broadcastMessage("Player: "+player.getName());
           
            plugin.cfg.set("arenas."+arenaname+".hills."+iall,"");
            plugin.cfg.set("arenas."+arenaname+".hills."+iall+".world",player.getWorld().getName());
            plugin.cfg.set("arenas."+arenaname+".hills."+iall+".x",player.getLocation().getX());
            plugin.cfg.set("arenas."+arenaname+".hills."+iall+".y",player.getLocation().getY());
            plugin.cfg.set("arenas."+arenaname+".hills."+iall+".z",player.getLocation().getZ());
           
            iall++;
           
            plugin.cfg.set("arenas."+arenaname+".hills.amount", iall);
           
            plugin.saveConfig();
        }
    Then my on Command which is also in my main class:
    Code:
    if(cmd.getName().equalsIgnoreCase("sethill") && args[0].length() > 0)
    {
    chandler.setArenaHills(args[0], player);
    player.sendMessage(message + "Created a hill spawn for " + args[0]);
    }
    
    My Config.yml
    Code:
    arenas:
      timmy:
        hills:
          amount: 0
    
    It does not throw off any errors and it adds a first '0' but I have to restart or reload the server to use the command again. Completely lost at this moment, any help would be thankful.

    Bump

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

    HungerCraftNL

    plugin.cfg = ?
     
  3. Offline

    sooon_mitch

    Sorry for the slow reply: Here is the Main and the onEnable. The plugin.cfg is represented below.
    Code:
        private static Main instance;
        SpawnHill spawnhill;
     
        FileConfiguration cfg;
     
        ArenaHandler arenahandler = new ArenaHandler(this);
        configHandler chandler = new configHandler(this);
     
        String message = ChatColor.GREEN + "<KOTH> " + ChatColor.YELLOW +"";
     
        public static Main getInstance(){
              return instance;
        }
     
        public void onEnable()
        { 
            instance = this;
         
            cfg = this.getConfig();
         
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new onPlayerMove(this), this);
            getServer().getPluginManager().registerEvents(new onPlayerJoin(this), this);
         
            this.saveDefaultConfig();
         
            this.saveConfig();
         
            if(getConfig().get("arenas") != "")
            {
                arenahandler.updateServerList();
            }
        }
     
  4. Offline

    HungerCraftNL

    I always use a SettingsManager class for this, here is an exemple:

    First creating the class, and make the virables.
    PHP:
    FileConfiguration arena;
    File afile;
    Give acces, with the instance way:
    PHP:
    static SettingsManager instance;
    public static 
    SettingsManager getInstance(){
      return 
    instance;
    }
    After that you just create the methodes:
    PHP:
      public void setupArena(Plugin p)
      {
        
    this.afile = new File(p.getDataFolder(), "arena.yml");
     
        if (!
    this.afile.exists()) {
          try {
            
    this.afile.createNewFile();
          }
          catch (
    IOException e) {
            
    Bukkit.getServer().getLogger().severe(ChatColor.RED "Could not create arena.yml!");
            
    e.printStackTrace();
          }
        }
        
    this.arena YamlConfiguration.loadConfiguration(this.afile);
      }
     
      public 
    FileConfiguration getArena() {
        return 
    this.arena;
      }
     
      public 
    void saveArena() {
        try {
          
    this.arena.save(this.afile);
        }
        catch (
    IOException e) {
          
    Bukkit.getServer().getLogger().severe(ChatColor.RED "Could not save arena.yml!");
        }
      }
     
      public 
    void reloadArena() {
        
    this.arena YamlConfiguration.loadConfiguration(this.afile);
      }
     
      public 
    void setupSign(Plugin p)
      {
        
    this.snfile = new File(p.getDataFolder(), "signs.yml");
     
        if (!
    this.snfile.exists()) {
          try {
            
    this.snfile.createNewFile();
          }
          catch (
    IOException e) {
            
    Bukkit.getServer().getLogger().severe(ChatColor.RED "Could not create spawns.yml!");
          }
        }
        
    this.sign YamlConfiguration.loadConfiguration(this.snfile);
      }
     
  5. Offline

    sooon_mitch

    This still does not answer my question. Why is it, that when I use the command (/sethill timmy) it will add the location that I am standing at to the config. As so:
    Code:
    players: ''
    arenas:
      timmy:
        hills:
          amount: 1
          '0':
            world: world
            x: 490.0606307778554
            y: 4.0
            z: 803.7885694879427
    But I can only do this command once for it to work. After that it stops working. Then when I reload/restart the server and type the command again it works and then looks like this:
    Code:
    players: ''
    arenas:
      timmy:
        hills:
          amount: 2
          '0':
            world: world
            x: 490.0606307778554
            y: 4.0
            z: 803.7885694879427
          '1':
            world: world
            x: 490.0606307778554
            y: 4.0
            z: 803.7885694879427
    But like the first time I can only type the command once before I have to reload the server again. What is causing this and please explain. I try to learn from my mistakes then having the answer handed to me. Thank you for helping me so far tho!

    Bump

    Bump also thinking maybe because its not static?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page