inventory #14
@@ -6,20 +6,19 @@ import Item.ItemStack;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Inventory {
|
||||
private final List<ItemStack> items;
|
||||
private final int weightLimit;
|
||||
private final int maxWeight;
|
||||
|
||||
public Inventory() {
|
||||
items = new ArrayList<>();
|
||||
weightLimit = 100;
|
||||
maxWeight = 100;
|
||||
}
|
||||
|
||||
public Inventory(int weightLimit) {
|
||||
items = new ArrayList<>();
|
||||
this.weightLimit = weightLimit;
|
||||
this.maxWeight = weightLimit;
|
||||
}
|
||||
|
||||
public List<ItemStack> getItems() {
|
||||
@@ -27,38 +26,28 @@ public class Inventory {
|
||||
}
|
||||
|
||||
public void addItem(ItemStack stack) {
|
||||
boolean isSingleItem = stack.getQuantity() == 1;
|
||||
if (isSingleItem) {
|
||||
if(stack.getItem().getWeight() > weightLimit) return;
|
||||
items.add(stack);
|
||||
if (stack.getWeight() + getCurrentWeight() > maxWeight) {
|
||||
if(stack.isSingleItem()) return;
|
||||
trySubstack(stack);
|
||||
return;
|
||||
}
|
||||
if (stack.getWeight() + getCurrentWeight() > weightLimit) addSubStack(stack);
|
||||
items.add(stack);
|
||||
}
|
||||
|
||||
private void addSubStack(ItemStack stack) {
|
||||
int maxQuantity = calculateMaxQuantity(stack);
|
||||
items.add(new ItemStack(stack.getItem(), maxQuantity));
|
||||
private void trySubstack(ItemStack stack) {
|
||||
int stackSize = getMaxStackSize(stack);
|
||||
if (stackSize == 0) return;
|
||||
items.add(new ItemStack(stack.getItem(), stackSize));
|
||||
}
|
||||
|
||||
private int calculateMaxQuantity(ItemStack stack) {
|
||||
int maxQuantity = 1;
|
||||
private int getMaxStackSize(ItemStack stack) {
|
||||
int weight = stack.getItem().getWeight();
|
||||
|
||||
|
||||
for (int i = maxQuantity; i <= stack.getQuantity(); i++) {
|
||||
int currentWeightWithStack = (weight * maxQuantity) + getCurrentWeight();
|
||||
boolean weightOutsideLimits = currentWeightWithStack >= weightLimit;
|
||||
if (weightOutsideLimits) {
|
||||
break;
|
||||
} else {
|
||||
maxQuantity++;
|
||||
}
|
||||
}
|
||||
return maxQuantity;
|
||||
int maxSize = (maxWeight - getCurrentWeight()) / weight;
|
||||
return Math.min(maxSize, stack.getQuantity());
|
||||
}
|
||||
|
||||
private int getCurrentWeight() {
|
||||
public int getCurrentWeight() {
|
||||
int currentWeight = 0;
|
||||
for (ItemStack item : items) {
|
||||
currentWeight += item.getItem().getWeight();
|
||||
@@ -78,4 +67,8 @@ public class Inventory {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getMaxWeight() {
|
||||
return maxWeight;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package Item;
|
||||
|
||||
import Inventory.Inventory;
|
||||
|
||||
public record ItemStack(Item item, int quantity) {
|
||||
public ItemStack {
|
||||
if (quantity <= 0) {
|
||||
@@ -14,6 +16,10 @@ public record ItemStack(Item item, int quantity) {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public boolean isSingleItem() {
|
||||
return quantity == 1;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return item.getWeight() * quantity;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user