From 44ea207b2ff6f2a7a8209b7a6dc4b8319c12f0f2 Mon Sep 17 00:00:00 2001 From: Mari023 <38946771+Mari023@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:50:06 +0200 Subject: [PATCH] Make the bound hotkey for wireless terminals/portable cells also close them (#7425) --------- Co-authored-by: James Daniels Co-authored-by: Sebastian Hartte --- .../appeng/api/storage/ITerminalHost.java | 11 +++++++++ src/main/java/appeng/client/Hotkeys.java | 11 +++++++++ .../client/gui/me/common/MEStorageScreen.java | 23 +++++++++++++++++++ .../helpers/WirelessTerminalMenuHost.java | 5 ++++ .../items/contents/PortableCellMenuHost.java | 15 ++++++++++++ 5 files changed, 65 insertions(+) diff --git a/src/main/java/appeng/api/storage/ITerminalHost.java b/src/main/java/appeng/api/storage/ITerminalHost.java index 54b7d8ec635..6f1f2ff784c 100644 --- a/src/main/java/appeng/api/storage/ITerminalHost.java +++ b/src/main/java/appeng/api/storage/ITerminalHost.java @@ -27,8 +27,19 @@ import appeng.api.upgrades.IUpgradeableObject; import appeng.api.util.IConfigurableObject; +import appeng.client.Hotkeys; public interface ITerminalHost extends IUpgradeableObject, IConfigurableObject, ISubMenuHost { @Nullable MEStorage getInventory(); + + /** + * An optional hotkey used to close the terminal while its open. + * + * @return Hotkey id as it would be registered by {@link Hotkeys}, or null if there isn't one + */ + @Nullable + default String getCloseHotkey() { + return null; + } } diff --git a/src/main/java/appeng/client/Hotkeys.java b/src/main/java/appeng/client/Hotkeys.java index 1b2f7ec4c80..36f34e2617d 100644 --- a/src/main/java/appeng/client/Hotkeys.java +++ b/src/main/java/appeng/client/Hotkeys.java @@ -2,11 +2,17 @@ import java.util.HashMap; +import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.KeyMapping; +import appeng.hotkeys.HotkeyActions; + +/** + * client side component of {@link HotkeyActions} + */ public class Hotkeys { private static final HashMap HOTKEYS = new HashMap<>(); @@ -27,4 +33,9 @@ public static void registerHotkey(String id) { public static void checkHotkeys() { HOTKEYS.forEach((name, hotkey) -> hotkey.check()); } + + @Nullable + public static Hotkey getHotkeyMapping(@Nullable String id) { + return HOTKEYS.get(id); + } } diff --git a/src/main/java/appeng/client/gui/me/common/MEStorageScreen.java b/src/main/java/appeng/client/gui/me/common/MEStorageScreen.java index d8317377617..1b1e84dbc7d 100644 --- a/src/main/java/appeng/client/gui/me/common/MEStorageScreen.java +++ b/src/main/java/appeng/client/gui/me/common/MEStorageScreen.java @@ -27,6 +27,8 @@ import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; @@ -52,6 +54,7 @@ import appeng.api.storage.AEKeyFilter; import appeng.api.util.IConfigManager; import appeng.api.util.IConfigurableObject; +import appeng.client.Hotkeys; import appeng.client.Point; import appeng.client.gui.AEBaseScreen; import appeng.client.gui.AESubScreen; @@ -90,6 +93,8 @@ public class MEStorageScreen extends AEBaseScreen implements ISortSource, IConfigManagerListener { + private static final Logger LOG = LoggerFactory.getLogger(MEStorageScreen.class); + private static final String TEXT_ID_ENTRIES_SHOWN = "entriesShown"; private static final int MIN_ROWS = 2; @@ -690,6 +695,11 @@ public boolean keyPressed(int keyCode, int scanCode, int p_keyPressed_3_) { return true; } + if (!this.searchField.isFocused() && isCloseHotkey(keyCode, scanCode)) { + this.getPlayer().closeContainer(); + return true; + } + return super.keyPressed(keyCode, scanCode, p_keyPressed_3_); } @@ -779,6 +789,19 @@ private void reinitalize() { this.init(); } + private boolean isCloseHotkey(int keyCode, int scanCode) { + var hotkeyId = getMenu().getHost().getCloseHotkey(); + if (hotkeyId != null) { + var hotkey = Hotkeys.getHotkeyMapping(hotkeyId); + if (hotkey != null) { + return hotkey.mapping().matches(keyCode, scanCode); + } else { + LOG.warn("Terminal host returned unknown hotkey id: {}", hotkeyId); + } + } + return false; + } + /** * Store current terminal state, so it can be restored by the next constructor call of {@link MEStorageScreen}. Call * this manually if you need to store state before switching screens without explicitly removing the previous diff --git a/src/main/java/appeng/helpers/WirelessTerminalMenuHost.java b/src/main/java/appeng/helpers/WirelessTerminalMenuHost.java index 1670af511f7..102ae1215cf 100644 --- a/src/main/java/appeng/helpers/WirelessTerminalMenuHost.java +++ b/src/main/java/appeng/helpers/WirelessTerminalMenuHost.java @@ -28,6 +28,7 @@ import appeng.api.config.Actionable; import appeng.api.config.PowerMultiplier; +import appeng.api.features.HotkeyAction; import appeng.api.implementations.blockentities.IWirelessAccessPoint; import appeng.api.implementations.menuobjects.IPortableTerminal; import appeng.api.implementations.menuobjects.ItemMenuHost; @@ -186,4 +187,8 @@ public void returnToMainMenu(Player player, ISubMenu subMenu) { public ItemStack getMainMenuIcon() { return getItemStack(); } + + public String getCloseHotkey() { + return HotkeyAction.WIRELESS_TERMINAL; + } } diff --git a/src/main/java/appeng/items/contents/PortableCellMenuHost.java b/src/main/java/appeng/items/contents/PortableCellMenuHost.java index 54e52e6ab98..0ab7ef2821e 100644 --- a/src/main/java/appeng/items/contents/PortableCellMenuHost.java +++ b/src/main/java/appeng/items/contents/PortableCellMenuHost.java @@ -35,10 +35,13 @@ import appeng.api.config.SortDir; import appeng.api.config.SortOrder; import appeng.api.config.ViewItems; +import appeng.api.features.HotkeyAction; import appeng.api.implementations.menuobjects.IPortableTerminal; import appeng.api.implementations.menuobjects.ItemMenuHost; +import appeng.api.stacks.AEKeyType; import appeng.api.storage.MEStorage; import appeng.api.storage.StorageCells; +import appeng.api.storage.cells.IBasicCellItem; import appeng.api.util.IConfigManager; import appeng.items.tools.powered.AbstractPortableCell; import appeng.menu.ISubMenu; @@ -106,4 +109,16 @@ public void returnToMainMenu(Player player, ISubMenu subMenu) { public ItemStack getMainMenuIcon() { return getItemStack(); } + + public String getCloseHotkey() { + if (item instanceof IBasicCellItem cellItem) { + if (cellItem.getKeyType().equals(AEKeyType.items())) { + return HotkeyAction.PORTABLE_ITEM_CELL; + } else if (cellItem.getKeyType().equals(AEKeyType.fluids())) { + return HotkeyAction.PORTABLE_FLUID_CELL; + } + } + + return null; // We don't know + } }