Solved Best way of instancing a plugin

Discussion in 'Plugin Development' started by ShadowRanger, Dec 29, 2014.

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

    ShadowRanger

    Hey everyone,

    I've just got a quick question which I was hoping I could get an answer from on here. Basically Im have g trouble deciding on a good way to instance a plugin so it can be accessed/retrieved easily in other classes. I understand that there are many different ways you can do it but I'd just like to know if there are any do's and dont's and or efficient ways to do it. Thanks in advance.
     
  2. Offline

    WarmakerT

    Make it a public static variable and you can access it from pretty much anywhere using <class>.<variable>.

    http://bukkit.org/threads/get-static-plugin-instance.107420/

    @teej107 Thanks, corrected.

    If it's your own plugin, do this on the main class:
    Code:
    public static MyPlugin myPlugin;
    
    public void onEnable() {
       myPlugin = this;
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 27, 2017
  3. Offline

    teej107

    @Phoenix_Mawson1
    And that statement makes a good example for my signature.

    Here is how you can access anything from anywhere while following good OOP structure.
    1. Every variable you declare is private.
    2. For every variable you want to get from a different class, make a getter method. (a public method returning the object or primitive)
    3. As long as you have an instance of the object, you can get any public declared member from the object.
    4. Read this: http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
    5. Static is not an access anywhere modifier. Public is an access anywhere modifier, but making fields public destroys encapsulation.
    6. http://stackoverflow.com/questions/19044362/java-encapsulation-concept-not-clear

    Why?!?! WTF. There is no need for the static at all. None. Zip! Nadda! Assigning a variable in a class to this is not needed.

    EDIT: That code goes against almost everything that my last post said.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 25, 2018
    es359, Konato_K and WarmakerT like this.
  4. Offline

    WarmakerT

    Correct me if I'm wrong, but if you remove the static, you will not be able to access it.
    Edit.: Especially since a static method would also require a static variable.
     
  5. Offline

    teej107

    I could quote that rest of my post too honestly.
     
  6. Offline

    Skionz

    You pass it to another class via constructor. He should just make it private, and create a getter (accessor) method.
     
  7. Offline

    nverdier

    Wrong wrong wrong wrong wrong wrong wrong. Static completely goes against the point of OOP. How did you get the idea that you wouldn't be able to access it? Just pass instances to the classes that need it.
     
  8. Offline

    WarmakerT

    Why is encapsulation important in this case?

    @Skionz Both ways work, from what I can see. Unless you know of a reason why one way is preferable over the other.
     
  9. Offline

    teej107

  10. Offline

    ShadowRanger

    Alright thanks for your help so far guys. Here's what I've managed to come up with based on the links @teej107 gave me and what was said on here:

    Code:
        private final myPlugin instance = this;
       
        public final myPlugin getPlugin() {
            return instance;
        }
    
    With myPlugin being the main class/the plugin. I'm not entirely sure if I have done this properly though. Have I? Thanks.
     
  11. Offline

    teej107

    @Phoenix_Mawson1 Creating a getter in the object to get the object is useless. And "myPlugin" is the variable name. It should be the class name.
     
  12. Offline

    ShadowRanger

    @teej107 myPlugin in the example I posted is just a placeholder/example I put to represent the main class, basically it's supposed to be the class name. You also said that creating a getter method in the object to get the object is useless. So are you saying I should create another seperate class specifically for the get method orr? I apologise for my ignorance.
     
  13. Offline

    teej107

    @Phoenix_Mawson1 Lets see if I can explain this.

    You have the object (which is your class). Any object inside your class you should create a getter if needed *but you do not need to create a getter for the object that you already have.

    *Is that explained well?

    If I have Object. I do not need to create a getter inside Object because I already have Object. Anything inside of Object I don't have since Object has it and not me. But creating a getter to get anything inside Object will allow me to get the anything that is inside of object.
     
  14. Offline

    Skionz

    @Phoenix_Mawson1 Basically right now you would be doing
    Code:
    plugin.getPlugin()
    which returns 'plugin' so why not just use 'plugin' to begin with instead of using 'plugin.getPlugin()'
     
    teej107 likes this.
  15. Offline

    teej107

    @Phoenix_Mawson1
    Take this for example. The String! String is an Object and all objects have a toString() method. But calling toString() on a String is redundant because it returns the same thing.
     
  16. Offline

    ShadowRanger

    Okay, thanks for all the help guys, much appreciated. I THINK I understand what you mean now @teej107. Okay so let me provide you with an example of what I think I need to do based on all the advice and help I have been given. Lets say I have a plugin called MyPlugin. I go on to create a new class for whatever purpose and call it ExampleClass. Within the ExampleClass class file, I would need to do something along the lines of the following code:

    Code:
        private final MyPlugin plugin;
    
        public MyPlugin getPlugin() {
            return plugin;
        }
    
    I know what you mean when you say that I don't need to create a getter when I essentially already have what I want there but how am I supposed to 'get' the plugin from another separate class? Would I use what I just put above? But then wouldn't I need to put something in the main class? I understand what your saying but I'm jut not entirely sure how to actually implement it into a plugin if that makes any sense. I must seem so stupid now...
     
  17. Offline

    teej107

    From the code posted, "plugin" would be null since you did not assign it a value. Let me add on to your current code.
    By passing information through constructors, this allows us to assign "plugin" to a variable, like so:
    Code:
    public class ExampleClass
    {
        private final MyPlugin plugin;
        public ExampleClass(MyPlugin objectYouArePassingThrough)
        {
            //Passing an object through the parameter in the constructor
            plugin = objectYouArePassingThrough;
        }
        public MyPlugin getPlugin() {
            return plugin;
        }
    }
    Now since you already have the "plugin" variable in the ExampleClass, making a getter is kind of pointless (since you can just reference "plugin" directly) unless you are passing the ExampleClass object through parameters to a method or constructor that needs the MyPlugin object.
     
  18. Offline

    ShadowRanger

    @teej107 Oh I see what I did wrong and left out. But what am I supposed to make 'objectYouArePassingThrough'? What object would I need to pass through? I thought it would be just 'plugin' but I don't think that would work.
     
  19. Offline

    MisterErwin

    @Phoenix_Mawson1 You were right, you just pass the plugin reference/object to your subclasses:

    Therefore you need a constructor that takes the object/reference that represents your main class as a parameter:

    Code:
    public MySubClassName (MyPlugin _plugin){
    ...
     
    ShadowRanger likes this.
  20. Offline

    teej107

    Variable names don't matter at run time. You can name the "objectYouArePassingThrough" anything you like. You need to pass MyPlugin through the parameters if you want to get an instance of it though.
     
    ShadowRanger likes this.
  21. Offline

    ShadowRanger

    Alright sweet, thanks @MisterErwin and @teej107. It seems to be working now. Thanks heaps, much appreciated!
     
Thread Status:
Not open for further replies.

Share This Page