Getting config inside Command?

Discussion in 'Plugin Development' started by Larsn, Jun 23, 2015.

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

    Larsn

    I want my class modeCommand to use this from my config:
    Code:
    path:
      to:
        string: Hello world :D
    But this isn't inside the main class. So how can I get the Config inside this class?
     
  2. Offline

    Boomer

    in your main class, you load/edit/save the config... and keep the config as a class-scoped variable.
    Then add a public getter function that returns the config, and call that function in your other class to access the config object
     
  3. You can do what Boomer said above, or you do what I prefer, and that is you add in your plugin class a public static JavaPlugin instance; variable (you can change JavaPlugin with your plugin class) and in the onEnable() you set the instance variable to this (the class instance). And then you can call the instance variable from other classes and call the getConfig() method.
     
  4. Offline

    Larsn

    Like this?
    Code:
    Object PathToString = this.getConfig.getString("path.to.string");
    Could make an example for this?
     
  5. this is kinda how you want your main plugin class to be

    public static JavaPlugin instance;

    onEnable(){
    instance = this;
    }

    and this is the other class

    Main.instance.getConfig()

    and Main is the name of the main plugin class.
     
    Larsn likes this.
  6. Offline

    Larsn

    Thanks a lot,
    but I am getting the Lore from a tool, but when I equal it to a string from the config, it doesn't do anything:
    Code:
    if(p.getItemInHand().getItemMeta().getLore().equals(Main.instance.getConfig().getString("LoreName")))
     
  7. Offline

    MCMatters

    @Larsn can we see more code and is there any stacktraces?
     
  8. Offline

    Larsn

    Code:
                        else if(Main.instance.getConfig().getBoolean("LoreCheck") == true){
                            if(p.getItemInHand().getType() == Material.getMaterial((Main.instance.getConfig().getString("Tool")))) {
                                if(p.getItemInHand().getItemMeta().getLore().equals(Main.instance.getConfig().getString("LoreName"))){
                                   // Do something
                                } else {
                                    p.sendMessage("Message");
                                }
                            }
                        } else {
                           p.sendMessage("Message");
                    }
     
  9. Offline

    MCMatters

    @Larsn where do you save the LoreCheck boolean let me see the code for that
     
  10. Offline

    Larsn

    It's in the config:
    # Check for lore
    LoreCheck: false

    # If LoreCheck = true, for what lore should it check?
    LoreName: 'AdvancedPickaxe'
     
  11. Offline

    MCMatters

  12. Offline

    Larsn

    The full config or the full class?
     
Thread Status:
Not open for further replies.

Share This Page