inventory #14
@@ -8,10 +8,17 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Inventory {
|
||||
List<ItemStack> items;
|
||||
private final List<ItemStack> items;
|
||||
private final int weightLimit;
|
||||
|
||||
public Inventory() {
|
||||
items = new ArrayList<>();
|
||||
weightLimit = 100;
|
||||
}
|
||||
|
||||
public Inventory(int weightLimit) {
|
||||
items = new ArrayList<>();
|
||||
this.weightLimit = weightLimit;
|
||||
}
|
||||
|
||||
public List<ItemStack> getItems() {
|
||||
@@ -19,9 +26,17 @@ public class Inventory {
|
||||
}
|
||||
|
||||
public void addItem(ItemStack item) {
|
||||
int totalWeight = calculateStackWeight(item);
|
||||
if (totalWeight > weightLimit) {
|
||||
return;
|
||||
}
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
private int calculateStackWeight(ItemStack stack) {
|
||||
return stack.getItem().getWeight() * stack.getQuantity();
|
||||
}
|
||||
|
||||
public boolean containsItem(Item item) {
|
||||
for (ItemStack stack : items) {
|
||||
if (stack.item().equals(item)) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package Item;
|
||||
|
||||
public record BasicItem(String id, String name) implements Item {
|
||||
public record BasicItem(String id, String name, int weight) implements Item {
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -10,4 +10,8 @@ public record BasicItem(String id, String name) implements Item {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@Override
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,5 @@ package Item;
|
||||
public interface Item {
|
||||
String getId();
|
||||
String getName();
|
||||
int getWeight();
|
||||
}
|
||||
@@ -10,27 +10,27 @@ import java.util.List;
|
||||
|
||||
public enum Biome {
|
||||
GRASSLAND("Grassland", new LootTable<>(new ArrayList<>(List.of(
|
||||
new LootEntry<>(new BasicItem("mud", "Mud"), 10),
|
||||
new LootEntry<>(new BasicItem("earth_root", "Earth Root"), 50),
|
||||
new LootEntry<>(new BasicItem("crystal_core", "Crystal core"), 180)
|
||||
new LootEntry<>(new BasicItem("mud", "Mud", 10), 10),
|
||||
new LootEntry<>(new BasicItem("earth_root", "Earth Root", 5), 50),
|
||||
new LootEntry<>(new BasicItem("crystal_core", "Crystal core", 20), 180)
|
||||
)) {
|
||||
})),
|
||||
MOUNTAIN("Mountain", new LootTable<>(new ArrayList<>(List.of(
|
||||
new LootEntry<>(new BasicItem("rock", "Rock"), 10),
|
||||
new LootEntry<>(new BasicItem("iron", "Iron"), 50),
|
||||
new LootEntry<>(new BasicItem("diamond", "Diamond"), 180)
|
||||
new LootEntry<>(new BasicItem("rock", "Rock", 50), 10),
|
||||
new LootEntry<>(new BasicItem("iron", "Iron", 80), 50),
|
||||
new LootEntry<>(new BasicItem("diamond", "Diamond", 8), 180)
|
||||
)) {
|
||||
})),
|
||||
COAST("Coast", new LootTable<>(new ArrayList<>(List.of(
|
||||
new LootEntry<>(new BasicItem("sand", "Sand"), 10),
|
||||
new LootEntry<>(new BasicItem("shell", "Shell"), 50),
|
||||
new LootEntry<>(new BasicItem("sandfish", "Sandfish"), 180)
|
||||
new LootEntry<>(new BasicItem("sand", "Sand", 1), 10),
|
||||
new LootEntry<>(new BasicItem("shell", "Shell", 4), 50),
|
||||
new LootEntry<>(new BasicItem("sandfish", "Sandfish", 11), 180)
|
||||
)) {
|
||||
})),
|
||||
FOREST("Forest", new LootTable<>(new ArrayList<>(List.of(
|
||||
new LootEntry<>(new BasicItem("moss", "Moss"), 10),
|
||||
new LootEntry<>(new BasicItem("fairy_rock", "Fairy rock"), 50),
|
||||
new LootEntry<>(new BasicItem("unicorn_horn", "Unicorn horn"), 230)
|
||||
new LootEntry<>(new BasicItem("moss", "Moss", 7 ), 10),
|
||||
new LootEntry<>(new BasicItem("fairy_rock", "Fairy rock", 89), 50),
|
||||
new LootEntry<>(new BasicItem("unicorn_horn", "Unicorn horn", 18), 230)
|
||||
)) {
|
||||
}));
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ public class InventoryTest {
|
||||
@Test
|
||||
void can_add_item_to_inventory() {
|
||||
var inventory = new Inventory();
|
||||
var item = new BasicItem("iron_sword", "Iron Sword");
|
||||
var item = new BasicItem("iron_sword", "Iron Sword", 5);
|
||||
inventory.addItem(new ItemStack(item, 1));
|
||||
assertThat(inventory.getItems(), hasItem(
|
||||
hasProperty("item",
|
||||
@@ -22,7 +22,7 @@ public class InventoryTest {
|
||||
@Test
|
||||
void can_add_quantity_to_inventory() {
|
||||
var inventory = new Inventory();
|
||||
var item = new BasicItem("iron_sword", "Iron Sword");
|
||||
var item = new BasicItem("iron_sword", "Iron Sword", 5);
|
||||
var stack = new ItemStack(item, 5);
|
||||
inventory.addItem(stack);
|
||||
assertThat(inventory.getItems(), hasItem(
|
||||
@@ -36,8 +36,7 @@ public class InventoryTest {
|
||||
|
||||
@Test
|
||||
void item_can_not_be_added_if_it_exeeds_weight_limit() {
|
||||
var capacity = new InventoryCapacity(5);
|
||||
var inventory = new Inventory(capacity);
|
||||
var inventory = new Inventory(5);
|
||||
var item = new BasicItem("iron_sword", "Iron Sword", 10);
|
||||
var stack = new ItemStack(item, 1);
|
||||
inventory.addItem(stack);
|
||||
|
||||
Reference in New Issue
Block a user