Skip to content

Commit

Permalink
feat: ✨ Backpack open keybind now works in all container screens if t…
Browse files Browse the repository at this point in the history
…he backpack in player's inventory is hovered. That also means that from backpack screen another backpack in player's inventory can be open without the need to go back to player's inventory and open from there.
  • Loading branch information
P3pp3rF1y committed Dec 2, 2023
1 parent 96fd77d commit fabe86e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 45 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
minecraft_version=1.19.2
forge_version=43.2.13
mod_version=3.19.2
mod_version=3.19.3
jei_mc_version=1.19.2-forge
jei_version=11.6.0.+
curios_version=1.19.2-5.1.1.+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.BlockHitResult;
Expand All @@ -35,9 +36,11 @@
import net.p3pp3rf1y.sophisticatedbackpacks.network.InventoryInteractionMessage;
import net.p3pp3rf1y.sophisticatedbackpacks.network.SBPPacketHandler;
import net.p3pp3rf1y.sophisticatedbackpacks.network.UpgradeToggleMessage;
import net.p3pp3rf1y.sophisticatedbackpacks.util.PlayerInventoryProvider;
import net.p3pp3rf1y.sophisticatedcore.util.WorldHelper;

import java.util.Map;
import java.util.Optional;

import static net.minecraftforge.client.settings.KeyConflictContext.GUI;

Expand All @@ -50,7 +53,8 @@ private KeybindHandler() {}
private static final int KEY_X = 88;
private static final int KEY_UNKNOWN = -1;
private static final int MIDDLE_BUTTON = 2;
private static final int CHEST_SLOT_INDEX = 6;
private static final int CHEST_SLOT_INDEX = 38;
private static final int OFFHAND_SLOT_INDEX = 40;
private static final String KEYBIND_SOPHISTICATEDBACKPACKS_CATEGORY = "keybind.sophisticatedbackpacks.category";
public static final KeyMapping BACKPACK_TOGGLE_UPGRADE_5 = new KeyMapping(SBPTranslationHelper.INSTANCE.translKeybind("toggle_upgrade_5"),
KeyConflictContext.UNIVERSAL, InputConstants.Type.KEYSYM.getOrCreate(KEY_UNKNOWN), KEYBIND_SOPHISTICATEDBACKPACKS_CATEGORY);
Expand Down Expand Up @@ -81,7 +85,7 @@ private KeybindHandler() {}

public static void register() {
IEventBus eventBus = MinecraftForge.EVENT_BUS;
eventBus.addListener(KeybindHandler::handleKeyInputEvent);
eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleKeyInputEvent);
eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleGuiMouseKeyPress);
eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleGuiKeyPress);
}
Expand All @@ -95,17 +99,16 @@ public static void registerKeyMappings(RegisterKeyMappingsEvent event) {
}

public static void handleGuiKeyPress(ScreenEvent.KeyPressed.Pre event) {
if (SORT_KEYBIND.isActiveAndMatches(InputConstants.getKey(event.getKeyCode(), event.getScanCode())) && tryCallSort(event.getScreen())) {
InputConstants.Key key = InputConstants.getKey(event.getKeyCode(), event.getScanCode());
if (SORT_KEYBIND.isActiveAndMatches(key) && tryCallSort(event.getScreen()) || BACKPACK_OPEN_KEYBIND.isActiveAndMatches(key) && sendBackpackOpenOrCloseMessage()) {
event.setCanceled(true);
}
}

public static void handleGuiMouseKeyPress(ScreenEvent.MouseButtonPressed.Pre event) {
InputConstants.Key input = InputConstants.Type.MOUSE.getOrCreate(event.getButton());
if (SORT_KEYBIND.isActiveAndMatches(input) && tryCallSort(event.getScreen())) {
if (SORT_KEYBIND.isActiveAndMatches(input) && tryCallSort(event.getScreen()) || BACKPACK_OPEN_KEYBIND.isActiveAndMatches(input) && sendBackpackOpenOrCloseMessage()) {
event.setCanceled(true);
} else if (BACKPACK_OPEN_KEYBIND.isActiveAndMatches(input)) {
sendBackpackOpenOrCloseMessage();
}
}

Expand Down Expand Up @@ -178,45 +181,57 @@ private static void sendInteractWithInventoryMessage() {
}

@SuppressWarnings({"java:S2440", "InstantiationOfUtilityClass"})
private static void sendBackpackOpenOrCloseMessage() {
private static boolean sendBackpackOpenOrCloseMessage() {
if (!GUI.isActive()) {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage());
return;
return false;
}

Screen screen = Minecraft.getInstance().screen;
if (screen instanceof BackpackScreen backpackScreen) {
Slot slot = backpackScreen.getSlotUnderMouse();
if (slot != null && slot.getItem().getItem() instanceof BackpackItem) {
if (slot.getItem().getCount() == 1) {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage(slot.index));
if (screen instanceof AbstractContainerScreen<?> containerScreen) {
Slot slot = containerScreen.getSlotUnderMouse();

if (slot != null && slot.container instanceof Inventory) {
Optional<String> handlerName = getPlayerInventoryHandlerName(slot.getSlotIndex());

if (handlerName.isPresent() && slot.getItem().getItem() instanceof BackpackItem) {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage(slot.getSlotIndex(), "", handlerName.get()));
return true;
}
} else {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackCloseMessage());
}
} else if (screen instanceof InventoryScreen inventoryScreen) {
Slot slot = inventoryScreen.getSlotUnderMouse();

if (slot != null && isSupportedPlayerInventorySlot(slot.index) && slot.getItem().getItem() instanceof BackpackItem) {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage(slot.getSlotIndex()));
if (screen instanceof BackpackScreen) {
if (slot != null && slot.getItem().getItem() instanceof BackpackItem) {
if (slot.getItem().getCount() == 1) {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage(slot.index));
return true;
}
} else {
SBPPacketHandler.INSTANCE.sendToServer(new BackpackCloseMessage());
return true;
}
}
}
return false;
}

private static boolean isSupportedPlayerInventorySlot(int slotIndex) {
return slotIndex == CHEST_SLOT_INDEX || (slotIndex > 8 && slotIndex < 46);
private static Optional<String> getPlayerInventoryHandlerName(int slotIndex) {
if (slotIndex == CHEST_SLOT_INDEX) {
return Optional.of(PlayerInventoryProvider.ARMOR_INVENTORY);
} else if (slotIndex == OFFHAND_SLOT_INDEX) {
return Optional.of(PlayerInventoryProvider.OFFHAND_INVENTORY);
} else if (slotIndex >= 0 && slotIndex < 36) {
return Optional.of(PlayerInventoryProvider.MAIN_INVENTORY);
}

return Optional.empty();
}

private static class BackpackKeyConflictContext implements IKeyConflictContext {
public static final BackpackKeyConflictContext INSTANCE = new BackpackKeyConflictContext();

@Override
public boolean isActive() {
if (!GUI.isActive()) {
return true;
}
Screen screen = Minecraft.getInstance().screen;
return screen instanceof BackpackScreen || screen instanceof InventoryScreen;
return !GUI.isActive() || Minecraft.getInstance().screen instanceof AbstractContainerScreen<?>;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BackpackScreen(BackpackContainer screenContainer, Inventory inv, Componen
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == 256 || KeybindHandler.BACKPACK_OPEN_KEYBIND.isActiveAndMatches(InputConstants.getKey(keyCode, scanCode))) {
if (getMenu().isFirstLevelStorage() && getMenu().getBackpackContext().wasOpenFromInventory() && mouseNotOverBackpack()) {
if (getMenu().isFirstLevelStorage() && getMenu().getBackpackContext().wasOpenFromInventory() && (keyCode == 256 || mouseNotOverBackpack())) {
getMinecraft().player.closeContainer();
getMinecraft().setScreen(new InventoryScreen(getMinecraft().player));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BackpackOpenMessage {
private static final int OFFHAND_SLOT = 40;
private final int slotIndex;
private final String identifier;
private final String handlerName;

public BackpackOpenMessage() {
this(-1);
Expand All @@ -28,18 +29,24 @@ public BackpackOpenMessage(int backpackSlot) {
this(backpackSlot, "");
}

public BackpackOpenMessage(int backpackSlot, String identifier) {
public BackpackOpenMessage(int backpackSlot, String identifier, String handlerName) {
slotIndex = backpackSlot;
this.identifier = identifier;
this.handlerName = handlerName;
}

public BackpackOpenMessage(int backpackSlot, String identifier) {
this(backpackSlot, identifier, "");
}

public static void encode(BackpackOpenMessage msg, FriendlyByteBuf packetBuffer) {
packetBuffer.writeInt(msg.slotIndex);
packetBuffer.writeUtf(msg.identifier);
packetBuffer.writeUtf(msg.handlerName);
}

public static BackpackOpenMessage decode(FriendlyByteBuf packetBuffer) {
return new BackpackOpenMessage(packetBuffer.readInt(), packetBuffer.readUtf());
return new BackpackOpenMessage(packetBuffer.readInt(), packetBuffer.readUtf(), packetBuffer.readUtf());
}

static void onMessage(BackpackOpenMessage msg, Supplier<NetworkEvent.Context> contextSupplier) {
Expand All @@ -53,7 +60,17 @@ private static void handleMessage(@Nullable ServerPlayer player, BackpackOpenMes
return;
}

if (player.containerMenu instanceof BackpackContainer backpackContainer) {
if (!msg.handlerName.isEmpty()) {
int slotIndex = msg.slotIndex;
if (msg.slotIndex == CHEST_SLOT) {
slotIndex -= 36;
} else if (msg.slotIndex == OFFHAND_SLOT) {
slotIndex = 0;
}
BackpackContext.Item backpackContext = new BackpackContext.Item(msg.handlerName, msg.identifier, slotIndex,
player.containerMenu instanceof InventoryMenu || (player.containerMenu instanceof BackpackContainer backpackContainer && backpackContainer.getBackpackContext().wasOpenFromInventory()));
openBackpack(player, backpackContext);
} else if (player.containerMenu instanceof BackpackContainer backpackContainer) {
BackpackContext backpackContext = backpackContainer.getBackpackContext();
if (msg.slotIndex == -1) {
openBackpack(player, backpackContext.getParentBackpackContext());
Expand All @@ -63,18 +80,6 @@ private static void handleMessage(@Nullable ServerPlayer player, BackpackOpenMes
} else if (player.containerMenu instanceof IContextAwareContainer contextAwareContainer) {
BackpackContext backpackContext = contextAwareContainer.getBackpackContext();
openBackpack(player, backpackContext);
} else if (msg.slotIndex > -1 && player.containerMenu instanceof InventoryMenu) {
int slotIndex = msg.slotIndex;
String inventoryProvider = PlayerInventoryProvider.MAIN_INVENTORY;
if (msg.slotIndex == CHEST_SLOT) {
inventoryProvider = PlayerInventoryProvider.ARMOR_INVENTORY;
} else if (msg.slotIndex == OFFHAND_SLOT) {
inventoryProvider = PlayerInventoryProvider.OFFHAND_INVENTORY;
slotIndex = 0;
}

BackpackContext.Item backpackContext = new BackpackContext.Item(inventoryProvider, msg.identifier, slotIndex, true);
openBackpack(player, backpackContext);
} else {
findAndOpenFirstBackpack(player);
}
Expand Down

0 comments on commit fabe86e

Please sign in to comment.