Skip to content

Commit

Permalink
feat: better runCommand (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
wafarm authored Sep 5, 2023
1 parent 491442a commit 7485380
Show file tree
Hide file tree
Showing 13 changed files with 672 additions and 23 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/github/zly2006/reden/RedenClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.zly2006.reden.malilib.KeyCallbacksKt;
import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import com.github.zly2006.reden.malilib.data.CommandHotkey;
import com.github.zly2006.reden.pearl.PearlTask;
import com.github.zly2006.reden.report.ReportKt;
import com.github.zly2006.reden.sponsor.SponsorKt;
Expand All @@ -17,6 +18,7 @@
import fi.dy.masa.malilib.util.FileUtils;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -64,6 +66,22 @@ public void addKeysToMap(IKeybindManager iKeybindManager) {
MalilibSettingsKt.HOTKEYS.stream()
.map(IHotkey::getKeybind)
.forEach(iKeybindManager::addKeybindToMap);

for (CommandHotkey commandHotkey : MalilibSettingsKt.RUN_COMMAND.getCommandHotkeyList()) {
iKeybindManager.addKeybindToMap(commandHotkey.getKeybind());
commandHotkey.getKeybind().setCallback((action, key) -> {
ClientPlayNetworkHandler networkHandler = MinecraftClient.getInstance().getNetworkHandler();
assert networkHandler != null;
for (String command : commandHotkey.getCommands()) {
if (command.startsWith("/")) {
networkHandler.sendChatCommand(command.substring(1));
} else {
networkHandler.sendChatMessage(command);
}
}
return true;
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ package com.github.zly2006.reden.malilib

import com.github.zly2006.reden.malilib.options.*
import com.github.zly2006.reden.utils.isClient
import com.google.common.collect.ImmutableList
import fi.dy.masa.malilib.config.IConfigBase
import fi.dy.masa.malilib.config.options.ConfigBase
import fi.dy.masa.malilib.config.options.ConfigHotkey
import fi.dy.masa.malilib.config.options.ConfigStringList
import fi.dy.masa.malilib.hotkeys.IHotkey
import net.minecraft.client.MinecraftClient

private val loadingGuard = run {
if (!isClient) {
Expand Down Expand Up @@ -59,6 +55,7 @@ private fun <T : IConfigBase?> ConfigBase<T>.debug() = this.apply(DEBUG_TAB::add
@JvmField val CHAT_RIGHT_CLICK_MENU = RedenConfigBoolean("chatRightClickMenu", true).sr()
@JvmField val STRUCTURE_BLOCK_LOAD = RedenConfigHotkey("structureBlockLoad", "LEFT_CONTROL,L").sr().hotkey()
@JvmField val STRUCTURE_BLOCK_SAVE = RedenConfigHotkey("structureBlockSave", "LEFT_CONTROL,S").sr().hotkey()
@JvmField val RUN_COMMAND = RedenConfigCommandHotkeyList("runCommand").sr()
@JvmField val DEBUG_LOGGER = RedenConfigBoolean("debugLogger", false).debug()
@JvmField val DEBUG_PACKET_LOGGER = RedenConfigBoolean("debugPacketLogger", false).debug()
@JvmField val DEBUG_TAG_BLOCK_POS = RedenConfigHotkey("debugTagBlockPos", "LEFT_CONTROL,LEFT_SHIFT,T").debug().hotkey()
Expand All @@ -70,23 +67,4 @@ private fun <T : IConfigBase?> ConfigBase<T>.debug() = this.apply(DEBUG_TAB::add
@JvmField val GITHUB_TOKEN = RedenConfigString("githubToken", "").debug()
@JvmField val ALLOW_SOCIAL_FOLLOW = RedenConfigBoolean("allowSocialFollow", true).debug()

fun ConfigHotkey.runCommand(commands: ConfigStringList) {
this.keybind.setCallback { _, _ ->
val net = MinecraftClient.getInstance().networkHandler!!
commands.strings.forEach {
if (it.startsWith('/')) net.sendChatCommand(it.substring(1))
else net.sendChatMessage(it)
}
true
}
}

fun createCommandHotkey(index: Int) {
// We should find a better way to implement this. It should be refactored.
val commands = ConfigStringList("command$index", ImmutableList.of(), "").sr()
val hotkey = ConfigHotkey("runCommand$index", "", "Hotkey for executing command $index").hotkey().sr().runCommand(commands)
}

private val commands = (1..20).map(::createCommandHotkey)

fun getAllOptions() = GENERIC_TAB + RVC_TAB + MICRO_TICK_TAB + SUPER_RIGHT_TAB + DEBUG_TAB
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.zly2006.reden.malilib.data

import fi.dy.masa.malilib.hotkeys.IKeybind
import fi.dy.masa.malilib.hotkeys.KeybindMulti
import fi.dy.masa.malilib.hotkeys.KeybindSettings

data class CommandHotkey(
val commands: MutableList<String>,
val keybind: IKeybind
) {
companion object {
fun new(): CommandHotkey {
return CommandHotkey(mutableListOf(), KeybindMulti.fromStorageString("", KeybindSettings.DEFAULT))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.github.zly2006.reden.malilib.gui

import com.github.zly2006.reden.malilib.data.CommandHotkey
import com.github.zly2006.reden.malilib.gui.widget.WidgetCommandHotkeyList
import com.github.zly2006.reden.malilib.gui.widget.WidgetCommandHotkeyListEntry
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList
import fi.dy.masa.malilib.config.ConfigManager
import fi.dy.masa.malilib.event.InputEventHandler
import fi.dy.masa.malilib.gui.GuiListBase
import fi.dy.masa.malilib.gui.button.ConfigButtonKeybind
import fi.dy.masa.malilib.gui.interfaces.IConfigGui
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler
import fi.dy.masa.malilib.render.RenderUtils
import fi.dy.masa.malilib.util.GuiUtils
import fi.dy.masa.malilib.util.KeyCodes
import fi.dy.masa.malilib.util.StringUtils
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.Screen

class GuiCommandHotkeyListEdit(
val config: RedenConfigCommandHotkeyList,
val configGui: IConfigGui,
val dialogHandler: IDialogHandler?,
parent: Screen?
) : GuiListBase<CommandHotkey, WidgetCommandHotkeyListEntry, WidgetCommandHotkeyList>(0, 0) {

private var dialogWidth: Int = 0
private var dialogHeight: Int = 0
private var dialogLeft: Int = 0
private var dialogTop: Int = 0

var activeKeybindButton: ConfigButtonKeybind? = null

init {
title = StringUtils.translate("reden.widget.title.commandHotkeyListEdit")
if (dialogHandler == null) {
setParent(parent)
}
}

private fun setWidthAndHeight() {
dialogWidth = 400
dialogHeight = GuiUtils.getScaledWindowHeight() - 90
}

private fun centerOnScreen() {
if (parent != null) {
val parent = parent!!
dialogLeft = parent.width / 2 - dialogWidth / 2
dialogTop = parent.height / 2 - dialogHeight / 2
} else {
dialogLeft = 20
dialogTop = 20
}
}

override fun initGui() {
setWidthAndHeight()
centerOnScreen()
reCreateListWidget()
super.initGui()
}

override fun removed() {
if (listWidget!!.wereConfigsModified()) {
listWidget!!.applyPendingModifications()
ConfigManager.getInstance().onConfigsChanged(configGui.modId)
InputEventHandler.getKeybindManager().updateUsedKeys()
}
super.removed()
}

override fun createListWidget(listX: Int, listY: Int): WidgetCommandHotkeyList {
return WidgetCommandHotkeyList(
dialogLeft + 10,
dialogTop + 20,
browserWidth,
browserHeight,
dialogWidth - 100,
this
)
}

override fun getBrowserWidth(): Int {
return dialogWidth - 14
}

override fun getBrowserHeight(): Int {
return dialogHeight - 30
}

override fun render(drawContext: DrawContext?, mouseX: Int, mouseY: Int, partialTicks: Float) {
if (parent != null) {
parent!!.render(drawContext, mouseX, mouseY, partialTicks)
}

super.render(drawContext, mouseX, mouseY, partialTicks)
}

override fun drawScreenBackground(mouseX: Int, mouseY: Int) {
RenderUtils.drawOutlinedBox(
dialogLeft, dialogTop, dialogWidth, dialogHeight,
0xFF000000.toInt(),
0xFF999999.toInt()
)
}

override fun drawTitle(drawContext: DrawContext?, mouseX: Int, mouseY: Int, partialTicks: Float) {
drawStringWithShadow(drawContext, title, dialogLeft + 10, dialogTop + 6, 0xFFFFFFFF.toInt())
}

override fun onKeyTyped(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
if (activeKeybindButton != null) {
if (activeKeybindButton!!.isSelected) {
activeKeybindButton!!.onKeyPressed(keyCode)
return true
}
}

return if (keyCode == KeyCodes.KEY_ESCAPE && dialogHandler != null) {
dialogHandler.closeDialog()
true
} else {
super.onKeyTyped(keyCode, scanCode, modifiers)
}
}

override fun onMouseClicked(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean {
if (super.onMouseClicked(mouseX, mouseY, mouseButton)) {
return true
}

if (activeKeybindButton != null) {
activeKeybindButton!!.onClearSelection()
activeKeybindButton = null
return true
}

return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.zly2006.reden.malilib.gui.button

import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList
import fi.dy.masa.malilib.gui.GuiBase
import fi.dy.masa.malilib.gui.button.ButtonGeneric
import fi.dy.masa.malilib.gui.interfaces.IConfigGui
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler
import fi.dy.masa.malilib.util.GuiUtils
import fi.dy.masa.malilib.util.StringUtils

class ConfigButtonCommandHotkeyList(
x: Int,
y: Int,
width: Int,
height: Int,
private val config: RedenConfigCommandHotkeyList,
private val configGui: IConfigGui,
private val dialogHandler: IDialogHandler?
) : ButtonGeneric(x, y, width, height, "") {

init {
updateDisplayString()
}

override fun onMouseClickedImpl(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean {
super.onMouseClickedImpl(mouseX, mouseY, mouseButton)
if (dialogHandler != null) {
dialogHandler.openDialog(GuiCommandHotkeyListEdit(config, configGui, dialogHandler, null))
} else {
GuiBase.openGui(GuiCommandHotkeyListEdit(config, configGui, null, GuiUtils.getCurrentScreen()))
}
return true
}

override fun updateDisplayString() {
displayString = StringUtils.translate("reden.widget.button.commandHotkeyListEdit")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.zly2006.reden.malilib.gui.button

import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit
import fi.dy.masa.malilib.gui.button.ConfigButtonKeybind
import fi.dy.masa.malilib.gui.interfaces.IKeybindConfigGui
import fi.dy.masa.malilib.hotkeys.IKeybind

class ConfigButtonKeybindInList(
x: Int,
y: Int,
width: Int,
height: Int,
keybind: IKeybind,
private val listGui: GuiCommandHotkeyListEdit
) : ConfigButtonKeybind(x, y, width, height, keybind, listGui.configGui as IKeybindConfigGui) {
override fun onMouseClickedImpl(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean {
super.onMouseClickedImpl(mouseX, mouseY, mouseButton)
if (selected) {
listGui.activeKeybindButton = this
}
return true
}

override fun onClearSelection() {
super.onClearSelection()
listGui.activeKeybindButton = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.github.zly2006.reden.malilib.gui.widget

import com.github.zly2006.reden.malilib.data.CommandHotkey
import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList
import fi.dy.masa.malilib.gui.widgets.WidgetListConfigOptionsBase

class WidgetCommandHotkeyList(
x: Int,
y: Int,
width: Int,
height: Int,
configWidth: Int,
val parent: GuiCommandHotkeyListEdit
) : WidgetListConfigOptionsBase<CommandHotkey, WidgetCommandHotkeyListEntry>(x, y, width, height, configWidth) {

val config: RedenConfigCommandHotkeyList = parent.config

init {
config.saveCallback = {
for (widget in listWidgets) {
widget.syncWithConfig()
}
}
}

override fun getAllEntries(): MutableCollection<CommandHotkey> {
return config.commandHotkeyList
}

override fun reCreateListEntryWidgets() {
if (listContents.size == 0) {
listWidgets.clear()
maxVisibleBrowserEntries = 1

val x = posX + 2
val y = posY + 4 + browserEntriesOffsetY

listWidgets.add(createListEntryWidget(x, y, -1, false, CommandHotkey.new()))
scrollbar.maxValue = 0
} else {
super.reCreateListEntryWidgets()
}
}

override fun createListEntryWidget(
x: Int,
y: Int,
listIndex: Int,
isOdd: Boolean,
entry: CommandHotkey?
): WidgetCommandHotkeyListEntry {
if (listIndex >= 0 && listIndex < config.commandHotkeyList.size) {
val defaultValue =
if (config.defaultList.size > listIndex) config.defaultList[listIndex] else CommandHotkey.new()
return WidgetCommandHotkeyListEntry(
x,
y,
browserEntryWidth,
browserEntryHeight,
listIndex,
config.commandHotkeyList[listIndex],
this,
isOdd,
defaultValue
)
} else {
return WidgetCommandHotkeyListEntry(
x,
y,
browserEntryWidth,
browserEntryHeight,
listIndex,
CommandHotkey.new(),
this,
isOdd,
CommandHotkey.new()
)
}
}
}
Loading

0 comments on commit 7485380

Please sign in to comment.