Solved Getting data from config returns null

Discussion in 'Plugin Development' started by Grief'd man, Feb 2, 2014.

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

    Grief'd man

    Hello! This is my old account, I'm in the process of changing it all.

    Anyways, back to the point. I have a small plugin for testing economies. Saving data works just fine! It's when I wish to load data stored inside is when it becomes a problem.

    Here is my one-class code. (Remember, just for testing)
    Code:
    package me.dudemister.crimsongaming;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CrimsonGaming extends JavaPlugin implements Listener
    {   
       
        public boolean log = true;
       
        public YamlConfiguration yc = YamlConfiguration.loadConfiguration(getResource("economy.yml"));
        File cf = new File(getDataFolder(), "economy.yml");
       
        public void log(Level level, String message)
        {
            if(log)
            {
                getLogger().log(level, message);
            }
        }
       
        public void enableListeners()
        {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        public void loadData()
        {
           
        }
       
        @Override
        public void onEnable()
        {
            enableListeners();
            loadData();
            unpack();
        }
       
        @Override
        public void onDisable()
        {
           
        }
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) throws IOException
        {
            Player player = event.getPlayer();
            String message = event.getMessage();
            String[] args = message.split(" ");
            String cmd = args[0];
           
            if(message.equalsIgnoreCase(".setupecon"))
            {
                log(Level.INFO, "Setting up economy...");
                player.sendMessage(ChatColor.RED + "Setting up economy...");
                yc.set("config.log", true);
                yc.set("config.seedmoney", "150");
                yc.set("config.bonusmoney", "100");
                yc.set("config.bank.min", "0");
                yc.set("config.bank.max", "1000000000");
                yc.save(cf);
                event.setCancelled(true);
            }
            if(message.equalsIgnoreCase(".money_give"))
            {
                log(Level.INFO, "Giving player " + player.getName() + " 1,000,000 dollars.");
                player.sendMessage(ChatColor.RED + "Giving you $100.");
                yc.set("player."+player.getName()+".balance", "100");
                yc.save(cf);
            }
            if(message.equalsIgnoreCase(".money"))
            {
                String balance = yc.getString("player."+player.getName()+".balance");
                log(Level.INFO, "Balance of player " + player.getName() + ": " + balance);
                player.sendMessage(ChatColor.RED + "Your current net worth: " + balance);
                event.setCancelled(true);
            }
        }
       
        private void unpack()
        {
            try
            {
                if (cf.exists())
                {
                    log(Level.INFO, "Economy detected, skipping unpacking process...");
                }
                else
                {
                    log(Level.INFO, "No economy detected, creating new one...");
                    yc.save(cf);
                }
            }
            catch (IOException e)
            {
                log(Level.SEVERE, "Could not create economy!");
            }
        }
    }
    
    Now, for the explanation.

    This part here returns null on reload, even though the config shows it has data:
    Code:
    String balance = yc.getString("player."+player.getName()+".balance");
    I don't quite have enough experience with this, so that's why I made a demo plugin for it, so I can use it on my main projects.

    Thanks for reading!
    -Alex / Dudemister1999
    (On my old Bukkit Forum Account)

    Also, I don't get an error, just this part right here:
    Code:
    log(Level.INFO, "Balance of player " + player.getName() + ": " + balance);
    Says this:
    Balance of player Dudemister1999: null

    I solved it. I forgot that getResource() gets a file inside the jar, which cannot be edited. Here is the code I used to fix it:
    Code:
    logUtil.info(prefixUtil.log() + "Loading economy.");
    if (statsConfigFile.exists())
    yc = YamlConfiguration.loadConfiguration(statsConfigFile);
    else
    yc = YamlConfiguration.loadConfiguration(getResource("stats.yml"));

    EDIT: That's from my main project.

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

Share This Page