Bukkit: Get data from a configuration file returns null

This one has been bothering me all day. I've been googling, searching and asking around to no avail.

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)

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, here is what happens:

Everything works great. It saves, and updates the config. All except for when I want to load it. Here's what it shows in the log & console:

Balance of player Dudemister1999: null

It does that until I give myself money.

Any ideas?

Answers


It turns out that getResource gets files that are inside the plugin jar.

Files inside jars cannot be edited from java through streams.

You are trying to read from the file inside the jar each time as opposed to the file you create inside the data folder. You will need to create a function that loads the appropriate file:

public YamlConfiguration yc;
//...
public void loadData()
{
    if (cf.exists())
        yc = YamlConfiguration.loadConfiguration(cf);
    else
        yc = YamlConfiguration.loadConfiguration(getResource("economy.yml"));
}

On a side note:

You should also use setInt and getInt and research into the standard Bukkit command system. (The one that uses the plugin.yml and command handlers that handle permissions and the like properly.)


Need Your Help

Threading Exception: The number of WaitHandles must be less than or equal to 64

vb.net multithreading threadpool

The title is to make this easy to find for others having this error. I'm new to Threading, so this is really giving me heck. I'm getting this runtime error that crashed Cassini. This is code that I'm

Fetching last rows in MySQL

mysql sql

I'd like to retrieve the last ocurrence of each user from the table below.