[SOLVED] Setting variable from main class from command execute

Hello,

The last 4 weeks I was learning java at school, since it’s vacation now I want to learn to develop Sponge Plugins to get more knowledge of Java. I don’t have experience with Bukkit.
I already made a small plugin that creates a house when an command is used.

Now my question:
I’m making a small Tag plugin where people can start a game if one doesn’t exist and join one if it does exist. I made these subcommands:
/McTag help
/McTag start
/McTag join

I’m wondering how to change a variable “gameStarted” that is created in the main class from the CmdTagStart class.
I already tried making the variable public but, as I expected, it doesn’t know about this variable.

How to make a variable global and/or change the variable from another class?
And is this the best way to keep track of the states of the game or should I save the variables elsewhere.

StefanJanssen

Does your CmdTagStart class have an instance of your main class?

If not, try something like this in your Main class:

private static MainPlugin instance;

@Subscribe
public void onPluginInit(InitializationEvent evt) {
     instance = this;
}

public static MainPlugin getInstance() {
    return instance;
}

You can then access your Main class instance from CmdTagStart and do this:

MainPlugin.getInstance().gameStarted;

If this wasn’t your problem, some code would help us, help you :wink:

1 Like

This is exactly what I needed. I did something like this before but I couldn’t make it work. It works now.

Thanks <3