Retrieving config

Discussion in 'Plugin Development' started by ehArcher, Mar 11, 2014.

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

    ehArcher

    I have my main class, and a class called CommandSpawn.
    I am trying to retrieve the config from my Main class and use it in CommandSpawn.

    How would I make it so that it loads the configuration from Main?
     
  2. Offline

    MrGermanrain

    I am assuming these are 2 different classes you are using ?
     
  3. Offline

    ehArcher

    Yes. Sorry. I'm just really annoyed by this, didn't think out what I was saying.
     
  4. Offline

    MrGermanrain

  5. Offline

    Wizehh

    Use a constructor in your other class:
    PHP:
    // Where 'Main' is your main class's name
    Main plugin;
     
    // Where 'ClassName' is your other class's name
    public ClassName(Main plugin) {
        
    this.plugin plugin;
    }
     
    /*
    * Calling fields and/or methods:
    * plugin.getConfig()...
    */
     
  6. Offline

    ehArcher

    Code:java
    1. public class CommandSpawn extends JavaPlugin implements CommandExecutor {
    2.  
    3. public CommandSpawn(tgMain plugin) {
    4. getConfig();
    5. }
    6.  
    7. @Override
    8. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    9.  
    10. Player player = (Player) sender;
    11.  
    12. if(cmd.getName().equalsIgnoreCase("setspawn")) {
    13. getConfig().set("spawn.world", player.getWorld().getName());
    14. getConfig().set("spawn.x", player.getLocation().getBlockX());
    15. getConfig().set("spawn.y", player.getLocation().getBlockY());
    16. getConfig().set("spawn.z", player.getLocation().getBlockZ());
    17. saveConfig();
    18. player.sendMessage(ChatColor.YELLOW + "Lobby spawn-point has been set at this position.");
    19. }

    That's in my CommandSpawn class, which executes my spawn command.
    My main class is:
    Code:java
    1. public class tgMain extends JavaPlugin implements Listener {
    2.  
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public PluginDescriptionFile pdfFile = getDescription();
    5. tgMain plugin;
    6.  
    7. @Override
    8. public void onEnable() {
    9.  
    10. FileConfiguration config = getConfig();
    11. config.addDefault("Test.String", "Testing your test");
    12. config.options().copyDefaults(true);
    13. saveConfig();


    It doesn't do anything... :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. Offline

    Gater12

    ehArcher
    Have you implemented Wizzeh's code? It seems like you have not.
     
  8. Offline

    MrGermanrain

    Have you tried using the tutorial I sent your way... It explains whats happening a bit more detailed then this. Maybeyour going to have to do a bit of work reading though in the end you will be able to use this ;).
     
  9. Offline

    ehArcher

    Yeah, I'm just going to take a look at that. I can't really explain what my problem is here :D. Thanks for all the help guys anyways.

    He didn't finish the tutorial all the way... :/.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    Wizehh

    I showed you exactly what to do.
     
  11. Offline

    ehArcher

    It doesn't work.
    In my CommandSpawn class I have:
    Code:java
    1.  
    2. public tgMain plugin;
    3.  
    4. public CommandSpawn(tgMain plugin) {
    5. this.plugin = plugin;
    6. }

    and
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("setspawn")) {
    2.  
    3. plugin.getConfig().set("spawn.world", player.getWorld().getName());
    4. plugin.getConfig().set("spawn.x", player.getLocation().getBlockX());
    5. plugin.getConfig().set("spawn.y", player.getLocation().getBlockY());
    6. plugin.getConfig().set("spawn.z", player.getLocation().getBlockZ());
    7. plugin.saveConfig();
    8. player.sendMessage(ChatColor.YELLOW + "Lobby spawn-point has been set at this position.");
    9.  
    10. }


    In my main class I have:
    Code:java
    1. tgMain plugin;
    2.  
    3. @Override
    4. public void onEnable() {
    5.  
    6. FileConfiguration config = getConfig();

    it does nothing whenever I try to /setspawn
     
  12. Offline

    Wizehh

    ehArcher
    PHP:
    // In your primary class, in the onEnable()
    getCommand("setspawn").setExecutor(new CommandSpawn(thisthis);
     
  13. Offline

    ehArcher

    Already have it.
     
  14. Offline

    Wizehh

    Okay, what exactly is the problem? Does the plugin enable? Does the console log execution of the command?
     
Thread Status:
Not open for further replies.

Share This Page