mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 14:49:03 +00:00
Refactor: changed the way game objects and cpu hardware are saved/loaded from the database #151
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package net.simon987.biomassplugin;
|
||||
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.InventoryHolder;
|
||||
import net.simon987.server.game.objects.GameObject;
|
||||
import net.simon987.server.game.objects.InventoryHolder;
|
||||
import org.bson.Document;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class BiomassBlob extends GameObject implements InventoryHolder {
|
||||
|
||||
private static final char MAP_INFO = 0x4000;
|
||||
public static final int ID = 2;
|
||||
|
||||
/**
|
||||
* Yield of the blob, in biomass units
|
||||
@@ -21,22 +20,26 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
|
||||
|
||||
private static final int ITM_BIOMASS = 1;
|
||||
|
||||
public BiomassBlob() {
|
||||
}
|
||||
|
||||
public BiomassBlob(Document document) {
|
||||
super(document);
|
||||
|
||||
biomassCount = document.getInteger("b");
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getMapInfo() {
|
||||
return MAP_INFO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
public JSONObject jsonSerialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject json = super.jsonSerialise();
|
||||
|
||||
json.put("t", ID);
|
||||
json.put("i", getObjectId());
|
||||
json.put("x", getX());
|
||||
json.put("y", getY());
|
||||
json.put("b", biomassCount);
|
||||
// json.put("style", style);
|
||||
|
||||
return json;
|
||||
}
|
||||
@@ -44,12 +47,8 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
|
||||
@Override
|
||||
public Document mongoSerialise() {
|
||||
|
||||
Document dbObject = new Document();
|
||||
Document dbObject = super.mongoSerialise();
|
||||
|
||||
dbObject.put("t", ID);
|
||||
dbObject.put("i", getObjectId());
|
||||
dbObject.put("x", getX());
|
||||
dbObject.put("y", getY());
|
||||
dbObject.put("b", biomassCount);
|
||||
|
||||
return dbObject;
|
||||
@@ -64,20 +63,6 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
|
||||
this.biomassCount = biomassCount;
|
||||
}
|
||||
|
||||
|
||||
public static BiomassBlob deserialize(Document obj) {
|
||||
|
||||
BiomassBlob biomassBlob = new BiomassBlob();
|
||||
|
||||
biomassBlob.setObjectId((long) obj.get("i"));
|
||||
biomassBlob.setX((int) obj.get("x"));
|
||||
biomassBlob.setY((int) obj.get("y"));
|
||||
// biomassBlob.style = (int) json.get("style");
|
||||
biomassBlob.biomassCount = (int) obj.get("b");
|
||||
|
||||
return biomassBlob;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when an object attempts to place an item in this BiomassBlob
|
||||
*
|
||||
|
||||
@@ -4,35 +4,28 @@ import net.simon987.biomassplugin.event.ObjectDeathListener;
|
||||
import net.simon987.biomassplugin.event.WorldCreationListener;
|
||||
import net.simon987.biomassplugin.event.WorldUpdateListener;
|
||||
import net.simon987.server.ServerConfiguration;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.io.GameObjectDeserializer;
|
||||
import net.simon987.server.game.objects.GameRegistry;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
import org.bson.Document;
|
||||
|
||||
|
||||
public class BiomassPlugin extends ServerPlugin implements GameObjectDeserializer {
|
||||
public class BiomassPlugin extends ServerPlugin {
|
||||
|
||||
|
||||
@Override
|
||||
public void init(ServerConfiguration config) {
|
||||
public void init(ServerConfiguration config, GameRegistry registry) {
|
||||
listeners.add(new WorldCreationListener());
|
||||
listeners.add(new WorldUpdateListener(config));
|
||||
listeners.add(new ObjectDeathListener(config));
|
||||
|
||||
LogManager.LOGGER.info("Initialised Biomass plugin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameObject deserializeObject(Document object) {
|
||||
|
||||
int objType = (int) object.get("t");
|
||||
|
||||
if (objType == BiomassBlob.ID) {
|
||||
|
||||
return BiomassBlob.deserialize(object);
|
||||
if (registry.isGameObjectRegistered("net.simon987.npcplugin.HarvesterNPC")) {
|
||||
listeners.add(new ObjectDeathListener(config));
|
||||
} else {
|
||||
LogManager.LOGGER.severe("(BiomassPlugin) NPC plugin is not loaded so biomass will not spawn on death of HarvesterNPC");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
registry.registerGameObject(BiomassBlob.class);
|
||||
|
||||
LogManager.LOGGER.info("(BiomassPlugin) Initialised Biomass plugin");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.simon987.biomassplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.game.TileMap;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.game.world.TileMap;
|
||||
import net.simon987.server.game.world.World;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@@ -6,8 +6,8 @@ import net.simon987.server.ServerConfiguration;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.ObjectDeathEvent;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.game.objects.GameObject;
|
||||
import net.simon987.server.game.world.World;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
/**
|
||||
@@ -29,12 +29,11 @@ public class ObjectDeathListener implements GameEventListener {
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
// TODO: setup enum with all GameObject type IDs
|
||||
if (((ObjectDeathEvent) event).getSourceObjectId() == 10) {
|
||||
|
||||
if (event.getSource().getClass().getCanonicalName().equals("net.simon987.npcplugin.HarvesterNPC")) {
|
||||
//An HarvesterNPC ObjectDeathEvent is received
|
||||
GameObject dyingHarvesterNPC = (GameObject)event.getSource();
|
||||
|
||||
|
||||
//Don't spawn biomass on World border
|
||||
if (dyingHarvesterNPC.getX() != 0 && dyingHarvesterNPC.getX() != dyingHarvesterNPC.getWorld().getWorldSize() - 1 &&
|
||||
dyingHarvesterNPC.getY() != 0 && dyingHarvesterNPC.getY() != dyingHarvesterNPC.getWorld().getWorldSize() - 1) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import net.simon987.server.ServerConfiguration;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.WorldUpdateEvent;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.game.world.World;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
Reference in New Issue
Block a user