Problem with Configuration file

Discussion in 'Spigot Plugin Development' started by Mariozgr8, Apr 17, 2017.

  1. Hi, I have a problem with my configuration file, so I am trying to code a plugin that will set a spawn location to the world that players can tp to using /spawn.
    Here is my code:
    Code (Text):
    package me.mariozgr8.finalserverplugin;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin{
     
        @Override
        public void onEnable()
        {
            getLogger().info("Final Server Plugin has been enabled");
            this.getCommand("heal").setExecutor(new HealCommand(this));
            this.getCommand("feed").setExecutor(new FeedCommand(this));
            this.getCommand("fly").setExecutor(new FlyCommand(this));
            this.getCommand("god").setExecutor(new GodCommand(this));
            this.getCommand("tp").setExecutor(new TeleportCommand(this));
            this.getCommand("tphere").setExecutor(new TeleportHereCommand(this));
            this.getCommand("spawn").setExecutor(new SpawnCommand(this));
            this.getCommand("setspawn").setExecutor(new SetSpawnCommand(this));
         
            new PlayerListener(this);
         
            getConfig().options().copyDefaults(true);
        }
     
        @Override
        public void onDisable()
        {
            getLogger().info("Final Server Plugin has been disabled");
        }

    }
    Code (Text):
    package me.mariozgr8.finalserverplugin;

    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class SpawnCommand implements CommandExecutor {
        public SpawnCommand(MainClass plugin){
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED+"This command can only be performed by a player!");
                return true;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("spawn"))
            {
                double x = getConfig().getConfigurationSection("spawn.x");
                double y = getConfig().getConfigurationSection("spawn.y");
                double z = getConfig().getConfigurationSection("spawn.z");
                World w = getConfig().getConfigurationSection("spawn.world");
                if(args.length == 0)
                {
                    p.sendMessage(ChatColor.AQUA+"Teleportnig to spawn....");
                }
            }
            return false;
        }

    }
    Code (Text):
    package me.mariozgr8.finalserverplugin;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SetSpawnCommand extends JavaPlugin implements CommandExecutor{
        public SetSpawnCommand(MainClass plugin){
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED+"This command can only be performed by a player!");
                return true;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("setspawn") && sender instanceof Player)
            {
                if(args.length >= 1)
                {
                    p.sendMessage(ChatColor.AQUA+"Too many arguments!");
                    return true;
                }
                getConfig().set("spawn.x", p.getLocation().getX());
                getConfig().set("spawn.y", p.getLocation().getY());
                getConfig().set("spawn.z", p.getLocation().getZ());
                getConfig().set("spawn.world", p.getWorld().toString());
                p.sendMessage(ChatColor.AQUA+"Spawn set to your location!");
                return true;
            }
            return false;
        }

    }
    I get a problem with the last Class the SpawnCommand, I get an error with the getConfig(). And I can't seem to be able to figure out what should I do. Can anyone help me please ?
     
  2. Try this if it don't work seed the config .

    Code (Text):
    package me.mariozgr8.finalserverplugin;

    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class SpawnCommand implements CommandExecutor {
        public SpawnCommand(MainClass plugin){
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED+"This command can only be performed by a player!");
                return true;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("spawn"))
            {
                int x = getConfig().getConfigurationSection("spawn.x");
                int y = getConfig().getConfigurationSection("spawn.y");
                int z = getConfig().getConfigurationSection("spawn.z");
                World w = getConfig().getConfigurationSection("spawn.world");
                if(args.length == 0)
                {
                    p.teleport(new Location(w,x,y,z));
                    p.sendMessage(ChatColor.AQUA+"Teleportnig to spawn....");
                }
            }
            return false;
        }

    }
     
    • Winner Winner x 1
  3. PLEASE LEARN JAVA

    Mini Lesson:

    You must create a config.yml, just like you need a plugin.yml for the Bukkit API to refer to, well you need a config.yml.

    *MAIN IS THE 1st CLASS TO LOAD YOU MUST PUT IT IN MAIN*

    Code (Text):
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        this.getConfig().options().copyDefaults(true);
        saveDefaultConfig();
    }

    @Override
    public void onDisable() {
        saveDefaultConfig();
    }
    When you start the server you want it to load the config, else it wont work.

    ------------------------------------------------------------------------------------------------
     
    • Like Like x 1
  4. you have extended JavaPlugin in ur main class and setspawncommand class. You must extend javaplugin only in your main class.
    To get the config from the main class. Provide the main class to other classes during their construction. Use this instance to get the configuration.

    And you dont need a config.yml file in your jar like the above member stated. You can have it, but u can also have spigot auto generate it for you. Use the following in the main class on enable.
    Code (Text):
    getConfig().options().copydefaults(true);
    saveConfig();
     
    • Like Like x 1
  5. Okay thanks for your help guys :)