From b2e794e5a4a80e25c663e6b5563c4a048a2e65af Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 23 Dec 2021 12:54:33 -0500 Subject: [PATCH] Add REI sync mode for terminal search field --- .../phycon/plugin/rei/PhyConPluginClient.kt | 48 ++++++++++++++++-- .../phycon/plugin/rei/PhyConPluginCommon.kt | 14 ++++- .../phycon/plugin/rei/REISyncMode.kt | 29 +++++++++++ plugin/rei/src/main/resources/fabric.mod.json | 6 +++ .../shadowfacts/phycon/api/PhyConPlugin.java | 4 +- .../phycon/api/TerminalSettingKey.java | 2 + .../net/shadowfacts/cacao/CacaoScreen.kt | 4 +- .../net/shadowfacts/phycon/DefaultPlugin.kt | 1 + .../block/terminal/AbstractTerminalScreen.kt | 24 +++++++++ .../AbstractTerminalViewController.kt | 2 +- .../phycon/client/screen/ScrollTrackView.kt | 2 +- .../net/shadowfacts/phycon/util/SortMode.kt | 6 +-- .../phycon/util/TerminalSettings.kt | 9 +++- .../assets/phycon/textures/gui/terminal.png | Bin 506 -> 4483 bytes 14 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/REISyncMode.kt diff --git a/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginClient.kt b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginClient.kt index e727f99..25c700b 100644 --- a/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginClient.kt +++ b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginClient.kt @@ -8,13 +8,19 @@ import me.shedaniel.rei.api.client.plugins.REIClientPlugin import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry import net.fabricmc.api.ClientModInitializer import net.minecraft.client.MinecraftClient +import net.shadowfacts.phycon.PhysicalConnectivityClient import net.shadowfacts.phycon.block.terminal.AbstractTerminalScreen +import org.apache.logging.log4j.LogManager +import java.lang.invoke.MethodHandle +import java.lang.invoke.MethodHandles /** * @author shadowfacts */ -object PhyConPluginClient: ClientModInitializer, REIClientPlugin { - const val MODID = "phycon_rei" +object PhyConPluginClient: ClientModInitializer, REIClientPlugin, AbstractTerminalScreen.SearchQueryListener { + + private val logger = LogManager.getLogger() + private var isHighlightingHandle: MethodHandle? = null override fun onInitializeClient() { ClientScreenInputEvent.MOUSE_RELEASED_PRE.register { client, screen, mouseX, mouseY, button -> @@ -29,6 +35,14 @@ object PhyConPluginClient: ClientModInitializer, REIClientPlugin { } EventResult.pass() } + + AbstractTerminalScreen.searchQueryListener = this + try { + val clazz = Class.forName("me.shedaniel.rei.impl.client.gui.widget.search.OverlaySearchField") + isHighlightingHandle = MethodHandles.publicLookup().findStaticGetter(clazz, "isHighlighting", Boolean::class.java) + } catch (e: ReflectiveOperationException) { + logger.warn("Unable to find OverlaySearchField.isHighlighting, highlight sync will be disabled", e) + } } override fun registerScreens(registry: ScreenRegistry) { @@ -37,9 +51,37 @@ object PhyConPluginClient: ClientModInitializer, REIClientPlugin { val view = screen.terminalVC.settingsView val rect = view.convert(view.bounds, to = null) listOf( - Rectangle(rect.left.toInt(), rect.top.toInt(), 20, 20) + Rectangle(rect.left.toInt(), rect.top.toInt(), view.bounds.width.toInt(), view.bounds.height.toInt()) ) } } + override fun terminalSearchQueryChanged(newValue: String) { + if (shouldSync()) { + REIRuntime.getInstance().searchTextField?.text = newValue + } + } + + override fun requestTerminalSearchFieldUpdate(): String? { + return if (shouldSync()) { + REIRuntime.getInstance().searchTextField?.text + } else { + null + } + } + + private fun shouldSync(): Boolean { + return when (PhysicalConnectivityClient.terminalSettings[PhyConPluginCommon.REI_SYNC_KEY]) { + REISyncMode.OFF -> false + REISyncMode.ON -> true + REISyncMode.HIGHLIGHT_ONLY -> { + if (isHighlightingHandle != null) { + isHighlightingHandle!!.invoke() as Boolean + } else { + false + } + } + } + } + } diff --git a/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginCommon.kt b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginCommon.kt index 86c1768..961d277 100644 --- a/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginCommon.kt +++ b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/PhyConPluginCommon.kt @@ -8,18 +8,30 @@ import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry import me.shedaniel.rei.api.common.transfer.info.simple.SimpleGridMenuInfo import me.shedaniel.rei.api.common.transfer.info.simple.SimpleMenuInfoProvider import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor +import net.minecraft.util.Identifier +import net.shadowfacts.phycon.api.PhyConAPI +import net.shadowfacts.phycon.api.PhyConPlugin +import net.shadowfacts.phycon.api.TerminalSettingKey import net.shadowfacts.phycon.block.terminal.CraftingTerminalScreenHandler import java.util.stream.IntStream /** * @author shadowfacts */ -object PhyConPluginCommon: REIServerPlugin { +object PhyConPluginCommon: REIServerPlugin, PhyConPlugin { + const val MODID = "phycon_rei" + + lateinit var REI_SYNC_KEY: TerminalSettingKey + private set override fun registerMenuInfo(registry: MenuInfoRegistry) { registry.register(CategoryIdentifier.of("minecraft", "plugins/crafting"), CraftingTerminalScreenHandler::class.java, SimpleMenuInfoProvider.of(::TerminalInfo)) } + override fun initializePhyCon(api: PhyConAPI) { + REI_SYNC_KEY = api.registerTerminalSetting(Identifier(MODID, "rei_sync"), REISyncMode.OFF) + } + class TerminalInfo( private val display: D, ): SimpleGridMenuInfo { diff --git a/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/REISyncMode.kt b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/REISyncMode.kt new file mode 100644 index 0000000..819e8c0 --- /dev/null +++ b/plugin/rei/src/main/kotlin/net/shadowfacts/phycon/plugin/rei/REISyncMode.kt @@ -0,0 +1,29 @@ +package net.shadowfacts.phycon.plugin.rei + +import net.minecraft.text.LiteralText +import net.minecraft.util.Identifier +import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.api.TerminalSetting + +/** + * @author shadowfacts + */ +enum class REISyncMode: TerminalSetting { + OFF, + ON, + HIGHLIGHT_ONLY; + + override fun getIconTexture() = Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png") + + override fun getUV() = when (this) { + OFF -> intArrayOf(0, 240) + ON -> intArrayOf(16, 240) + HIGHLIGHT_ONLY -> intArrayOf(32, 240) + } + + override fun getTooltip() = when (this) { + OFF -> LiteralText("Don't sync with REI") + ON -> LiteralText("Sync with REI") + HIGHLIGHT_ONLY -> LiteralText("Sync in highlight mode") + } +} diff --git a/plugin/rei/src/main/resources/fabric.mod.json b/plugin/rei/src/main/resources/fabric.mod.json index 16f2408..6336a67 100644 --- a/plugin/rei/src/main/resources/fabric.mod.json +++ b/plugin/rei/src/main/resources/fabric.mod.json @@ -28,6 +28,12 @@ "adapter": "kotlin", "value": "net.shadowfacts.phycon.plugin.rei.PhyConPluginCommon" } + ], + "phycon": [ + { + "adapter": "kotlin", + "value": "net.shadowfacts.phycon.plugin.rei.PhyConPluginCommon" + } ] }, "mixins": [ diff --git a/src/main/java/net/shadowfacts/phycon/api/PhyConPlugin.java b/src/main/java/net/shadowfacts/phycon/api/PhyConPlugin.java index 6ea49ca..8f284dd 100644 --- a/src/main/java/net/shadowfacts/phycon/api/PhyConPlugin.java +++ b/src/main/java/net/shadowfacts/phycon/api/PhyConPlugin.java @@ -1,10 +1,12 @@ package net.shadowfacts.phycon.api; +import org.jetbrains.annotations.NotNull; + /** * @author shadowfacts */ public interface PhyConPlugin { - void initializePhyCon(PhyConAPI api); + void initializePhyCon(@NotNull PhyConAPI api); } diff --git a/src/main/java/net/shadowfacts/phycon/api/TerminalSettingKey.java b/src/main/java/net/shadowfacts/phycon/api/TerminalSettingKey.java index f109ea6..f4c45ae 100644 --- a/src/main/java/net/shadowfacts/phycon/api/TerminalSettingKey.java +++ b/src/main/java/net/shadowfacts/phycon/api/TerminalSettingKey.java @@ -11,4 +11,6 @@ public interface TerminalSettingKey & TerminalSetting> { E getValue(); + void setPriority(int priority); + } diff --git a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt index 2a95d38..f9cb7e7 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt @@ -79,8 +79,8 @@ open class CacaoScreen(title: Text = LiteralText("CacaoScreen")): Screen(title), } } - override fun resize(client: MinecraftClient, width: Int, height: Int) { - super.resize(client, width, height) + override fun init() { + super.init() windows.forEach { it.resize(width, height) diff --git a/src/main/kotlin/net/shadowfacts/phycon/DefaultPlugin.kt b/src/main/kotlin/net/shadowfacts/phycon/DefaultPlugin.kt index 98de308..29d30f4 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/DefaultPlugin.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/DefaultPlugin.kt @@ -16,6 +16,7 @@ object DefaultPlugin: PhyConPlugin { override fun initializePhyCon(api: PhyConAPI) { SORT_MODE = api.registerTerminalSetting(Identifier(PhysicalConnectivity.MODID, "sort"), SortMode.COUNT_HIGH_FIRST) + SORT_MODE.setPriority(Int.MAX_VALUE) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalScreen.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalScreen.kt index 38c3beb..7cecc46 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalScreen.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/AbstractTerminalScreen.kt @@ -35,12 +35,29 @@ abstract class AbstractTerminalScreen(handler, playerInv, title) { + interface SearchQueryListener { + fun terminalSearchQueryChanged(newValue: String) + fun requestTerminalSearchFieldUpdate(): String? + } + + companion object { + var searchQueryListener: SearchQueryListener? = null + } + abstract val backgroundTexture: Identifier val terminalVC: AbstractTerminalViewController<*, *, *> var amountVC: TerminalRequestAmountViewController? = null + private var prevSearchQuery = "" var searchQuery = "" + set(value) { + field = value + if (prevSearchQuery != value) { + searchQueryListener?.terminalSearchQueryChanged(value) + } + prevSearchQuery = value + } var scrollPosition = 0.0 init { @@ -147,6 +164,13 @@ abstract class AbstractTerminalScreen + TerminalSettings.allKeys.sortedByDescending { it.priority }.forEach { key -> val button = SettingButton(key) button.handler = { settingsChanged() } settingsStack.addArrangedSubview(button) diff --git a/src/main/kotlin/net/shadowfacts/phycon/client/screen/ScrollTrackView.kt b/src/main/kotlin/net/shadowfacts/phycon/client/screen/ScrollTrackView.kt index c0533a3..fc98f31 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/client/screen/ScrollTrackView.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/client/screen/ScrollTrackView.kt @@ -18,7 +18,7 @@ class ScrollTrackView( ): View() { companion object { - private val THUMB = Texture(Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png"), 52, 230) + private val THUMB = Texture(Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png"), 52, 224) private const val THUMB_WIDTH = 12.0 private const val THUMB_HEIGHT = 15.0 } diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/SortMode.kt b/src/main/kotlin/net/shadowfacts/phycon/util/SortMode.kt index 7d304e4..f4cfebc 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/SortMode.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/SortMode.kt @@ -17,9 +17,9 @@ enum class SortMode: TerminalSetting { override fun getIconTexture() = Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png") override fun getUV() = when (this) { - COUNT_HIGH_FIRST -> intArrayOf(0, 230) - COUNT_LOW_FIRST -> intArrayOf(16, 230) - ALPHABETICAL -> intArrayOf(32, 230) + COUNT_HIGH_FIRST -> intArrayOf(0, 224) + COUNT_LOW_FIRST -> intArrayOf(16, 224) + ALPHABETICAL -> intArrayOf(32, 224) } override fun getTooltip() = when (this) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/TerminalSettings.kt b/src/main/kotlin/net/shadowfacts/phycon/util/TerminalSettings.kt index 10c6899..3e59bb3 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/TerminalSettings.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/TerminalSettings.kt @@ -19,7 +19,7 @@ class TerminalSettings { get() = SETTINGS.values fun register(id: Identifier, defaultValue: E): SettingKey where E: Enum, E: TerminalSetting { - val setting = SettingKey(id, defaultValue.javaClass) + val setting = SettingKey(id, defaultValue.javaClass, 0) SETTINGS[id] = setting DEFAULTS[setting] = defaultValue return setting @@ -34,7 +34,11 @@ class TerminalSettings { class SettingKey( val id: Identifier, val clazz: Class, + priority: Int, ): TerminalSettingKey where E: Enum, E: TerminalSetting { + var priority: Int = priority + private set + fun value(ordinal: Int): E? { @Suppress("UNCHECKED_CAST") return if (clazz.enumConstants.size <= ordinal) null @@ -43,6 +47,9 @@ class TerminalSettings { override fun getID() = id override fun getValue() = PhysicalConnectivityClient.terminalSettings[this] + override fun setPriority(priority: Int) { + this.priority = priority + } } private val settings = mutableMapOf, Enum<*>>() diff --git a/src/main/resources/assets/phycon/textures/gui/terminal.png b/src/main/resources/assets/phycon/textures/gui/terminal.png index 04b0615b7b267a49dc3676c1eb4e5592b5a842c1..4d2bf183c20ce1929838dd65bf475a3984a99d1d 100644 GIT binary patch literal 4483 zcmcgw2Ut^C7QXNCfB?>lpduECU}GZ$!di$)2ptBg(t;YAfei>j5}LF`X9NXBS40P$ zfpNw*R4F0?l34^@EHDD1Ac|5VDqSEHVQ)}J*41yP?C1Nw`|i2tp8wp_-g)nP`|WKl zzK~xm4*>YW%F@&k00fc zameC710Ylvfajh7kWT=h5`5~40|jp6vD~bDZEV0_A&mpl$YLPTAUWepWXZ3zBccaT z$Xf)5J3^YF=R-Oqo((WKOXT;#A-OE!kgR8B^PQ3GAV0hf%ZrC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