1
0
This commit is contained in:
Lee
2024-04-05 17:07:57 +01:00
parent d7d2bb76ac
commit 4e4e122f31
19 changed files with 179 additions and 166 deletions

View File

@ -13,7 +13,6 @@ import cc.fascinated.motd.MotdManager;
import cc.fascinated.placeholder.PlaceholderManager;
import cc.fascinated.playercolor.PlayerColorManager;
import cc.fascinated.staffchat.StaffChatManager;
import cc.fascinated.staffchat.command.StaffChatCommand;
import cc.fascinated.utils.BuildData;
import cc.fascinated.vote.VoteManager;
import cc.fascinated.worldsize.WorldSizeManager;
@ -26,15 +25,14 @@ import java.util.concurrent.TimeUnit;
public class Aetheria extends JavaPlugin {
@Getter
private static final BuildData buildData = new BuildData();
/**
* The instance of the plugin.
*/
public static Aetheria INSTANCE;
public static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(2, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
@Getter private static final BuildData buildData = new BuildData();
public Aetheria() {
INSTANCE = this;
}

View File

@ -18,7 +18,10 @@ import org.bukkit.entity.Player;
import java.io.File;
import java.util.UUID;
@Getter @Log4j2 @EqualsAndHashCode
@Getter
@Log4j2
@EqualsAndHashCode
public class Account {
/**
@ -26,7 +29,30 @@ public class Account {
*/
private static final String playerColorProfileId = "playerColorProfile";
private static final String voteProfileId = "voteProfile";
/**
* The UUID of the player.
*/
@EqualsAndHashCode.Exclude
private final UUID uuid;
/**
* The name of the player.
*/
private final String name;
/**
* The file for this account.
*/
@EqualsAndHashCode.Exclude
private final File file;
/**
* The configuration for this account.
*/
@EqualsAndHashCode.Exclude
private final FileConfiguration config;
/**
* Account profiles.
*/
private final PlayerColorProfile playerColorProfile;
private final VoteProfile voteProfile;
/**
* The last hashcode of the account. This is used to check if any changes
* have been made to the account. If no changes have been made,
@ -34,46 +60,15 @@ public class Account {
*/
@EqualsAndHashCode.Exclude
private int lastHash;
/**
* The UUID of the player.
*/
@EqualsAndHashCode.Exclude
private final UUID uuid;
/**
* The name of the player.
*/
private final String name;
/**
* The first time the player joined the server.
*/
private long firstJoin;
/**
* The last time the player logged in.
*/
private long lastLogin;
/**
* The file for this account.
*/
@EqualsAndHashCode.Exclude
private final File file;
/**
* The configuration for this account.
*/
@EqualsAndHashCode.Exclude
private final FileConfiguration config;
/**
* Account profiles.
*/
private final PlayerColorProfile playerColorProfile;
private final VoteProfile voteProfile;
public Account(UUID uuid) {
//log.info("Loading account for " + uuid);
boolean newAccount = false;
@ -120,7 +115,7 @@ public class Account {
* Save a profile to the configuration.
*
* @param profile the profile to save
* @param key the key to save the profile under
* @param key the key to save the profile under
*/
private void saveProfile(Profile profile, String key) {
profile.save(this.getProfileSection(key));
@ -180,7 +175,7 @@ public class Account {
/**
* Send a message to the player with a prefix.
*
* @param prefix the prefix to use
* @param prefix the prefix to use
* @param message the message to send
*/
public void sendMessage(String prefix, Component message) {
@ -193,7 +188,7 @@ public class Account {
/**
* Send a message to the player with a prefix.
*
* @param prefix the prefix to use
* @param prefix the prefix to use
* @param message the message to send
*/
public void sendMessage(String prefix, String message) {

View File

@ -27,8 +27,8 @@ import java.util.concurrent.TimeUnit;
@Log4j2
public class AccountManager extends Manager implements Listener {
private final long SAVE_INTERVAL = 5; // in minutes
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>();
private final long SAVE_INTERVAL = 5; // in minutes
public AccountManager() {
CommandManager.registerCommand(new SaveAccountsCommand());
@ -46,11 +46,6 @@ public class AccountManager extends Manager implements Listener {
}, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES);
}
@Override
public Priority getPriority() {
return Priority.LOWEST;
}
/**
* Gets the account for the specified player.
*
@ -61,16 +56,6 @@ public class AccountManager extends Manager implements Listener {
return ACCOUNTS.get(uuid);
}
/**
* Checks if an account is already registered for the specified player.
*
* @param uuid the player's UUID
* @return true if the account is already registered, false otherwise
*/
private boolean isAccountLoaded(UUID uuid) {
return ACCOUNTS.containsKey(uuid);
}
/**
* Registers an account for the specified player.
*
@ -102,6 +87,21 @@ public class AccountManager extends Manager implements Listener {
log.info("Saved {}/{} accounts. ({}ms)", saved, ACCOUNTS.size(), System.currentTimeMillis() - before);
}
@Override
public Priority getPriority() {
return Priority.LOWEST;
}
/**
* Checks if an account is already registered for the specified player.
*
* @param uuid the player's UUID
* @return true if the account is already registered, false otherwise
*/
private boolean isAccountLoaded(UUID uuid) {
return ACCOUNTS.containsKey(uuid);
}
@EventHandler
public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) {
UUID uuid = event.getUniqueId();

View File

@ -15,7 +15,8 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.HashMap;
import java.util.regex.Pattern;
@RequiredArgsConstructor @Getter
@RequiredArgsConstructor
@Getter
enum BlockReason {
DOMAIN("Domain was detected in the message."),
DUPLICATE("Duplicate message was detected."),

View File

@ -38,7 +38,7 @@ public abstract class Command implements CommandExecutor, TabCompleter {
* Executes the command.
*
* @param account The account executing the command.
* @param args The arguments of the command.
* @param args The arguments of the command.
*/
public abstract void execute(Account account, String[] args);
@ -46,7 +46,7 @@ public abstract class Command implements CommandExecutor, TabCompleter {
* Tab completes the command.
*
* @param account The account executing the command.
* @param args The arguments of the command.
* @param args The arguments of the command.
* @return The tab completions.
*/
public List<String> tabComplete(Account account, String[] args) {

View File

@ -21,24 +21,29 @@ public enum Config {
VERSION_WARNING_VERSION("version-warning.min-version"),
VERSION_WARNING_MESSAGE("version-warning.message");
/**
* Cache of the config values.
*/
private static final HashMap<String, Object> cache = new HashMap<>();
/**
* The path of the lang in the lang.yml file.
*/
private final String path;
/**
* The configuration.
*/
private final cc.fascinated.utils.io.Config config = new cc.fascinated.utils.io.Config(Aetheria.INSTANCE, "config.yml", null);
Config(String path) {
this.path = path;
}
/**
* Cache of the config values.
* Clear the cache.
*/
private static final HashMap<String, Object> cache = new HashMap<>();
/**
* The configuration.
*/
private final cc.fascinated.utils.io.Config config = new cc.fascinated.utils.io.Config(Aetheria.INSTANCE, "config.yml", null);
public static void clear() {
cache.clear();
}
/**
* Gets as an object.
@ -76,11 +81,4 @@ public enum Config {
public int getAsInt() {
return (int) get();
}
/**
* Clear the cache.
*/
public static void clear() {
cache.clear();
}
}

View File

@ -42,24 +42,29 @@ public enum Lang {
STAFF_CHAT_FORMAT("staff-chat.format"),
STAFF_CHAT_USAGE("staff-chat.usage");
/**
* Cache of the lang values.
*/
private static final HashMap<String, Object> cache = new HashMap<>();
/**
* The path of the lang in the lang.yml file.
*/
private final String path;
/**
* The lang configuration.
*/
private final Config langConfig = new Config(Aetheria.INSTANCE, "lang.yml", null);
Lang(String path) {
this.path = path;
}
/**
* Cache of the lang values.
* Clear the cache.
*/
private static final HashMap<String, Object> cache = new HashMap<>();
/**
* The lang configuration.
*/
private final Config langConfig = new Config(Aetheria.INSTANCE, "lang.yml", null);
public static void clear() {
cache.clear();
}
/**
* Gets as an object.
@ -93,11 +98,4 @@ public enum Lang {
public List<String> getAsStringList() {
return (List<String>) get();
}
/**
* Clear the cache.
*/
public static void clear() {
cache.clear();
}
}

View File

@ -17,13 +17,30 @@ public interface EventListener {
return Priority.NORMAL;
}
default void onAetheriaDisable() {}
default void onPlayerJoin(Account account, PlayerJoinEvent event) {}
default void onPlayerLogin(Account account, PlayerLoginEvent event) {}
default void onPlayerQuit(Account account, PlayerQuitEvent event) {}
default void onAsyncChat(Account account, AsyncChatEvent event) {}
default void onCommandPreProcess(Account account, PlayerCommandPreprocessEvent event) {}
default void onServerListPing(ServerListPingEvent event) {}
default void onEntityDeath(EntityDeathEvent event) {}
default void onVote(Account account, VotifierEvent event) {}
default void onAetheriaDisable() {
}
default void onPlayerJoin(Account account, PlayerJoinEvent event) {
}
default void onPlayerLogin(Account account, PlayerLoginEvent event) {
}
default void onPlayerQuit(Account account, PlayerQuitEvent event) {
}
default void onAsyncChat(Account account, AsyncChatEvent event) {
}
default void onCommandPreProcess(Account account, PlayerCommandPreprocessEvent event) {
}
default void onServerListPing(ServerListPingEvent event) {
}
default void onEntityDeath(EntityDeathEvent event) {
}
default void onVote(Account account, VotifierEvent event) {
}
}

View File

@ -2,6 +2,7 @@ package cc.fascinated.metrics;
import com.influxdb.client.write.Point;
import lombok.Getter;
@Getter
public abstract class Metric {

View File

@ -24,9 +24,9 @@ import java.util.List;
@Log4j2
public class MetricManager extends Manager {
private static final List<Metric> metrics = new ArrayList<>();
private InfluxDBClient influxDB;
private WriteApiBlocking writeApi;
private static final List<Metric> metrics = new ArrayList<>();
public MetricManager() {
String url = Config.INFLUXDB_URL.getAsString();
@ -71,6 +71,15 @@ public class MetricManager extends Manager {
Bukkit.getScheduler().runTaskTimer(Aetheria.INSTANCE, (task) -> this.collectMetrics(), 30 * 20, 30 * 20);
}
/**
* Register a new metric
*
* @param metric the metric to register
*/
public static void registerMetric(Metric metric) {
metrics.add(metric);
}
/**
* Collect all metrics and write them to influx
*/
@ -101,15 +110,6 @@ public class MetricManager extends Manager {
.forEach(entry -> log.warn("Metric {} took {}ms to collect", entry.getKey().getName(), System.currentTimeMillis() - entry.getValue()));
}
/**
* Register a new metric
*
* @param metric the metric to register
*/
public static void registerMetric(Metric metric) {
metrics.add(metric);
}
@Override
public void onAetheriaDisable() {
influxDB.close(); // close the connection

View File

@ -19,7 +19,7 @@ public class CpuUsageMetric extends Metric {
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
HardwareAbstractionLayer hardware = Oshi.SYSTEM_INFO.getHardware();
cpuLoad = hardware.getProcessor().getSystemCpuLoad(500) * 100; // get the CPU load in percentage
},0, 30, TimeUnit.SECONDS);
}, 0, 30, TimeUnit.SECONDS);
}
@Override

View File

@ -15,7 +15,7 @@ public interface AetheriaPlaceholder {
* Parse this placeholder and return the value for the provided player
*
* @param player the player to parse the placeholder for, null if offline
* @param params the parameters for the placeholder
* @param params the parameters for the placeholder
* @return the placeholder string
*/
String parse(@Nullable Player player, String[] params);

View File

@ -45,7 +45,7 @@ public class PlaceholderManager {
/**
* Replace PAPI placeholders in the given content.
*
* @param player the player to replace the placeholders for, null if offline
* @param player the player to replace the placeholders for, null if offline
* @param content the content to replace the placeholders in
* @return the content with the placeholders replaced
*/

View File

@ -9,7 +9,9 @@ import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scoreboard.Team;
@Getter @Setter @EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class PlayerColorProfile extends Profile {
/**

View File

@ -2,8 +2,8 @@ package cc.fascinated.playercolor.command;
import cc.fascinated.account.Account;
import cc.fascinated.command.Command;
import cc.fascinated.playercolor.PlayerColorProfile;
import cc.fascinated.playercolor.PlayerColorManager;
import cc.fascinated.playercolor.PlayerColorProfile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

View File

@ -16,7 +16,7 @@ public class DiscordWebhook {
private String username;
private String avatarUrl;
private boolean tts;
private List<EmbedObject> embeds = new ArrayList<>();
private final List<EmbedObject> embeds = new ArrayList<>();
/**
* Constructs a new DiscordWebhook instance
@ -159,24 +159,44 @@ public class DiscordWebhook {
private Thumbnail thumbnail;
private Image image;
private Author author;
private List<Field> fields = new ArrayList<>();
private final List<Field> fields = new ArrayList<>();
public String getTitle() {
return title;
}
public EmbedObject setTitle(String title) {
this.title = title;
return this;
}
public String getDescription() {
return description;
}
public EmbedObject setDescription(String description) {
this.description = description;
return this;
}
public String getUrl() {
return url;
}
public EmbedObject setUrl(String url) {
this.url = url;
return this;
}
public Color getColor() {
return color;
}
public EmbedObject setColor(Color color) {
this.color = color;
return this;
}
public Footer getFooter() {
return footer;
}
@ -185,10 +205,20 @@ public class DiscordWebhook {
return thumbnail;
}
public EmbedObject setThumbnail(String url) {
this.thumbnail = new Thumbnail(url);
return this;
}
public Image getImage() {
return image;
}
public EmbedObject setImage(String url) {
this.image = new Image(url);
return this;
}
public Author getAuthor() {
return author;
}
@ -197,41 +227,11 @@ public class DiscordWebhook {
return fields;
}
public EmbedObject setTitle(String title) {
this.title = title;
return this;
}
public EmbedObject setDescription(String description) {
this.description = description;
return this;
}
public EmbedObject setUrl(String url) {
this.url = url;
return this;
}
public EmbedObject setColor(Color color) {
this.color = color;
return this;
}
public EmbedObject setFooter(String text, String icon) {
this.footer = new Footer(text, icon);
return this;
}
public EmbedObject setThumbnail(String url) {
this.thumbnail = new Thumbnail(url);
return this;
}
public EmbedObject setImage(String url) {
this.image = new Image(url);
return this;
}
public EmbedObject setAuthor(String name, String url, String icon) {
this.author = new Author(name, url, icon);
return this;
@ -243,8 +243,8 @@ public class DiscordWebhook {
}
private class Footer {
private String text;
private String iconUrl;
private final String text;
private final String iconUrl;
private Footer(String text, String iconUrl) {
this.text = text;
@ -261,7 +261,7 @@ public class DiscordWebhook {
}
private class Thumbnail {
private String url;
private final String url;
private Thumbnail(String url) {
this.url = url;
@ -273,7 +273,7 @@ public class DiscordWebhook {
}
private class Image {
private String url;
private final String url;
private Image(String url) {
this.url = url;
@ -285,9 +285,9 @@ public class DiscordWebhook {
}
private class Author {
private String name;
private String url;
private String iconUrl;
private final String name;
private final String url;
private final String iconUrl;
private Author(String name, String url, String iconUrl) {
this.name = name;
@ -309,9 +309,9 @@ public class DiscordWebhook {
}
private class Field {
private String name;
private String value;
private boolean inline;
private final String name;
private final String value;
private final boolean inline;
private Field(String name, String value, boolean inline) {
this.name = name;
@ -361,7 +361,7 @@ public class DiscordWebhook {
} else if (val instanceof Boolean) {
builder.append(val);
} else if (val instanceof JSONObject) {
builder.append(val.toString());
builder.append(val);
} else if (val.getClass().isArray()) {
builder.append("[");
int len = Array.getLength(val);

View File

@ -8,7 +8,7 @@ public class MathUtils {
/**
* Format a number to a specific amount of decimal places.
*
* @param number the number to format
* @param number the number to format
* @param additional the additional decimal places to format
* @return the formatted number
*/

View File

@ -23,7 +23,7 @@ public final class TimeUtils {
/**
* Format a time in millis to a readable time format.
*
* @param millis the millis to format
* @param millis the millis to format
* @param timeUnit the time unit to format the millis to
* @return the formatted time
*/
@ -34,9 +34,9 @@ public final class TimeUtils {
/**
* Format a time in millis to a readable time format.
*
* @param millis the millis to format
* @param millis the millis to format
* @param timeUnit the time unit to format the millis to
* @param compact whether to use a compact display
* @param compact whether to use a compact display
* @return the formatted time
*/
public static String format(long millis, WildTimeUnit timeUnit, boolean compact) {
@ -46,10 +46,10 @@ public final class TimeUtils {
/**
* Format a time in millis to a readable time format.
*
* @param millis the millis to format
* @param millis the millis to format
* @param timeUnit the time unit to format the millis to
* @param decimals whether to include decimals
* @param compact whether to use a compact display
* @param compact whether to use a compact display
* @return the formatted time
*/
public static String format(long millis, WildTimeUnit timeUnit, boolean decimals, boolean compact) {
@ -71,7 +71,7 @@ public final class TimeUtils {
}
String formatted = time + (compact ? timeUnit.getSuffix() : " " + timeUnit.getDisplay()); // Append the time unit
if (time != 1.0 && !compact) { // Pluralize the time unit
formatted+= "s";
formatted += "s";
}
return formatted;
}
@ -95,7 +95,7 @@ public final class TimeUtils {
String suffix = matcher.group(2); // The unit suffix
WildTimeUnit timeUnit = WildTimeUnit.fromSuffix(suffix); // The time unit to add
if (timeUnit != null) { // Increment the total millis
millis+= amount * timeUnit.getMillis();
millis += amount * timeUnit.getMillis();
}
}
return millis;
@ -104,8 +104,10 @@ public final class TimeUtils {
/**
* Represents a unit of time.
*/
@NoArgsConstructor @AllArgsConstructor
@Getter(AccessLevel.PRIVATE) @ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter(AccessLevel.PRIVATE)
@ToString
public enum WildTimeUnit {
FIT,
YEARS("Year", "y", TimeUnit.DAYS.toMillis(365L)),

View File

@ -6,7 +6,8 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
@Getter @EqualsAndHashCode(callSuper = false)
@Getter
@EqualsAndHashCode(callSuper = false)
public class VoteProfile extends Profile {
private int totalVotes;
@ -27,7 +28,7 @@ public class VoteProfile extends Profile {
/**
* Adds a vote to the player.
* <p>
* This gets called when a player votes for the server.
* This gets called when a player votes for the server.
* </p>
*/
public void addVote() {