mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 02:36:41 +00:00
commit
be7304aa0b
@ -3,25 +3,24 @@ package net.simon987.server;
|
|||||||
|
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for Java Property
|
* Wrapper for Java Properties class
|
||||||
*/
|
*/
|
||||||
public class ServerConfiguration {
|
public class ServerConfiguration {
|
||||||
|
|
||||||
/**
|
|
||||||
* Properties
|
|
||||||
*/
|
|
||||||
private Properties properties;
|
private Properties properties;
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
public ServerConfiguration(String fileName) {
|
||||||
|
|
||||||
|
this.fileName = fileName;
|
||||||
|
|
||||||
public ServerConfiguration(String file) {
|
|
||||||
try {
|
try {
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
InputStream is = new FileInputStream("config.properties");
|
InputStream is = new FileInputStream(this.fileName);
|
||||||
properties.load(is);
|
properties.load(is);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -29,6 +28,17 @@ public class ServerConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveConfig() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
OutputStream os = new FileOutputStream(this.fileName);
|
||||||
|
properties.store(os, "");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogManager.LOGGER.severe("Problem saving server configuration: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getInt(String key) {
|
public int getInt(String key) {
|
||||||
return Integer.valueOf((String) properties.get(key));
|
return Integer.valueOf((String) properties.get(key));
|
||||||
|
|
||||||
@ -39,4 +49,14 @@ public class ServerConfiguration {
|
|||||||
return (String) properties.get(key);
|
return (String) properties.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setInt(String key, int value) {
|
||||||
|
properties.setProperty(key, String.valueOf(value));
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setString(String key, String value) {
|
||||||
|
properties.setProperty(key, value);
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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