Added an untested way of saving and loading boosters
This commit is contained in:
parent
0408b34c1d
commit
6b95f89fd6
|
|
@ -7,6 +7,11 @@ dependencies {
|
|||
compileOnly("net.kyori:adventure-text-minimessage:4.2.0-SNAPSHOT") // Minimessage
|
||||
compileOnly("org.spongepowered:configurate-yaml:4.1.2") // Configurate
|
||||
compileOnly("net.luckperms:api:5.3") // Luckperms
|
||||
|
||||
//Jackson (json)
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-core:2.8.8")
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-annotations:2.8.8")
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-databind:2.8.8")
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
package com.alttd.boosterapi.config;
|
||||
|
||||
import com.alttd.boosterapi.Booster;
|
||||
import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class BoosterStorage {
|
||||
|
||||
private File CONFIG_FILE;
|
||||
private final Map<UUID, Booster> boosters;
|
||||
protected BoosterStorage() {
|
||||
init();
|
||||
boosters = loadBoosters();
|
||||
}
|
||||
private void init() {
|
||||
File CONFIG_PATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "Boosters");
|
||||
CONFIG_FILE = new File(CONFIG_PATH, "storage.json");
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
}
|
||||
|
||||
public Map<UUID, Booster> getBoosters() {
|
||||
return boosters;
|
||||
}
|
||||
|
||||
public Map<UUID, Booster> loadBoosters() {
|
||||
Map<UUID, Booster> boosters = new HashMap<>();
|
||||
|
||||
try {
|
||||
JsonParser parser = new JsonFactory().createParser(CONFIG_FILE);
|
||||
Booster booster = loadBooster(parser);
|
||||
boosters.put(booster.getUUID(), booster);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return boosters;
|
||||
}
|
||||
|
||||
public abstract Booster loadBooster(JsonParser parser) throws IOException;
|
||||
|
||||
public void saveBoosters(Collection<Booster> boosters) {
|
||||
try {
|
||||
JsonGenerator generator = new JsonFactory().createGenerator(CONFIG_FILE, JsonEncoding.UTF8);
|
||||
for (Booster booster : boosters) {
|
||||
saveBooster(booster, generator);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
private void saveBooster(Booster booster, JsonGenerator generator) throws IOException {
|
||||
generator.writeStartObject();
|
||||
|
||||
generator.writeStringField("uuid", booster.getUUID().toString());
|
||||
generator.writeStringField("activator", booster.getActivator());
|
||||
generator.writeStringField("type", booster.getType().getBoosterName());
|
||||
generator.writeNumberField("startingTime", booster.getStartingTime());
|
||||
generator.writeNumberField("duration", booster.getDuration());
|
||||
generator.writeNumberField("multiplier", booster.getMultiplier());
|
||||
generator.writeBooleanField("active", booster.isActive());
|
||||
generator.writeBooleanField("finished", booster.finished());
|
||||
|
||||
generator.writeEndObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,6 +16,11 @@ dependencies {
|
|||
compileOnly("com.sk89q.worldguard:worldguard-core:7.0.4") {
|
||||
exclude("com.google.code.findbugs")
|
||||
}
|
||||
|
||||
//Jackson (json)
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-core:2.8.8")
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-annotations:2.8.8")
|
||||
compileOnly("com.fasterxml.jackson.core:jackson-databind:2.8.8")
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.boosters.data;
|
||||
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.boosterapi.util.ALogger;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -23,13 +24,23 @@ public class Booster implements com.alttd.boosterapi.Booster {
|
|||
this.multiplier = multiplier;
|
||||
this.active = false;
|
||||
this.finished = false;
|
||||
saveBooster();
|
||||
}
|
||||
|
||||
public Booster(BoosterType type, String playerName, long duration, double multiplier) {
|
||||
this(UUID.randomUUID(), type, playerName, duration, multiplier);
|
||||
}
|
||||
|
||||
public Booster(UUID uuid, String activator, BoosterType boosterType, long startingTime, long duration, double multiplier, boolean active, boolean finished) {
|
||||
this.uuid = uuid;
|
||||
this.activator = activator;
|
||||
this.boosterType = boosterType;
|
||||
this.startingTime = startingTime;
|
||||
this.duration = duration;
|
||||
this.multiplier = multiplier;
|
||||
this.active = active;
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
|
|
@ -105,17 +116,19 @@ public class Booster implements com.alttd.boosterapi.Booster {
|
|||
|
||||
@Override
|
||||
public void stopBooster() {
|
||||
|
||||
setDuration(getTimeRemaining());
|
||||
setActive(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveBooster() {
|
||||
|
||||
ALogger.error("Tried saving booster from server instead of proxy, only proxy should handle saving boosters");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
|
||||
finished = true;
|
||||
stopBooster();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
package com.alttd.boosters.storage;
|
||||
|
||||
import com.alttd.boosterapi.Booster;
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.boosterapi.config.BoosterStorage;
|
||||
import com.alttd.boosterapi.util.ALogger;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ServerBoosterStorage extends BoosterStorage {
|
||||
|
||||
private static ServerBoosterStorage serverBoosterStorage = null;
|
||||
|
||||
public static ServerBoosterStorage getServerBoosterStorage() {
|
||||
if (serverBoosterStorage == null)
|
||||
serverBoosterStorage = new ServerBoosterStorage();
|
||||
return serverBoosterStorage;
|
||||
}
|
||||
|
||||
private ServerBoosterStorage() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Booster loadBooster(JsonParser parser) throws IOException {
|
||||
JsonToken jsonToken = parser.nextToken();
|
||||
if (!jsonToken.isStructStart())
|
||||
return error("Didn't find struct start");
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"uuid".equals(parser.getCurrentName()))
|
||||
return error("Didn't find uuid at expected location");
|
||||
UUID uuid = UUID.fromString(parser.getValueAsString());
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"activator".equals(parser.getCurrentName()))
|
||||
return error("Didn't find activator at expected location");
|
||||
String activator = parser.getValueAsString();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"type".equals(parser.getCurrentName()))
|
||||
return error("Didn't find type at expected location");
|
||||
BoosterType boosterType = BoosterType.getByName(parser.getValueAsString());
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"startingTime".equals(parser.getCurrentName()))
|
||||
return error("Didn't find startingTime at expected location");
|
||||
long startingTime = parser.getValueAsLong();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"duration".equals(parser.getCurrentName()))
|
||||
return error("Didn't find duration at expected location");
|
||||
long duration = parser.getValueAsLong();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"multiplier".equals(parser.getCurrentName()))
|
||||
return error("Didn't find multiplier at expected location");
|
||||
double multiplier = parser.getValueAsDouble();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"active".equals(parser.getCurrentName()))
|
||||
return error("Didn't find active at expected location");
|
||||
boolean active = parser.getValueAsBoolean();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"finished".equals(parser.getCurrentName()))
|
||||
return error("Didn't find finished at expected location");
|
||||
boolean finished = parser.getValueAsBoolean();
|
||||
return new com.alttd.boosters.data.Booster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
|
||||
}
|
||||
|
||||
private static Booster error(String error) {
|
||||
ALogger.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,10 @@ package com.alttd.vboosters.data;
|
|||
|
||||
import com.alttd.boosterapi.Booster;
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.vboosters.storage.VelocityBoosterStorage;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VelocityBooster implements Booster {
|
||||
|
|
@ -31,6 +34,18 @@ public class VelocityBooster implements Booster {
|
|||
this(UUID.randomUUID(), type, playerName, duration, multiplier);
|
||||
}
|
||||
|
||||
public VelocityBooster(UUID uuid, String activator, BoosterType boosterType, long startingTime,
|
||||
long duration, double multiplier, boolean active, boolean finished) {
|
||||
this.uuid = uuid;
|
||||
this.activator = activator;
|
||||
this.boosterType = boosterType;
|
||||
this.startingTime = startingTime;
|
||||
this.duration = duration;
|
||||
this.multiplier = multiplier;
|
||||
this.active = active;
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
|
|
@ -105,7 +120,7 @@ public class VelocityBooster implements Booster {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void stopBooster() {
|
||||
public void stopBooster() { //TODO stop it on the servers as well
|
||||
setDuration(getTimeRemaining());
|
||||
setActive(false);
|
||||
saveBooster();
|
||||
|
|
@ -113,10 +128,12 @@ public class VelocityBooster implements Booster {
|
|||
|
||||
@Override
|
||||
public void saveBooster() {
|
||||
// logic to save to yaml or to db
|
||||
VelocityBoosterStorage vbs = VelocityBoosterStorage.getVelocityBoosterStorage();
|
||||
vbs.getBoosters().put(uuid, this);
|
||||
vbs.saveBoosters(vbs.getBoosters().values());
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
public void finish() { //TODO finish it on the servers as well
|
||||
finished = true;
|
||||
stopBooster();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
package com.alttd.vboosters.storage;
|
||||
|
||||
import com.alttd.boosterapi.Booster;
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.boosterapi.config.BoosterStorage;
|
||||
import com.alttd.boosterapi.util.ALogger;
|
||||
import com.alttd.vboosters.data.VelocityBooster;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VelocityBoosterStorage extends BoosterStorage {
|
||||
|
||||
private static VelocityBoosterStorage velocityBoosterStorage = null;
|
||||
|
||||
public static VelocityBoosterStorage getVelocityBoosterStorage() {
|
||||
if (velocityBoosterStorage == null)
|
||||
velocityBoosterStorage = new VelocityBoosterStorage();
|
||||
return velocityBoosterStorage;
|
||||
}
|
||||
|
||||
private VelocityBoosterStorage() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Booster loadBooster(JsonParser parser) throws IOException {
|
||||
JsonToken jsonToken = parser.nextToken();
|
||||
if (!jsonToken.isStructStart())
|
||||
return error("Didn't find struct start");
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"uuid".equals(parser.getCurrentName()))
|
||||
return error("Didn't find uuid at expected location");
|
||||
UUID uuid = UUID.fromString(parser.getValueAsString());
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"activator".equals(parser.getCurrentName()))
|
||||
return error("Didn't find activator at expected location");
|
||||
String activator = parser.getValueAsString();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"type".equals(parser.getCurrentName()))
|
||||
return error("Didn't find type at expected location");
|
||||
BoosterType boosterType = BoosterType.getByName(parser.getValueAsString());
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"startingTime".equals(parser.getCurrentName()))
|
||||
return error("Didn't find startingTime at expected location");
|
||||
long startingTime = parser.getValueAsLong();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"duration".equals(parser.getCurrentName()))
|
||||
return error("Didn't find duration at expected location");
|
||||
long duration = parser.getValueAsLong();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"multiplier".equals(parser.getCurrentName()))
|
||||
return error("Didn't find multiplier at expected location");
|
||||
double multiplier = parser.getValueAsDouble();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"active".equals(parser.getCurrentName()))
|
||||
return error("Didn't find active at expected location");
|
||||
boolean active = parser.getValueAsBoolean();
|
||||
|
||||
jsonToken = parser.nextToken();
|
||||
if (jsonToken != JsonToken.FIELD_NAME || !"finished".equals(parser.getCurrentName()))
|
||||
return error("Didn't find finished at expected location");
|
||||
boolean finished = parser.getValueAsBoolean();
|
||||
return new VelocityBooster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
|
||||
}
|
||||
|
||||
private static Booster error(String error) {
|
||||
ALogger.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user