Skip to content

Commit

Permalink
Merge pull request #929 from P3pp3rF1y/1.20.x-dev
Browse files Browse the repository at this point in the history
Release merge
  • Loading branch information
P3pp3rF1y authored Dec 11, 2023
2 parents fc8dc96 + 5b70066 commit 94e904f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 68 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ repositories {
username = System.getenv("USERNAME")
password = System.getenv("READ_PACKAGES_TOKEN")
}
content {
includeGroupByRegex "sophisticatedcore.*"
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

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,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);
}
Expand All @@ -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();
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -186,45 +181,50 @@ 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 && 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<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,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());
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -60,7 +64,14 @@
public class ToolSwapperUpgradeWrapper extends UpgradeWrapperBase<ToolSwapperUpgradeWrapper, ToolSwapperUpgradeItem>
implements IBlockClickResponseUpgrade, IAttackEntityResponseUpgrade, IBlockToolSwapUpgrade, IEntityToolSwapUpgrade {

private static final Set<Item> notTools = new HashSet<>();
private static final LoadingCache<ItemStack, Boolean> isToolCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build(
new CacheLoader<>() {
@Override
public Boolean load(ItemStack key) {
return canPerformToolAction(key);
}
}
);

private final FilterLogic filterLogic;
@Nullable
Expand Down Expand Up @@ -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<ToolAction> toolActions) {
private static boolean canPerformAnyAction(ItemStack stack, Set<ToolAction> toolActions) {
for (ToolAction toolAction : toolActions) {
if (stack.canPerformAction(toolAction)) {
return true;
Expand Down

0 comments on commit 94e904f

Please sign in to comment.