diff --git a/src/main/java/cactusServer/entities/Character.java b/src/main/java/cactusServer/entities/Character.java index 1c46d45..d04e582 100644 --- a/src/main/java/cactusServer/entities/Character.java +++ b/src/main/java/cactusServer/entities/Character.java @@ -1,7 +1,9 @@ package cactusServer.entities; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import framework.model3D.Position3D; @@ -47,13 +49,17 @@ public boolean isActive() { return isActive; } - - public HashMap getItemMap() { - return itemMap; + + public Item getItem(String itemId) { + return itemMap.get(itemId); } - public Item getItemMap(String itemId) { - return itemMap.get(itemId); + public List getItems() { + List itemList = new ArrayList<>(); + for (Item item : itemMap.values()) { + itemList.add(item); + } + return itemList; } public void setPosition(Position3D position) { @@ -65,12 +71,24 @@ } public Map createItem(String name, int amount) { - String id = new RandomStringGenerator().createUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, itemMap.keySet()); Item item = new Item(name, amount); - itemMap.put(id, item); - return new HashMap() { - {put(id, item);} - }; + if (!hasItem(item)) { + String id = new RandomStringGenerator().createUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, itemMap.keySet()); + itemMap.put(id, item); + return new HashMap() { + {put(id, item);} + }; + } + return null; + } + + public boolean hasItem(Item item) { + for (Map.Entry entry : itemMap.entrySet()) { + if (entry.getValue().equals(item)) { + return true; + } + } + return false; } public Item changeAmountOfItem(String itemId, int amountOfChange) {