Allow taking out less than a stack if there is not space for more

This commit is contained in:
Bruno Rybársky 2024-05-25 12:55:29 +02:00
parent 8c50e0fd52
commit fce0e3ffa1
3 changed files with 19 additions and 11 deletions

@ -8,7 +8,7 @@ yarn_mappings=1.20.6+build.3
loader_version=0.15.11 loader_version=0.15.11
# Mod Properties # Mod Properties
mod_version=1.6 mod_version=1.7
maven_group=systems.brn maven_group=systems.brn
archives_base_name=Server_storage archives_base_name=Server_storage

@ -240,24 +240,28 @@ public class StorageOperations {
return stack; return stack;
} }
// Method to check if an item can be inserted into a player's inventory public static int canInsertItemIntoPlayerInventory(PlayerEntity player, ItemStack itemStack) {
public static boolean canInsertItemIntoPlayerInventory(PlayerEntity player, ItemStack itemStack) {
// Get the player's inventory // Get the player's inventory
PlayerInventory playerInventory = player.getInventory(); PlayerInventory playerInventory = player.getInventory();
int maxInsert = 0;
// Iterate through the slots in the player's inventory // Iterate through the slots in the player's inventory
for (int i = 0; i < playerInventory.main.size(); i++) { for (int i = 0; i < playerInventory.main.size(); i++) {
ItemStack slotStack = playerInventory.main.get(i); ItemStack slotStack = playerInventory.main.get(i);
// Check if the slot is empty or if there's space for the item // Check if the slot is empty or if there's space for the item
if ( if (slotStack.isEmpty() || ItemStack.areItemsEqual(slotStack, itemStack)) {
slotStack.isEmpty() || int remainingSpace = slotStack.isEmpty() ? itemStack.getMaxCount() : slotStack.getMaxCount() - slotStack.getCount();
(ItemStack.areItemsEqual(slotStack, itemStack) && slotStack.getCount() + itemStack.getCount() <= slotStack.getMaxCount())) { maxInsert += remainingSpace;
return true; // Space available // If the maximum insertion count is greater than or equal to the item count, return the item count
if (maxInsert >= itemStack.getCount()) {
return Math.min(itemStack.getCount(), remainingSpace);
}
} }
} }
return false; // No space available return maxInsert; // Return the maximum insertion count
} }
private static boolean canCombine(ItemStack stack1, ItemStack stack2) { private static boolean canCombine(ItemStack stack1, ItemStack stack2) {

@ -87,9 +87,13 @@ public class StorageScreen extends PagedGui {
if (noLoreStack.getCount() > noLoreStack.getMaxCount()) { if (noLoreStack.getCount() > noLoreStack.getMaxCount()) {
noLoreStack.setCount(noLoreStack.getMaxCount()); noLoreStack.setCount(noLoreStack.getMaxCount());
} }
if (canRemoveFromInventory(inventory, noLoreStack) && canInsertItemIntoPlayerInventory(player, noLoreStack)) { int maxToInsert = canInsertItemIntoPlayerInventory(player, noLoreStack);
player.getInventory().insertStack(noLoreStack.copy()); int insertCount = Math.min(maxToInsert, noLoreStack.getCount());
removeItemStackFromChests(inventories, noLoreStack); ItemStack insertingStack = noLoreStack.copy();
insertingStack.setCount(insertCount);
if (canRemoveFromInventory(inventory, noLoreStack) && insertCount > 0) {
player.getInventory().insertStack(insertingStack.copy());
removeItemStackFromChests(inventories, insertingStack);
} }
} }
this.updateDisplay(); this.updateDisplay();