Working With Configs

Discussion in 'BungeeCord Plugin Development' started by SuperOmegaCow, Jul 22, 2013.

  1. I have posted on the bukkit forums but noticed how little people want to help you so hopefully the spigot community will be more generous. For a plugin that will store player kills and deaths I have done this:
    Code (Text):
       @EventHandler
             public void onPlayerDeath(PlayerDeathEvent event){
             Player killer = event.getEntity().getKiller();
             Player victim = event.getEntity();
               if(killer instanceof Player && victim instanceof Player){
                String killerName = killer.getName();
                String victimName = victim.getName();
                int killerKills = this.getConfig().getInt(killerName + "kills");
                int victimDeaths = this.getConfig().getInt(victimName + "deaths");
                victimDeaths++;
                killerKills++;
                this.getConfig().set(killerName + "kills", killerKills);
     
                this.getConfig().set(victimName + "deaths", victimDeaths);
                }
    But I want to store all the data in a custom config which I tried to implements here:
    Code (Text):
        public static void loadYamls() {
            try {
                Main.config.load(Main.configFile);
                Main.kills.load(Main.killsFile);
                Main.deaths.load(Main.deathsFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void saveYamls() {
            try {
                Main.config.save(Main.configFile);
                Main.kills.save(Main.killsFile);
                Main.deaths.save(Main.deathsFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    Code (Text):
    configFile = new File(getDataFolder(), "config.yml");
                killsFile = new File(getDataFolder(), "kills.yml");
                deathsFile = new File(getDataFolder(), "deaths.yml");
                try {
                    firstRun();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            private void firstRun() throws Exception {
                if(!configFile.exists()){
                    configFile.getParentFile().mkdirs();
                    copy(getResource("config.yml"), configFile);
                }
                if(!killsFile.exists()){
                    killsFile.getParentFile().mkdirs();
                    copy(getResource("kills.yml"), killsFile);
                }
                if(!deathsFile.exists()){
                    deathsFile.getParentFile().mkdirs();
                    copy(getResource("deaths.yml"), deathsFile);
                }
            }
            private void copy(InputStream in, File file) {
                try {
                    OutputStream out = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int len;
                    while((len=in.read(buf))>0){
                        out.write(buf,0,len);
                    }
                    out.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                   
                }
    But in the first block of code how I get the custom configs kill and death and store the data there and not the default config? *Note* I realize that in the EventHandler the getConfig doesnt work it is just a place holder for when I find out how to get custom configs.
     
    • Agree Agree x 1
  2. joehot200

    Supporter

    Yup, a lot of unnecessary code.​
    Once i make the config file, my code for promoting people on their nob kills is like this:​

    Code (Java):

    @EventHandler
    public void onEntityDeathEvent(EntityDeathEvent event){
    if (!(event.getEntity().getKiller() instanceof Player)) return;
    Player p = (Player) event.getEntity().getKiller();
    if (!Main.config.contains(p.getName())){
    Main.config.addDefault(p.getName(), 1);
    return;
    }
    int i = (int) Main.config.get(p.getName());
    Main.config.set(p.getName(), i += 1);
    p.sendMessage(ChatColor.GREEN + "Mob kills: " + ChatColor.ITALIC + Main.config.get(p.getName()));
    if (p.hasPermission("MobRanks.bypass")){
    return; //Stops donators/staff being demoted for killing mobs
    }
    if (i == 20){
    p.getServer().dispatchCommand(p.getServer().getConsoleSender(), "manuadd " + p.getName() + " fighter");
    p.getServer().dispatchCommand(p.getServer().getConsoleSender(), "broadcast &e" + p.getName() + " has killed 20 monsters (With his sword), and is now a fighter!");
    p.sendMessage(ChatColor.GREEN + "Commands gained: / /kit fighter (Speed potion, wood sword, leather armour)");
    }
     
     
  3. Vault. Use it.
     
  4. joehot200

    Supporter

    Could you please link me to the vault documentation where it tells me how to do that?
     
    • Informative Informative x 1
  5. Thanks guys I will post anything if It still does not work.