Add keybinds and a feature list overlay

main
Charlotte Som 2022-02-03 22:01:41 +00:00
parent e30198fdab
commit 45d97bacc9
9 changed files with 117 additions and 3 deletions

View File

@ -1,7 +1,7 @@
package codes.som.hibiscus.mixins; package codes.som.hibiscus.mixins;
import codes.som.hibiscus.HibiscusMod; import codes.som.hibiscus.HibiscusMod;
import codes.som.hibiscus.events.RenderOverlayEvent; import codes.som.hibiscus.events.PostRenderAllEvent;
import codes.som.hibiscus.gui.ImGuiRenderer; import codes.som.hibiscus.gui.ImGuiRenderer;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.RunArgs; import net.minecraft.client.RunArgs;
@ -39,7 +39,7 @@ public abstract class MixinMinecraftClient {
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gl/Framebuffer;endWrite()V", shift = At.Shift.BEFORE)) @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gl/Framebuffer;endWrite()V", shift = At.Shift.BEFORE))
private void onPostRenderEverything(boolean tick, CallbackInfo ci) { private void onPostRenderEverything(boolean tick, CallbackInfo ci) {
float delta = this.paused ? this.pausedTickDelta : this.renderTickCounter.tickDelta; float delta = this.paused ? this.pausedTickDelta : this.renderTickCounter.tickDelta;
HibiscusMod.bus().fire(new RenderOverlayEvent(delta)); HibiscusMod.bus().fire(new PostRenderAllEvent(delta));
ImGuiRenderer.INSTANCE.finishFrame(delta); ImGuiRenderer.INSTANCE.finishFrame(delta);
} }
} }

View File

@ -3,10 +3,12 @@ package codes.som.hibiscus
import codes.som.hibiscus.api.command.CommandManager import codes.som.hibiscus.api.command.CommandManager
import codes.som.hibiscus.api.event.EventBus import codes.som.hibiscus.api.event.EventBus
import codes.som.hibiscus.api.event.EventPhase import codes.som.hibiscus.api.event.EventPhase
import codes.som.hibiscus.api.keybinds.KeybindRegistry
import codes.som.hibiscus.events.KeyEvent import codes.som.hibiscus.events.KeyEvent
import codes.som.hibiscus.features.FeaturesRegistry import codes.som.hibiscus.features.FeaturesRegistry
import codes.som.hibiscus.gui.ImGuiScreen import codes.som.hibiscus.gui.ImGuiScreen
import codes.som.hibiscus.util.command.ChatCommandListener import codes.som.hibiscus.util.command.ChatCommandListener
import codes.som.hibiscus.util.input.KeybindDispatcher
import codes.som.hibiscus.util.netmoving.NetworkMovingDispatcher import codes.som.hibiscus.util.netmoving.NetworkMovingDispatcher
import net.fabricmc.api.ModInitializer import net.fabricmc.api.ModInitializer
import org.lwjgl.glfw.GLFW import org.lwjgl.glfw.GLFW
@ -22,12 +24,15 @@ object HibiscusMod : ModInitializer {
val features = FeaturesRegistry() val features = FeaturesRegistry()
val commands = CommandManager() val commands = CommandManager()
val keybinds = KeybindRegistry()
override fun onInitialize() { override fun onInitialize() {
for (feature in features.getAllFeatures()) { for (feature in features.getAllFeatures()) {
commands.register(feature.createFeatureCommand()) commands.register(feature.createFeatureCommand())
} }
keybinds.register(GLFW.GLFW_KEY_R, "flight")
bus.register { event: KeyEvent -> bus.register { event: KeyEvent ->
if (event.key != GLFW_KEY_RIGHT_SHIFT || event.action != GLFW.GLFW_PRESS) if (event.key != GLFW_KEY_RIGHT_SHIFT || event.action != GLFW.GLFW_PRESS)
return@register return@register
@ -40,5 +45,6 @@ object HibiscusMod : ModInitializer {
bus.register(NetworkMovingDispatcher(), EventPhase.AFTER) bus.register(NetworkMovingDispatcher(), EventPhase.AFTER)
bus.register(ChatCommandListener()) bus.register(ChatCommandListener())
bus.register(KeybindDispatcher())
} }
} }

View File

@ -3,4 +3,5 @@ package codes.som.hibiscus.api.feature
enum class FeatureCategory(val humanName: String) { enum class FeatureCategory(val humanName: String) {
PLAYER("Player"), PLAYER("Player"),
MOVEMENT("Movement"), MOVEMENT("Movement"),
OVERLAY("Overlay"),
} }

View File

@ -0,0 +1,22 @@
package codes.som.hibiscus.api.keybinds
class KeybindRegistry {
private val keybinds = mutableMapOf<Int, MutableList<String>>()
fun register(key: Int, command: String) {
keybinds.getOrPut(key, ::mutableListOf).add(command)
}
fun unregister(key: Int, command: String) {
keybinds.getOrPut(key, ::mutableListOf).remove(command)
}
operator fun iterator(): Iterator<Map.Entry<Int, List<String>>> = keybinds.iterator()
fun getBinds(key: Int): List<String> =
keybinds.getOrDefault(key, emptyList())
fun reset() {
keybinds.clear()
}
}

View File

@ -0,0 +1,14 @@
package codes.som.hibiscus.api.keybinds
import codes.som.hibiscus.util.input.Keys
data class KeyboardKey(val code: Int) {
val name
get() =
Keys.KEY_MAP.inverse()[code] ?: "UNKNOWN"
companion object {
fun fromName(name: String) =
Keys.KEY_MAP[name]?.let(::KeyboardKey)
}
}

View File

@ -2,4 +2,4 @@ package codes.som.hibiscus.events
import codes.som.hibiscus.api.event.Event import codes.som.hibiscus.api.event.Event
class RenderOverlayEvent(val delta: Float) : Event class PostRenderAllEvent(val delta: Float) : Event

View File

@ -2,9 +2,11 @@ package codes.som.hibiscus.features
import codes.som.hibiscus.api.feature.Feature import codes.som.hibiscus.api.feature.Feature
import codes.som.hibiscus.features.movement.Flight import codes.som.hibiscus.features.movement.Flight
import codes.som.hibiscus.features.overlay.FeatureList
import codes.som.hibiscus.features.player.NoFallDamage import codes.som.hibiscus.features.player.NoFallDamage
val ALL_FEATURES: Array<() -> Feature> = arrayOf( val ALL_FEATURES: Array<() -> Feature> = arrayOf(
::NoFallDamage, ::NoFallDamage,
::Flight, ::Flight,
::FeatureList,
) )

View File

@ -0,0 +1,44 @@
package codes.som.hibiscus.features.overlay
import codes.som.hibiscus.HibiscusMod
import codes.som.hibiscus.api.feature.Feature
import codes.som.hibiscus.api.feature.FeatureCategory
import codes.som.hibiscus.events.PostRenderAllEvent
import imgui.ImGui
import imgui.flag.ImGuiCol
import imgui.flag.ImGuiWindowFlags
class FeatureList : Feature("Feature List", FeatureCategory.OVERLAY) {
init {
on { _: PostRenderAllEvent ->
val viewport = ImGui.getMainViewport()
ImGui.setNextWindowPos(viewport.posX, viewport.posY)
val enabledFeatures =
HibiscusMod.features.getAllFeatures()
.filter { it.category != FeatureCategory.OVERLAY }
.filter { it.enabled }.toList()
if (enabledFeatures.isEmpty())
return@on
ImGui.pushStyleColor(ImGuiCol.WindowBg, 10, 10, 10, 104)
if (ImGui.begin(
"Feature List",
ImGuiWindowFlags.NoDecoration + ImGuiWindowFlags.NoInputs +
ImGuiWindowFlags.NoMove + ImGuiWindowFlags.AlwaysAutoResize
)
) {
ImGui.pushStyleColor(ImGuiCol.Text, 244, 161, 255, 255)
for (feature in enabledFeatures)
ImGui.text(feature.name)
ImGui.popStyleColor()
}
ImGui.popStyleColor()
ImGui.end()
}
enabled = true
}
}

View File

@ -0,0 +1,25 @@
package codes.som.hibiscus.util.input
import codes.som.hibiscus.HibiscusLog
import codes.som.hibiscus.HibiscusMod
import codes.som.hibiscus.api.command.CommandContext
import codes.som.hibiscus.api.command.exceptions.CommandExecutionException
import codes.som.hibiscus.api.event.TypedListener
import codes.som.hibiscus.events.KeyEvent
import codes.som.hibiscus.mc
import org.lwjgl.glfw.GLFW
class KeybindDispatcher : TypedListener<KeyEvent>(KeyEvent::class.java) {
override fun on(event: KeyEvent) {
if (mc.isWindowFocused && mc.currentScreen == null && event.action == GLFW.GLFW_PRESS) {
HibiscusMod.keybinds.getBinds(event.key).forEach {
HibiscusMod.commands.context = CommandContext.KEYBIND
try {
HibiscusMod.commands.executeCommand(it)
} catch (e: CommandExecutionException) {
e.cause?.message?.let { HibiscusLog.error(it) }
}
}
}
}
}