Skip to content

Commit

Permalink
Make the bound hotkey for wireless terminals/portable cells also clos…
Browse files Browse the repository at this point in the history
…e them (#7425)


---------

Co-authored-by: James Daniels <danielsj9@gmail.com>
Co-authored-by: Sebastian Hartte <shartte@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 5, 2023
1 parent 453f1f6 commit 44ea207
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/appeng/api/storage/ITerminalHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions src/main/java/appeng/client/Hotkeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Hotkey> HOTKEYS = new HashMap<>();
Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/appeng/client/gui/me/common/MEStorageScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,6 +93,8 @@
public class MEStorageScreen<C extends MEStorageMenu>
extends AEBaseScreen<C> 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;
Expand Down Expand Up @@ -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_);
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/appeng/helpers/WirelessTerminalMenuHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,4 +187,8 @@ public void returnToMainMenu(Player player, ISubMenu subMenu) {
public ItemStack getMainMenuIcon() {
return getItemStack();
}

public String getCloseHotkey() {
return HotkeyAction.WIRELESS_TERMINAL;
}
}
15 changes: 15 additions & 0 deletions src/main/java/appeng/items/contents/PortableCellMenuHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}

0 comments on commit 44ea207

Please sign in to comment.