Reworked everything to be much easier to read and just over all better (didnt rewrite the ranks part tho)

This commit is contained in:
2023-06-27 04:37:56 +02:00
parent fb41198073
commit 84e306f45f
39 changed files with 1500 additions and 1708 deletions
@@ -1,53 +1,38 @@
package com.alttd.boosters;
import com.alttd.boosterapi.BoosterAPI;
import com.alttd.boosterapi.BoosterImplementation;
import com.alttd.boosterapi.config.BoosterFileStorage;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosterapi.util.ALogger;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.util.Logger;
import com.alttd.boosters.commands.BoosterCommand;
import com.alttd.boosters.listeners.MCmmoListener;
import com.alttd.boosters.listeners.MyPetListener;
import com.alttd.boosters.listeners.PhantomSpawnListener;
import com.alttd.boosters.listeners.PluginMessage;
import com.alttd.boosters.managers.BoosterManager;
import com.alttd.boosters.storage.ServerBoosterStorage;
import com.alttd.boosters.listeners.mcMMOListener;
import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public final class BoostersPlugin extends JavaPlugin {
private static BoostersPlugin instance;
private static BoosterAPI boosterAPI;
private static BoosterManager boosterManager;
@Override
public void onEnable() {
instance = this;
ALogger.init(getSLF4JLogger());
boosterAPI = new BoosterImplementation();
boosterManager = new BoosterManager();
Logger logger = new Logger(getSLF4JLogger());
BoosterCache boosterCache = new BoosterCache(new BoosterFileStorage(logger));
if (getServer().getPluginManager().isPluginEnabled("MyPet")) {
registerListener(new MyPetListener());
registerListener(new MyPetListener(boosterCache));
}
if (getServer().getPluginManager().isPluginEnabled("mcMMO")) {
registerListener(new MCmmoListener());
registerListener(new mcMMOListener(boosterCache));
}
registerListener(new PhantomSpawnListener());
registerListener(new PhantomSpawnListener(boosterCache));
getServer().getMessenger().registerOutgoingPluginChannel(this, Config.pluginMessageChannel);
getServer().getMessenger().registerIncomingPluginChannel(this, Config.pluginMessageChannel, new PluginMessage());
registerCommand("listboosters", new BoosterCommand());
ServerBoosterStorage.getServerBoosterStorage(); //this loads the boosters in
}
@Override
public void onDisable() {
instance = null;
boosterAPI = null;
getServer().getMessenger().registerOutgoingPluginChannel(this, Config.SETTINGS.PLUGIN_MESSAGE_CHANNEL);
getServer().getMessenger().registerIncomingPluginChannel(this, Config.SETTINGS.PLUGIN_MESSAGE_CHANNEL, new PluginMessage(logger, boosterCache));
registerCommand("listboosters", new BoosterCommand(boosterCache, logger));
}
public void registerListener(Listener... listeners) {
@@ -59,16 +44,4 @@ public final class BoostersPlugin extends JavaPlugin {
public void registerCommand(String commandName, CommandExecutor CommandExecutor) {
getCommand(commandName).setExecutor(CommandExecutor);
}
public static BoostersPlugin getInstance() {
return instance;
}
public BoosterAPI getAPI() {
return boosterAPI;
}
public BoosterManager getBoosterManager() {
return boosterManager;
}
}
@@ -1,7 +1,9 @@
package com.alttd.boosters.commands;
import com.alttd.boosterapi.Booster;
import com.alttd.boosters.storage.ServerBoosterStorage;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.util.BoosterParser;
import com.alttd.boosterapi.util.Logger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -10,15 +12,18 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class BoosterCommand implements CommandExecutor {
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
private final BoosterCache boosterCache;
private final Logger logger;
public BoosterCommand(BoosterCache boosterCache, Logger logger) {
this.boosterCache = boosterCache;
this.logger = logger;
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
@@ -26,31 +31,19 @@ public class BoosterCommand implements CommandExecutor {
commandSender.sendMiniMessage("<red>You don't have permission for this command", null);
return true;
}
String message = "Active boosters:\n<active_boosters>\n\nQueued boosters:\n<queued_boosters>";
String activeBooster = "<type> activated by <activator> until <end_time> [UTC], boosts <multiplier> times";
String queuedBooster = "<type> queued by <activator> starts at <start_time> [UTC] and will be active for <duration>, boosts <multiplier> times";
List<Component> activeBoosterComponents = new ArrayList<>();
List<Component> queuedBoosterComponents = new ArrayList<>();
for (Booster booster : ServerBoosterStorage.getServerBoosterStorage().getBoosters().values()) {
long expiryTime = new Date().getTime() + booster.getDuration();
TagResolver templates = TagResolver.resolver(
Placeholder.unparsed("type", booster.getType().getBoosterName()),
Placeholder.unparsed("activator", booster.getActivator()),
Placeholder.unparsed("start_time", DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(booster.getStartingTime())),
Placeholder.unparsed("duration", TimeUnit.MILLISECONDS.toHours(booster.getDuration()) + " hours"),
Placeholder.unparsed("multiplier", String.valueOf(booster.getMultiplier())),
Placeholder.unparsed("end_time", booster.isActive() ? DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(expiryTime) : "unknown")
);
if (booster.isActive())
activeBoosterComponents.add(miniMessage.deserialize(activeBooster, templates));
else if (!booster.finished())
queuedBoosterComponents.add(miniMessage.deserialize(queuedBooster, templates));
}
commandSender.sendMiniMessage(message, TagResolver.resolver(
Placeholder.component("active_boosters", Component.join(JoinConfiguration.newlines(), activeBoosterComponents)),
Placeholder.component("queued_boosters", Component.join(JoinConfiguration.newlines(), queuedBoosterComponents))
));
commandSender.sendMiniMessage(Config.BOOSTER_MESSAGES.LIST_BOOSTER_MESSAGE, TagResolver.resolver(
Placeholder.component("active_boosters", Component.join(JoinConfiguration.newlines(),
BoosterParser.parseBoosters(logger, boosterCache.getAllActiveBoosters(),
Config.BOOSTER_MESSAGES.ACTIVE_BOOSTER_PART, true))),
Placeholder.component("queued_boosters", Component.join(JoinConfiguration.newlines(),
BoosterParser.parseBoosters(logger, boosterCache.getAllQueuedBoosters(),
Config.BOOSTER_MESSAGES.QUEUED_BOOSTER_PART, false))))
);
if (!(commandSender instanceof Player))
boosterCache.reloadBoosters();
return true;
}
}
@@ -1,158 +0,0 @@
package com.alttd.boosters.data;
import com.alttd.boosterapi.Booster;
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 {
private UUID uuid;
private String activator;
private Long startingTime;
private long duration;
private BoosterType boosterType;
private Double multiplier;
private Boolean active;
private Boolean finished;
public ServerBooster(UUID uuid, BoosterType boosterType, String reason, long duration, double multiplier) {
this.uuid = uuid;
this.boosterType = boosterType;
this.activator = reason;
this.duration = duration;
this.multiplier = multiplier;
this.active = false;
this.finished = false;
}
public ServerBooster(BoosterType type, String playerName, long duration, double multiplier) {
this(UUID.randomUUID(), type, playerName, duration, multiplier);
}
public ServerBooster(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;
}
@Override
public void setActive(Boolean active) {
this.startingTime = new Date().getTime();
this.active = active;
}
@Override
public BoosterType getType() {
return boosterType;
}
@Override
public void setType(BoosterType boosterType) {
this.boosterType = boosterType;
}
@Override
public double getMultiplier() {
return multiplier;
}
@Override
public void setMultiplier(double multiplier) {
this.multiplier = multiplier;
}
@Override
public Long getStartingTime() {
return startingTime;
}
@Override
public void setStartingTime(long startingTime) {
this.startingTime = startingTime;
}
@Override
public Long getEndTime() {
return startingTime + duration;
}
@Override
public Long getDuration() {
return duration;
}
@Override
public void setDuration(long duration) {
this.duration = duration;
}
@Override
public String getActivator() {
return activator;
}
@Override
public void setActivator(String activationReason) {
this.activator = activationReason;
}
@Override
public long getTimeRemaining() {
if(active) {
return startingTime + duration - System.currentTimeMillis();
}
return duration;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public void stopBooster() {
setDuration(getTimeRemaining());
setActive(false);
}
@Override
public void saveBooster() {
ALogger.error("Tried saving booster from server instead of proxy, only proxy should handle saving boosters");
}
@Override
public void finish() {
finished = true;
stopBooster();
}
@Override
public boolean finished() {
return false;
}
@Override
public int compareTo(@NotNull Object o) {
Booster booster = (Booster) o;
if (booster.getMultiplier() < getMultiplier())
return -1;
if (booster.getMultiplier() > getMultiplier())
return 1;
return booster.isActive() ? 1 : -1;
}
}
@@ -1,32 +1,32 @@
package com.alttd.boosters.listeners;
import com.alttd.boosterapi.Booster;
import com.alttd.boosterapi.BoosterType;
import com.alttd.boosters.BoostersPlugin;
import com.alttd.boosters.managers.BoosterManager;
import com.alttd.boosterapi.data.Booster;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.data.BoosterType;
import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class MCmmoListener implements Listener {
import java.util.Optional;
public class mcMMOListener implements Listener {
private final BoosterCache boosterCache;
public mcMMOListener(BoosterCache boosterCache) {
this.boosterCache = boosterCache;
}
@EventHandler
public void onMcMMOExperienceEvent(McMMOPlayerXpGainEvent event) {
BoosterManager bm = BoostersPlugin.getInstance().getBoosterManager();
if (bm.isBoosted(BoosterType.MCMMO)) {
Booster b = bm.getBooster(BoosterType.MCMMO);
double multiplier = b.getMultiplier() + 1;
event.setRawXpGained(Math.round(event.getRawXpGained() * multiplier));
return;
}
String skillName = event.getSkill().name();
BoosterType type = BoosterType.getByName(skillName);
if (bm.isBoosted(type)) {
Booster b = bm.getBooster(type);
double multiplier = b.getMultiplier() + 1;
event.setRawXpGained(Math.round(event.getRawXpGained() * multiplier));
Optional<Booster> optionalBooster = boosterCache.getActiveBooster(type);
if (optionalBooster.isEmpty())
return;
}
Booster booster = optionalBooster.get();
event.setRawXpGained(Math.round(booster.useMultiplier(event.getRawXpGained())));
}
// TODO : add individual mcmmo skill boosters
}
@@ -1,26 +1,34 @@
package com.alttd.boosters.listeners;
import com.alttd.boosterapi.Booster;
import com.alttd.boosterapi.BoosterType;
import com.alttd.boosters.BoostersPlugin;
import com.alttd.boosters.managers.BoosterManager;
import com.alttd.boosterapi.data.Booster;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.data.BoosterType;
import de.Keyle.MyPet.api.event.MyPetExpEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.Optional;
public class MyPetListener implements Listener {
private final BoosterCache boosterCache;
public MyPetListener(BoosterCache boosterCache) {
this.boosterCache = boosterCache;
}
@EventHandler
public void onMyPetExpEvent(MyPetExpEvent event) {
double exp = event.getPet().getExp();
if (exp == 0) {
return;
}
BoosterManager bm = BoostersPlugin.getInstance().getBoosterManager();
if(bm.isBoosted(BoosterType.MYPET)) {
Booster b = bm.getBooster(BoosterType.MYPET);
double multiplier = b.getMultiplier() + 1;
event.setExp(event.getExp() * multiplier);
}
Optional<Booster> myPetBooster = boosterCache.getActiveBooster(BoosterType.MYPET);
if (myPetBooster.isEmpty())
return;
Booster booster = myPetBooster.get();
event.setExp(booster.useMultiplier(exp));
}
}
@@ -1,7 +1,7 @@
package com.alttd.boosters.listeners;
import com.alttd.boosterapi.BoosterType;
import com.alttd.boosters.BoostersPlugin;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.data.BoosterType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -9,11 +9,17 @@ import org.bukkit.event.Listener;
public class PhantomSpawnListener implements Listener {
private final BoosterCache boosterCache;
public PhantomSpawnListener(BoosterCache boosterCache) {
this.boosterCache = boosterCache;
}
@EventHandler
public void onPhantomPreSpawn(com.destroystokyo.paper.event.entity.PhantomPreSpawnEvent event) {
Entity spawningEntity = event.getSpawningEntity();
if (spawningEntity instanceof Player player
&& BoostersPlugin.getInstance().getBoosterManager().isBoosted(BoosterType.PHANTOM)) {
if (spawningEntity instanceof Player
&& boosterCache.getActiveBooster(BoosterType.PHANTOM).isPresent()) {
event.setCancelled(true);
event.setShouldAbortSpawn(true);
}
@@ -1,61 +1,34 @@
package com.alttd.boosters.listeners;
import com.alttd.boosterapi.Booster;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosterapi.util.ALogger;
import com.alttd.boosters.storage.ServerBoosterStorage;
import com.alttd.boosterapi.data.BoosterCache;
import com.alttd.boosterapi.util.Logger;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.UUID;
public class PluginMessage implements PluginMessageListener {
private final Logger logger;
private final BoosterCache boosterCache;
public PluginMessage(Logger logger, BoosterCache boosterCache) {
this.logger = logger;
this.boosterCache = boosterCache;
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
if(!channel.equals(Config.pluginMessageChannel)) return;
if (Config.DEBUG)
ALogger.info("Received plugin message");
if(!channel.equals(Config.SETTINGS.PLUGIN_MESSAGE_CHANNEL))
return;
logger.debug("Received plugin message");
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
String subChannel = in.readUTF();
switch (subChannel) {
case "activate" -> {
UUID uuid = UUID.fromString(in.readUTF());
Booster booster = ServerBoosterStorage.getServerBoosterStorage().getBoosters().get(uuid);
if (booster == null) {
ServerBoosterStorage.getServerBoosterStorage().reload();
booster = ServerBoosterStorage.getServerBoosterStorage().getBoosters().get(uuid);
}
if (booster == null) {
ALogger.warn("Unable to load and activate booster [" + uuid + "]");
break;
}
booster.setActive(true);
}
case "finish" -> {
UUID uuid = UUID.fromString(in.readUTF());
Booster booster = ServerBoosterStorage.getServerBoosterStorage().getBoosters().get(uuid);
if (booster == null) {
ALogger.error("Tried to finish booster that was never loaded [" + uuid + "]");
break;
}
booster.finish();
}
case "stop" -> {
UUID uuid = UUID.fromString(in.readUTF());
Booster booster = ServerBoosterStorage.getServerBoosterStorage().getBoosters().get(uuid);
if (booster == null) {
ALogger.error("Tried to stop booster that was never loaded [" + uuid + "]");
break;
}
booster.stopBooster();
}
case "reload" -> {
ServerBoosterStorage.getServerBoosterStorage().reload();
}
default -> {}
if (subChannel.equals("reload")) {
boosterCache.reloadBoosters();
} else {
logger.severe("Received invalid plugin message");
}
}
}
@@ -1,43 +0,0 @@
package com.alttd.boosters.managers;
import com.alttd.boosterapi.Booster;
import com.alttd.boosterapi.BoosterType;
import com.alttd.boosterapi.config.Config;
import com.alttd.boosters.BoostersPlugin;
import com.alttd.boosters.storage.ServerBoosterStorage;
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()) {
return true;
}
}
return false;
}
public Booster getBooster(BoosterType type) {
for (Booster b : ServerBoosterStorage.getServerBoosterStorage().getBoosters().values()) {
if (b.getType() == type && b.isActive()) {
return b;
}
}
return null;
}
}
@@ -1,88 +0,0 @@
package com.alttd.boosters.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.boosters.data.ServerBooster;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.IOException;
import java.util.UUID;
public class ServerBoosterStorage extends BoosterStorage {
private static ServerBoosterStorage serverBoosterStorage = null;
public static ServerBoosterStorage getServerBoosterStorage() {
if (serverBoosterStorage == null)
serverBoosterStorage = new ServerBoosterStorage();
return serverBoosterStorage;
}
private ServerBoosterStorage() {
super();
}
@Override
public Booster loadBooster(JsonParser parser) throws IOException {
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 ServerBooster(uuid, activator, boosterType, startingTime, duration, multiplier, active, finished);
}
private static Booster error(String error) {
ALogger.error(error);
return null;
}
}