mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
changed implementation to list
This commit is contained in:
parent
a6f0ce1dfb
commit
f89c39c756
@ -4,25 +4,23 @@ import net.simon987.server.game.item.Item;
|
|||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ItemsContainer extends GameObject implements InventoryHolder {
|
public class ItemsContainer extends GameObject implements InventoryHolder {
|
||||||
|
|
||||||
private static final char MAP_INFO = 0x0240;
|
private static final char MAP_INFO = 0x0240;
|
||||||
|
|
||||||
private final Map<Integer, Integer> itemsTypeCount;
|
private final List<Item> items;
|
||||||
private int containerCapacity;
|
private int containerCapacity;
|
||||||
private int size = 0;
|
|
||||||
|
|
||||||
public ItemsContainer(int containerCapacity) {
|
public ItemsContainer(int containerCapacity) {
|
||||||
this.containerCapacity = containerCapacity;
|
this.containerCapacity = containerCapacity;
|
||||||
this.itemsTypeCount = new HashMap<>();
|
this.items = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemsContainer(Document document) {
|
public ItemsContainer(Document document) {
|
||||||
super(document);
|
super(document);
|
||||||
itemsTypeCount = (Map<Integer, Integer>) document.get("itemsCount");
|
this.items = (List<Item>) document.get("itemsCount");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,41 +30,40 @@ public class ItemsContainer extends GameObject implements InventoryHolder {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean placeItem(Item item) {
|
public boolean placeItem(Item item) {
|
||||||
if (size < containerCapacity) {
|
if (items.size() < containerCapacity) {
|
||||||
int itemId = item.getId();
|
items.add(item);
|
||||||
itemsTypeCount.putIfAbsent(itemId, 0);
|
|
||||||
Integer oldCount = itemsTypeCount.get(itemId);
|
|
||||||
itemsTypeCount.replace(itemId, ++oldCount);
|
|
||||||
size++;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void takeItem(int itemId) {
|
public void takeItem(int itemId) {
|
||||||
Integer oldCount = itemsTypeCount.get(itemId);
|
Optional<Item> first = items.stream()
|
||||||
itemsTypeCount.replace(itemId, --oldCount);
|
.filter(item -> item.getId() == itemId)
|
||||||
size--;
|
.findFirst();
|
||||||
|
|
||||||
|
items.remove(first.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canTakeItem(int itemId) {
|
public boolean canTakeItem(int itemId) {
|
||||||
Integer integer = itemsTypeCount.get(itemId);
|
return items.stream()
|
||||||
return integer != null && integer > 0;
|
.anyMatch(item -> item.getId() == itemId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject jsonSerialise() {
|
public JSONObject jsonSerialise() {
|
||||||
JSONObject json = super.jsonSerialise();
|
JSONObject json = super.jsonSerialise();
|
||||||
json.put("itemsCount", itemsTypeCount);
|
json.put("itemsCount", items);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Document mongoSerialise() {
|
public Document mongoSerialise() {
|
||||||
Document dbObject = super.mongoSerialise();
|
Document dbObject = super.mongoSerialise();
|
||||||
dbObject.put("itemsCount", itemsTypeCount);
|
dbObject.put("itemsCount", items);
|
||||||
return dbObject;
|
return dbObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user