mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 02:36:41 +00:00
Merge pull request #176 from Wiewiogr/138-add-items-container
Added Container object #138
This commit is contained in:
commit
564a692c2e
@ -0,0 +1,69 @@
|
|||||||
|
package net.simon987.server.game.objects;
|
||||||
|
|
||||||
|
import net.simon987.server.game.item.Item;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class ItemsContainer extends GameObject implements InventoryHolder {
|
||||||
|
|
||||||
|
private static final char MAP_INFO = 0x0240;
|
||||||
|
|
||||||
|
private final List<Item> items;
|
||||||
|
private int containerCapacity;
|
||||||
|
|
||||||
|
public ItemsContainer(int containerCapacity) {
|
||||||
|
this.containerCapacity = containerCapacity;
|
||||||
|
this.items = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemsContainer(Document document) {
|
||||||
|
super(document);
|
||||||
|
this.items = (List<Item>) document.get("items");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getMapInfo() {
|
||||||
|
return MAP_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean placeItem(Item item) {
|
||||||
|
if (items.size() < containerCapacity) {
|
||||||
|
items.add(item);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void takeItem(int itemId) {
|
||||||
|
Optional<Item> first = items.stream()
|
||||||
|
.filter(item -> item.getId() == itemId)
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
items.remove(first.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canTakeItem(int itemId) {
|
||||||
|
return items.stream()
|
||||||
|
.anyMatch(item -> item.getId() == itemId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject jsonSerialise() {
|
||||||
|
JSONObject json = super.jsonSerialise();
|
||||||
|
json.put("items", items);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Document mongoSerialise() {
|
||||||
|
Document dbObject = super.mongoSerialise();
|
||||||
|
dbObject.put("items", items);
|
||||||
|
return dbObject;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package net.simon987.server.game.objects;
|
||||||
|
|
||||||
|
import net.simon987.server.game.item.ItemCopper;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ItemsContainerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeSerializableToMongoFormat() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(2);
|
||||||
|
ItemCopper item = new ItemCopper();
|
||||||
|
itemsContainer.placeItem(item);
|
||||||
|
|
||||||
|
Document document = itemsContainer.mongoSerialise();
|
||||||
|
|
||||||
|
ItemsContainer deserialized = new ItemsContainer(document);
|
||||||
|
Assert.assertTrue(deserialized.canTakeItem(item.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPlaceItemWhenCapacityAllowsIt() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(1);
|
||||||
|
|
||||||
|
boolean result = itemsContainer.placeItem(new ItemCopper());
|
||||||
|
|
||||||
|
Assert.assertEquals(true, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotPlaceItemWhenThereIsNoCapacityLeft() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(1);
|
||||||
|
|
||||||
|
itemsContainer.placeItem(new ItemCopper());
|
||||||
|
boolean result = itemsContainer.placeItem(new ItemCopper());
|
||||||
|
|
||||||
|
Assert.assertEquals(false, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotBeAbleToTakeItemWhenItWasNotPlacedBefore() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(1);
|
||||||
|
|
||||||
|
boolean result = itemsContainer.canTakeItem(1);
|
||||||
|
|
||||||
|
Assert.assertEquals(false, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeAbleToTakeItemIfItWasPlacedBefore() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(1);
|
||||||
|
ItemCopper item = new ItemCopper();
|
||||||
|
|
||||||
|
itemsContainer.placeItem(item);
|
||||||
|
boolean result = itemsContainer.canTakeItem(item.getId());
|
||||||
|
|
||||||
|
Assert.assertEquals(true, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotBeAbleToTakeItemAfterItWasTaken() {
|
||||||
|
ItemsContainer itemsContainer = new ItemsContainer(1);
|
||||||
|
ItemCopper item = new ItemCopper();
|
||||||
|
|
||||||
|
itemsContainer.placeItem(item);
|
||||||
|
itemsContainer.takeItem(item.getId());
|
||||||
|
boolean result = itemsContainer.canTakeItem(item.getId());
|
||||||
|
|
||||||
|
Assert.assertEquals(false, result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user