Bukkit.getOnlinePlayers() vs. getServer().getOnlinePlayers()

Discussion in 'Plugin Development' started by peteyandpika, Oct 1, 2013.

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

    peteyandpika

    In my code the getServer().getOnlinePlayers() method returns null when i am online, and the Bukkit.getOnlinePlayers() return the correct value without error. So, what is the difference between these two methods? besides that one is static and the other isn't
     
  2. Offline

    chasechocolate

  3. Offline

    peteyandpika

    interesting. Could this possibly be caused from accessing getServer() from a subclass of my main class, rather than in the main class itself?
     
  4. Offline

    The_Doctor_123

    The Bukkit class basically a shortcut to accessing Server. And if you're trying to access the Server class from another class(that doesn't extend JavaPlugin), it's probably most efficient to call Bukkit.getServer() and store that in a private variable instead of using the Bukkit class repeatedly.


    So like chasechocolate said, you should be getting the same data in the end.

    peteyandpika
    You extended your main class..?
     
  5. Offline

    peteyandpika

    yeah i extended my main class to use its useful fields and methods
     
  6. Offline

    The_Doctor_123

    peteyandpika
    Extending probably shouldn't be used. If you want to access everything in your main class, put a constructor in your Listener class and let it take in whatever your class is named. Then provide it with this when constructing that object.

    But I'm quite puzzled, even extending should make any difference to the data in Server with the instance you have...
     
  7. Offline

    peteyandpika

    not my Listener. just another class. I'm quite sure that extending won't do any harm
     
  8. Offline

    xTrollxDudex

    peteyandpika
    It's not the extending part that does the damage, it's the methods being accessed from the class that does.
     
  9. Offline

    The_Doctor_123

    peteyandpika xTrollxDudex
    Wait, I just realized, that would be creating practically a whole new instance of JavaPlugin if he created a new instance of the class that extends his main class, correct?
     
  10. Offline

    peteyandpika

    ok TRollxDudex, we allready concluded that the problem was the null return value of the getServer() method. So what is wrong about this?

    True that Doctor. When you create a new class, the class doesn't directly extend JavaPlugin. But it is still able to access the methods from JavaPlugin because it is still an instance of it.

    So, to get a field or method directly from JavaPlugin, the syntax would be ((JavaPlugin)this).getServer().getOnlinePlayers();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  11. Offline

    The_Doctor_123

    peteyandpika
    You're not supposed to extend JavaPlugin more than once, though. It supposedly causes problems, but I've never tried it myself though.
     
  12. Offline

    peteyandpika

    But does this really matter? because in my plugin, i didn't ever override the method getServer().

    Yes, you indeed get an error if you extend JavaPlugin more than once, but we are not extending it. We are extending one of its subclasses. But the instances of our subclass of our main class that extends JavaPlugin is still indirectly a valid JavaPlugin Object.

    You can look at it this way:
    JavaPlugin - Mainclass - OurSubclass (valid)
    JavaPlugin - Mainclass
    - OurSubclass (invalid)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  13. Offline

    hatstand

    Doesn't matter. Plugin objects are set up in a special manner when the plugin is loaded. Creating a new instance of one like this means that doesn't happen, so a bunch of things won't work correctly. Store a reference to your plugin in your listener class, you'll be able to achieve exactly the same thing, and it is much cleaner.
     
  14. Offline

    peteyandpika

    However, this is true, but for my subclass of my main class, i use a lot of JavaPlugin objects, in other words It works much better for me to subclass the main class to be able to easily use all of its static, and nonstatic members.
     
  15. Offline

    hatstand

    Well it obviously doesn't work better, because it's not working. If you insist on doing things the wrong way, you aren't going to find much help.
     
    evilmidget38 likes this.
  16. Offline

    xTrollxDudex

    peteyandpika
    You've heard of CommandHandlers correct? They all implement or extend a single class and therefore "create" a new instance of the interface class to store in memory. That's why you can store them on a hashmap without them interconnecting. Same with JavaPlugin, if you extend a subclass of JavaPlugin, then you already create a new instance of the class, and an entirely new instance of JavaPlugin and Bukkit rejects multiple JavaPlugins...
     
  17. Offline

    The_Doctor_123

    May work better if it worked the way you thought it would, but it clearly does not. I bet if you tried the ways that most people use that it will all work as it should.
     
  18. Offline

    Garris0n


    I wouldn't think it would do anything wrong, any more information on this?
     
  19. Offline

    The_Doctor_123

    Garris0n
    I wouldn't think so either, but that would seem to be the issue.
     
  20. Offline

    Janmm14

    peteyandpika
    Many values are not initialized if you extend JavaPlugin not in your main class.
     
  21. Offline

    peteyandpika

    I believe this thread is no longer needed as i see no issue
     
  22. Offline

    The_Doctor_123

    peteyandpika
    Because you see no issue means that there is none? You do not control the language of Java.

    You're being a little ignorant with the answers we gave you.
     
  23. Offline

    nisovin

    It's bad practice. If the argument that it's broken doesn't convince you, then perhaps that will?

    When programming in Java (or any OOP language), your class setup should make sense. Your second class is NOT a java plugin, nor is it a sub-type of your main plugin class, so it should not be extending those classes. Extending a class so that you can access its methods easier is not a good reason to extend a class.

    As others have said, it makes much more sense to pass an instance of your main plugin to your second class through the constructor. That is the way it should be done.
     
  24. Offline

    xTrollxDudex

    nisovin
    I think it is commonly referred to as "passing an instance of JavaPlugin" but I sometimes question the description. I think it should be "pass a reference of the main class or JavaPlugin"
     
Thread Status:
Not open for further replies.

Share This Page