Added an untested way of saving and loading boosters
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user