Last active
January 28, 2021 03:30
-
-
Save zortax/5437c9f3420f69447ce68823387b7706 to your computer and use it in GitHub Desktop.
inject Shadow instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.zortax.flint.test; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Inject; | |
import com.google.inject.Singleton; | |
import net.flintmc.framework.eventbus.event.subscribe.PostSubscribe; | |
import net.flintmc.framework.inject.primitive.InjectionHolder; | |
import net.flintmc.mcapi.chat.event.ChatSendEvent; | |
import net.flintmc.mcapi.player.ClientPlayer; | |
import net.flintmc.util.taskexecutor.TaskExecutor; | |
import net.minecraft.client.Minecraft; | |
@Singleton | |
public class Test { | |
boolean registered = false; | |
@PostSubscribe | |
public void onChat(ChatSendEvent chatSendEvent, ClientPlayer clientPlayer, TaskExecutor taskExecutor) { | |
if (!registered) { | |
InjectionHolder.getInstance().addModules(new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(MinecraftAccessor.class).toInstance((MinecraftAccessor) Minecraft.getInstance()); | |
} | |
}); | |
registered = true; | |
} | |
taskExecutor.scheduleSync(20, () -> { | |
InjectionHolder.getInjectedInstance(SomeOtherClass.class).printFps(); | |
}); | |
} | |
public static class SomeOtherClass { | |
private final MinecraftAccessor minecraft; | |
private final ClientPlayer clientPlayer; | |
@Inject | |
private SomeOtherClass(MinecraftAccessor minecraft, ClientPlayer clientPlayer) { | |
this.minecraft = minecraft; | |
this.clientPlayer = clientPlayer; | |
} | |
public void printFps() { | |
this.clientPlayer.sendChatMessage(minecraft.getFPS() + " FPS"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment