Fixed boosters not updating on galaxy and not being stored properly
This commit is contained in:
parent
bf88098f12
commit
a8cae97ef8
|
|
@ -50,15 +50,17 @@ public abstract class BoosterStorage {
|
|||
}
|
||||
|
||||
public void reload() {
|
||||
if (Config.DEBUG)
|
||||
ALogger.info("Reloading boosters...");
|
||||
boosters.clear();
|
||||
boosters.putAll(loadBoosters());
|
||||
}
|
||||
|
||||
public Map<UUID, Booster> getBoosters() {
|
||||
public synchronized Map<UUID, Booster> getBoosters() {
|
||||
return boosters;
|
||||
}
|
||||
|
||||
public Map<UUID, Booster> loadBoosters() {
|
||||
public synchronized Map<UUID, Booster> loadBoosters() {
|
||||
Map<UUID, Booster> boosters = new HashMap<>();
|
||||
|
||||
try {
|
||||
|
|
@ -72,7 +74,7 @@ public abstract class BoosterStorage {
|
|||
Booster booster = loadBooster(parser);
|
||||
if (Config.DEBUG)
|
||||
ALogger.info("Loading booster [" + booster.getType() + "] activated by [" + booster.getActivator()+ "].");
|
||||
if (new Date(booster.getEndTime()).after(new Date()))
|
||||
if (booster.getTimeRemaining() < 1)
|
||||
continue;
|
||||
boosters.put(booster.getUUID(), booster);
|
||||
if (parser.nextToken() != null && !parser.currentToken().isStructEnd()) {
|
||||
|
|
@ -86,18 +88,17 @@ public abstract class BoosterStorage {
|
|||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
saveBoosters(boosters.values());
|
||||
return boosters;
|
||||
}
|
||||
|
||||
public abstract Booster loadBooster(JsonParser parser) throws IOException;
|
||||
|
||||
public void saveBoosters(Collection<Booster> boosters) {
|
||||
public synchronized void saveBoosters(Collection<Booster> boosters) {
|
||||
try {
|
||||
JsonGenerator generator = new JsonFactory().createGenerator(CONFIG_FILE, JsonEncoding.UTF8);
|
||||
Date date = new Date();
|
||||
for (Booster booster : boosters) {
|
||||
if (booster.finished() || new Date(booster.getEndTime()).after(date))
|
||||
if (booster.finished() || (booster.isActive() && new Date(booster.getEndTime()).before(date)))
|
||||
continue;
|
||||
saveBooster(booster, generator);
|
||||
}
|
||||
|
|
@ -105,8 +106,12 @@ public abstract class BoosterStorage {
|
|||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void saveBoosters() {
|
||||
saveBoosters(boosters.values());
|
||||
}
|
||||
|
||||
private void saveBooster(Booster booster, JsonGenerator generator) throws IOException {
|
||||
generator.writeStartObject();
|
||||
|
||||
|
|
@ -122,7 +127,11 @@ public abstract class BoosterStorage {
|
|||
generator.writeEndObject();
|
||||
}
|
||||
|
||||
public Collection<Booster> getBoosters(BoosterType type) {
|
||||
public synchronized Collection<Booster> getBoosters(BoosterType type) {
|
||||
return boosters.values().stream().filter(booster -> booster.getType().equals(type)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public synchronized void add(Booster booster) {
|
||||
boosters.put(booster.getUUID(), booster);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import com.alttd.boosterapi.BoosterType;
|
|||
import com.alttd.boosterapi.util.ALogger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ServerBooster implements Booster {
|
||||
|
|
@ -50,6 +51,7 @@ public class ServerBooster implements Booster {
|
|||
|
||||
@Override
|
||||
public void setActive(Boolean active) {
|
||||
this.startingTime = new Date().getTime();
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +87,7 @@ public class ServerBooster implements Booster {
|
|||
|
||||
@Override
|
||||
public Long getEndTime() {
|
||||
return startingTime = duration;
|
||||
return startingTime + duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class MCmmoListener implements Listener {
|
|||
BoosterManager bm = BoostersPlugin.getInstance().getBoosterManager();
|
||||
if (bm.isBoosted(BoosterType.MCMMO)) {
|
||||
Booster b = bm.getBooster(BoosterType.MCMMO);
|
||||
double multiplier = b.getMultiplier();
|
||||
double multiplier = b.getMultiplier() + 1;
|
||||
event.setRawXpGained(Math.round(event.getRawXpGained() * multiplier));
|
||||
return;
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ public class MCmmoListener implements Listener {
|
|||
BoosterType type = BoosterType.getByName(skillName);
|
||||
if (bm.isBoosted(type)) {
|
||||
Booster b = bm.getBooster(type);
|
||||
double multiplier = b.getMultiplier();
|
||||
double multiplier = b.getMultiplier() + 1;
|
||||
event.setRawXpGained(Math.round(event.getRawXpGained() * multiplier));
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class MyPetListener implements Listener {
|
|||
BoosterManager bm = BoostersPlugin.getInstance().getBoosterManager();
|
||||
if(bm.isBoosted(BoosterType.MYPET)) {
|
||||
Booster b = bm.getBooster(BoosterType.MYPET);
|
||||
double multiplier = b.getMultiplier();
|
||||
double multiplier = b.getMultiplier() + 1;
|
||||
event.setExp(event.getExp() * multiplier);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ public class PluginMessage implements PluginMessageListener {
|
|||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
|
||||
if(!channel.equals(Config.pluginMessageChannel)) return;
|
||||
if (Config.DEBUG)
|
||||
ALogger.info("Received plugin message");
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
|
||||
String subChannel = in.readUTF();
|
||||
switch (subChannel) {
|
||||
|
|
|
|||
|
|
@ -2,13 +2,27 @@ package com.alttd.boosters.managers;
|
|||
|
||||
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.boosters.BoostersPlugin;
|
||||
import com.alttd.boosters.storage.ServerBoosterStorage;
|
||||
|
||||
import java.util.List;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class BoosterManager {
|
||||
|
||||
public BoosterManager() {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Booster booster: ServerBoosterStorage.getServerBoosterStorage().getBoosters().values()) {
|
||||
if (!booster.isActive())
|
||||
continue;
|
||||
if (booster.getTimeRemaining() > 0) continue;
|
||||
booster.finish();
|
||||
}
|
||||
}
|
||||
}.runTaskTimerAsynchronously(BoostersPlugin.getInstance(), 0, Config.activeTaskCheckFrequency * 20);
|
||||
}
|
||||
|
||||
public boolean isBoosted(BoosterType type) {
|
||||
for (Booster b : ServerBoosterStorage.getServerBoosterStorage().getBoosters().values()) {
|
||||
if (b.getType() == type && b.isActive()) {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import com.alttd.vboosters.VelocityBoosters;
|
|||
import com.alttd.vboosters.data.VelocityBooster;
|
||||
import com.alttd.vboosters.managers.BoosterManager;
|
||||
import com.alttd.vboosters.storage.VelocityBoosterStorage;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.alttd.vboosters.data;
|
|||
|
||||
import com.alttd.boosterapi.Booster;
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.boosterapi.config.BoosterStorage;
|
||||
import com.alttd.vboosters.VelocityBoosters;
|
||||
import com.alttd.vboosters.storage.VelocityBoosterStorage;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
|
|
@ -35,7 +36,6 @@ public class VelocityBooster implements Booster {
|
|||
this.active = false;
|
||||
this.finished = false;
|
||||
this.startingTime = new Date().getTime();
|
||||
saveBooster();
|
||||
}
|
||||
|
||||
public VelocityBooster(BoosterType type, String playerName, long duration, double multiplier) {
|
||||
|
|
@ -62,6 +62,7 @@ public class VelocityBooster implements Booster {
|
|||
@Override
|
||||
public void setActive(Boolean active) {
|
||||
this.active = active;
|
||||
updateQueue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -150,28 +151,17 @@ public class VelocityBooster implements Booster {
|
|||
public void saveBooster() {
|
||||
VelocityBoosterStorage vbs = VelocityBoosterStorage.getVelocityBoosterStorage();
|
||||
vbs.getBoosters().put(uuid, this);
|
||||
vbs.saveBoosters(vbs.getBoosters().values());
|
||||
updateQueue();
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("reload");
|
||||
VelocityBoosters.getPlugin().getProxy().getAllServers()
|
||||
.forEach(registeredServer -> registeredServer.sendPluginMessage(VelocityBoosters.getPlugin().getChannelIdentifier(), out.toByteArray()));
|
||||
}
|
||||
|
||||
public void finish() { //TODO finish it on the servers as well
|
||||
finished = true;
|
||||
stopBooster();
|
||||
saveBooster(); //Deletes inactive boosters
|
||||
updateQueue(); //Deletes inactive boosters
|
||||
List<Booster> collect = VelocityBoosterStorage.getVelocityBoosterStorage().getBoosters(boosterType).stream().sorted().collect(Collectors.toList());
|
||||
if (collect.size() <= 1)
|
||||
return;
|
||||
Booster booster = collect.get(1);
|
||||
booster.setActive(true);
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("activate");
|
||||
out.writeUTF(booster.getUUID().toString());
|
||||
VelocityBoosters.getPlugin().getProxy().getAllServers()
|
||||
.forEach(registeredServer -> registeredServer.sendPluginMessage(VelocityBoosters.getPlugin().getChannelIdentifier(), out.toByteArray()));
|
||||
//TODO send plugin message that this is finished
|
||||
}
|
||||
|
||||
|
|
@ -180,7 +170,7 @@ public class VelocityBooster implements Booster {
|
|||
return finished;
|
||||
}
|
||||
|
||||
private void updateQueue() {
|
||||
public void updateQueue() {
|
||||
Collection<Booster> boosters = VelocityBoosterStorage.getVelocityBoosterStorage().getBoosters(getType());
|
||||
if (boosters.isEmpty())
|
||||
return;
|
||||
|
|
@ -192,6 +182,12 @@ public class VelocityBooster implements Booster {
|
|||
}
|
||||
if (collect.size() > 1)
|
||||
fixTimes(collect);
|
||||
|
||||
VelocityBoosterStorage.getVelocityBoosterStorage().saveBoosters();
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("reload");
|
||||
VelocityBoosters.getPlugin().getProxy().getAllServers()
|
||||
.forEach(registeredServer -> registeredServer.sendPluginMessage(VelocityBoosters.getPlugin().getChannelIdentifier(), out.toByteArray()));
|
||||
}
|
||||
|
||||
private void fixTimes(List<Booster> sorted) {
|
||||
|
|
|
|||
|
|
@ -3,13 +3,23 @@ package com.alttd.vboosters.managers;
|
|||
import com.alttd.boosterapi.Booster;
|
||||
import com.alttd.boosterapi.BoosterType;
|
||||
import com.alttd.boosterapi.config.Config;
|
||||
import com.alttd.boosterapi.util.ALogger;
|
||||
import com.alttd.vboosters.VelocityBoosters;
|
||||
import com.alttd.vboosters.data.VelocityBooster;
|
||||
import com.alttd.vboosters.storage.VelocityBoosterStorage;
|
||||
import com.mysql.cj.log.Log;
|
||||
import com.velocitypowered.api.scheduler.ScheduledTask;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BoosterManager {
|
||||
|
|
@ -32,6 +42,8 @@ public class BoosterManager {
|
|||
for (Booster booster: getActiveBoosters()) {
|
||||
if (booster.getTimeRemaining() > 0) continue;
|
||||
booster.finish();
|
||||
|
||||
plugin.getProxy().sendMessage(MiniMessage.miniMessage().deserialize("<green>Booster <booster> ended!</green>", Placeholder.unparsed("booster", booster.getType().getBoosterName())));
|
||||
// send data to the backend servers to let them know the booster is no longer active
|
||||
}
|
||||
getActiveBoosters().removeIf(Booster::finished);
|
||||
|
|
@ -67,16 +79,21 @@ public class BoosterManager {
|
|||
}
|
||||
|
||||
public void addBooster(Booster booster) {
|
||||
BoosterType type = booster.getType();
|
||||
if(isBoosted(type)) {
|
||||
Booster activeBooster = getBoosted(type);
|
||||
Booster queuedBooster = getHighestBooster(type);
|
||||
if (queuedBooster != null && queuedBooster.getMultiplier() > activeBooster.getMultiplier()) {
|
||||
swapBooster(activeBooster, queuedBooster);
|
||||
}
|
||||
} else {
|
||||
activateBooster(booster);
|
||||
}
|
||||
// BoosterType type = booster.getType();
|
||||
// if (isBoosted(type)) {
|
||||
// Booster activeBooster = getBoosted(type);
|
||||
// Booster queuedBooster = getHighestBooster(type);
|
||||
// if (queuedBooster != null && queuedBooster.getMultiplier() > activeBooster.getMultiplier()) {
|
||||
// swapBooster(activeBooster, queuedBooster);
|
||||
// }
|
||||
// } else {
|
||||
// activateBooster(booster);
|
||||
// }
|
||||
VelocityBoosterStorage.getVelocityBoosterStorage().add(booster);
|
||||
if (booster instanceof VelocityBooster velocityBooster)
|
||||
velocityBooster.updateQueue();
|
||||
else
|
||||
ALogger.error("Tried to add a not velocity booster from velocity");
|
||||
}
|
||||
|
||||
public void removeBooster(Booster booster) {
|
||||
|
|
@ -96,7 +113,8 @@ public class BoosterManager {
|
|||
}
|
||||
|
||||
public void deactivateBooster(Booster booster) {
|
||||
queuedBoosters.add(booster);
|
||||
if (booster.isActive())
|
||||
queuedBoosters.add(booster);
|
||||
activeBoosters.remove(booster);
|
||||
booster.setActive(false);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user