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