[SOLVED] I can't make a config for multiple classes?

Discussion in 'Spigot Plugin Development' started by shieken, Jun 27, 2015.

  1. So im working on am multi-class plugin, and im trying to make a config for it. Whats extremely annoying though is that it just wont work. So far ive tried:

    1. making the command classes extend JavaPlugin

    2. Making the command classes extend the main

    3. Running my plugin with the errors

    4. Trying to make the config public (which would give me errors)

    5. Posting my issue on bukkit (no one ever goes on anymore)

    6. Trying to find an online solution (none)

    Im gonna try to study some plugins with multi-class configs but in the meantime what can i do to fix this issue?

    -shieken

    (and no im not posting my code on here, its pretty pointless to do so)
     
  2. You have to send an instance of the main class to the other classes that you want to have access to the Bukkit methods
     
  3. Use constructors to send main instance.
     
  4. Your main class should extend JavaPlugin,

    Your command class should implement CommandExecutor.

    In your command class, you need to pass an instance of the main class, like this:

    private Main instance;

    public Command(Main instance) {
    this.instance = instance;
    }

    Now that you have that, you call your config methods in your command class like:

    this.instance.getConfig().<method>;
     
    • Useful Useful x 1
  5. I did extend JavaPlugin

    I did implement the command executor

    And i definitely will try doing this. Thanks for the help
     
  6. Thanks, it worked. Any way I can put the main instance into a variable though?
     
  7. Code (Text):
    private Main instance;
    This is your variable... I highly suggest going through the basic Java tutorials on the Oracle site.
     
    • Like Like x 1
  8. I know its my variable idiot. Im asking if theres a way to reduce this into a variable since this.instance.getConfig().getString() is pretty annoying to type.

    And since you dont think I know java, i guess i should just explain it so you know i do

    private: Tells the variable is private, in other words only accessible by the class it's in

    Main: the class that the variable is being based from, in this case the main class in your plugin

    instance: the variable name. If i changed instance to i or something, i would have to change lines of code that use instance.

    Ex: this.instance.getConfig().getString() would become this.i.getConfig().getString()

    Its just like you billy to start fights on threads isnt it?
     
    • Optimistic Optimistic x 1
  9. Pass the config rather than the Main class then?
     
  10. maldahleh

    Wiki Team

    The way described above is the best way of doing it, there is no real other way to reduce it besides making the variable name shorter.
     
  11. I like having my plugins use multiple classes though. Why? It's way more organized

    Ah, oh well. That sucks
     
  12. Pretty rude to call the people who are helping you idiots, especially considering the question you have in the first place is a basic java question (and on top of the fact that per the question he replied to, his reply made sense).

    That being said, you can easily just store the FileConfiguration in a field:

    Code (Java):
    private final Main plugin;
    private final FileConfiguration config;

    public YourClass(Main plugin) {
        this.plugin = plugin;
        this.config = this.plugin.getConfig();
    }

    public void doSomething() {
        this.config.getString("butts");
    }
     
  13. Oh, trust me, Billy isnt helping me. Billy and I have a bad relationship.
     
  14. He directly answered your question correctly, and offered advice which would be helpful. You insulted him in return. As far as I can see, that's fairly instigative on your part.
     
  15. Ok, i dont want billy to help me. Why dont I? Because hes close minded and doesnt listen to other ideas, as well as starts fights

    if you cant tell that this is supposed to be a rude comment, then theres a problem
     
  16. Code (Text):

    public YourClass(FileConfiguration config) {

    }
    I was thinking more of that ^ not doing everything in one class.;)
     
    • Like Like x 1
  17. It's not rude at all, if you're missing an understanding of some basic java concepts (which in this case, you are), then reviewing those tutorials would be immensely helpful. There is information in those tutorials that I can easily cite which a good 90-95% of the users on this forum wouldn't know about (and I actually got to test that).

    One good starting point would be http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
     
  18. Except i dont need tutorials...
     
  19. Learning concepts which would help for these exact situations beforehand might be helpful though, wouldn't it?
     
  20. Instead of having,

    Code (Text):
    private Main instance;

    public Commands(Main instance) {
    this.instance = instance;
    }
    You can write less by adding a method and by shortening name of Main instance field,

    Code (Text):
    private Main in;

    public Commands(Main instance) {
    in = instance;
    }

    private YamlConfiguration getString(String path) {
    return in.getConfig().getString(path);
    }

    // I don't even think you need the method above but if you're really determined to write less, then you can use that above  method.

    So, let's say you wanted to send a player a message.

    You can write,

    sender.sendMessage(getString("MAIN" + ".sender-message));