[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 ]
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
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.
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.
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)
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