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
# Mod Properties
mod_version=1.6
mod_version=1.7
maven_group=systems.brn
archives_base_name=Server_storage

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

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