Accessing Custom yml from Main

Discussion in 'Plugin Development' started by IcyFlameX, Jul 23, 2017.

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

    IcyFlameX

    I have 2 config files, named Config.yml and Data.yml
    The config one stores all the values set by the user and the Data.yml stores all the player stats like kills and deaths.
    The problem is i cant store the player stats in data.yml in listener class. The plugin stores it in config.yml
    GTACops Class (Main Class ):

    Code:
    package me.bukkit.IcyFlameX;
    import java.io.File;
    import org.bukkit.*;
    
    import java.util.logging.Logger;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class GTACops extends JavaPlugin {
        File cfile;
        File file;
        FileConfiguration config;
        FileConfiguration data;
        public static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().enablePlugin(this);
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            createConfig();
            new KillClass(this);
            }
    
        @Override
        public void onDisable() {
            DisableConfig();
            ConfigSave();
        }
        private void createConfig() {
            try {
                if (!getDataFolder().exists()) {
                    getDataFolder().mkdirs();
                }
                 file = new File(getDataFolder(), "config.yml");
                 cfile= new File(getDataFolder(), "data.yml");
                if (!file.exists()|| !cfile.exists()) {
                    cfile.createNewFile();
                    getLogger().info("config.yml not found, creating!");
                    getLogger().info("data.yml not found, creating!");
                    getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Enabled");
                    saveDefaultConfig();
                }
                else
                    loadConfig();
            }
            catch (Exception e) {
            e.printStackTrace();
            }
        }
        private void loadConfig() {
            data=YamlConfiguration.loadConfiguration(cfile);
            getLogger().info("config.yml found, loading!");
    
    
    
            getLogger().info("data.yml found, loading!");
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Enabled");
        }
         public void ConfigSave() {
                saveResource("data.yml", true);
                saveResource("confg.yml", true);
            }
        private void DisableConfig()
        {
            getLogger().info("GTACops Disabled");
            Bukkit.getPluginManager().disablePlugin(this);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Disabled");
    KillerClass(Listener Class)

    Code:
    package me.bukkit.IcyFlameX;
    import java.util.List;
    
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.PigZombie;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.Team;
    public class KillClass implements Listener {
        GTACops plugin;
        public static Economy econ = null;
        FileConfiguration config;
        int enemieslength;
        public KillClass(GTACops plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
            config =plugin.getConfig();
            data= plugin.getConfig();
        }
    To save the plugin i m using
    saveResource("data.yml",true); in killer class

    Note: Its not the full main class and Killer Class
    Thanks for your help :D
    I think the issue is in killer class where it says data=plugin.getConfig(); i presume its invoking the config.yml and not the data.yml


    Ok Can someone tell me how to access a custom yml from the main class
    Apparently
    Code:
    public class KillClass implements Listener {
       GTACops plugin;
       public static Economy econ = null;
       FileConfiguration config;
       public KillClass(GTACops plugin) {
           plugin.getServer().getPluginManager().registerEvents(this, plugin);
           this.plugin = plugin;
           config =plugin.getConfig();
           data= plugin.getConfig();
       }
       int enemieslength;
    .getConfig() accesses the config.yml and not my custom yml so to speak "data.yml".
    Anyone knows ?
     
    Last edited by a moderator: Jul 24, 2017
  2. Offline

    Horsey

    You need to use
    Code:
    YamlConfiguration.loadConfiguration(new File(this.getDataFolder(), "data.yml")
     
  3. Offline

    Machine Maker

    @IcyFlameX You are correct. That is the issue. Create a new method in your main class that returns the data FileConfiguration (probably called getData()).
     
  4. Offline

    Zombie_Striker

  5. Offline

    Horsey

    @Zombie_Striker He already said it's from the main class, so he should use this..
     
  6. Offline

    IcyFlameX

    upload_2017-7-24_9-13-12.png
    It shows red color saying u need to define the method
    But isnt getDataFolder predefined method in bukkit ?
     

    Attached Files:

  7. Offline

    Zombie_Striker

    @IcyFlameX
    Are you using this line inside the KillClass class, or the GTACops class? For KillClass, replace "this" with "plugin". If it is the latter, post the full class.
     
  8. Offline

    IcyFlameX

    @Zombie_Striker
    upload_2017-7-24_17-52-29.png

    Line 77:
    Code:
    data.save(cfile);
    Line 82:
    ConfigSave();
    Line 42:
    DisableConfig();

    KillerClass Plugin inheritance Code:
    Code:
    public KillClass(GTACops plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
            config =plugin.getConfig();
            data=YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "data.yml");
    EventHandlerClass code (KillerClass) :
    Code:
    public void WantedLvL(PlayerDeathEvent e) {
            //Player getting killed
            Entity deader = e.getEntity();
            //Player killing
            Entity killer = e.getEntity().getKiller();
            if(killer instanceof Player && deader instanceof Player)
            {      Player player = (Player) killer;
                    int killcount=0;int killcount1=0;
                String path = "Kills." + player.getName();
                String path1 = "Kills." + player.getName();
                if(config.contains(path))
                {
                    killcount1= data.getInt(path1);
                    killcount= config.getInt(path);
                    }
                config.set(path, killcount+1);
                data.set(path1,killcount+1);
                 plugin.saveConfig()
                }
           }
    
        }
    Main Class Code :
    Code:
    public class GTACops extends JavaPlugin {
        File cfile;
        File file;
        FileConfiguration config;
        FileConfiguration data;
        public static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().enablePlugin(this);
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            createConfig();
            new KillClass(this);
            }
    
        @Override
        public void onDisable() {
            try {
                DisableConfig();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        private void createConfig() {
            try {
                if (!getDataFolder().exists()) {
                    getDataFolder().mkdirs();
                }
                 file = new File(getDataFolder(), "config.yml");
                 cfile= new File(getDataFolder(), "data.yml");
                if (!file.exists()|| !cfile.exists()) {
                    cfile.createNewFile();
                    saveDefaultConfig();
                    getLogger().info("config.yml not found, creating!");
                    getLogger().info("data.yml not found, creating!");
                    getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Enabled");
                }
                else
                    loadConfig();
            }
            catch (Exception e) {
            e.printStackTrace();
            }
        }
        private void loadConfig() {
            data=YamlConfiguration.loadConfiguration(cfile);
            config= YamlConfiguration.loadConfiguration(file);
            getLogger().info("config.yml found, loading!");
            getLogger().info("data.yml found, loading!");
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Enabled");
        }
         public void ConfigSave() throws IOException {
                data.save(cfile);
                config.save(file);
            }
        private void DisableConfig() throws IOException
        {
            ConfigSave();
            getLogger().info("GTACops Disabled");
            getServer().getPluginManager().disablePlugin(this);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN+"GTACops Disabled");
        }
        public void ReloadConfig()
         {
              saveResource("config.yml",true);
              reloadConfig();
         }
    
     
    Last edited: Jul 24, 2017
  9. Offline

    timtower Administrator Administrator Moderator

    Merged 3 threads about the same subject.
     
  10. Offline

    IcyFlameX

    Anyone?
     
  11. Offline

    RRGamingZ

    Null pointer on files probably means it does not exist...

    If I'm not mistaken, you have two files. However, if either one does not exist, it creates the data.yml only. What happens to config.yml? You might want to do checks for either of them, then create the file for them seperately.

    For Example,
    Code:
    if (!file.exists()) {
                //create and save file
            } else {
                //do something/say something to console                     
            }
            if (!cfile.exists()) {
                //create and save file
             } else {
                //do something     
             }
    
    You might also want to save them seperately, try using this resource for help. If you create a custom file with a custom name, you might want to load and save it with <fileconfig>.load(<file>); and <fileconfig>.save(<file>); respectively.

    EDIT - now that i saw the other posts, i think my post is irrelevant...
     
    Last edited: Jul 27, 2017
Thread Status:
Not open for further replies.

Share This Page