[Solved] Accessing the game object outside of the main class

[note: I am a java and spongeforums noob, if this is in the wrong part of the forum, please say so. If I am just being a total idiot, please dont be too hard on me :smile: ]
Hi, I’m trying to make my first plugin and I cant seem to understand how I should access the logger object from outside of the main class without turning it into a static object, which breaks @Inject. I have looked at this but I dont see how I could use that to access the game object in a method that is not called from the main class (a command executor)
As this has been solved by pretty much everyone in the community (because they have made plugins that work ;)) I think there is a really easy solution to this problem

Source for my plugin here (line 122 is where I am trying to access the logger object)

If I need to clarify anything for you to know what Im talking about, just comment and I will try to answer ASAP :smile:

Make your main class a singleton :stuck_out_tongue:

Is that even possible? I thought Sponge creates the instance of your main class.

I don’t know how everyone else does it but in my plugins i’ve used a singleton

Basically, you have 2 classes: PluginName and PluginNameSponge.

PluginNameSponge is your main class.

PluginName looks something like this:

private static PluginNameSponge instance;

public static PluginNameSponge get(){
  // You get it
}
static void set(PluginNameSponge instance){
  // You get it
}

Then in PluginNameSponge, use PluginName.set(this)

Now just use PluginName.get().getLogger() when you want to. :slight_smile:

Right. And in the init phase you set your static INSTANCE to this

@WetSponge You have the set(PluginNameSponge instance) public? Otherwise I agree with you.

Public static setter for singleton ???

My mistake. It needs package-level visibility, right? [fixed original post]

That or I’d make it private.

Relevant:

Personally, I use a static proxy system.

EDIT: Note, that this plugin is for private use. Anything for public use, and those methods within the proxy class would be wrapped in Optional. Keep that in mind, if you use this system.

@FerusGrim got me on his proxy train; which is kind of a singleton actually. (SpongeWarden’s proxy/singleton)

do bear in mind that using this system means that you assume there will only ever be one instance of the class you’re turning into a singleton. don’t try to do this for all of your classes like its a way to shortcut things; that’s lazy and goes against the purpose of OOP (Object-Oriented Programming)

1 Like

Yes, do keep that in mind. The system above is not for Objects in general, but Singletons.

It does, however, work well for Managers which require being instantiated, but there’s only ever one of (Which is what a Singleton is).

If you don’t want to use the singleton pattern:

Pass your plugin instance to your other class in a constructor:

PluginClass:

@Inject public Logger logger;
@Inject public Game game;

public void onInitialize(...) {
    OtherClass o = new OtherClass(this);
}
4 Likes

This, while having a couple disadvantages, such as ease-of-access, is arguably the most correct way.

1 Like

Thanks everyone for anwering! When I have the time, I will try everything you suggested but I have to admit I dont really understand everything :confused:

Pass it as a parameter into whatever class needs it??? or brush up on guice injectors.

2 Likes

I have now got everything to work by using

and I want to thank everybody for answering :smile:

I also want to ask another question about plugin development, but I dont know if it should be put in a linked topic or a new topic entirely, so if anyone knows please reply :smile:

Create a new topic. I’ll mark this one as solved.

Ok, thank you for answering :smile:, also I managed to solve the problem, so no need to look for a new topic by me :smiley: