From 4cd58c86a51e2d76791f990392661e9e3b95eb44 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 12 May 2018 15:32:41 -0400 Subject: [PATCH] Refactor: changed the way game objects and cpu hardware are saved/loaded from the database #151 --- .../java/net/simon987/cubotplugin/Cubot.java | 53 +++------ .../simon987/cubotplugin/CubotBattery.java | 27 +---- .../simon987/cubotplugin/CubotComPort.java | 31 ++--- .../net/simon987/cubotplugin/CubotCore.java | 28 +---- .../net/simon987/cubotplugin/CubotDrill.java | 33 ++---- .../cubotplugin/CubotFloppyDrive.java | 42 ++----- .../simon987/cubotplugin/CubotHardware.java | 27 +++++ .../simon987/cubotplugin/CubotHologram.java | 27 +---- .../simon987/cubotplugin/CubotInventory.java | 27 +---- .../simon987/cubotplugin/CubotKeyboard.java | 27 +---- .../net/simon987/cubotplugin/CubotLaser.java | 35 ++---- .../net/simon987/cubotplugin/CubotLeg.java | 47 ++------ .../net/simon987/cubotplugin/CubotLidar.java | 40 +------ .../net/simon987/cubotplugin/CubotPlugin.java | 72 +++--------- .../net/simon987/cubotplugin/CubotShield.java | 24 +--- .../net/simon987/cubotplugin/FloppyDisk.java | 15 +-- .../event/ChargeShieldCommandListener.java | 2 +- .../java/net/simon987/mischwplugin/Clock.java | 17 +-- .../simon987/mischwplugin/MiscHWPlugin.java | 25 +--- .../mischwplugin/RandomNumberGenerator.java | 14 +-- .../net/simon987/npcplugin/ElectricBox.java | 43 ++----- .../java/net/simon987/npcplugin/Factory.java | 51 +++----- .../net/simon987/npcplugin/HarvestTask.java | 9 +- .../net/simon987/npcplugin/HarvesterNPC.java | 44 ++----- .../npcplugin/NonPlayerCharacter.java | 19 ++- .../net/simon987/npcplugin/NpcPlugin.java | 59 +++------- .../java/net/simon987/npcplugin/Obstacle.java | 34 ++---- .../java/net/simon987/npcplugin/Portal.java | 59 ++++------ .../npcplugin/RadioReceiverHardware.java | 16 +-- .../net/simon987/npcplugin/RadioTower.java | 59 +++------- .../simon987/npcplugin/VaultDimension.java | 9 +- .../net/simon987/npcplugin/VaultDoor.java | 61 ++++------ .../simon987/npcplugin/VaultExitPortal.java | 48 +++----- .../npcplugin/VaultWorldGenerator.java | 6 +- .../simon987/npcplugin/VaultWorldUtils.java | 4 +- .../event/VaultWorldUpdateListener.java | 2 +- .../event/WorldCreationListener.java | 2 +- .../simon987/biomassplugin/BiomassBlob.java | 43 +++---- .../simon987/biomassplugin/BiomassPlugin.java | 29 ++--- .../simon987/biomassplugin/WorldUtils.java | 4 +- .../event/ObjectDeathListener.java | 9 +- .../event/WorldUpdateListener.java | 2 +- .../RadioactiveCloudPlugin.java | 14 +-- .../java/net/simon987/server/GameServer.java | 22 ++-- .../net/simon987/server/assembly/CPU.java | 4 +- .../simon987/server/assembly/CpuHardware.java | 30 ++--- .../net/simon987/server/assembly/Memory.java | 54 ++++----- .../server/event/ObjectDeathEvent.java | 11 +- .../server/event/WorldGenerationEvent.java | 2 +- .../server/event/WorldUpdateEvent.java | 2 +- .../simon987/server/game/GameUniverse.java | 5 +- .../game/debug/ComPortMsgCommandListener.java | 4 +- .../debug/CreateWorldCommandListener.java | 2 +- .../game/debug/DamageObjCommandListener.java | 4 +- .../game/debug/HealObjCommandListener.java | 4 +- .../game/debug/KillAllCommandListener.java | 4 +- .../game/debug/MoveObjCommandListener.java | 2 +- .../game/debug/ObjInfoCommandListener.java | 6 +- .../game/debug/SetEnergyCommandListener.java | 4 +- .../game/debug/SetTileAtCommandListener.java | 2 +- .../game/debug/SpawnObjCommandListener.java | 6 +- .../game/debug/TpObjectCommandListener.java | 6 +- .../game/debug/UserInfoCommandListener.java | 2 +- .../server/game/{ => objects}/Action.java | 2 +- .../server/game/{ => objects}/Attackable.java | 5 +- .../game/{ => objects}/ControllableUnit.java | 3 +- .../server/game/{ => objects}/Direction.java | 2 +- .../server/game/{ => objects}/Enterable.java | 2 +- .../server/game/{ => objects}/GameObject.java | 58 ++++++---- .../server/game/objects/GameRegistry.java | 70 +++++++++++ .../game/{ => objects}/InventoryHolder.java | 2 +- .../game/{ => objects}/Programmable.java | 2 +- .../game/{ => objects}/Radioactive.java | 2 +- .../game/{ => objects}/Rechargeable.java | 2 +- .../server/game/{ => objects}/Updatable.java | 2 +- .../server/game/pathfinding/Pathfinder.java | 2 +- .../game/{ => world}/DayNightCycle.java | 2 +- .../server/game/{ => world}/Location.java | 2 +- .../server/game/{ => world}/TileMap.java | 5 +- .../server/game/{ => world}/World.java | 9 +- .../game/{ => world}/WorldGenerator.java | 4 +- .../server/io/CpuHardwareDeserializer.java | 10 -- .../simon987/server/io/DatabaseManager.java | 45 -------- .../net/simon987/server/io/FileUtils.java | 22 ---- .../server/io/GameObjectDeserializer.java | 10 -- .../simon987/server/io/JSONSerialisable.java | 2 +- .../simon987/server/plugin/PluginManager.java | 5 +- .../simon987/server/plugin/ServerPlugin.java | 5 +- .../java/net/simon987/server/user/User.java | 2 +- .../websocket/ObjectsRequestHandler.java | 6 +- .../server/websocket/SocketServer.java | 2 +- .../websocket/SocketServerDatabase.java | 109 ------------------ .../websocket/TerrainRequestHandler.java | 2 +- .../websocket/UserInfoRequestHandler.java | 2 +- Server/src/main/resources/static/js/mar.js | 18 +-- Server/src/main/typescript/GameObject.ts | 18 +-- 96 files changed, 628 insertions(+), 1221 deletions(-) create mode 100644 Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java rename Server/src/main/java/net/simon987/server/game/{ => objects}/Action.java (77%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Attackable.java (85%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/ControllableUnit.java (87%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Direction.java (97%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Enterable.java (87%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/GameObject.java (86%) create mode 100644 Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java rename Server/src/main/java/net/simon987/server/game/{ => objects}/InventoryHolder.java (92%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Programmable.java (65%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Radioactive.java (68%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Rechargeable.java (84%) rename Server/src/main/java/net/simon987/server/game/{ => objects}/Updatable.java (78%) rename Server/src/main/java/net/simon987/server/game/{ => world}/DayNightCycle.java (96%) rename Server/src/main/java/net/simon987/server/game/{ => world}/Location.java (92%) rename Server/src/main/java/net/simon987/server/game/{ => world}/TileMap.java (98%) rename Server/src/main/java/net/simon987/server/game/{ => world}/World.java (97%) rename Server/src/main/java/net/simon987/server/game/{ => world}/WorldGenerator.java (98%) delete mode 100644 Server/src/main/java/net/simon987/server/io/CpuHardwareDeserializer.java delete mode 100755 Server/src/main/java/net/simon987/server/io/DatabaseManager.java delete mode 100644 Server/src/main/java/net/simon987/server/io/FileUtils.java delete mode 100644 Server/src/main/java/net/simon987/server/io/GameObjectDeserializer.java delete mode 100644 Server/src/main/java/net/simon987/server/websocket/SocketServerDatabase.java diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java index 3da3052..041ddf3 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java @@ -3,7 +3,7 @@ package net.simon987.cubotplugin; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; import net.simon987.server.assembly.Memory; -import net.simon987.server.game.*; +import net.simon987.server.game.objects.*; import net.simon987.server.logging.LogManager; import net.simon987.server.user.User; import org.bson.Document; @@ -16,7 +16,6 @@ import java.util.Random; public class Cubot extends GameObject implements Updatable, ControllableUnit, Programmable, Attackable, Rechargeable { private static final char MAP_INFO = 0x0080; - public static final int ID = 1; /** * Hologram value that is displayed @@ -171,11 +170,25 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr NORMAL } - public Cubot() { } + public Cubot(Document document) { + super(document); + + hp = document.getInteger("hp"); + shield = document.getInteger("shield"); + setDirection(Direction.getDirection(document.getInteger("direction"))); + heldItem = document.getInteger("heldItem"); + energy = document.getInteger("energy"); + + ServerConfiguration config = GameServer.INSTANCE.getConfig(); + maxEnergy = config.getInt("battery_max_energy"); + maxHp = config.getInt("cubot_max_hp"); + maxShield = config.getInt("cubot_max_shield"); + } + @Override public char getMapInfo() { return MAP_INFO; @@ -225,12 +238,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr } @Override - public JSONObject serialise() { - JSONObject json = new JSONObject(); - json.put("i", getObjectId()); - json.put("t", ID); - json.put("x", getX()); - json.put("y", getY()); + public JSONObject jsonSerialise() { + JSONObject json = super.jsonSerialise(); json.put("direction", getDirection().ordinal()); json.put("heldItem", heldItem); json.put("hp", hp); @@ -251,12 +260,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("t", ID); - dbObject.put("x", getX()); - dbObject.put("y", getY()); dbObject.put("direction", getDirection().ordinal()); dbObject.put("heldItem", heldItem); dbObject.put("hp", hp); @@ -275,26 +280,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr return dbObject; } - public static Cubot deserialize(Document obj) { - - Cubot cubot = new Cubot(); - cubot.setObjectId((long) obj.get("i")); - cubot.setX((int) obj.get("x")); - cubot.setY((int) obj.get("y")); - cubot.hp = (int) obj.get("hp"); - cubot.shield = (int) obj.get("shield"); - cubot.setDirection(Direction.getDirection((int) obj.get("direction"))); - cubot.heldItem = (int) obj.get("heldItem"); - cubot.energy = (int) obj.get("energy"); - - ServerConfiguration config = GameServer.INSTANCE.getConfig(); - cubot.maxEnergy = config.getInt("battery_max_energy"); - cubot.maxHp = config.getInt("cubot_max_hp"); - cubot.maxShield = config.getInt("cubot_max_shield"); - - return cubot; - } - /** * Reset to 'factory settings', as it were when it was first created */ diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java index 530a7f5..384e452 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java @@ -1,11 +1,9 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotBattery extends CpuHardware { +public class CubotBattery extends CubotHardware { public static final int DEFAULT_ADDRESS = 0x000A; @@ -14,12 +12,15 @@ public class CubotBattery extends CpuHardware { */ public static final char HWID = 0x000A; - private Cubot cubot; private static final int BATTERY_POLL = 1; private static final int BATTERY_GET_MAX_CAPACITY = 2; public CubotBattery(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotBattery(Document document) { + super(document); } @Override @@ -43,20 +44,4 @@ public class CubotBattery extends CpuHardware { return HWID; } - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - - public static CubotBattery deserialize(Document obj) { - return new CubotBattery((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } - } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java index 176803c..6052796 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java @@ -1,22 +1,18 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Programmable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Programmable; import org.bson.Document; import java.awt.*; import java.util.ArrayList; -public class CubotComPort extends CpuHardware { +public class CubotComPort extends CubotHardware { public static final char HWID = 0xD; public static final int DEFAULT_ADDRESS = 0xD; - private Cubot cubot; - private static final int COMPORT_BUFFER_CLEAR = 0; private static final int COMPORT_POLL = 1; private static final int COMPORT_FRONT_PORT_OUT = 2; @@ -24,7 +20,11 @@ public class CubotComPort extends CpuHardware { private static final int COMPORT_CONSOLE_CLEAR = 4; public CubotComPort(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotComPort(Document document) { + super(document); } private static final int MESSAGE_LENGTH = 8; @@ -125,19 +125,4 @@ public class CubotComPort extends CpuHardware { public char getId() { return HWID; } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotComPort deserialize(Document obj) { - return new CubotComPort((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java index 5e4d50e..4a4bd45 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java @@ -1,11 +1,9 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotCore extends CpuHardware { +public class CubotCore extends CubotHardware { public static final int DEFAULT_ADDRESS = 0x000E; @@ -17,10 +15,12 @@ public class CubotCore extends CpuHardware { private static final int CORE_STATUS_POLL = 1; private static final int CORE_HULL_POLL = 2; - private Cubot cubot; - public CubotCore(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotCore(Document document) { + super(document); } @Override @@ -39,20 +39,4 @@ public class CubotCore extends CpuHardware { public char getId() { return HWID; } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - - public static CubotCore deserialize(Document obj) { - return new CubotCore((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java index edc7bc1..5d5f6af 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java @@ -1,13 +1,11 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; -import net.simon987.server.game.Action; -import net.simon987.server.game.TileMap; +import net.simon987.server.game.objects.Action; +import net.simon987.server.game.world.TileMap; import org.bson.Document; -public class CubotDrill extends CpuHardware { +public class CubotDrill extends CubotHardware { /** * Hardware ID (Should be unique) @@ -19,10 +17,12 @@ public class CubotDrill extends CpuHardware { private static final int DRILL_POLL = 1; private static final int DRILL_GATHER = 2; // simplified gather - private Cubot cubot; - public CubotDrill(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotDrill(Document document) { + super(document); } @Override @@ -55,23 +55,6 @@ public class CubotDrill extends CpuHardware { } } } - - } } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotDrill deserialize(Document obj) { - return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java index 8c08a1c..947dee4 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java @@ -1,11 +1,9 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotFloppyDrive extends CpuHardware { +public class CubotFloppyDrive extends CubotHardware { /** * Hardware ID (Should be unique) @@ -18,15 +16,24 @@ public class CubotFloppyDrive extends CpuHardware { private static final int FLOPPY_READ_SECTOR = 2; private static final int FLOPPY_WRITE_SECTOR = 3; - private Cubot cubot; private FloppyDisk floppyDisk; public CubotFloppyDrive(Cubot cubot) { - this.cubot = cubot; + super(cubot); floppyDisk = new FloppyDisk(); } + public CubotFloppyDrive(Document document) { + super(document); + + if (document.containsKey("floppy")) { + floppyDisk = new FloppyDisk((Document) document.get("floppy")); + } else { + floppyDisk = new FloppyDisk(); + } + } + @Override public void handleInterrupt(Status status) { int a = getCpu().getRegisterSet().getRegister("A").getValue(); @@ -77,31 +84,6 @@ public class CubotFloppyDrive extends CpuHardware { return HWID; } - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - if (floppyDisk != null) { - dbObject.put("floppy", floppyDisk.mongoSerialise()); - } - - return dbObject; - } - - public static CubotFloppyDrive deserialize(Document obj) { - - CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - - if (obj.containsKey("floppy")) { - drive.floppyDisk = FloppyDisk.deserialise((Document) obj.get("floppy")); - } - - return drive; - } public FloppyDisk getFloppy() { return floppyDisk; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java new file mode 100644 index 0000000..4bc6ba9 --- /dev/null +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java @@ -0,0 +1,27 @@ +package net.simon987.cubotplugin; + +import net.simon987.server.GameServer; +import net.simon987.server.assembly.CpuHardware; +import org.bson.Document; + +public abstract class CubotHardware extends CpuHardware { + + protected Cubot cubot; + + public CubotHardware(Document document) { + this.cubot = (Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot")); + } + + public CubotHardware(Cubot cubot) { + this.cubot = cubot; + } + + @Override + public Document mongoSerialise() { + Document document = new Document(); + + document.put("type", getClass().getCanonicalName()); + document.put("cubot", cubot.getObjectId()); + return document; + } +} diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java index 29060fd..44f8709 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java @@ -1,11 +1,9 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotHologram extends CpuHardware { +public class CubotHologram extends CubotHardware { /** @@ -15,8 +13,6 @@ public class CubotHologram extends CpuHardware { public static final int DEFAULT_ADDRESS = 9; - private Cubot cubot; - private static final int HOLO_CLEAR = 0; private static final int HOLO_DISPLAY_HEX = 1; private static final int HOLO_DISPLAY_STRING = 2; @@ -26,7 +22,11 @@ public class CubotHologram extends CpuHardware { private static final int STR_MAX_LEN = 8; public CubotHologram(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotHologram(Document document) { + super(document); } @Override @@ -82,19 +82,4 @@ public class CubotHologram extends CpuHardware { return HWID; } - public static CubotHologram deserialize(Document obj) { - return new CubotHologram((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java index 68a8d13..9883ade 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java @@ -1,11 +1,10 @@ package net.simon987.cubotplugin; import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotInventory extends CpuHardware { +public class CubotInventory extends CubotHardware { /** * Hardware ID (Should be unique) @@ -14,13 +13,15 @@ public class CubotInventory extends CpuHardware { public static final int DEFAULT_ADDRESS = 6; - private Cubot cubot; - private static final int INV_CLEAR = 0; private static final int INV_POLL = 1; public CubotInventory(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotInventory(Document document) { + super(document); } @Override @@ -48,20 +49,4 @@ public class CubotInventory extends CpuHardware { } } - - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotInventory deserialize(Document obj) { - return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java index af15e5f..5a4670f 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java @@ -1,11 +1,9 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotKeyboard extends CpuHardware { +public class CubotKeyboard extends CubotHardware { public static final int DEFAULT_ADDRESS = 4; @@ -17,10 +15,12 @@ public class CubotKeyboard extends CpuHardware { */ public static final char HWID = 0x0004; - private Cubot cubot; - public CubotKeyboard(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotKeyboard(Document document) { + super(document); } @Override @@ -50,19 +50,4 @@ public class CubotKeyboard extends CpuHardware { } } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotKeyboard deserialize(Document obj) { - return new CubotKeyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java index a45f01a..f1a6d6d 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java @@ -1,18 +1,16 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; -import net.simon987.server.game.Action; -import net.simon987.server.game.Attackable; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.InventoryHolder; +import net.simon987.server.game.objects.Action; +import net.simon987.server.game.objects.Attackable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.InventoryHolder; import org.bson.Document; import java.awt.*; import java.util.ArrayList; -public class CubotLaser extends CpuHardware { +public class CubotLaser extends CubotHardware { /** * Hardware ID (Should be unique) @@ -21,8 +19,6 @@ public class CubotLaser extends CpuHardware { public static final int DEFAULT_ADDRESS = 2; - private Cubot cubot; - private static final int LASER_WITHDRAW = 1; private static final int LASER_DEPOSIT = 2; private static final int LASER_ATTACK = 3; @@ -30,7 +26,11 @@ public class CubotLaser extends CpuHardware { private static final int LASER_DAMAGE = 25; public CubotLaser(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotLaser(Document document) { + super(document); } @Override @@ -90,19 +90,4 @@ public class CubotLaser extends CpuHardware { } } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotLaser deserialize(Document obj) { - return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java index e648ee0..b24b4bc 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java @@ -1,20 +1,14 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; -import net.simon987.server.game.Action; -import net.simon987.server.game.Direction; -import net.simon987.server.io.JSONSerialisable; +import net.simon987.server.game.objects.Action; +import net.simon987.server.game.objects.Direction; import org.bson.Document; -import org.json.simple.JSONObject; -public class CubotLeg extends CpuHardware implements JSONSerialisable { +public class CubotLeg extends CubotHardware { public static final int DEFAULT_ADDRESS = 1; - public static final String NAME = "Cubot Leg"; - private static final int LEGS_SET_DIR = 1; private static final int LEGS_SET_DIR_AND_WALK = 2; @@ -23,10 +17,12 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable { */ static final char HWID = 0x0001; - private Cubot cubot; - public CubotLeg(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotLeg(Document document) { + super(document); } @Override @@ -73,31 +69,4 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable { } } } - - @Override - public JSONObject serialise() { - - JSONObject json = new JSONObject(); - json.put("hwid", (int) HWID); - json.put("cubot", cubot.getObjectId()); - - return json; - } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotLeg deserialize(Document obj) { - return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } - - } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java index aee6ad8..f09e0d6 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java @@ -1,19 +1,15 @@ package net.simon987.cubotplugin; -import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Memory; import net.simon987.server.assembly.Status; import net.simon987.server.game.pathfinding.Node; import net.simon987.server.game.pathfinding.Pathfinder; -import net.simon987.server.io.JSONSerialisable; import net.simon987.server.logging.LogManager; import org.bson.Document; -import org.json.simple.JSONObject; import java.util.ArrayList; -public class CubotLidar extends CpuHardware implements JSONSerialisable { +public class CubotLidar extends CubotHardware { /** * Hardware ID (Should be unique) @@ -22,8 +18,6 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable { public static final int DEFAULT_ADDRESS = 3; - private Cubot cubot; - private static final int LIDAR_GET_POS = 1; private static final int LIDAR_GET_PATH = 2; private static final int LIDAR_GET_MAP = 3; @@ -33,9 +27,12 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable { private static final int MEMORY_PATH_START = 0x0000; public CubotLidar(Cubot cubot) { - this.cubot = cubot; + super(cubot); } + public CubotLidar(Document document) { + super(document); + } @Override public char getId() { @@ -134,32 +131,5 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable { break; } - - - } - - @Override - public JSONObject serialise() { - - JSONObject json = new JSONObject(); - json.put("hwid", (int) HWID); - json.put("cubot", cubot.getObjectId()); - - return json; - } - - @Override - public Document mongoSerialise() { - - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - - public static CubotLidar deserialize(Document obj) { - return new CubotLidar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java index 2fb9e15..a0e3407 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java @@ -4,70 +4,34 @@ import net.simon987.cubotplugin.event.ChargeShieldCommandListener; import net.simon987.cubotplugin.event.CpuInitialisationListener; import net.simon987.cubotplugin.event.UserCreationListener; import net.simon987.server.ServerConfiguration; -import net.simon987.server.assembly.CpuHardware; -import net.simon987.server.game.GameObject; -import net.simon987.server.io.CpuHardwareDeserializer; -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 CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer { +public class CubotPlugin extends ServerPlugin { @Override - public void init(ServerConfiguration config) { + public void init(ServerConfiguration config, GameRegistry registry) { listeners.add(new CpuInitialisationListener()); listeners.add(new UserCreationListener()); listeners.add(new ChargeShieldCommandListener()); + registry.registerGameObject(Cubot.class); + + registry.registerHardware(CubotLeg.class); + registry.registerHardware(CubotLaser.class); + registry.registerHardware(CubotLidar.class); + registry.registerHardware(CubotDrill.class); + registry.registerHardware(CubotInventory.class); + registry.registerHardware(CubotKeyboard.class); + registry.registerHardware(CubotHologram.class); + registry.registerHardware(CubotBattery.class); + registry.registerHardware(CubotFloppyDrive.class); + registry.registerHardware(CubotComPort.class); + registry.registerHardware(CubotShield.class); + registry.registerHardware(CubotCore.class); + LogManager.LOGGER.info("Initialised Cubot plugin"); } - - @Override - public GameObject deserializeObject(Document object) { - - int objType = (int) object.get("t"); - - if (objType == Cubot.ID) { - - return Cubot.deserialize(object); - } - - return null; - } - - @Override - public CpuHardware deserializeHardware(Document obj) { - int hwid = (int) obj.get("hwid"); - - switch (hwid) { - case CubotLeg.HWID: - return CubotLeg.deserialize(obj); - case CubotLaser.HWID: - return CubotLaser.deserialize(obj); - case CubotLidar.HWID: - return CubotLidar.deserialize(obj); - case CubotDrill.HWID: - return CubotDrill.deserialize(obj); - case CubotInventory.HWID: - return CubotInventory.deserialize(obj); - case CubotKeyboard.HWID: - return CubotKeyboard.deserialize(obj); - case CubotHologram.HWID: - return CubotHologram.deserialize(obj); - case CubotBattery.HWID: - return CubotBattery.deserialize(obj); - case CubotFloppyDrive.HWID: - return CubotFloppyDrive.deserialize(obj); - case CubotComPort.HWID: - return CubotComPort.deserialize(obj); - case CubotShield.HWID: - return CubotShield.deserialize(obj); - case CubotCore.HWID: - return CubotCore.deserialize(obj); - } - - return null; - } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java index 7bc652e..69a1743 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java @@ -1,11 +1,10 @@ package net.simon987.cubotplugin; import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import org.bson.Document; -public class CubotShield extends CpuHardware { +public class CubotShield extends CubotHardware { public static final char DEFAULT_ADDRESS = 0x000F; @@ -13,12 +12,15 @@ public class CubotShield extends CpuHardware { private static final int SHIELD_CHARGE = 1; private static final int SHIELD_POLL = 2; - private Cubot cubot; public static final int COST = GameServer.INSTANCE.getConfig().getInt("shield_energy_cost"); public CubotShield(Cubot cubot) { - this.cubot = cubot; + super(cubot); + } + + public CubotShield(Document document) { + super(document); } @Override @@ -26,16 +28,6 @@ public class CubotShield extends CpuHardware { return HWID; } - @Override - public Document mongoSerialise() { - Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); - dbObject.put("cubot", cubot.getObjectId()); - - return dbObject; - } - @Override public void handleInterrupt(Status status) { int a = getCpu().getRegisterSet().getRegister("A").getValue(); @@ -48,8 +40,4 @@ public class CubotShield extends CpuHardware { getCpu().getRegisterSet().getRegister("B").setValue(shield); } } - - public static CubotShield deserialize(Document obj) { - return new CubotShield((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } \ No newline at end of file diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/FloppyDisk.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/FloppyDisk.java index 017620c..037bc3c 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/FloppyDisk.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/FloppyDisk.java @@ -28,6 +28,11 @@ public class FloppyDisk implements MongoSerializable { this.memory = new Memory(512 * 1440); } + public FloppyDisk(Document document) { + this.rwHeadTrack = (int) document.get("rwHeadTrack"); + this.memory = new Memory((Document) document.get("memory")); + } + /** * Read 512 words from the specified sector to cpu memory at specified address * @@ -92,16 +97,6 @@ public class FloppyDisk implements MongoSerializable { return dbObject; } - public static FloppyDisk deserialise(Document obj) { - - FloppyDisk floppyDisk = new FloppyDisk(); - - floppyDisk.rwHeadTrack = (int) obj.get("rwHeadTrack"); - floppyDisk.memory = Memory.deserialize((Document) obj.get("memory")); - - return floppyDisk; - } - public Memory getMemory() { return memory; } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/ChargeShieldCommandListener.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/ChargeShieldCommandListener.java index 877685d..9afd3e3 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/ChargeShieldCommandListener.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/ChargeShieldCommandListener.java @@ -5,7 +5,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.GameObject; /** * Debug command to add shield points to a Cubot diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java index a7140dc..6ebef6c 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java @@ -11,10 +11,18 @@ import org.bson.Document; */ public class Clock extends CpuHardware { - public static final char HWID = 0x0008; + private static final char HWID = 0x0008; public static final char DEFAULT_ADDRESS = 0x0008; + public Clock() { + + } + + public Clock(Document document) { + super(document); + } + @Override public void handleInterrupt(Status status) { @@ -31,17 +39,12 @@ public class Clock extends CpuHardware { return HWID; } - public static Clock deserialize() { - return new Clock(); - } - @Override public Document mongoSerialise() { Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); + dbObject.put("type", getClass().getCanonicalName()); return dbObject; } diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/MiscHWPlugin.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/MiscHWPlugin.java index 1ff6377..2bf52f7 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/MiscHWPlugin.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/MiscHWPlugin.java @@ -2,36 +2,23 @@ package net.simon987.mischwplugin; import net.simon987.mischwplugin.event.CpuInitialisationListener; import net.simon987.server.ServerConfiguration; -import net.simon987.server.assembly.CpuHardware; -import net.simon987.server.io.CpuHardwareDeserializer; +import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.logging.LogManager; import net.simon987.server.plugin.ServerPlugin; -import org.bson.Document; /** * Plugin that adds miscellaneous hardware to the game */ -public class MiscHWPlugin extends ServerPlugin implements CpuHardwareDeserializer { +public class MiscHWPlugin extends ServerPlugin { @Override - public void init(ServerConfiguration config) { + public void init(ServerConfiguration config, GameRegistry registry) { listeners.add(new CpuInitialisationListener()); + registry.registerHardware(RandomNumberGenerator.class); + registry.registerHardware(Clock.class); + LogManager.LOGGER.info("Initialised Misc Hardware Plugin"); } - - @Override - public CpuHardware deserializeHardware(Document hwJson) { - int hwid = (int) hwJson.get("hwid"); - - switch (hwid) { - case RandomNumberGenerator.HWID: - return RandomNumberGenerator.deserialize(); - case Clock.HWID: - return Clock.deserialize(); - } - - return null; - } } diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java index c6a5cbe..32c8fd7 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java @@ -11,7 +11,7 @@ import java.util.Random; */ public class RandomNumberGenerator extends CpuHardware { - public static final char HWID = 0x0007; + private static final char HWID = 0x0007; public static final char DEFAULT_ADDRESS = 0x0007; @@ -21,6 +21,11 @@ public class RandomNumberGenerator extends CpuHardware { random = new Random(); } + public RandomNumberGenerator(Document document) { + super(document); + random = new Random(); + } + @Override public void handleInterrupt(Status status) { @@ -37,13 +42,8 @@ public class RandomNumberGenerator extends CpuHardware { public Document mongoSerialise() { Document dbObject = new Document(); - - dbObject.put("hwid", (int) HWID); + dbObject.put("type", getClass().getCanonicalName()); return dbObject; } - - public static RandomNumberGenerator deserialize() { - return new RandomNumberGenerator(); - } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/ElectricBox.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/ElectricBox.java index 84514a7..833a39e 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/ElectricBox.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/ElectricBox.java @@ -2,11 +2,10 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.assembly.Util; -import net.simon987.server.game.Attackable; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Rechargeable; -import net.simon987.server.game.Updatable; -import net.simon987.server.logging.LogManager; +import net.simon987.server.game.objects.Attackable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Rechargeable; +import net.simon987.server.game.objects.Updatable; import org.bson.Document; import org.json.simple.JSONObject; @@ -17,8 +16,6 @@ import java.util.ArrayList; */ public class ElectricBox extends GameObject implements Updatable, Attackable { - public static final int ID = 7; - /** * Hit points */ @@ -42,8 +39,12 @@ public class ElectricBox extends GameObject implements Updatable, Attackable { private ArrayList nearObjects = new ArrayList<>(); public ElectricBox() { + hp = maxHp; + } - this.hp = maxHp; + public ElectricBox(Document document) { + super(document); + hp = (int) document.get("hp"); } /** @@ -92,7 +93,6 @@ public class ElectricBox extends GameObject implements Updatable, Attackable { //YOU ARE DEAD if (hp <= 0) { setDead(true); - LogManager.LOGGER.severe("BOX DEAD"); } } @@ -136,13 +136,9 @@ public class ElectricBox extends GameObject implements Updatable, Attackable { } @Override - public JSONObject serialise() { - JSONObject json = new JSONObject(); + public JSONObject jsonSerialise() { + JSONObject json = super.jsonSerialise(); - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); json.put("hp", hp); return json; @@ -150,28 +146,13 @@ public class ElectricBox extends GameObject implements Updatable, Attackable { @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); dbObject.put("hp", getHp()); return dbObject; } - public static ElectricBox deserialize(Document obj) { - - ElectricBox electricBox = new ElectricBox(); - electricBox.setHp((int) obj.get("hp")); - electricBox.setObjectId((long) obj.get("i")); - electricBox.setX((int) obj.get("x")); - electricBox.setY((int) obj.get("y")); - - return electricBox; - } - @Override public boolean onDeadCallback() { getWorld().decUpdatable(); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java index 94eb7b0..2aa91d3 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java @@ -1,10 +1,9 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Updatable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Updatable; import org.bson.Document; -import org.json.simple.JSONObject; import java.awt.*; import java.util.ArrayList; @@ -16,7 +15,6 @@ import java.util.List; public class Factory extends GameObject implements Updatable { private static final int MAP_INFO = 0x0200; - static final int ID = 3; /** * Maximum number of NonPlayerCharacters assigned to this Factory @@ -49,6 +47,19 @@ public class Factory extends GameObject implements Updatable { */ private boolean initialised = false; + public Factory() { + } + + public Factory(Document document) { + super(document); + + setObjectId((long) document.get("i")); + setX((int) document.get("x")); + setY((int) document.get("y")); + + tmpNpcArray = ((ArrayList) document.get("tmpNpcArray")).toArray(); + } + @Override public char getMapInfo() { return MAP_INFO; @@ -118,27 +129,9 @@ public class Factory extends GameObject implements Updatable { return (x == getX() + 1 || x == getX()) && (y == getY() + 1 || y == getY()); } - @Override - public JSONObject serialise() { - - JSONObject json = new JSONObject(); - - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); - - return json; - } - @Override public Document mongoSerialise() { - Document dbObject = new Document(); - - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); + Document dbObject = super.mongoSerialise(); List tmpNpcArray = new ArrayList<>(npcs.size()); @@ -151,18 +144,6 @@ public class Factory extends GameObject implements Updatable { return dbObject; } - public static Factory deserialise(Document obj) { - - Factory factory = new Factory(); - factory.setObjectId((long) obj.get("i")); - factory.setX((int) obj.get("x")); - factory.setY((int) obj.get("y")); - - factory.tmpNpcArray = ((ArrayList) obj.get("tmpNpcArray")).toArray(); - - return factory; - } - /** * Get the first non-blocked tile that is directly adjacent to the factory, starting from the north-east corner * going clockwise. diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java index 81e15ef..35fbb7d 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java @@ -2,9 +2,9 @@ package net.simon987.npcplugin; import net.simon987.server.assembly.Util; -import net.simon987.server.game.Direction; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.InventoryHolder; +import net.simon987.server.game.objects.Direction; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.InventoryHolder; import net.simon987.server.logging.LogManager; import java.util.ArrayList; @@ -105,8 +105,5 @@ public class HarvestTask extends NPCTask { } else { pause--; } - } - - } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java index 3995c6f..dc45445 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java @@ -2,18 +2,13 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.event.ObjectDeathEvent; -import net.simon987.server.game.Direction; +import net.simon987.server.game.objects.Direction; import org.bson.Document; import org.json.simple.JSONObject; public class HarvesterNPC extends NonPlayerCharacter { - public static final int ID = 10; - - /** - * - */ public static final int MAX_HEALTH = GameServer.INSTANCE.getConfig().getInt("harvester_hp_max"); public static final int HEAL_RATE = GameServer.INSTANCE.getConfig().getInt("harvester_regen"); @@ -26,6 +21,12 @@ public class HarvesterNPC extends NonPlayerCharacter { setHealRate(HEAL_RATE); } + public HarvesterNPC(Document document) { + super(document); + + setDirection(Direction.getDirection(document.getInteger("direction"))); + } + @Override public void update() { @@ -56,53 +57,30 @@ public class HarvesterNPC extends NonPlayerCharacter { getFactory().getNpcs().remove(this); } - GameServer.INSTANCE.getEventDispatcher().dispatch( - new ObjectDeathEvent(this, ID)); + GameServer.INSTANCE.getEventDispatcher().dispatch(new ObjectDeathEvent(this)); return false; } @Override - public JSONObject serialise() { - JSONObject json = super.serialise(); + public JSONObject jsonSerialise() { + JSONObject json = super.jsonSerialise(); - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); json.put("direction", getDirection().ordinal()); json.put("hp", getHp()); - json.put("energy", energy); json.put("action", getAction().ordinal()); - json.put("t", ID); return json; } @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); dbObject.put("direction", getDirection().ordinal()); dbObject.put("hp", getHp()); dbObject.put("action", getAction().ordinal()); - dbObject.put("t", ID); return dbObject; } - - public static HarvesterNPC deserialize(Document obj) { - - HarvesterNPC npc = new HarvesterNPC(); - npc.setObjectId((long) obj.get("i")); - npc.setX((int) obj.get("x")); - npc.setY((int) obj.get("y")); - npc.setHp((int) obj.get("hp")); - npc.setDirection(Direction.getDirection((int) obj.get("direction"))); - - return npc; - - } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java index 028db48..1220ed0 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java @@ -2,10 +2,11 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.assembly.Util; -import net.simon987.server.game.*; +import net.simon987.server.game.objects.*; import net.simon987.server.game.pathfinding.Node; import net.simon987.server.game.pathfinding.Pathfinder; import net.simon987.server.logging.LogManager; +import org.bson.Document; import java.util.ArrayList; @@ -30,11 +31,6 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable public static final int HP_MAX_DEFAULT = 100; public static final int HP_REGEN_RATE_DEFAULT = 0; - /** - * Currently unused - */ - int energy; - /** * Current task */ @@ -76,6 +72,17 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable */ private int maxHp = HP_MAX_DEFAULT; + public NonPlayerCharacter() { + + } + + public NonPlayerCharacter(Document document) { + super(document); + + hp = document.getInteger("hp"); + setDirection(Direction.getDirection(document.getInteger("direction"))); + } + @Override public char getMapInfo() { return MAP_INFO; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java index 0054f49..abc7c14 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java @@ -4,17 +4,13 @@ import net.simon987.npcplugin.event.CpuInitialisationListener; import net.simon987.npcplugin.event.VaultWorldUpdateListener; import net.simon987.npcplugin.event.WorldCreationListener; import net.simon987.server.ServerConfiguration; -import net.simon987.server.assembly.CpuHardware; -import net.simon987.server.game.GameObject; -import net.simon987.server.io.CpuHardwareDeserializer; -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; import java.util.ArrayList; -public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer { +public class NpcPlugin extends ServerPlugin { /** * Radio tower cache @@ -22,55 +18,28 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C private static ArrayList radioTowers; @Override - public void init(ServerConfiguration configuration) { + public void init(ServerConfiguration configuration, GameRegistry registry) { listeners.add(new WorldCreationListener()); listeners.add(new CpuInitialisationListener()); listeners.add(new VaultWorldUpdateListener(configuration)); + registry.registerGameObject(HarvesterNPC.class); + registry.registerGameObject(Factory.class); + registry.registerGameObject(RadioTower.class); + registry.registerGameObject(VaultDoor.class); + registry.registerGameObject(Obstacle.class); + registry.registerGameObject(ElectricBox.class); + registry.registerGameObject(Portal.class); + registry.registerGameObject(VaultExitPortal.class); + + registry.registerHardware(RadioReceiverHardware.class); + radioTowers = new ArrayList<>(32); LogManager.LOGGER.info("Initialised NPC plugin"); } - @Override - public GameObject deserializeObject(Document obj) { - - int objType = (int) obj.get("t"); - - if (objType == HarvesterNPC.ID) { - return HarvesterNPC.deserialize(obj); - } else if (objType == Factory.ID) { - return Factory.deserialise(obj); - } else if (objType == RadioTower.ID) { - return RadioTower.deserialize(obj); - } else if (objType == VaultDoor.ID) { - return VaultDoor.deserialize(obj); - } else if (objType == Obstacle.ID) { - return Obstacle.deserialize(obj); - } else if (objType == ElectricBox.ID) { - return ElectricBox.deserialize(obj); - } else if (objType == Portal.ID) { - return Portal.deserialize(obj); - } else if (objType == VaultExitPortal.ID) { - return VaultExitPortal.deserialize(obj); - } - - return null; - } - - @Override - public CpuHardware deserializeHardware(Document obj) { - int hwid = (int) obj.get("hwid"); - - switch (hwid) { - case RadioReceiverHardware.HWID: - return RadioReceiverHardware.deserialize(obj); - } - - return null; - } - public static ArrayList getRadioTowers() { return radioTowers; } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java index dce701f..4eb597f 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java @@ -1,7 +1,7 @@ package net.simon987.npcplugin; -import net.simon987.server.game.Attackable; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.Attackable; +import net.simon987.server.game.objects.GameObject; import org.bson.Document; import org.json.simple.JSONObject; @@ -10,7 +10,6 @@ import org.json.simple.JSONObject; */ public class Obstacle extends GameObject implements Attackable { - public static final int ID = 6; public static final int MAP_INFO = 0x0400; /** @@ -33,6 +32,10 @@ public class Obstacle extends GameObject implements Attackable { this.maxHp = hp; } + public Obstacle(Document document) { + style = document.getInteger("style"); + } + @Override public void setHealRate(int hp) { //No op @@ -89,12 +92,8 @@ public class Obstacle extends GameObject implements Attackable { @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); dbObject.put("hp", hp); dbObject.put("style", style); @@ -102,27 +101,12 @@ public class Obstacle extends GameObject implements Attackable { } @Override - public JSONObject serialise() { - JSONObject json = new JSONObject(); + public JSONObject jsonSerialise() { + JSONObject json = super.jsonSerialise(); - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); json.put("hp", hp); json.put("style", style); return json; } - - public static Obstacle deserialize(Document obj) { - - Obstacle obstacle = new Obstacle((int) obj.get("hp")); - obstacle.setObjectId((long) obj.get("i")); - obstacle.setX((int) obj.get("x")); - obstacle.setY((int) obj.get("y")); - obstacle.setStyle((int) obj.get("style")); - - return obstacle; - } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java index 6b44973..09f9a9e 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java @@ -1,9 +1,13 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.game.*; +import net.simon987.server.game.objects.Enterable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Updatable; +import net.simon987.server.game.world.Location; +import net.simon987.server.game.world.World; import org.bson.Document; -import org.json.simple.JSONObject; + public class Portal extends GameObject implements Enterable { @@ -14,7 +18,22 @@ public class Portal extends GameObject implements Enterable { public static final int MAP_INFO = 0x0020; - public static final int ID = 8; + public Portal() { + + } + + public Portal(Document document) { + super(document); + + destination = new Location( + document.getInteger("dstWorldX"), + document.getInteger("dstWorldY"), + document.getString("dstDimension"), + document.getInteger("dstX"), + document.getInteger("dstY")); + setX(document.getInteger("x")); + setY(document.getInteger("y")); + } /** * Called when an object attempts to walk directly into a Enterable object @@ -48,12 +67,8 @@ public class Portal extends GameObject implements Enterable { @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); dbObject.put("dstWorldX", destination.worldX); dbObject.put("dstWorldY", destination.worldY); dbObject.put("dstX", destination.x); @@ -63,34 +78,6 @@ public class Portal extends GameObject implements Enterable { return dbObject; } - public static Portal deserialize(Document obj) { - - Portal portal = new Portal(); - - portal.destination = new Location( - (int) obj.get("dstWorldX"), - (int) obj.get("dstWorldY"), - (String) obj.get("dstDimension"), - (int) obj.get("dstX"), - (int) obj.get("dstY")); - portal.setX((int) obj.get("x")); - portal.setY((int) obj.get("y")); - - return portal; - } - - @Override - public JSONObject serialise() { - JSONObject json = new JSONObject(); - - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); - - return json; - } - public Location getDestination() { return destination; } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java index 363672c..23bfbef 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Util; -import net.simon987.server.game.Action; -import net.simon987.server.game.ControllableUnit; +import net.simon987.server.game.objects.Action; +import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; import java.util.ArrayList; @@ -24,6 +24,12 @@ public class RadioReceiverHardware extends CpuHardware { this.cubot = cubot; } + public RadioReceiverHardware(Document document) { + super(document); + + this.cubot = (ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot")); + } + @Override public void handleInterrupt(Status status) { int x = getCpu().getRegisterSet().getRegister("X").getValue(); @@ -70,13 +76,9 @@ public class RadioReceiverHardware extends CpuHardware { Document dbObject = new Document(); - dbObject.put("hwid", (int) HWID); + dbObject.put("type", getClass().getCanonicalName()); dbObject.put("cubot", cubot.getObjectId()); return dbObject; } - - public static RadioReceiverHardware deserialize(Document obj) { - return new RadioReceiverHardware((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot"))); - } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java index 21e8034..fca225c 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java @@ -1,10 +1,9 @@ package net.simon987.npcplugin; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Programmable; -import net.simon987.server.game.Updatable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Programmable; +import net.simon987.server.game.objects.Updatable; import org.bson.Document; -import org.json.simple.JSONObject; import java.util.ArrayList; @@ -12,12 +11,19 @@ public class RadioTower extends GameObject implements Programmable, Updatable { private static final int MAP_INFO = 0x1000; - public static final int ID = 4; - public static final int MAX_RANGE = 3; //todo load from config private static final int MAX_MESSAGES = 16; + public RadioTower() { + + } + + public RadioTower(Document document) { + super(document); + NpcPlugin.getRadioTowers().add(this); + } + @Override public char getMapInfo() { return MAP_INFO; @@ -50,46 +56,7 @@ public class RadioTower extends GameObject implements Programmable, Updatable { } } - @Override - public JSONObject serialise() { - - JSONObject json = new JSONObject(); - - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); - - return json; - - } - - @Override - public Document mongoSerialise() { - Document dbObject = new Document(); - - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); - - return dbObject; - } - - public static RadioTower deserialize(Document obj) { - - RadioTower tower = new RadioTower(); - tower.setObjectId((long) obj.get("i")); - tower.setX((int) obj.get("x")); - tower.setY((int) obj.get("y")); - - NpcPlugin.getRadioTowers().add(tower); - - return tower; - } - - - public ArrayList getMessages() { + ArrayList getMessages() { return lastMessages; } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java index a281b78..241f659 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java @@ -1,10 +1,10 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.game.Direction; -import net.simon987.server.game.Location; -import net.simon987.server.game.TileMap; -import net.simon987.server.game.World; +import net.simon987.server.game.objects.Direction; +import net.simon987.server.game.world.Location; +import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import java.awt.*; @@ -250,5 +250,4 @@ public class VaultDimension { } } - } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java index 7a3de3b..b7c7e2b 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java @@ -2,10 +2,13 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.crypto.RandomStringGenerator; -import net.simon987.server.game.*; +import net.simon987.server.game.objects.Enterable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Programmable; +import net.simon987.server.game.objects.Updatable; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import org.bson.Document; -import org.json.simple.JSONObject; import java.util.Arrays; @@ -14,7 +17,6 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up private static final int MAP_INFO = 0x0800; - public static final int ID = 5; /** * Password to open the vault door */ @@ -48,6 +50,21 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up this.password = "12345678".toCharArray(); } + public VaultDoor(Document document) { + super(document); + + setX(document.getInteger("x")); + setY(document.getInteger("y")); + + + if (document.containsKey("homeX") && document.containsKey("homeY")) { + homeX = document.getInteger("homeX"); + homeY = document.getInteger("homeY"); + } + + password = document.getString("password").toCharArray(); + } + @Override public void update() { @@ -124,51 +141,15 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up @Override public Document mongoSerialise() { - Document dbObject = new Document(); + Document dbObject = super.mongoSerialise(); - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); dbObject.put("homeX", getHomeX()); dbObject.put("homeY", getHomeY()); - dbObject.put("t", ID); dbObject.put("pw", new String(password)); return dbObject; } - @Override - public JSONObject serialise() { - - JSONObject json = new JSONObject(); - - json.put("i", getObjectId()); - json.put("x", getX()); - json.put("y", getY()); - json.put("t", ID); - //Don't send the password to the client! - - return json; - } - - public static VaultDoor deserialize(Document obj) { - - VaultDoor vaultDoor = new VaultDoor(0); //cypherId ? - vaultDoor.setX((int) obj.get("x")); - vaultDoor.setY((int) obj.get("y")); - vaultDoor.setObjectId((long) obj.get("i")); - - - if (obj.containsKey("homeX") && obj.containsKey("homeY")) { - vaultDoor.setHomeX((int) obj.get("homeX")); - vaultDoor.setHomeY((int) obj.get("homeY")); - } - - vaultDoor.password = ((String) obj.get("pw")).toCharArray(); - - return vaultDoor; - } - @Override public void initialize() { //Get or generate vault world diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java index e377391..384fd12 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java @@ -1,8 +1,8 @@ package net.simon987.npcplugin; -import net.simon987.server.game.ControllableUnit; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Location; +import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.world.Location; import net.simon987.server.logging.LogManager; import org.bson.Document; @@ -11,23 +11,21 @@ import org.bson.Document; */ public class VaultExitPortal extends Portal { - public static final int ID = 9; + public VaultExitPortal() { - @Override - public Document mongoSerialise() { - Document dbObject = new Document(); + } - dbObject.put("i", getObjectId()); - dbObject.put("x", getX()); - dbObject.put("y", getY()); - dbObject.put("t", ID); - dbObject.put("dstWorldX", getDestination().worldX); - dbObject.put("dstWorldY", getDestination().worldY); - dbObject.put("dstX", getDestination().x); - dbObject.put("dstY", getDestination().y); - dbObject.put("dstDimension", getDestination().dimension); + public VaultExitPortal(Document document) { + super(document); - return dbObject; + setDestination(new Location( + document.getInteger("dstWorldX"), + document.getInteger("dstWorldY"), + document.getString("dstDimension"), + document.getInteger("dstX"), + document.getInteger("dstY"))); + setX(document.getInteger("x")); + setY(document.getInteger("y")); } @Override @@ -42,20 +40,4 @@ public class VaultExitPortal extends Portal { return super.enter(object); } - - public static Portal deserialize(Document obj) { - - VaultExitPortal portal = new VaultExitPortal(); - - portal.setDestination(new Location( - (int) obj.get("dstWorldX"), - (int) obj.get("dstWorldY"), - (String) obj.get("dstDimension"), - (int) obj.get("dstX"), - (int) obj.get("dstY"))); - portal.setX((int) obj.get("x")); - portal.setY((int) obj.get("y")); - - return portal; - } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java index d44113f..e9d5be4 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java @@ -1,8 +1,8 @@ package net.simon987.npcplugin; -import net.simon987.server.game.Direction; -import net.simon987.server.game.TileMap; -import net.simon987.server.game.World; +import net.simon987.server.game.objects.Direction; +import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import java.awt.*; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java index 6c06c25..59e93fb 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java @@ -1,8 +1,8 @@ package net.simon987.npcplugin; 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 java.awt.*; import java.util.ArrayList; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultWorldUpdateListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultWorldUpdateListener.java index 74b7dba..9e3188b 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultWorldUpdateListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultWorldUpdateListener.java @@ -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; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java index 4f53a19..21a26a3 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java @@ -8,7 +8,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.event.WorldGenerationEvent; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import java.awt.*; diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java index aafe0f1..4a34045 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java @@ -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 * diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java index d70ef59..525a713 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java @@ -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"); } } diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java index 6f4c05c..6894f53 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java @@ -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.*; diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java index 45788c0..92ece42 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java @@ -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) { diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/WorldUpdateListener.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/WorldUpdateListener.java index ae4868e..bf65a37 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/WorldUpdateListener.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/WorldUpdateListener.java @@ -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; diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveCloudPlugin.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveCloudPlugin.java index 5ec94d0..efe34f2 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveCloudPlugin.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveCloudPlugin.java @@ -1,23 +1,15 @@ package net.simon987.pluginradioactivecloud; 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 RadioactiveCloudPlugin extends ServerPlugin implements GameObjectDeserializer { +public class RadioactiveCloudPlugin extends ServerPlugin { @Override - public void init(ServerConfiguration config) { + public void init(ServerConfiguration config, GameRegistry registry) { LogManager.LOGGER.info("Initialised Radioactive cloud plugin."); } - - @Override - public GameObject deserializeObject(Document object) { - - return null; - } } diff --git a/Server/src/main/java/net/simon987/server/GameServer.java b/Server/src/main/java/net/simon987/server/GameServer.java index 8ac7633..e95be4b 100644 --- a/Server/src/main/java/net/simon987/server/GameServer.java +++ b/Server/src/main/java/net/simon987/server/GameServer.java @@ -5,15 +5,16 @@ import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.UpdateOptions; +import com.mongodb.client.model.ReplaceOptions; import net.simon987.server.crypto.CryptoProvider; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventDispatcher; import net.simon987.server.event.TickEvent; -import net.simon987.server.game.DayNightCycle; import net.simon987.server.game.GameUniverse; -import net.simon987.server.game.World; import net.simon987.server.game.debug.*; +import net.simon987.server.game.objects.GameRegistry; +import net.simon987.server.game.world.DayNightCycle; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import net.simon987.server.plugin.PluginManager; import net.simon987.server.user.User; @@ -43,14 +44,15 @@ public class GameServer implements Runnable { private CryptoProvider cryptoProvider; - private MongoClient mongo = null; + private MongoClient mongo; private UserManager userManager; private UserStatsHelper userStatsHelper; - public GameServer() { + private GameRegistry gameRegistry; + public GameServer() { this.config = new ServerConfiguration("config.properties"); mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port")); @@ -64,6 +66,7 @@ public class GameServer implements Runnable { gameUniverse = new GameUniverse(config); gameUniverse.setMongo(mongo); pluginManager = new PluginManager(); + gameRegistry = new GameRegistry(); maxExecutionTime = config.getInt("user_timeout"); @@ -79,7 +82,7 @@ public class GameServer implements Runnable { for (File pluginFile : pluginDirListing) { if (pluginFile.getName().endsWith(".jar")) { - pluginManager.load(pluginFile, config); + pluginManager.load(pluginFile, config, gameRegistry); } } } else { @@ -238,7 +241,7 @@ public class GameServer implements Runnable { LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount()); try{ MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname")); - UpdateOptions updateOptions = new UpdateOptions(); + ReplaceOptions updateOptions = new ReplaceOptions(); updateOptions.upsert(true); int unloaded_worlds = 0; @@ -260,7 +263,6 @@ public class GameServer implements Runnable { } } - for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) { if (!u.isGuest()) { users.replaceOne(new Document("_id", u.getUsername()), u.mongoSerialise(), updateOptions); @@ -303,4 +305,8 @@ public class GameServer implements Runnable { public UserStatsHelper getUserStatsHelper() { return userStatsHelper; } + + public GameRegistry getRegistry() { + return gameRegistry; + } } diff --git a/Server/src/main/java/net/simon987/server/assembly/CPU.java b/Server/src/main/java/net/simon987/server/assembly/CPU.java index ebc869a..4efddcd 100755 --- a/Server/src/main/java/net/simon987/server/assembly/CPU.java +++ b/Server/src/main/java/net/simon987/server/assembly/CPU.java @@ -381,12 +381,12 @@ public class CPU implements MongoSerializable { ArrayList hardwareList = (ArrayList) obj.get("hardware"); for (Object serialisedHw : hardwareList) { - CpuHardware hardware = CpuHardware.deserialize((Document) serialisedHw); + CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw); hardware.setCpu(cpu); cpu.attachHardware(hardware, (int) ((Document) serialisedHw).get("address")); } - cpu.memory = Memory.deserialize((Document) obj.get("memory")); + cpu.memory = new Memory((Document) obj.get("memory")); cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet")); return cpu; diff --git a/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java b/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java index d53ff16..dba92db 100644 --- a/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java +++ b/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java @@ -1,15 +1,21 @@ package net.simon987.server.assembly; -import net.simon987.server.GameServer; -import net.simon987.server.io.CpuHardwareDeserializer; import net.simon987.server.io.MongoSerializable; -import net.simon987.server.plugin.ServerPlugin; import org.bson.Document; + public abstract class CpuHardware implements MongoSerializable { - CPU cpu; + private CPU cpu; + + public CpuHardware() { + + } + + public CpuHardware(Document document) { + + } /** * Handle an HWI instruction @@ -26,22 +32,6 @@ public abstract class CpuHardware implements MongoSerializable { public abstract char getId(); - public static CpuHardware deserialize(Document obj) { - - for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) { - - if (plugin instanceof CpuHardwareDeserializer) { - CpuHardware hw = ((CpuHardwareDeserializer) plugin).deserializeHardware(obj); - - if (hw != null) { - return hw; - } - } - } - - return null; - } - @Override public String toString() { return String.format("<%04X>", (int) getId()); diff --git a/Server/src/main/java/net/simon987/server/assembly/Memory.java b/Server/src/main/java/net/simon987/server/assembly/Memory.java index 87d17e1..29374fe 100755 --- a/Server/src/main/java/net/simon987/server/assembly/Memory.java +++ b/Server/src/main/java/net/simon987/server/assembly/Memory.java @@ -38,6 +38,31 @@ public class Memory implements Target, MongoSerializable { words = new char[size]; } + public Memory(Document document) { + + String zipBytesStr = (String) document.get("zipBytes"); + + if (zipBytesStr != null) { + byte[] compressedBytes = Base64.getDecoder().decode((String) document.get("zipBytes")); + + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Inflater decompressor = new Inflater(true); + InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor); + inflaterOutputStream.write(compressedBytes); + inflaterOutputStream.close(); + + setBytes(baos.toByteArray()); + + } catch (IOException e) { + e.printStackTrace(); + } + } else { + LogManager.LOGGER.severe("Memory was manually deleted"); + words = new char[GameServer.INSTANCE.getConfig().getInt("memory_size")]; + } + } + /** * Get the value at an address * @@ -153,35 +178,6 @@ public class Memory implements Target, MongoSerializable { return dbObject; } - public static Memory deserialize(Document obj) { - - Memory memory = new Memory(0); - - String zipBytesStr = (String) obj.get("zipBytes"); - - if (zipBytesStr != null) { - byte[] compressedBytes = Base64.getDecoder().decode((String) obj.get("zipBytes")); - - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Inflater decompressor = new Inflater(true); - InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor); - inflaterOutputStream.write(compressedBytes); - inflaterOutputStream.close(); - - memory.setBytes(baos.toByteArray()); - - } catch (IOException e) { - e.printStackTrace(); - } - } else { - LogManager.LOGGER.severe("Memory was manually deleted"); - memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size")); - } - - return memory; - } - public void setBytes(byte[] bytes) { this.words = new char[bytes.length / 2]; ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(this.words); diff --git a/Server/src/main/java/net/simon987/server/event/ObjectDeathEvent.java b/Server/src/main/java/net/simon987/server/event/ObjectDeathEvent.java index 1ffb8aa..a2ea5f1 100644 --- a/Server/src/main/java/net/simon987/server/event/ObjectDeathEvent.java +++ b/Server/src/main/java/net/simon987/server/event/ObjectDeathEvent.java @@ -4,17 +4,8 @@ package net.simon987.server.event; * Event dispatched by a GameObject who has needed callbacks on death */ public class ObjectDeathEvent extends GameEvent { - /** - * The GameObject type ID of object that initialize this event - */ - private long sourceObjectId; - public ObjectDeathEvent(Object source, int sourceObjectId) { + public ObjectDeathEvent(Object source) { setSource(source); - this.sourceObjectId = sourceObjectId; - } - - public long getSourceObjectId() { - return sourceObjectId; } } diff --git a/Server/src/main/java/net/simon987/server/event/WorldGenerationEvent.java b/Server/src/main/java/net/simon987/server/event/WorldGenerationEvent.java index 62f7670..636b401 100644 --- a/Server/src/main/java/net/simon987/server/event/WorldGenerationEvent.java +++ b/Server/src/main/java/net/simon987/server/event/WorldGenerationEvent.java @@ -1,6 +1,6 @@ package net.simon987.server.event; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; public class WorldGenerationEvent extends GameEvent { diff --git a/Server/src/main/java/net/simon987/server/event/WorldUpdateEvent.java b/Server/src/main/java/net/simon987/server/event/WorldUpdateEvent.java index aa6c3eb..77efdee 100644 --- a/Server/src/main/java/net/simon987/server/event/WorldUpdateEvent.java +++ b/Server/src/main/java/net/simon987/server/event/WorldUpdateEvent.java @@ -1,6 +1,6 @@ package net.simon987.server.event; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; public class WorldUpdateEvent extends GameEvent { diff --git a/Server/src/main/java/net/simon987/server/game/GameUniverse.java b/Server/src/main/java/net/simon987/server/game/GameUniverse.java index d95e0e2..7251bb4 100644 --- a/Server/src/main/java/net/simon987/server/game/GameUniverse.java +++ b/Server/src/main/java/net/simon987/server/game/GameUniverse.java @@ -10,6 +10,9 @@ import net.simon987.server.assembly.Assembler; import net.simon987.server.assembly.AssemblyResult; import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.exception.CancelledException; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.world.World; +import net.simon987.server.game.world.WorldGenerator; import net.simon987.server.logging.LogManager; import net.simon987.server.user.User; import org.bson.Document; @@ -115,7 +118,7 @@ public class GameUniverse { } } - World getLoadedWorld(int x, int y, String dimension) { + public World getLoadedWorld(int x, int y, String dimension) { // Wrapping coordinates around cyclically x %= maxWidth; y %= maxWidth; diff --git a/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java index c5bd185..3048e6a 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Programmable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Programmable; public class ComPortMsgCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/CreateWorldCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/CreateWorldCommandListener.java index 8142073..7fee741 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/CreateWorldCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/CreateWorldCommandListener.java @@ -4,7 +4,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; public class CreateWorldCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/DamageObjCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/DamageObjCommandListener.java index 03f214e..fa430d4 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/DamageObjCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/DamageObjCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.Attackable; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.Attackable; +import net.simon987.server.game.objects.GameObject; public class DamageObjCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/HealObjCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/HealObjCommandListener.java index a108a66..4019ab4 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/HealObjCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/HealObjCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.Attackable; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.Attackable; +import net.simon987.server.game.objects.GameObject; public class HealObjCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/KillAllCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/KillAllCommandListener.java index a6e0ed6..6045d8f 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/KillAllCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/KillAllCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -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 java.util.ArrayList; import java.util.Arrays; diff --git a/Server/src/main/java/net/simon987/server/game/debug/MoveObjCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/MoveObjCommandListener.java index fb2621b..7c296e7 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/MoveObjCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/MoveObjCommandListener.java @@ -4,7 +4,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.GameObject; public class MoveObjCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/ObjInfoCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/ObjInfoCommandListener.java index 1e6ccfc..f4ea865 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/ObjInfoCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/ObjInfoCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -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 java.util.Arrays; import java.util.Collection; @@ -37,7 +37,7 @@ public class ObjInfoCommandListener implements GameEventListener { if (obj.isAt(e.getInt("x"), e.getInt("y")) || (obj.getX() == e.getInt("x") && obj.getY() == e.getInt("y"))) { str += "Mongo:" + obj.mongoSerialise() + "\n"; - str += "JSON :" + obj.serialise().toJSONString() + "\n\n"; + str += "JSON :" + obj.jsonSerialise().toJSONString() + "\n\n"; } } diff --git a/Server/src/main/java/net/simon987/server/game/debug/SetEnergyCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/SetEnergyCommandListener.java index 63495b5..9ff5fa0 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/SetEnergyCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/SetEnergyCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Rechargeable; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Rechargeable; public class SetEnergyCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/SetTileAtCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/SetTileAtCommandListener.java index 8920463..b757919 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/SetTileAtCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/SetTileAtCommandListener.java @@ -4,7 +4,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; public class SetTileAtCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/SpawnObjCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/SpawnObjCommandListener.java index d486e58..8fc6bf9 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/SpawnObjCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/SpawnObjCommandListener.java @@ -4,8 +4,8 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -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 org.bson.Document; import java.util.Arrays; @@ -30,7 +30,7 @@ public class SpawnObjCommandListener implements GameEventListener { Document dbObj = Document.parse(e.getString("data")); dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId()); - GameObject object = GameObject.deserialize(dbObj); + GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject(dbObj); if (object != null) { world.addObject(object); diff --git a/Server/src/main/java/net/simon987/server/game/debug/TpObjectCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/TpObjectCommandListener.java index 95f4ad4..5f370e5 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/TpObjectCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/TpObjectCommandListener.java @@ -4,9 +4,9 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Updatable; -import net.simon987.server.game.World; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Updatable; +import net.simon987.server.game.world.World; public class TpObjectCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java index 372c412..150dd07 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java @@ -4,7 +4,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.game.ControllableUnit; +import net.simon987.server.game.objects.ControllableUnit; import net.simon987.server.user.User; public class UserInfoCommandListener implements GameEventListener { diff --git a/Server/src/main/java/net/simon987/server/game/Action.java b/Server/src/main/java/net/simon987/server/game/objects/Action.java similarity index 77% rename from Server/src/main/java/net/simon987/server/game/Action.java rename to Server/src/main/java/net/simon987/server/game/objects/Action.java index 5a1cbfd..3d8d7b8 100644 --- a/Server/src/main/java/net/simon987/server/game/Action.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Action.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; public enum Action { IDLE, diff --git a/Server/src/main/java/net/simon987/server/game/Attackable.java b/Server/src/main/java/net/simon987/server/game/objects/Attackable.java similarity index 85% rename from Server/src/main/java/net/simon987/server/game/Attackable.java rename to Server/src/main/java/net/simon987/server/game/objects/Attackable.java index 444428e..72c280d 100644 --- a/Server/src/main/java/net/simon987/server/game/Attackable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Attackable.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; /** * Objects that can be attacked or healed @@ -8,12 +8,15 @@ public interface Attackable { void setHealRate(int hp); int getHp(); + void setHp(int hp); int getMaxHp(); + void setMaxHp(int hp); void heal(int amount); + void damage(int amount); } diff --git a/Server/src/main/java/net/simon987/server/game/ControllableUnit.java b/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java similarity index 87% rename from Server/src/main/java/net/simon987/server/game/ControllableUnit.java rename to Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java index a352376..be32be4 100644 --- a/Server/src/main/java/net/simon987/server/game/ControllableUnit.java +++ b/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java @@ -1,6 +1,7 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; import net.simon987.server.assembly.Memory; +import net.simon987.server.game.world.World; import net.simon987.server.user.User; import java.util.ArrayList; diff --git a/Server/src/main/java/net/simon987/server/game/Direction.java b/Server/src/main/java/net/simon987/server/game/objects/Direction.java similarity index 97% rename from Server/src/main/java/net/simon987/server/game/Direction.java rename to Server/src/main/java/net/simon987/server/game/objects/Direction.java index 183db52..8d9cfa9 100755 --- a/Server/src/main/java/net/simon987/server/game/Direction.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Direction.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; /** * Direction of a game object in a 4-direction grid-based diff --git a/Server/src/main/java/net/simon987/server/game/Enterable.java b/Server/src/main/java/net/simon987/server/game/objects/Enterable.java similarity index 87% rename from Server/src/main/java/net/simon987/server/game/Enterable.java rename to Server/src/main/java/net/simon987/server/game/objects/Enterable.java index fc0c7ea..f6a47c7 100644 --- a/Server/src/main/java/net/simon987/server/game/Enterable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Enterable.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; public interface Enterable { diff --git a/Server/src/main/java/net/simon987/server/game/GameObject.java b/Server/src/main/java/net/simon987/server/game/objects/GameObject.java similarity index 86% rename from Server/src/main/java/net/simon987/server/game/GameObject.java rename to Server/src/main/java/net/simon987/server/game/objects/GameObject.java index b88434d..8b055a3 100755 --- a/Server/src/main/java/net/simon987/server/game/GameObject.java +++ b/Server/src/main/java/net/simon987/server/game/objects/GameObject.java @@ -1,10 +1,9 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; import net.simon987.server.GameServer; -import net.simon987.server.io.GameObjectDeserializer; +import net.simon987.server.game.world.World; import net.simon987.server.io.JSONSerialisable; import net.simon987.server.io.MongoSerializable; -import net.simon987.server.plugin.ServerPlugin; import org.bson.Document; import org.json.simple.JSONObject; @@ -12,7 +11,7 @@ import java.awt.*; import java.util.ArrayList; /** - * An INSTANCE of an object (e.g. a Tree, a character ...) inside the + * An instance of an object (e.g. a Cubot, a NPC...) inside the * game universe */ public abstract class GameObject implements JSONSerialisable, MongoSerializable { @@ -43,6 +42,15 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable */ private World world; + public GameObject() { + + } + + public GameObject(Document document) { + objectId = document.getLong("id"); + x = document.getInteger("x"); + y = document.getInteger("y"); + } /** * Increment the location of the game object by 1 tile @@ -101,9 +109,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable } else { return false; } - } - } public abstract char getMapInfo(); @@ -119,7 +125,6 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable } else { return new Point(x - 1, y); } - } /** @@ -219,24 +224,13 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable } @Override - public JSONObject serialise() { - return new JSONObject(); - } - - public static GameObject deserialize(Document obj) { - - for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) { - - if (plugin instanceof GameObjectDeserializer) { - GameObject object = ((GameObjectDeserializer) plugin).deserializeObject(obj); - - if (object != null) { - return object; - } - } - } - - return null; + public JSONObject jsonSerialise() { + JSONObject json = new JSONObject(); + json.put("i", getObjectId()); + json.put("t", getClass().getCanonicalName()); + json.put("x", getX()); + json.put("y", getY()); + return json; } @@ -254,6 +248,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable /** * Called before this GameObject is removed from the world - defaults to doing nothing + * * @return true if cancelled */ public boolean onDeadCallback() { @@ -263,4 +258,17 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable public void initialize() { } + + + @Override + public Document mongoSerialise() { + Document document = new Document(); + + document.put("id", getObjectId()); + document.put("type", getClass().getCanonicalName()); + document.put("x", getX()); + document.put("y", getY()); + + return document; + } } \ No newline at end of file diff --git a/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java new file mode 100644 index 0000000..dbb5742 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java @@ -0,0 +1,70 @@ +package net.simon987.server.game.objects; + +import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.logging.LogManager; +import org.bson.Document; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; + +public class GameRegistry { + + private HashMap> gameObjects; + private HashMap> hardware; + + + public GameRegistry() { + gameObjects = new HashMap<>(); + hardware = new HashMap<>(); + } + + public void registerGameObject(Class clazz) { + gameObjects.put(clazz.getCanonicalName(), clazz); + } + + public void registerHardware(Class clazz) { + hardware.put(clazz.getCanonicalName(), clazz); + } + + + public CpuHardware deserializeHardware(Document document) { + String type = document.getString("type"); + + if (hardware.containsKey(type)) { + + try { + return hardware.get(type).getConstructor(Document.class).newInstance(document); + } catch (InstantiationException | IllegalAccessException | + InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + } else { + LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type); + return null; + } + } + + public GameObject deserializeGameObject(Document document) { + + String type = document.getString("type"); + + if (gameObjects.containsKey(type)) { + + try { + return gameObjects.get(type).getConstructor(Document.class).newInstance(document); + } catch (InstantiationException | IllegalAccessException | + InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + } else { + LogManager.LOGGER.severe("Trying to deserialize unknown GameObject type: " + type); + return null; + } + } + + public boolean isGameObjectRegistered(String type) { + return gameObjects.containsKey(type); + } +} diff --git a/Server/src/main/java/net/simon987/server/game/InventoryHolder.java b/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java similarity index 92% rename from Server/src/main/java/net/simon987/server/game/InventoryHolder.java rename to Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java index 3900316..60c9ec6 100644 --- a/Server/src/main/java/net/simon987/server/game/InventoryHolder.java +++ b/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; public interface InventoryHolder { diff --git a/Server/src/main/java/net/simon987/server/game/Programmable.java b/Server/src/main/java/net/simon987/server/game/objects/Programmable.java similarity index 65% rename from Server/src/main/java/net/simon987/server/game/Programmable.java rename to Server/src/main/java/net/simon987/server/game/objects/Programmable.java index b31b67c..eed27ce 100644 --- a/Server/src/main/java/net/simon987/server/game/Programmable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Programmable.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; public interface Programmable { diff --git a/Server/src/main/java/net/simon987/server/game/Radioactive.java b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java similarity index 68% rename from Server/src/main/java/net/simon987/server/game/Radioactive.java rename to Server/src/main/java/net/simon987/server/game/objects/Radioactive.java index 4795639..dc1fcf1 100644 --- a/Server/src/main/java/net/simon987/server/game/Radioactive.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; //Alpha: ±5cm //Beta: 10-20 feet diff --git a/Server/src/main/java/net/simon987/server/game/Rechargeable.java b/Server/src/main/java/net/simon987/server/game/objects/Rechargeable.java similarity index 84% rename from Server/src/main/java/net/simon987/server/game/Rechargeable.java rename to Server/src/main/java/net/simon987/server/game/objects/Rechargeable.java index 8787804..f6a5e78 100644 --- a/Server/src/main/java/net/simon987/server/game/Rechargeable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Rechargeable.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; public interface Rechargeable { diff --git a/Server/src/main/java/net/simon987/server/game/Updatable.java b/Server/src/main/java/net/simon987/server/game/objects/Updatable.java similarity index 78% rename from Server/src/main/java/net/simon987/server/game/Updatable.java rename to Server/src/main/java/net/simon987/server/game/objects/Updatable.java index 067238a..1df79f3 100644 --- a/Server/src/main/java/net/simon987/server/game/Updatable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Updatable.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.objects; /** * Updatable objects needs to be updated each tick diff --git a/Server/src/main/java/net/simon987/server/game/pathfinding/Pathfinder.java b/Server/src/main/java/net/simon987/server/game/pathfinding/Pathfinder.java index 785b163..76f24a9 100755 --- a/Server/src/main/java/net/simon987/server/game/pathfinding/Pathfinder.java +++ b/Server/src/main/java/net/simon987/server/game/pathfinding/Pathfinder.java @@ -1,7 +1,7 @@ package net.simon987.server.game.pathfinding; import net.simon987.server.assembly.Util; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; import java.util.ArrayList; import java.util.Collections; diff --git a/Server/src/main/java/net/simon987/server/game/DayNightCycle.java b/Server/src/main/java/net/simon987/server/game/world/DayNightCycle.java similarity index 96% rename from Server/src/main/java/net/simon987/server/game/DayNightCycle.java rename to Server/src/main/java/net/simon987/server/game/world/DayNightCycle.java index 655b1c9..3f9aad7 100644 --- a/Server/src/main/java/net/simon987/server/game/DayNightCycle.java +++ b/Server/src/main/java/net/simon987/server/game/world/DayNightCycle.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.world; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; diff --git a/Server/src/main/java/net/simon987/server/game/Location.java b/Server/src/main/java/net/simon987/server/game/world/Location.java similarity index 92% rename from Server/src/main/java/net/simon987/server/game/Location.java rename to Server/src/main/java/net/simon987/server/game/world/Location.java index 7ad9a10..df2069a 100644 --- a/Server/src/main/java/net/simon987/server/game/Location.java +++ b/Server/src/main/java/net/simon987/server/game/world/Location.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.world; /** * Represents a location in the game universe diff --git a/Server/src/main/java/net/simon987/server/game/TileMap.java b/Server/src/main/java/net/simon987/server/game/world/TileMap.java similarity index 98% rename from Server/src/main/java/net/simon987/server/game/TileMap.java rename to Server/src/main/java/net/simon987/server/game/world/TileMap.java index 93d0dd3..da2a2d5 100755 --- a/Server/src/main/java/net/simon987/server/game/TileMap.java +++ b/Server/src/main/java/net/simon987/server/game/world/TileMap.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.world; import net.simon987.server.io.JSONSerialisable; @@ -110,7 +110,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable { } @Override - public JSONObject serialise() { + public JSONObject jsonSerialise() { JSONObject json = new JSONObject(); byte[] terrain = new byte[width * width]; @@ -164,7 +164,6 @@ public class TileMap implements JSONSerialisable, MongoSerializable { ArrayList terrain = (ArrayList) object.get("tiles"); int[][] tiles = new int[size][size]; - for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { tiles[x][y] = terrain.get(x * size + y); diff --git a/Server/src/main/java/net/simon987/server/game/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java similarity index 97% rename from Server/src/main/java/net/simon987/server/game/World.java rename to Server/src/main/java/net/simon987/server/game/world/World.java index 2abbbf9..6027db5 100644 --- a/Server/src/main/java/net/simon987/server/game/World.java +++ b/Server/src/main/java/net/simon987/server/game/world/World.java @@ -1,8 +1,11 @@ -package net.simon987.server.game; +package net.simon987.server.game.world; import net.simon987.server.GameServer; import net.simon987.server.event.GameEvent; import net.simon987.server.event.WorldUpdateEvent; +import net.simon987.server.game.GameUniverse; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Updatable; import net.simon987.server.game.pathfinding.Pathfinder; import net.simon987.server.io.MongoSerializable; import org.bson.Document; @@ -221,11 +224,11 @@ public class World implements MongoSerializable { world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize()); - ArrayList objects = (ArrayList) dbObject.get("objects"); + ArrayList objects = (ArrayList) dbObject.get("objects"); for (Object obj : objects) { - GameObject object = GameObject.deserialize((Document) obj); + GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject((Document) obj); object.setWorld(world); world.addObject(object); diff --git a/Server/src/main/java/net/simon987/server/game/WorldGenerator.java b/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java similarity index 98% rename from Server/src/main/java/net/simon987/server/game/WorldGenerator.java rename to Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java index 0a5b7ec..2f0d74c 100755 --- a/Server/src/main/java/net/simon987/server/game/WorldGenerator.java +++ b/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java @@ -1,4 +1,4 @@ -package net.simon987.server.game; +package net.simon987.server.game.world; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; @@ -43,7 +43,7 @@ public class WorldGenerator { private HashMap centerPointsMap; - WorldGenerator(ServerConfiguration config) { + public WorldGenerator(ServerConfiguration config) { centerPointCountMin = config.getInt("wg_centerPointCountMin"); centerPointCountMax = config.getInt("wg_centerPointCountMax"); diff --git a/Server/src/main/java/net/simon987/server/io/CpuHardwareDeserializer.java b/Server/src/main/java/net/simon987/server/io/CpuHardwareDeserializer.java deleted file mode 100644 index e472eec..0000000 --- a/Server/src/main/java/net/simon987/server/io/CpuHardwareDeserializer.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.simon987.server.io; - -import net.simon987.server.assembly.CpuHardware; -import org.bson.Document; - -public interface CpuHardwareDeserializer { - - - CpuHardware deserializeHardware(Document hwJson); -} diff --git a/Server/src/main/java/net/simon987/server/io/DatabaseManager.java b/Server/src/main/java/net/simon987/server/io/DatabaseManager.java deleted file mode 100755 index 8d57a9a..0000000 --- a/Server/src/main/java/net/simon987/server/io/DatabaseManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.simon987.server.io; - - -import net.simon987.server.ServerConfiguration; -import net.simon987.server.logging.LogManager; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -/** - * Manages the database: this class manages the interactions with the database - */ -public abstract class DatabaseManager { - - /** - * SQL connection url - */ - private String url; - - /** - * SQL username - */ - private String username; - - /** - * SQL password - */ - private String password; - - public DatabaseManager(ServerConfiguration config) { - this.url = config.getString("mysql_url"); - this.username = config.getString("mysql_user"); - this.password = config.getString("mysql_pass"); - } - - protected Connection getConnection() { - try { - return DriverManager.getConnection(url, this.username, password); - } catch (SQLException e) { - LogManager.LOGGER.severe("Couldn't connect to MySQL server state:" + e.getSQLState() + " error:" + e.getErrorCode()); - return null; - } - } -} diff --git a/Server/src/main/java/net/simon987/server/io/FileUtils.java b/Server/src/main/java/net/simon987/server/io/FileUtils.java deleted file mode 100644 index 820dba1..0000000 --- a/Server/src/main/java/net/simon987/server/io/FileUtils.java +++ /dev/null @@ -1,22 +0,0 @@ -package net.simon987.server.io; - -import java.text.SimpleDateFormat; -import java.util.Date; - -public class FileUtils { - - private static final String DATE_FORMAT = "yyyyMMddHHmmss"; - - - /** - * Creates a new stamp containing the current date and time - * - * @return date and time stamp - */ - private static String getDateTimeStamp() { - Date millisToDate = new Date(System.currentTimeMillis()); - SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT); - return f.format(millisToDate); - } - -} diff --git a/Server/src/main/java/net/simon987/server/io/GameObjectDeserializer.java b/Server/src/main/java/net/simon987/server/io/GameObjectDeserializer.java deleted file mode 100644 index bab1a67..0000000 --- a/Server/src/main/java/net/simon987/server/io/GameObjectDeserializer.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.simon987.server.io; - -import net.simon987.server.game.GameObject; -import org.bson.Document; - -public interface GameObjectDeserializer { - - GameObject deserializeObject(Document object); - -} diff --git a/Server/src/main/java/net/simon987/server/io/JSONSerialisable.java b/Server/src/main/java/net/simon987/server/io/JSONSerialisable.java index eff743e..67c157c 100644 --- a/Server/src/main/java/net/simon987/server/io/JSONSerialisable.java +++ b/Server/src/main/java/net/simon987/server/io/JSONSerialisable.java @@ -4,6 +4,6 @@ import org.json.simple.JSONObject; public interface JSONSerialisable { - JSONObject serialise(); + JSONObject jsonSerialise(); } diff --git a/Server/src/main/java/net/simon987/server/plugin/PluginManager.java b/Server/src/main/java/net/simon987/server/plugin/PluginManager.java index 98296c5..74f0b92 100644 --- a/Server/src/main/java/net/simon987/server/plugin/PluginManager.java +++ b/Server/src/main/java/net/simon987/server/plugin/PluginManager.java @@ -1,6 +1,7 @@ package net.simon987.server.plugin; import net.simon987.server.ServerConfiguration; +import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.logging.LogManager; import java.io.File; @@ -21,7 +22,7 @@ public class PluginManager { this.plugins = new ArrayList<>(10); } - public void load(File pluginFile, ServerConfiguration config) { + public void load(File pluginFile, ServerConfiguration config, GameRegistry gameRegistry) { LogManager.LOGGER.info("Loading plugin file " + pluginFile.getName()); @@ -53,7 +54,7 @@ public class PluginManager { plugins.add(plugin); //Init the plugin - plugin.init(config); + plugin.init(config, gameRegistry); } else { LogManager.LOGGER.severe("Couldn't find plugin.properties in " + pluginFile.getName()); diff --git a/Server/src/main/java/net/simon987/server/plugin/ServerPlugin.java b/Server/src/main/java/net/simon987/server/plugin/ServerPlugin.java index 04b22a5..f7622e2 100644 --- a/Server/src/main/java/net/simon987/server/plugin/ServerPlugin.java +++ b/Server/src/main/java/net/simon987/server/plugin/ServerPlugin.java @@ -2,6 +2,7 @@ package net.simon987.server.plugin; import net.simon987.server.ServerConfiguration; import net.simon987.server.event.GameEventListener; +import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.io.JSONSerialisable; import org.json.simple.JSONObject; @@ -27,7 +28,7 @@ public abstract class ServerPlugin implements JSONSerialisable { /** * Called when the plugin is loaded */ - public abstract void init(ServerConfiguration config); + public abstract void init(ServerConfiguration config, GameRegistry gameRegistry); public String getName() { return name; @@ -50,7 +51,7 @@ public abstract class ServerPlugin implements JSONSerialisable { } @Override - public JSONObject serialise() { + public JSONObject jsonSerialise() { JSONObject json = new JSONObject(); diff --git a/Server/src/main/java/net/simon987/server/user/User.java b/Server/src/main/java/net/simon987/server/user/User.java index b263731..b9e49dd 100755 --- a/Server/src/main/java/net/simon987/server/user/User.java +++ b/Server/src/main/java/net/simon987/server/user/User.java @@ -5,7 +5,7 @@ import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.exception.CancelledException; import net.simon987.server.event.GameEvent; import net.simon987.server.event.UserCreationEvent; -import net.simon987.server.game.ControllableUnit; +import net.simon987.server.game.objects.ControllableUnit; import net.simon987.server.io.MongoSerializable; import org.bson.Document; diff --git a/Server/src/main/java/net/simon987/server/websocket/ObjectsRequestHandler.java b/Server/src/main/java/net/simon987/server/websocket/ObjectsRequestHandler.java index 9cd5451..bdd2f10 100644 --- a/Server/src/main/java/net/simon987/server/websocket/ObjectsRequestHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/ObjectsRequestHandler.java @@ -1,8 +1,8 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; -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; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -37,7 +37,7 @@ public class ObjectsRequestHandler implements MessageHandler { for (GameObject object : world.getGameObjects()) { - objects.add(object.serialise()); + objects.add(object.jsonSerialise()); } response.put("t", "object"); diff --git a/Server/src/main/java/net/simon987/server/websocket/SocketServer.java b/Server/src/main/java/net/simon987/server/websocket/SocketServer.java index 698f57a..4abf1e0 100644 --- a/Server/src/main/java/net/simon987/server/websocket/SocketServer.java +++ b/Server/src/main/java/net/simon987/server/websocket/SocketServer.java @@ -1,7 +1,7 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; -import net.simon987.server.game.ControllableUnit; +import net.simon987.server.game.objects.ControllableUnit; import net.simon987.server.logging.LogManager; import net.simon987.server.user.User; import org.eclipse.jetty.websocket.api.Session; diff --git a/Server/src/main/java/net/simon987/server/websocket/SocketServerDatabase.java b/Server/src/main/java/net/simon987/server/websocket/SocketServerDatabase.java deleted file mode 100644 index e675e25..0000000 --- a/Server/src/main/java/net/simon987/server/websocket/SocketServerDatabase.java +++ /dev/null @@ -1,109 +0,0 @@ -package net.simon987.server.websocket; - -import net.simon987.server.ServerConfiguration; -import net.simon987.server.io.DatabaseManager; -import net.simon987.server.logging.LogManager; - -import java.sql.*; - -class SocketServerDatabase extends DatabaseManager { - - public SocketServerDatabase(ServerConfiguration config) { - super(config); - } - - String validateAuthToken(String token) { - - Connection connection = null; - try { - connection = getConnection(); - - PreparedStatement p = connection.prepareStatement("SELECT username FROM mar_user WHERE authToken=?"); - p.setString(1, token); - - ResultSet rs = p.executeQuery(); - - if (rs.next()) { - - return rs.getString("username"); - - } else { - return null; - } - - } catch (SQLException e) { - LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage()); - - } finally { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - return null; - } - - boolean isModerator(String username) { - - Connection connection = null; - try { - connection = getConnection(); - - PreparedStatement p = connection.prepareStatement("SELECT moderator FROM mar_user WHERE username=?"); - p.setString(1, username); - - ResultSet rs = p.executeQuery(); - - return rs.next() && rs.getBoolean("moderator"); - - } catch (SQLException e) { - LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage()); - - } finally { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - return false; - } - - byte[] getFloppy(String username) { - - Connection connection = null; - try { - connection = getConnection(); - - PreparedStatement p = connection.prepareStatement("SELECT floppyData FROM mar_user WHERE username=?"); - p.setString(1, username); - - ResultSet rs = p.executeQuery(); - - if (rs.next()) { - Blob blob = rs.getBlob("floppyData"); - - if (blob != null) { - return blob.getBytes(1, (int) blob.length() - 1); - } - } - - } catch (SQLException e) { - LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage()); - - } finally { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - return null; - - } - - -} diff --git a/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java b/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java index 42ce475..92c7750 100644 --- a/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java @@ -1,7 +1,7 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; -import net.simon987.server.game.World; +import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import org.json.simple.JSONArray; import org.json.simple.JSONObject; diff --git a/Server/src/main/java/net/simon987/server/websocket/UserInfoRequestHandler.java b/Server/src/main/java/net/simon987/server/websocket/UserInfoRequestHandler.java index 540a930..2eb1bf0 100644 --- a/Server/src/main/java/net/simon987/server/websocket/UserInfoRequestHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/UserInfoRequestHandler.java @@ -1,7 +1,7 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; -import net.simon987.server.game.GameObject; +import net.simon987.server.game.objects.GameObject; import net.simon987.server.logging.LogManager; import org.json.simple.JSONObject; diff --git a/Server/src/main/resources/static/js/mar.js b/Server/src/main/resources/static/js/mar.js index 6ce3431..40d0235 100644 --- a/Server/src/main/resources/static/js/mar.js +++ b/Server/src/main/resources/static/js/mar.js @@ -785,15 +785,15 @@ var GameClient = (function () { }()); var ObjectType; (function (ObjectType) { - ObjectType[ObjectType["CUBOT"] = 1] = "CUBOT"; - ObjectType[ObjectType["BIOMASS"] = 2] = "BIOMASS"; - ObjectType[ObjectType["HARVESTER_NPC"] = 10] = "HARVESTER_NPC"; - ObjectType[ObjectType["FACTORY"] = 3] = "FACTORY"; - ObjectType[ObjectType["RADIO_TOWER"] = 4] = "RADIO_TOWER"; - ObjectType[ObjectType["VAULT_DOOR"] = 5] = "VAULT_DOOR"; - ObjectType[ObjectType["OBSTACLE"] = 6] = "OBSTACLE"; - ObjectType[ObjectType["ELECTRIC_BOX"] = 7] = "ELECTRIC_BOX"; - ObjectType[ObjectType["PORTAL"] = 8] = "PORTAL"; + ObjectType["CUBOT"] = "net.simon987.cubotplugin.Cubot"; + ObjectType["BIOMASS"] = "net.simon987.biomassplugin.BiomassBlob"; + ObjectType["HARVESTER_NPC"] = "net.simon987.npcplugin.HarvesterNPC"; + ObjectType["FACTORY"] = "net.simon987.npcplugin.Factory"; + ObjectType["RADIO_TOWER"] = "net.simon987.npcplugin.RadioTower"; + ObjectType["VAULT_DOOR"] = "net.simon987.npcplugin.VaultDoor"; + ObjectType["OBSTACLE"] = "net.simon987.npcplugin.Obstacle"; + ObjectType["ELECTRIC_BOX"] = "net.simon987.npcplugin.ElectricBox"; + ObjectType["PORTAL"] = "net.simon987.npcplugin.Portal"; })(ObjectType || (ObjectType = {})); var ItemType; (function (ItemType) { diff --git a/Server/src/main/typescript/GameObject.ts b/Server/src/main/typescript/GameObject.ts index 80c6d7b..22ab0fb 100644 --- a/Server/src/main/typescript/GameObject.ts +++ b/Server/src/main/typescript/GameObject.ts @@ -1,13 +1,13 @@ enum ObjectType { - CUBOT = 1, - BIOMASS = 2, - HARVESTER_NPC = 10, - FACTORY = 3, - RADIO_TOWER = 4, - VAULT_DOOR = 5, - OBSTACLE = 6, - ELECTRIC_BOX = 7, - PORTAL = 8 + CUBOT = "net.simon987.cubotplugin.Cubot", + BIOMASS = "net.simon987.biomassplugin.BiomassBlob", + HARVESTER_NPC = "net.simon987.npcplugin.HarvesterNPC", + FACTORY = "net.simon987.npcplugin.Factory", + RADIO_TOWER = "net.simon987.npcplugin.RadioTower", + VAULT_DOOR = "net.simon987.npcplugin.VaultDoor", + OBSTACLE = "net.simon987.npcplugin.Obstacle", + ELECTRIC_BOX = "net.simon987.npcplugin.ElectricBox", + PORTAL = "net.simon987.npcplugin.Portal" } enum ItemType {