Refactor: changed the way game objects and cpu hardware are saved/loaded from the database #151

This commit is contained in:
simon
2018-05-12 15:32:41 -04:00
parent 8d029cf621
commit 4cd58c86a5
96 changed files with 628 additions and 1221 deletions

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}