getServer() in classes other than the main class?

Discussion in 'Plugin Development' started by Randy Schouten, Jun 23, 2011.

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

    Randy Schouten

    I can't seem to use "eq.getServer().getPlayer(player)" in any other class than my main class.
    I can use "this.getServer().getPlayer(player)" in the main class and it works just fine.

    The 'error' eclipse gives is: "Cannot make a static reference to the non-static method "getServer()" from the type JavaPlugin".

    Anyone knows how I can get this to work?
     
  2. Offline

    garbagemule

    The method you're calling getServer() from is static. In your main class, you should also be able to omit the "this.", because the class you are calling getServer() on is indeed JavaPlugin.

    To get the server object from elsewhere in your plugin, you need to make the plugin instance available somehow. You can do this by passing the instance in constructors, or just in the methods that actually need to use it. One way to sort of "cheat" yourself around having to pass the instance in a bunch of methods and/or constructors is to have a static field which holds the instance, in some class (not your main class, though, as this doesn't really make any sense). This would serve as a "global variable", which isn't very good programming practice in general, but if you don't care about "correctness" and just need something that works, just go for it :)
     
  3. Offline

    Randy Schouten

    You talk in difficult language for me.
    Could you maybe write down the code of what you explained?
    I think that would be a lot more useful to learn off, since this is basically rambling to me (sorry :oops:).

    Or if there's any other way to get a player and send him a message, that's also alright.
    This is the only way I knew to get a player. :p
    If this is any use too, I have the player name in a hashmap and I can retrieve it easily.
     
  4. Offline

    Shamebot

    Simply use
    Code:java
    1. Bukkit.getServer()...
     
    garbagemule likes this.
  5. Offline

    Randy Schouten

    Thank you kindly, sir. :)
    Also thanks to GarbadgeMule ofcourse :D
     
Thread Status:
Not open for further replies.

Share This Page