"this" in public static

Discussion in 'Spigot Plugin Development' started by Swedz, Jun 4, 2016.

  1. Hi there, I'm fairly new to Java, and I'm trying to make a public static String that returns the player count of any server I want. This code is in the API.java class, where my main code is in Lobby.java (I want to use getBungeePlayers() in Lobby.java, note that there is nothing wrong with anything else. Simply just at "this")

    My current code is:
    Code (Text):
    public static int specificPlayerCount = 0;
    public static int globalPlayerCount = 0;
    public void onEnable() {
        Bukkit.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
        Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    public static String getBungeePlayers(String server) throws Exception {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(b);
        if(server.equalsIgnoreCase("global")) {
            server = "ALL";
        Bukkit.getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
        return globalPlayerCount + "";
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if(!channel.equals("Bungeecord")) {
        try {
            DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
            String subchannel = in.readUTF();
            if(subchannel.equals("PlayerCount")) {
                String server = in.readUTF();
                if(server.equalsIgnoreCase("ALL")) {
                    globalPlayerCount = in.readInt();
                } else {
                    specificPlayerCount = in.readInt();
        } catch (Exception e) {
    The error I'm getting is on the 17th line of this code, at "this".
    Thank you in advance!
  2. Pass an instance of your class in replacement of "this"

    Edit: or just make that not static and use an instance to get it
  3. Inkzzz

    Resource Staff

    Why is everything static?
  4. I'm pretty stupid when it comes to this stuff, could you please show me what I must add?

    Thank you for the quick reply! :)
  5. Because this is in the API.java class, where I want to use these functions is the Lobby.java class.
  6. Dont abuse static methods, create a new instance of the other classes in your main class, then create a constructor in your other classes and a geter in the main class to get those classes.
    • Winner Winner x 1
  7. Inkzzz

    Resource Staff

    I am also thinking that you should handle this differently. Perhaps a Map<String, Integer> which stores the server name, with the player count? Additionally, there's no need for any static abuse here, even if it is an API - It's just a bad habbit and will run you into errors, hence why you are here.
  8. Why shouldn't I? Does it decrease performance?

    I'm just using a lot of them because I'd like to simplify my code so instead of having to retype all this code I could just use API.getBungeePlayers(*server name*);
  9. Spend a couple days just learning Java, ignore bukkit for those days. Trust me, it helps a lot :p
    • Agree Agree x 1
  10. How would I do this specifically? As I said, I'm very new to Java and still trying to learn.
  11. Inkzzz

    Resource Staff

    Without sounding like we're being mean, it will help a lot. The first time I went into plug-in development, I didn't do much Java learning. However, I stopped, went away for several months and concentrated on just Java, and now I have an outside of Minecraft job will is extremely well-paid for my age. To conclude, learn Java! :p
  12. Inkzzz

    Resource Staff

    Get yourself a Java book, or perhaps, read online documentary such as Oracle.
  13. Currently I'm not looking for an outside of Minecraft job, I'm just trying to get my server running in the next month or so. It's not that I'm really struggling with Java, it's just that I have a question on this small portion of my code.

    Note: By "new" to Java I mean like I've been doing it for a month now, I just haven't messed with "this" statements in static before just a couple days ago, I've posted on another forum but nobody seemed to really answer me so I just came here.
  14. "A month" rn if you spend atleast 4 days just doing it seperately you can make this whole thing better :p

    Also I already gave the solution at post after the thread
  15. I know, but how would I do that? (I'm stupid I know) Can you give me an example?
  16. public static final Main instance = new Main();

    public static Main getInstance() {
    return instance;
  17. Now you just do Main.getInstance().yourMethod()

    Replace main with ur class

    And make your method not static
  18. How do you make something that is non static return a value?
  19. Inkzzz

    Resource Staff

    But don't do that for your class which is extending JavaPlugin, otherwise you'll have issues.
  20. Inkzzz

    Resource Staff

    Just don't call "this".variable call the variable, or even better, call the class, then variable.