Solved How to save something in config

Discussion in 'Plugin Help/Development/Requests' started by Bowser_Jr, Jan 19, 2015.

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

    mythbusterma

    @Synic


    Except not, I wasn't kidding when it is just "getConfig()," that's all you need, no qualifiers.
     
  2. Offline

    Synic

    But how can you call getConfig() without getting an instance from the main class (which extends JavaPlugin)?

    (Assuming that the main class doesn't implement CommandExecutor)
    @mythbusterma
     
  3. Offline

    mythbusterma

    @Synic

    Because it is in the class? Function calls within a class do not need qualifiers. "this.instance.getConfig()" implies that it is in the class that extends JavaPlugin, otherwise he seriously needs to rethink his variable names, as a reference to that class shouldn't be called "instance."

    EDIT: Oh, I see where you posted that.

    Why in the world would you store another reference to the configuration? It's nothing but a waste. Also, don't just ask the plugin manager for your plugin, pass a reference to it around, and invoke getConfig() on it as necessary.
     
  4. Offline

    Synic

    Could you show me an example? (Sorry if it looks like I'm hijacking the thread but your response to my question may help the OP as well).
    @mythbusterma
     
  5. Offline

    Bowser_Jr

  6. Offline

    mythbusterma

    @Synic

    In your class that extends JavaPlugin:

    Code:
    /* Where SomeClass is the other class*/
    
    SomeClass foo = new SomeClass(this);
    In SomeClass:

    Code:
    JavaPlugin plugin;
    
    public SomeClass(JavaPlugin plugin) {
          this.plugin = plugin;
    }
    At any point in SomeClass, you can refer to the main class via "plugin," e.g. "plugin.getConfig()".

    Also, there's nothing stopping you from chaining these references, i.e.

    In SomeClass:

    Code:
    ReferringClass otherClass = new otherClass(this.plugin);
    (Where "this" is only used for clarity, and ReferringClass is set up similarly to SomeClass)
     
  7. Offline

    Synic

    @mythbusterma
    Alright, I think I get it but I want to make sure.

    Code:
    public class mainClass extends JavaPlugin {
    
    CommandClass cmds = new CommandClass(this);
    
    public void onEnable() {
    saveDefaultConfig();
    }
    
    public void onDisable() {
    saveConfig();
    }

    Code:
    public class CommandClass implements CommandExecutor {
    
    private JavaPlugin plugin;
    
    public CommandClass(JavaPlugin plugin) {
    this.plugin = plugin;
    }
    
    }
    What's the point of the "cmds" object created in the class that extended JavaPlugin?
     
  8. Offline

    nverdier

  9. Offline

    Synic

    @mythbusterma gave me an example of how I should properly reference other classes and I made a more detailed example to see if I got the concept right. However, in his example he constructs a "SomeClass" object named "foo" so I created a "CommandClass" object named "cmds". I don't understand the significance of the instantiation of that object in the main class.
     
  10. Offline

    nverdier

    @Synic Oh I see. So the 'cmds' object creates a new instance of the "CommandClass" class, and you put the "this" inside of the "new CommandClass(this)" so it passes an instance of the mainClass class to the instance of the CommandClass class, so it the CommandClass can access mainClass without statics etc. So with that 'cmds' Object, you can do getCommand("test command").setExecutor(cmds);

    Now with this case, you wouldn't need to create the field because you're only using 'cmds' once (when you set the executor), so you could just do
    Code:
    getCommand("testcommand").setExecutor(new CommandClass(this);
    Not sure if that makes any sense, but hope it helped a bit :D
     
  11. Offline

    Synic

    That was EXACTLY what I needed. It's all clear to me now. Thank you so much!
     
    nverdier likes this.
  12. Offline

    nverdier

    @Synic Glad it made sense! And glad to be of help :D
     
    Synic likes this.
Thread Status:
Not open for further replies.

Share This Page