Config values from other classes

Discussion in 'Plugin Development' started by pc_h8r, Feb 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    pc_h8r

    Hello, how can I get values from my config file in classes other that the main class? And can someone help me with contractors...I know that is an easy way to share variables but I cant find a good explanation on how to correctly use them.
     
  2. Offline

    AmShaegar

    Did you mean "constructors"? How does you code look like at the moment? Just a rough outline.
     
  3. Offline

    pc_h8r

    Oh lol, yes "Constructors" spell check was a little off:)

    there is a lotof code because I am updating a plugin and adding features for myself.

    So if you could give some examples...

    Here is part of my class that I need to access the config:

    Code:
    public class CommandStartMatch implements CommandExecutor {
        private WarFighter plugin;
        private final String PREFIX = ChatColor.DARK_PURPLE + "[WarFighter] ";
        public int timeLimit;
       
        public CommandStartMatch(WarFighter plugin) {
            this.plugin = plugin;
        }
       
        public void getTime(){
            timeLimit = Integer.getInteger("General.time limit");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (label.equalsIgnoreCase("startmatch")) {
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("tdm")) {
                        if (sender.hasPermission("wf.start.tdm")) {
                            this.plugin.getServer().broadcastMessage(this.PREFIX + "Starting new match of type \"Team-Deathmatch\"!");
                            EntityDamageByEntityEvent.getHandlerList().unregister(WarFighter.ff_handler);
                            this.plugin.getServer().getPluginManager().registerEvents(WarFighter.ff_handler, this.plugin);
     
                            WarFighter.dead = new ArrayList<Entity>();
                            WarFighter.deaths = new HashMap<Player, Integer>();
                            WarFighter.respawn_left = new ArrayList<Integer>();
                            WarFighter.scores = new HashMap<Player, Integer>();
     
                            WarFighter.isDeatmatch = false;
     
                            this.plugin.getServer().broadcastMessage(
                                    this.PREFIX + ChatColor.DARK_GREEN
                                            + "New match started!");
                            this.plugin
                            .getServer()
                            .broadcastMessage(
                                    this.PREFIX
                                            + timeLimit + " minutes left in the \"Deathmatch\"!");
                        } else {
                            sender.sendMessage(this.PREFIX
                                    + "You don't have permission!");
                        }
                    } else if (args[0].equalsIgnoreCase("dm")) {
                        if (sender.hasPermission("wf.start.dm")) {
                            this.plugin
                                    .getServer()
                                    .broadcastMessage(
                                            this.PREFIX
                                                    + "Starting new match of type \"Deathmatch\"!");
                            this.plugin
                            .getServer()
                            .broadcastMessage(
                                    this.PREFIX
                                            + timeLimit / 60 + " minutes left in the \"Deathmatch\"!");
                            EntityDamageByEntityEvent.getHandlerList().unregister(
                                    WarFighter.ff_handler);
     
                            this.plugin
                                    .getServer()
                                    .getPluginManager()
                                    .registerEvents(WarFighter.dm_handler,
                                            this.plugin);
     
                            WarFighter.dead = new ArrayList<Entity>();
                            WarFighter.deaths = new HashMap<Player, Integer>();
                            WarFighter.respawn_left = new ArrayList<Integer>();
                            WarFighter.scores = new HashMap<Player, Integer>();
     
                            WarFighter.isDeatmatch = true;
     
                            this.plugin.getServer().broadcastMessage(
                                    this.PREFIX + ChatColor.DARK_GREEN
                                            + "New match started!");
                        }
                    } else {
                        sender.sendMessage(this.PREFIX + ChatColor.RED
                                + "Known match types: dm, tdm");
                    }
                }
            }
            return true;
        }
    }
    Well, just have it all:)
     
  4. Offline

    Lecrayen

    You should be able to just type WarFighter.<config> assuming you have already made the config and said config is in the main class
     
  5. Offline

    AmShaegar

    Reagarding constructors: Take a look at your one.
    Code:
        public CommandStartMatch(WarFighter plugin) {
            this.plugin = plugin;
        }
    You can add as many parameters as you want. So you might do the same with your config what you did with the plugin.
    Code:
        private Config config;
        private WarFighter plugin;
     
        public CommandStartMatch(WarFighter plugin, Config config) {
            this.plugin = plugin;
            this.config = config;
        }
    You have to replace Config by the type of config you are using. That is how you do it. Now, you have your config available in CommandStartMatch.
     
  6. Offline

    pc_h8r

    do I need to do anything special in the main class?
     
  7. Offline

    Lecrayen

    if you got any red squiggles, make sure you put in "this" in the parenths in the args in the main class.
     
Thread Status:
Not open for further replies.

Share This Page