Trouble with Config.yml Files

Discussion in 'Spigot Plugin Development' started by bmac20, Apr 15, 2017.

  1. Here is my code so far:
    Code (Text):
    package me.chromenized.firstplugin;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class FirstPlugin extends JavaPlugin implements Listener {

        public void onEnable() {
            this.getConfig().options().copyDefaults(true);
            this.saveDefaultConfig();
        }

        public void onDisable() {
            saveConfig();
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (cmd.getName().equalsIgnoreCase("test")) {
                this.getServer().broadcastMessage(this.getConfig().getInt("PlayerMessage") + "");
            }

            return true;
        }

    }
     
    This is my config.yml file that I made where plugin.yml is:
    Code (Text):
    PlayerMessage: 5
    I originally had the value set to 4, and when I ran /test, it printed out "4". But when i changed the 4 to a 5 in the config.yml in eclipse and then reexported it, the config continued to generate 4 instead of 5...
     
  2. Strahan

    Benefactor

    Did you reload it after changing it? The game needs to know you diddled with the config file externally. Also if you are using the data in a String related function, get .getString instead.
     
  3. I found a better way to create a config file and it works wonderfully. My next question is about storing items in config files.

    Here is my class:
    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (cmd.getName().equalsIgnoreCase("test")) {
                this.getServer().broadcastMessage(this.getConfig().getInt("PlayerMessage") + "");
                int id = this.getConfig().getInt("ItemToGive");
               
                @SuppressWarnings("deprecation")
                ItemStack item = new ItemStack(id, 1);
                Player p = (Player)sender;
                p.getInventory().addItem(item);
            }

            return true;
        }
    My config file:
    Code (Text):
    PlayerMessage: 5
    ItemToGive: 49
    This successfully gives me one obsidian because thats what I defined, but how would I make the amount customizable so instead of
    ItemToGive: 49, it would be ItemToGive: 49 7 and that would give me 7 obsidian. Would I split on the space or what?
     
  4. Strahan

    Benefactor

    You can set it up however you like. Space delimiting, tab delimiting, commas whatever. Just so long as you stick to the chosen format you'll be fine. You shouldn't be using numeric item IDs though, Mojang is moving away from that. Personally, I'd just have the system serialize an ItemStack to the config. Also you are still getting an int for use in a String method, there is no point to that. It doesn't matter if the data in the config.yml is an integer - you can still .getString which is what you should be doing when using it in a String method to avoid hacky shit like + "".

    PS: If your code has a line to suppress deprecration, you're doing it wrong. Don't be lazy; go learn the right way to do stuff instead of using deprecated methods.