Using a config [solved]

Discussion in 'Spigot Plugin Development' started by blohod, Jul 12, 2015.

  1. I looked on the wiki and the stuff on configs does not make sense to me, it all seems to be about setting config values from the code, I just want to have a config file with values in and have those values used as ints. here is my code:

    Main:
    Code (Text):
    public class Main extends JavaPlugin {
        // Fired when plugin is first enabled
        @Override
        public void onEnable() {
            // Register our command "rtp" (set an instance of your command class as executor)
            this.getCommand("rtp").setExecutor(new Command());
            saveDefaultConfig();
        }
        // Fired when plugin is disabled
        @Override
        public void onDisable() {
            saveConfig();
        }
    }
    my command, i want int minX, maxX, minY, maxY to be set from the config:
    Code (Text):
    Random randomNo = new Random();
                int minX = 50;
                int maxX = 60;
                int minY = 50;
                int maxY = 60;
                int XCoord = randomNo.nextInt((maxX-minX) + 1) + minX;
                int ZCoord = randomNo.nextInt((maxY-minY) + 1) + minY;
    my config:
    Code (Text):
    * set area for random teleport
    *lowest possible x coordinate
    minX: -100000
    *highest possible x coordinate
    maxX: 100000
    *lowest possible y coordinate
    minY: -100000
    *highest possible y coordinate
    MaxY: 100000
     
  2. Main:
    Code (Text):
    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            saveDefaultConfig();
            this.getCommand("rtp").setExecutor(new MyCommand(this));
        }

        @Override
        public void onDisable() {
            saveConfig();
        }
    }
    MyCommand:
    Code (Text):
    public class MyCommand extends CommandExecutor {
        private Main plugin;
        private Random rand = new Random();

        public MyCommand(Main plugin) {
            this.plugin = plugin;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            int minX = plugin.getConfig().getInt("minX", 50);
            int maxX = plugin.getConfig().getInt("maxX", 60);
            int minY = plugin.getConfig().getInt("minY", 50);
            int maxY = plugin.getConfig().getInt("maxY", 60);
            int XCoord = rand.nextInt((maxX-minX) + 1) + minX;
            int ZCoord = rand.nextInt((maxY-minY) + 1) + minY;
            // more stuffs...
        }
    }
    config.yml:
    Code (Text):

    minX: -100000
    maxX: 100000
    minY: -100000
    MaxY: 100000
    Take notice to passing the instance of your main class to your command executor. This is the key to getting the config from other classes than your main class. ;)
     
    • Like Like x 1
  3. what does this bit do?

    public MyCommand(Main plugin) {
    this.plugin = plugin;
    }

    also line 12 of main is now giving me this error
    The constructor Command() is undefined
     
  4. I told you in my last post. It passes the instance of your main class to your command executor class.
     
    • Like Like x 1
  5. ok, thanks

    also line 12 of main is now giving me this error
    The constructor Command() is undefined
     
  6. You need to pass the instance. Look at my example code. I passed "this" (no quotes) as the parameter.
     
    • Agree Agree x 1
  7. sorry, i missed the this

    but now when i run the command it always teleports me to 0,0. My best guess is bad yml syntax, do you have any ideas?
     
  8. Add some debug messages to see what the values its pulling from config are.
     
    • Like Like x 1
  9. i fixed it, turns out comments are # not *