Question about package layout

Discussion in 'Plugin Development' started by DutchChris, May 9, 2020.

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

    DutchChris

    Hello!

    I'm really new to bukkit and everything that comes with it which is why I regularly get stuck. A problem I often get is with classes/subclasses, which comes from the fact that I don't know where I should put what. My question this time is how a good plugin is built: whats the best place to store things like commands/methods/variables? Which class should be my main class and what should be in the main class? And the last question: What does public/private mean and what's their use, I read that public means it can be accessed from all other classes while private means only the class it's written in can use it. Thing is that I don't see a benefit in making something private, why would I want to limit the access of something?

    Sorry if this was a bit much, but I really do appreciate all the help and it really helps me develop as a 'developer'.

    - DutchChris
     
  2. Offline

    KarimAKL

    I don't really know how to answer this one but, you can create a new class for every command in your plugin, and put them all in a package named 'command' or 'commands'.
    For methods and variables, it really just depends where you need to access them.

    The main class should usually be named after your plugin, e.g. ExamplePlugin -> ExamplePlugin.java.
    For the main class, you want to extend 'JavaPlugin', then override the method 'onEnable()', and if you need to do something upon the plugin being disabled, override the method 'onDisable()' as well.

    There's 4 access modifiers, "public", "private", "protected", and "default".
    public: Every class with a reference to the method can access it.
    private: Only the same file (note: file, not class) can access it.
    protected: Subclasses and files in the same package can access it.
    default: Files in the same package can access it.

    To declare these:
    Code:Java
    1. public void exampleMethod() {}

    Code:Java
    1. private void exampleMethod() {}

    Code:Java
    1. protected void exampleMethod() {}

    Code:Java
    1. void exampleMethod() {}


    Why would you limit access?:
    I'm not sure how exactly i'd answer this but, you could for example have an 'init()' method you don't want to be called more than once, then you can limit access to it and only call it once in the same file.
    I can't really think of another example at the moment.
     
  3. Offline

    DutchChris

    Thanks Man! This really helps me a lot and I appreciate the help a ton!
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page