Beginner Programming Mistakes and Why You're Making Them

Discussion in 'Spigot Plugin Development' started by Choco, Oct 14, 2017.

  1. At least understand the basics of an OOP language also the basics of Java
    (I recommand java 8)
    The key to success at everything is to use the brain and understand it
    Once you get the general idea of what's it and understand it you'll quickly find your way in it and find it very easy my friend
  2. Lol.
  3. Very useful thread
  4. My ressource got rejected most probebly because most of my methods in the main class are static but please, answer one question, whats the difference between using Main.getInstance().whatever() or just Main.whatever()? .getInstance() is static! Why do I get an instance from a class where only one instance can exist via a static method and not just make everyhting static. It doesnt make a fkin difference. Its not more readable, its not gonna make it faster, its not gonna make it better, its just personal preference!

    Its also not a mistake if you do it on purpose. And also I am not a beginner. I programm since I am 10 years old.
    #104 EmnichtdaYT, Oct 12, 2020
    Last edited: Oct 12, 2020
  5. drives_a_ford


    I'll start off by saying I am not part of the resource staff team. Thus, I cannot directly comment on the decision of the resource staff member who rejected your plugin.

    If you make all the methods in a class static, you're making a utility class (although these are also frowned upon in some circles, but that's another matter). Utility classes should not be instantiated.
    The class that extends JavaPlugin is not a utility class. It does get instantiated.
    When you create a static method or field, you're declaring that this method or field belongs to the class rather than an instance of said class. Which (most of the time) is not the case. In your case you're (most likely) trying to interact with your plugin instance rather than the class.

    I always like to point out this quora post.
    • Like Like x 1
  6. With the first paragraph you proofed that you're a beginner.
    Just instantiate your listener etc. and pass your dependencies using dependency injection.

    Yes the Javaplugin is by definition a singleton. You're other classes must not and you end up getting nullpointerexceptions because stuff you depend on isn't initialised at that moment.
  7. Nope, these methods are static for a reason. Most of these methods do something with static variables in the class which mostly get their correct value on plugin load/enable but they all have default values if the arent set yet. If I use some sort of a method which uses the config or the server or something I use getInstance(). I still think its personal preference because it doesnt make a difference.

    Also there are methods which fire events in my main, making them non static would be retarded I hope we can agree on that.

    I could have also wrote all of these static methods in a utils class but I decieded not to do so.
    #107 EmnichtdaYT, Oct 12, 2020
    Last edited: Oct 12, 2020
  8. drives_a_ford


    Again, you've made these variable static. However, they (likely) do not belong to the class but the instance. They should not be static.
    static is not an access modifier.

    While I've not seen your code, I do not see a reason why something like that would need to be static.
  9. Oof, because they can be acsessed always & from everywhere. There is no need for an instance!

    Its still preference. I prefear to have some default values if the plugin happens to not load rather than no way to get these values
  10. Share your code and a lot of people will happily give you detailed critism
  11. Won't change my oppinion ;3, just gonna do what the SpigotMC gods say, NONSTATIIICC
  12. They have a reason, but if you just want to do what is needed, nobody is stopping you from it. But then don't whine on the forums.
    • Optimistic Optimistic x 1
  13. It's not about an opinion.
    Here is the "definition" of how it should be used.

    But, again, it's hard to point out exactly why what you did is wrong, without the code.
  14. drives_a_ford


    I guess you didn't read what I wrote:
    It's not preference. And I've given you a number of reasons why.
    Most of the fields and methods you've made static belong to the plugin instance and not the class. Thus they should not be static.

    Let's look at, for example, this tutorial:
    The variables are not independent of objects because in order for your plugin to be run, an instance needs to be created.
    The values are not shared across multiple objects.
    Since most of your variables depend on objects and should thus not be static, your methods should also not bestatic.
    Your class is not a utility class.

    One could always argue that the JavaPlugin extension is a singleton.
    However, looking at the description of the singleton pattern, it'll be pretty clear that it refers to one single object of a class. Its methods are still non-static, only the the method used to access the singleton is static.

    Perhaps the SO question Why are static variables considered evil will also be of help to you.
    • Agree Agree x 1
    • Informative Informative x 1
  15. I don't disagree at all, but resource rules shouldn't be stricter than Bukkit itself with the org.bukkit.Bukkit class that practically does nothing else than using static as an access modifier to the Server interface.
    • Agree Agree x 1
  16. drives_a_ford


    Then again, Bukkit is provided for free and premium resources are not.

    I'm not sure why or when this class was implemented. I certainly wasn't around these parts of the interwebs back then. I've always tried to refrain from using it in favor of traditional object oriented approaches such as dependency injection.