worked on storage but it still doesn't work

This commit is contained in:
Teriuihi 2022-08-04 03:52:02 +02:00
parent 8b4178122e
commit 4818f0b509
3 changed files with 34 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package com.alttd.boosterapi.config;
import com.alttd.boosterapi.Booster;
import com.alttd.boosterapi.util.ALogger;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
@ -22,7 +23,19 @@ public abstract class BoosterStorage {
}
private void init() {
File CONFIG_PATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "Boosters");
if (!CONFIG_PATH.exists()) {
if (!CONFIG_PATH.mkdir())
ALogger.error("Unable to create json storage directory");
}
CONFIG_FILE = new File(CONFIG_PATH, "storage.json");
if (!CONFIG_FILE.exists()) {
try {
if (!CONFIG_FILE.createNewFile())
ALogger.error("Unable to create json storeage file");
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
@ -42,8 +55,13 @@ public abstract class BoosterStorage {
try {
JsonParser parser = new JsonFactory().createParser(CONFIG_FILE);
Booster booster = loadBooster(parser);
boosters.put(booster.getUUID(), booster);
if (parser == null)
return boosters;
while (parser.hasCurrentToken()) {
Booster booster = loadBooster(parser);
boosters.put(booster.getUUID(), booster);
parser.nextToken();
}
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -20,10 +20,13 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.GameProfile;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@ -57,17 +60,22 @@ public class BoosterCommand {
.map(BoosterType::getBoosterName)
.collect(Collectors.toList())))
.then(RequiredArgumentBuilder.<CommandSource, Integer>argument("time", IntegerArgumentType.integer(0, 525960))
.suggests((context, builder) -> buildRemainingString(builder, List.of("60", "120", "180", "240", "300", "360",
"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 -> {
String username = context.getArgument("username", String.class);
BoosterType boosterType = BoosterType.getByName(context.getArgument("booster", String.class));
long duration = context.getArgument("time", Integer.class) * 60;
double multiplier = context.getArgument("multiplier", Double.class);
VelocityBoosters.getPlugin().getBoosterManager().addBooster(new VelocityBooster(boosterType, username, duration, multiplier));
String msg = "[" + username + "] activated booster of type [" + Utils.capitalize(boosterType.getBoosterName()) + "] until <t:"
+ (new Date().getTime() + duration) + ":f>."; //TODO check if there was a booster active already and change message based on that
DiscordSendMessage.sendEmbed(Config.BOOST_ANNOUNCE_CHANNEL, "Booster Activated", msg);
VelocityBoosters.getPlugin().getLogger().info(msg);
long expiryTime = new Date().getTime() + duration;
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));
VelocityBoosters.getPlugin().getProxy().sendMessage(MiniMessage.markdown().parse(mcMessage));
VelocityBoosters.getPlugin().getLogger().info(mcMessage);
return 1;
})
)

View File

@ -6,6 +6,7 @@ import com.alttd.vboosters.storage.VelocityBoosterStorage;
import java.util.Collection;
import java.util.List;
import java.util.Date;
import java.util.UUID;
public class VelocityBooster implements Booster {
@ -27,6 +28,7 @@ public class VelocityBooster implements Booster {
this.multiplier = multiplier;
this.active = false;
this.finished = false;
this.startingTime = new Date().getTime();
saveBooster();
}