Made boosters load and save properly
This commit is contained in:
parent
4818f0b509
commit
7fa2370498
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.boosterapi;
|
package com.alttd.boosterapi;
|
||||||
|
|
||||||
import com.alttd.boosterapi.config.Config;
|
import com.alttd.boosterapi.config.Config;
|
||||||
|
import com.alttd.boosterapi.config.ServerConfig;
|
||||||
import com.alttd.boosterapi.database.Database;
|
import com.alttd.boosterapi.database.Database;
|
||||||
import net.luckperms.api.LuckPerms;
|
import net.luckperms.api.LuckPerms;
|
||||||
import net.luckperms.api.LuckPermsProvider;
|
import net.luckperms.api.LuckPermsProvider;
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ public abstract class BoosterStorage {
|
||||||
private File CONFIG_FILE;
|
private File CONFIG_FILE;
|
||||||
private final Map<UUID, Booster> boosters;
|
private final Map<UUID, Booster> boosters;
|
||||||
protected BoosterStorage() {
|
protected BoosterStorage() {
|
||||||
|
ALogger.info("Loading boosters...");
|
||||||
init();
|
init();
|
||||||
boosters = loadBoosters();
|
boosters = loadBoosters();
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +44,7 @@ public abstract class BoosterStorage {
|
||||||
|
|
||||||
public void reload() {
|
public void reload() {
|
||||||
boosters.clear();
|
boosters.clear();
|
||||||
loadBoosters();
|
boosters.putAll(loadBoosters());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<UUID, Booster> getBoosters() {
|
public Map<UUID, Booster> getBoosters() {
|
||||||
|
|
@ -55,13 +56,24 @@ public abstract class BoosterStorage {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JsonParser parser = new JsonFactory().createParser(CONFIG_FILE);
|
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;
|
return boosters;
|
||||||
while (parser.hasCurrentToken()) {
|
}
|
||||||
|
parser.nextToken();
|
||||||
|
while (parser.currentToken() != null && parser.currentToken().isStructStart()) {
|
||||||
Booster booster = loadBooster(parser);
|
Booster booster = loadBooster(parser);
|
||||||
|
if (Config.DEBUG)
|
||||||
|
ALogger.info("Loading booster [" + booster.getType() + "] activated by [" + booster.getActivator()+ "].");
|
||||||
boosters.put(booster.getUUID(), booster);
|
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.nextToken();
|
||||||
}
|
}
|
||||||
|
parser.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
@ -77,6 +89,7 @@ public abstract class BoosterStorage {
|
||||||
for (Booster booster : boosters) {
|
for (Booster booster : boosters) {
|
||||||
saveBooster(booster, generator);
|
saveBooster(booster, generator);
|
||||||
}
|
}
|
||||||
|
generator.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -200,4 +200,9 @@ public final class Config {
|
||||||
DEMOTE_MESSAGE = getString("messages.demote", DEMOTE_MESSAGE);
|
DEMOTE_MESSAGE = getString("messages.demote", DEMOTE_MESSAGE);
|
||||||
PROMOTE_MESSAGE = getString("messages.promote", PROMOTE_MESSAGE);
|
PROMOTE_MESSAGE = getString("messages.promote", PROMOTE_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean DEBUG = false;
|
||||||
|
private static void loadSettings() {
|
||||||
|
DEBUG = getBoolean("settings.debug", DEBUG);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ public class ServerBoosterStorage extends BoosterStorage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Booster loadBooster(JsonParser parser) throws IOException {
|
public Booster loadBooster(JsonParser parser) throws IOException {
|
||||||
JsonToken jsonToken = parser.nextToken();
|
JsonToken jsonToken = parser.currentToken();
|
||||||
if (!jsonToken.isStructStart())
|
if (!jsonToken.isStructStart())
|
||||||
return error("Didn't find struct start");
|
return error("Didn't find struct start");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package com.alttd.vboosters;
|
||||||
|
|
||||||
import com.alttd.boosterapi.BoosterAPI;
|
import com.alttd.boosterapi.BoosterAPI;
|
||||||
import com.alttd.boosterapi.BoosterImplementation;
|
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.boosterapi.util.ALogger;
|
||||||
import com.alttd.proxydiscordlink.DiscordLink;
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
import com.alttd.proxydiscordlink.bot.api.DiscordSendMessage;
|
import com.alttd.proxydiscordlink.bot.api.DiscordSendMessage;
|
||||||
|
|
@ -55,6 +57,7 @@ public class VelocityBoosters {
|
||||||
server.getEventManager().register(this, new PluginMessageListener(channelIdentifier));
|
server.getEventManager().register(this, new PluginMessageListener(channelIdentifier));
|
||||||
|
|
||||||
loadCommands();
|
loadCommands();
|
||||||
|
reloadConfig();
|
||||||
VelocityBoosterStorage.getVelocityBoosterStorage(); //this loads the boosters in
|
VelocityBoosterStorage.getVelocityBoosterStorage(); //this loads the boosters in
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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")))
|
"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))
|
.then(RequiredArgumentBuilder.<CommandSource, Double>argument("multiplier", DoubleArgumentType.doubleArg(0, 10))
|
||||||
.suggests((context, builder) -> buildRemainingString(builder, List.of("0.5", "1", "1.5", "2")))
|
.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);
|
String username = context.getArgument("username", String.class);
|
||||||
BoosterType boosterType = BoosterType.getByName(context.getArgument("booster", String.class));
|
BoosterType boosterType = BoosterType.getByName(context.getArgument("booster", String.class));
|
||||||
long duration = context.getArgument("time", Integer.class) * 60;
|
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
|
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>"));
|
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));
|
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().getProxy().sendMessage(MiniMessage.markdown().parse(mcMessage));
|
||||||
VelocityBoosters.getPlugin().getLogger().info(mcMessage);
|
VelocityBoosters.getPlugin().getLogger().info(mcMessage);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
||||||
|
|
@ -142,8 +142,8 @@ public class BoosterManager {
|
||||||
for (Booster b : queuedBoosters) {
|
for (Booster b : queuedBoosters) {
|
||||||
b.saveBooster();
|
b.saveBooster();
|
||||||
}
|
}
|
||||||
activeBoosters = null;
|
activeBoosters.clear();
|
||||||
queuedBoosters = null;
|
queuedBoosters.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.alttd.vboosters.storage;
|
||||||
import com.alttd.boosterapi.Booster;
|
import com.alttd.boosterapi.Booster;
|
||||||
import com.alttd.boosterapi.BoosterType;
|
import com.alttd.boosterapi.BoosterType;
|
||||||
import com.alttd.boosterapi.config.BoosterStorage;
|
import com.alttd.boosterapi.config.BoosterStorage;
|
||||||
|
import com.alttd.boosterapi.config.Config;
|
||||||
import com.alttd.boosterapi.util.ALogger;
|
import com.alttd.boosterapi.util.ALogger;
|
||||||
import com.alttd.vboosters.data.VelocityBooster;
|
import com.alttd.vboosters.data.VelocityBooster;
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
|
@ -27,48 +28,56 @@ public class VelocityBoosterStorage extends BoosterStorage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Booster loadBooster(JsonParser parser) throws IOException {
|
public Booster loadBooster(JsonParser parser) throws IOException {
|
||||||
JsonToken jsonToken = parser.nextToken();
|
JsonToken jsonToken = parser.getCurrentToken();
|
||||||
if (!jsonToken.isStructStart())
|
if (!jsonToken.isStructStart())
|
||||||
return error("Didn't find struct start");
|
return error("Didn't find struct start");
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"uuid".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"uuid".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find uuid at expected location");
|
return error("Didn't find uuid at expected location");
|
||||||
|
parser.nextValue();
|
||||||
UUID uuid = UUID.fromString(parser.getValueAsString());
|
UUID uuid = UUID.fromString(parser.getValueAsString());
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"activator".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"activator".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find activator at expected location");
|
return error("Didn't find activator at expected location");
|
||||||
|
parser.nextValue();
|
||||||
String activator = parser.getValueAsString();
|
String activator = parser.getValueAsString();
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"type".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"type".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find type at expected location");
|
return error("Didn't find type at expected location");
|
||||||
|
parser.nextValue();
|
||||||
BoosterType boosterType = BoosterType.getByName(parser.getValueAsString());
|
BoosterType boosterType = BoosterType.getByName(parser.getValueAsString());
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"startingTime".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"startingTime".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find startingTime at expected location");
|
return error("Didn't find startingTime at expected location");
|
||||||
|
parser.nextValue();
|
||||||
long startingTime = parser.getValueAsLong();
|
long startingTime = parser.getValueAsLong();
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"duration".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"duration".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find duration at expected location");
|
return error("Didn't find duration at expected location");
|
||||||
|
parser.nextValue();
|
||||||
long duration = parser.getValueAsLong();
|
long duration = parser.getValueAsLong();
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"multiplier".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"multiplier".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find multiplier at expected location");
|
return error("Didn't find multiplier at expected location");
|
||||||
|
parser.nextValue();
|
||||||
double multiplier = parser.getValueAsDouble();
|
double multiplier = parser.getValueAsDouble();
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"active".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"active".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find active at expected location");
|
return error("Didn't find active at expected location");
|
||||||
|
parser.nextValue();
|
||||||
boolean active = parser.getValueAsBoolean();
|
boolean active = parser.getValueAsBoolean();
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
if (jsonToken != JsonToken.FIELD_NAME || !"finished".equals(parser.getCurrentName()))
|
if (jsonToken != JsonToken.FIELD_NAME || !"finished".equals(parser.getCurrentName()))
|
||||||
return error("Didn't find finished at expected location");
|
return error("Didn't find finished at expected location");
|
||||||
|
parser.nextValue();
|
||||||
boolean finished = parser.getValueAsBoolean();
|
boolean finished = parser.getValueAsBoolean();
|
||||||
return new VelocityBooster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
|
return new VelocityBooster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user