diff --git a/build.gradle b/build.gradle index 310fc5e3..5b1aa29a 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,9 @@ repositories { username = System.getenv("USERNAME") password = System.getenv("READ_PACKAGES_TOKEN") } + content { + includeGroupByRegex "sophisticatedcore.*" + } } } diff --git a/gradle.properties b/gradle.properties index b3615b69..fd84b704 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.daemon=false mod_id=sophisticatedbackpacks mod_group_id=sophisticatedbackpacks -mod_version=3.19.1 +mod_version=3.19.4 sonar_project_key=sophisticatedbackpacks:SophisticatedBackpacks github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedBackpacks diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/KeybindHandler.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/KeybindHandler.java index 0f23c9b6..7e52ad36 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/KeybindHandler.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/KeybindHandler.java @@ -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; @@ -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; @@ -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); @@ -81,8 +85,7 @@ private KeybindHandler() {} public static void register() { IEventBus eventBus = MinecraftForge.EVENT_BUS; - eventBus.addListener(KeybindHandler::handleKeyInputEvent); - eventBus.addListener(KeybindHandler::screenKeyPressed); + eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleKeyInputEvent); eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleGuiMouseKeyPress); eventBus.addListener(EventPriority.HIGH, KeybindHandler::handleGuiKeyPress); } @@ -97,19 +100,15 @@ public static void registerKeyMappings(RegisterKeyMappingsEvent event) { public static void handleGuiKeyPress(ScreenEvent.KeyPressed.Pre event) { InputConstants.Key key = InputConstants.getKey(event.getKeyCode(), event.getScanCode()); - if (SORT_KEYBIND.isActiveAndMatches(key) && tryCallSort(event.getScreen())) { + if (SORT_KEYBIND.isActiveAndMatches(key) && tryCallSort(event.getScreen()) || BACKPACK_OPEN_KEYBIND.isActiveAndMatches(key) && sendBackpackOpenOrCloseMessage()) { event.setCanceled(true); - } else if (BACKPACK_OPEN_KEYBIND.isActiveAndMatches(key)) { - sendBackpackOpenOrCloseMessage(); } } 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(); } } @@ -129,10 +128,6 @@ public static void handleKeyInputEvent(TickEvent.ClientTickEvent event) { } } - private static void screenKeyPressed(ScreenEvent.KeyPressed.Post event) { - - } - private static boolean tryCallSort(Screen gui) { Minecraft mc = Minecraft.getInstance(); if (mc.player != null && mc.player.containerMenu instanceof BackpackContainer container && gui instanceof BackpackScreen screen) { @@ -186,33 +181,42 @@ 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 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 && slot != null && slot.getItem().getItem() instanceof BackpackItem && slot.getItem().getCount() == 1) { + SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage(slot.index)); + return true; } } + return false; } - private static boolean isSupportedPlayerInventorySlot(int slotIndex) { - return slotIndex == CHEST_SLOT_INDEX || (slotIndex > 8 && slotIndex < 46); + private static Optional 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 { @@ -220,11 +224,7 @@ private static class BackpackKeyConflictContext implements IKeyConflictContext { @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 diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/gui/BackpackScreen.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/gui/BackpackScreen.java index ac3e09f0..ccb9166a 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/gui/BackpackScreen.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/gui/BackpackScreen.java @@ -24,9 +24,13 @@ 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()) { - getMinecraft().player.closeContainer(); - getMinecraft().setScreen(new InventoryScreen(getMinecraft().player)); + if (getMenu().isFirstLevelStorage() && (keyCode == 256 || mouseNotOverBackpack())) { + if (getMenu().getBackpackContext().wasOpenFromInventory()) { + getMinecraft().player.closeContainer(); + getMinecraft().setScreen(new InventoryScreen(getMinecraft().player)); + } else { + onClose(); + } return true; } else if (!getMenu().isFirstLevelStorage()) { SBPPacketHandler.INSTANCE.sendToServer(new BackpackOpenMessage()); diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/network/BackpackOpenMessage.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/network/BackpackOpenMessage.java index 95f270bb..5cf16805 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/network/BackpackOpenMessage.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/network/BackpackOpenMessage.java @@ -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); @@ -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 contextSupplier) { @@ -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()); @@ -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); } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/toolswapper/ToolSwapperUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/toolswapper/ToolSwapperUpgradeWrapper.java index 7faeb932..861e0eb5 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/toolswapper/ToolSwapperUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/toolswapper/ToolSwapperUpgradeWrapper.java @@ -1,5 +1,8 @@ package net.p3pp3rf1y.sophisticatedbackpacks.upgrades.toolswapper; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.Multimap; import com.google.common.util.concurrent.AtomicDouble; import net.minecraft.core.BlockPos; @@ -50,6 +53,7 @@ import java.util.LinkedList; import java.util.Queue; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; @@ -60,7 +64,14 @@ public class ToolSwapperUpgradeWrapper extends UpgradeWrapperBase implements IBlockClickResponseUpgrade, IAttackEntityResponseUpgrade, IBlockToolSwapUpgrade, IEntityToolSwapUpgrade { - private static final Set notTools = new HashSet<>(); + private static final LoadingCache isToolCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build( + new CacheLoader<>() { + @Override + public Boolean load(ItemStack key) { + return canPerformToolAction(key); + } + } + ); private final FilterLogic filterLogic; @Nullable @@ -163,26 +174,16 @@ public boolean onAttackEntity(Player player) { } private boolean isNotTool(ItemStack stack) { - if (notTools.contains(stack.getItem())) { - return true; - } - - if (canPerformToolAction(stack)) { - return false; - } - - notTools.add(stack.getItem()); - - return true; + return !isToolCache.getUnchecked(stack); } - private boolean canPerformToolAction(ItemStack stack) { + private static boolean canPerformToolAction(ItemStack stack) { return canPerformAnyAction(stack, ToolActions.DEFAULT_AXE_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_HOE_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_PICKAXE_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_SHOVEL_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_SHEARS_ACTIONS); } - private boolean canPerformAnyAction(ItemStack stack, Set toolActions) { + private static boolean canPerformAnyAction(ItemStack stack, Set toolActions) { for (ToolAction toolAction : toolActions) { if (stack.canPerformAction(toolAction)) { return true;