Skip to content

Instantly share code, notes, and snippets.

@zortax
Last active January 28, 2021 03:30
Show Gist options
  • Save zortax/5437c9f3420f69447ce68823387b7706 to your computer and use it in GitHub Desktop.
Save zortax/5437c9f3420f69447ce68823387b7706 to your computer and use it in GitHub Desktop.
inject Shadow instance
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