Made boosters load and save properly

This commit is contained in:
Teriuihi 2022-08-05 22:17:53 +02:00
parent 4818f0b509
commit 7fa2370498
8 changed files with 40 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package com.alttd.boosterapi;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosterapi.config.ServerConfig;
import com.alttd.boosterapi.database.Database;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;

View File

@ -18,6 +18,7 @@ public abstract class BoosterStorage {
private File CONFIG_FILE;
private final Map<UUID, Booster> boosters;
protected BoosterStorage() {
ALogger.info("Loading boosters...");
init();
boosters = loadBoosters();
}
@ -43,7 +44,7 @@ public abstract class BoosterStorage {
public void reload() {
boosters.clear();
loadBoosters();
boosters.putAll(loadBoosters());
}
public Map<UUID, Booster> getBoosters() {
@ -55,13 +56,24 @@ public abstract class BoosterStorage {
try {
JsonParser parser = new JsonFactory().createParser(CONFIG_FILE);
if (parser == null)
if (parser == null) {
ALogger.warn("Unable to load in boosters from storage file.");
return boosters;
while (parser.hasCurrentToken()) {
}
parser.nextToken();
while (parser.currentToken() != null && parser.currentToken().isStructStart()) {
Booster booster = loadBooster(parser);
if (Config.DEBUG)
ALogger.info("Loading booster [" + booster.getType() + "] activated by [" + booster.getActivator()+ "].");
boosters.put(booster.getUUID(), booster);
if (parser.nextToken() != null && !parser.currentToken().isStructEnd()) {
ALogger.warn("Last loaded booster had more data than expected, skipping it...");
while (!parser.nextToken().isStructEnd())
;
}
parser.nextToken();
}
parser.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -77,6 +89,7 @@ public abstract class BoosterStorage {
for (Booster booster : boosters) {
saveBooster(booster, generator);
}
generator.close();
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -200,4 +200,9 @@ public final class Config {
DEMOTE_MESSAGE = getString("messages.demote", DEMOTE_MESSAGE);
PROMOTE_MESSAGE = getString("messages.promote", PROMOTE_MESSAGE);
}
public static boolean DEBUG = false;
private static void loadSettings() {
DEBUG = getBoolean("settings.debug", DEBUG);
}
}

View File

@ -26,7 +26,7 @@ public class ServerBoosterStorage extends BoosterStorage {
@Override
public Booster loadBooster(JsonParser parser) throws IOException {
JsonToken jsonToken = parser.nextToken();
JsonToken jsonToken = parser.currentToken();
if (!jsonToken.isStructStart())
return error("Didn't find struct start");

View File

@ -2,6 +2,8 @@ package com.alttd.vboosters;
import com.alttd.boosterapi.BoosterAPI;
import com.alttd.boosterapi.BoosterImplementation;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosterapi.config.ServerConfig;
import com.alttd.boosterapi.util.ALogger;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.bot.api.DiscordSendMessage;
@ -55,6 +57,7 @@ public class VelocityBoosters {
server.getEventManager().register(this, new PluginMessageListener(channelIdentifier));
loadCommands();
reloadConfig();
VelocityBoosterStorage.getVelocityBoosterStorage(); //this loads the boosters in
}

View File

@ -64,7 +64,7 @@ public class BoosterCommand {
"420", "480", "540", "600", "660", "720", "780", "840", "900", "960", "1020", "1080", "1140", "1200", "1260", "1320", "1380", "1440")))
.then(RequiredArgumentBuilder.<CommandSource, Double>argument("multiplier", DoubleArgumentType.doubleArg(0, 10))
.suggests((context, builder) -> buildRemainingString(builder, List.of("0.5", "1", "1.5", "2")))
.executes(context -> {
.executes(context -> { //TODO make messages configurable
String username = context.getArgument("username", String.class);
BoosterType boosterType = BoosterType.getByName(context.getArgument("booster", String.class));
long duration = context.getArgument("time", Integer.class) * 60;
@ -74,6 +74,7 @@ public class BoosterCommand {
String msg = "[" + username + "] activated booster of type [" + Utils.capitalize(boosterType.getBoosterName()) + "] until %date%."; //TODO check if there was a booster active already and change message based on that
DiscordSendMessage.sendEmbed(Config.BOOST_ANNOUNCE_CHANNEL, "Booster Activated", msg.replaceAll("%date%", "<t:" + expiryTime + ":f>"));
String mcMessage = msg.replaceAll("%date%", DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(expiryTime));
mcMessage += " [UTC]";
VelocityBoosters.getPlugin().getProxy().sendMessage(MiniMessage.markdown().parse(mcMessage));
VelocityBoosters.getPlugin().getLogger().info(mcMessage);
return 1;

View File

@ -142,8 +142,8 @@ public class BoosterManager {
for (Booster b : queuedBoosters) {
b.saveBooster();
}
activeBoosters = null;
queuedBoosters = null;
activeBoosters.clear();
queuedBoosters.clear();
}
}

View File

@ -3,6 +3,7 @@ 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.config.Config;
import com.alttd.boosterapi.util.ALogger;
import com.alttd.vboosters.data.VelocityBooster;
import com.fasterxml.jackson.core.JsonParser;
@ -27,48 +28,56 @@ public class VelocityBoosterStorage extends BoosterStorage {
@Override
public Booster loadBooster(JsonParser parser) throws IOException {
JsonToken jsonToken = parser.nextToken();
JsonToken jsonToken = parser.getCurrentToken();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
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");
parser.nextValue();
boolean finished = parser.getValueAsBoolean();
return new VelocityBooster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
}