Beginner Programming Mistakes and Why You're Making Them

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

  1. so this is a bad way?

    Code (Text):
    public class Main extends JavaPlugin {
        private static Main instance;  
       
        public void onEnable() {
            instance = this;
        }
       
        public static Main get() {
            return instance;
        }
    }
    and this for other classes?

    Code (Text):
    public class OnMessage {
        private static OnMessage instance = null;

        public static OnMessage get() {
            return instance == null ? instance = new OnMessage() : instance;
        }
    }
     
  2. Maximvdw

    Benefactor

    Just because you are not planning to extend something now doesn't mean you never will. I've originally created FeatherBoard (anti flicker) in August 2014 for a server. It only had to do specific things and freelancing meant I had to get things done quickly. Eventually that server never paid, I had a good idea and I wanted to sell it on spigot. In my rush I indeed had the mistake of creating a "Scoreboard" class with static map containing players and scoreboard instances...

    I had no intention to change that, of all possible scenario's there was no scenario in my head that said why I would need to waste more time on it to get it fixed.... until protocol hacks and things like viaversion became a thing that actually required to send different packets to different people.... and I created the thing I had to do long before that - some centralized place to give everyone a scoreboard. It was a big change because changing all of that caused a lot of testing ... people are impatient, so you build up technical dept to get the update finished as soon as possible.

    What I want to say, its best to do things right - separate as much as possible before you release a plugin - because once you publish it, people will be impatient for updates and fixed.. it will cause you sleepless nights or bad reviews
     
    • Informative Informative x 2
  3. Maximvdw

    Benefactor

    1) is bad because there is literally no use for it to be a singleton. Plugins like plugman can even break those things. You should instantiate classes that use the main plugin with a reference to the Main instance.

    2) As a singleton it is not bad, but I have no idea what OnMessage does or why it needs to be a singleton because its name looks like a method name.
     
    • Agree Agree x 1
  4. To be fair, NMS/CB/Bukkit/Spigot/whatever layer is doing this pretty much already turns your main class into a singleton for you without doing anything. With that I mean, provide an access point to it via a static method (MyMainClass.getPlugin(MyMainClass.class)) and block instantiating the class again for the second time (calling the constructor will throw an error iirc). Even if you don't want this, it's pretty much forced upon you. You can of course use DI, but then you end up with a Singleton you pass around with DI and I'm uncertain if that's desirable either.
     
  5. Maximvdw

    Benefactor

    the problem is that it can be broken by loading the plugin with another class loader
     
  6. Wouldn't that be the case with every Singleton?
     
  7. MiniDigger

    Supporter

    Depends on how the Singleton is implemented. Guice Singletons are thread safe.
     
  8. 42 is always constant, its the answer to life the universe and everything...

    Good post. It deserves a bump every 10 months or so. ;)

    My wife is a librarian, she says anything that gets a kid to read is good.
    I'm a programmer, I say anything that gets a kid to program is good.
    There are books you can buy about how to make a minecraft plugin, my wife's library has a copy because i donated it. Don't hate the beginners, help them.
     
    #48 Tarluin, Dec 1, 2019 at 7:12 AM
    Last edited: Dec 1, 2019 at 7:18 AM