Many weird problems with my config

Discussion in 'Spigot Plugin Development' started by alessio2010, May 15, 2017.

  1. Hello! I just started learning about configs and i'm having problems getting a message from my config and saving all of the comments in my config. I'm trying to make it get the string helpermessage but it's not getting it..

    Here is my code
    https://hastebin.com/afuroguxuh.java
    And there is an error in the console
    https://hastebin.com/uxecalivac.vbs

    How to fix
     
  2. Just to make this clear,

    The following classes aren't required for what you do, can be simplified with the spigot methods

    Code (Text):

    FileConfiguration config;
           File cfile;
     
    Obviosly your error is a nullpointerexception that means somewhere is a not existing file.
    Have you checked your config file ? Does it exist ?
     
  3. I don't see how it's not existent..
     
  4. https://www.spigotmc.org/wiki/config-files/ Go read that, it will help clear up your confusion. Your basically using default config stuff along side non-standard config stuff which might work eventually but its like having a flat tire and flattening the other 3 so the car sits level :confused:
     
    • Winner Winner x 1
  5. Code (Text):
    Plugin plugin = this;
    Configuration config = plugin.getConfig();

        void initConfig() {
            if (!getDataFolder().exists()) {
                getDataFolder().mkdirs();
            }
            File file = new File(getDataFolder(), "config.yml");
            if (!file.exists())
                plugin.saveDefaultConfig();
            config = plugin.getConfig();
        }

        @Override
        public void onEnable() {
            initConfig();
            }
     
    Now once you have this you can access the configuration thru the variable config.

    String msg;
    msg = config.getString("helpermessage");

    Now do whatever you need to do to display msg to the player.
     
    • Winner Winner x 1
  6. For some reason it's not grabbing what i wrote in the config for the string helpermessage
     
    • Like Like x 1
  7. WAS

    WAS

    What is the problem now? Error if any? What is happening that is unexpected now, and what is expected?
     
  8. Alright so, There are no errors what so ever. I am trying to set a join message to a player taken from the config but it's not taking it from the config it's taking the one that if there's nothing in the config but there is.. i added it.
     
  9. My comments in my config aren't showing up either
     
  10. WAS

    WAS

    Well, initially you getting the config, which doesn't exist yet on line 26.

    Than you are loading cfile, for some reason. You have already loaded the config on line 34, you don't need cfile, or loading any YamlConfiguration. And the variable could be FileConfiguration because getConfig() is already loading a YamlConfiguration.
     
  11. in onEnable remove this
    cfile = new File(getDataFolder(), "config.yml");

    in onCommand change

    if (player.hasPermission("staffjoin.reload")) {
    config = YamlConfiguration.loadConfiguration(cfile);
    saveConfig();
    player.sendMessage(ChatColor.GREEN + "Config succesfully reloaded!");
    return true;
    }

    to this

    if (player.hasPermission("staffjoin.reload")) {
    initConfig();
    player.sendMessage(ChatColor.GREEN + "Config succesfully reloaded!");
    return true;
    }

    Your onJoin is a mess
    String msg;
    msg = config.getString("helpermessage");
    e.setJoinMessage(config.getString(msg, "" + ChatColor.YELLOW + e.getPlayer().getName() + " joined the game" ).replaceAll("%PLAYER%", e.getPlayer().getName().toString()).replaceAll("&", "§"));

    this should be more sane but it assumes the helpermessage from your config is something like "&e%PLAYER% joined the game"

    String msg = config.getString("helpermessage").replaceAll("%PLAYER%", e.getPlayer().getName().toString());
    e.setJoinMessage(ChatColor.translateAlternateColorCodes('&', msg));

    and make sure your default config.yml is in your plugin and remove the existing one from your prior attempts from the plugin folder on the server, this code doesn't overwrite it
     
    • Winner Winner x 1
  12. WAS

    WAS

    His config var should already be loaded with the config onEnable() wjere initConfig() is first used, shouldn't need to run the method again in the command section.
     
  13. @WAS he has a reload command, i assume for editing the config by hand then reloading it in game.
     
    • Winner Winner x 1
  14. ok you have an extra msg = in there
    remove msg = config.getString("helpermessage");
    you already defined it in the line above, by doing this your deleting the name out of it.

    also is there an error or just not working as intended?

    and remember when you put the updated plugin in it wont delete the old existing config.yml in your plugins folder on the server, you gotta do that by hand before you start the server
     
    • Winner Winner x 1
  15. also
    File cfile;
    is never used remove that line
     
    • Winner Winner x 1
  16. Yea thats a null point error, repost your code again
     
    • Winner Winner x 1