From 872279a9c5c558f4d1a6877dbfc23da656b27db9 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 5 Apr 2024 21:43:32 +0100 Subject: [PATCH] stuff --- src/main/java/cc/fascinated/Aetheria.java | 3 ++ .../java/cc/fascinated/account/Account.java | 33 +++++++-------- .../cc/fascinated/account/AccountManager.java | 15 +++++++ .../account/command/DeleteAccountCommand.java | 3 +- .../account/command/SaveAccountsCommand.java | 3 +- .../java/cc/fascinated/command/Command.java | 39 ++++------------- .../cc/fascinated/command/CommandManager.java | 30 ++++++++----- .../command/impl/DiscordCommand.java | 20 +++++++++ .../fascinated/command/impl/GitCommand.java | 3 +- .../command/impl/TotalJoinsCommand.java | 3 +- .../commandspy/command/CommandSpyCommand.java | 3 +- .../java/cc/fascinated/config/Config.java | 4 +- src/main/java/cc/fascinated/config/Lang.java | 6 ++- .../staffchat/StaffChatManager.java | 1 - .../staffchat/command/StaffChatCommand.java | 3 +- .../java/cc/fascinated/tips/TipManager.java | 42 +++++++++++++++++++ .../worldsize/WorldSizeManager.java | 2 +- .../{impl => command}/WorldSizeCommand.java | 2 +- src/main/resources/config.yml | 3 ++ src/main/resources/lang.yml | 15 ++++++- src/main/resources/plugin.yml | 38 +---------------- 21 files changed, 162 insertions(+), 109 deletions(-) create mode 100644 src/main/java/cc/fascinated/command/impl/DiscordCommand.java create mode 100644 src/main/java/cc/fascinated/tips/TipManager.java rename src/main/java/cc/fascinated/worldsize/{impl => command}/WorldSizeCommand.java (97%) diff --git a/src/main/java/cc/fascinated/Aetheria.java b/src/main/java/cc/fascinated/Aetheria.java index a397fc1..b30e636 100644 --- a/src/main/java/cc/fascinated/Aetheria.java +++ b/src/main/java/cc/fascinated/Aetheria.java @@ -14,6 +14,7 @@ import cc.fascinated.motd.MotdManager; import cc.fascinated.placeholder.PlaceholderManager; import cc.fascinated.playercolor.PlayerColorManager; import cc.fascinated.staffchat.StaffChatManager; +import cc.fascinated.tips.TipManager; import cc.fascinated.utils.BuildData; import cc.fascinated.vote.VoteManager; import cc.fascinated.worldsize.WorldSizeManager; @@ -60,6 +61,8 @@ public class Aetheria extends JavaPlugin { new RenderDistanceManager(); new VoteManager(); new StaffChatManager(); + new TipManager(); + new DiscordBot(); } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/account/Account.java b/src/main/java/cc/fascinated/account/Account.java index 6849585..6401806 100644 --- a/src/main/java/cc/fascinated/account/Account.java +++ b/src/main/java/cc/fascinated/account/Account.java @@ -29,30 +29,36 @@ 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, @@ -60,10 +66,12 @@ public class Account { */ @EqualsAndHashCode.Exclude private int lastHash; + /** * The first time the player joined the server. */ private long firstJoin; + /** * The last time the player logged in. */ @@ -83,7 +91,7 @@ public class Account { try { file.createNewFile(); } catch (Exception e) { - log.warn("Failed to create account file for " + this.uuid); + log.warn("Failed to create account file for {}", this.uuid); e.printStackTrace(); } } @@ -92,8 +100,7 @@ public class Account { if (newAccount) { this.firstJoin = System.currentTimeMillis(); this.lastLogin = System.currentTimeMillis(); - this.save(false); // Save default values - log.info("Created new account for " + this.uuid); + log.info("Created new account for {}", this.uuid); } this.firstJoin = config.getLong("firstJoin"); @@ -217,10 +224,9 @@ public class Account { /** * Saves the account to disk. * - * @param saveProfiles if the profiles should be saved * @return true if the account was saved, false otherwise */ - public boolean save(boolean saveProfiles) { + public boolean save() { if (this.lastHash == this.hashCode()) { return false; // No changes have been made } @@ -228,28 +234,17 @@ public class Account { this.config.set("firstJoin", this.firstJoin); this.config.set("lastLogin", this.lastLogin); - if (saveProfiles) { - this.saveProfile(this.playerColorProfile, playerColorProfileId); - this.saveProfile(this.voteProfile, voteProfileId); - } + this.saveProfile(this.playerColorProfile, playerColorProfileId); + this.saveProfile(this.voteProfile, voteProfileId); try { this.config.save(this.file); // Save the account to disk } catch (Exception ex) { - log.warn("Failed to save account for " + this.uuid); + log.warn("Failed to save account for {}", this.uuid); ex.printStackTrace(); return false; } this.lastHash = this.hashCode(); // Update the last hash return true; } - - /** - * Saves the account and profiles to disk. - * - * @return true if the account was saved, false otherwise - */ - public boolean save() { - return this.save(true); - } } diff --git a/src/main/java/cc/fascinated/account/AccountManager.java b/src/main/java/cc/fascinated/account/AccountManager.java index e549f8e..b227211 100644 --- a/src/main/java/cc/fascinated/account/AccountManager.java +++ b/src/main/java/cc/fascinated/account/AccountManager.java @@ -14,6 +14,7 @@ import cc.fascinated.utils.Style; import com.viaversion.viaversion.api.Via; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.EmbedBuilder; +import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -106,6 +107,20 @@ public class AccountManager extends Manager implements Listener { log.info("Saved {}/{} accounts. ({}ms)", saved, ACCOUNTS.size(), System.currentTimeMillis() - before); } + /** + * Deletes an account from the server. + * This will remove the account from memory and delete the account file. + * + * @param account the account to delete + */ + public void deleteAccount(Account account) { + account.getFile().delete(); + ACCOUNTS.remove(account.getUuid()); + + account.getPlayer().kick(Component.text("Your account has been deleted. Please reconnect.")); + log.info("Deleted account for {}", account.getUuid()); + } + @Override public Priority getPriority() { return Priority.LOWEST; diff --git a/src/main/java/cc/fascinated/account/command/DeleteAccountCommand.java b/src/main/java/cc/fascinated/account/command/DeleteAccountCommand.java index d6f9b61..3f6567a 100644 --- a/src/main/java/cc/fascinated/account/command/DeleteAccountCommand.java +++ b/src/main/java/cc/fascinated/account/command/DeleteAccountCommand.java @@ -6,7 +6,8 @@ import cc.fascinated.command.Command; public class DeleteAccountCommand extends Command { public DeleteAccountCommand() { - super("deleteaccount", "aetheria.command.deleteaccount"); + super("deleteaccount"); + super.setPermission("aetheria.command.deleteaccount"); } @Override diff --git a/src/main/java/cc/fascinated/account/command/SaveAccountsCommand.java b/src/main/java/cc/fascinated/account/command/SaveAccountsCommand.java index eee5243..1737a90 100644 --- a/src/main/java/cc/fascinated/account/command/SaveAccountsCommand.java +++ b/src/main/java/cc/fascinated/account/command/SaveAccountsCommand.java @@ -8,7 +8,8 @@ import cc.fascinated.config.Lang; public class SaveAccountsCommand extends Command { public SaveAccountsCommand() { - super("saveaccounts", "command.aetheria.saveaccounts"); + super("saveaccounts"); + super.setPermission("command.aetheria.saveaccounts"); } @Override diff --git a/src/main/java/cc/fascinated/command/Command.java b/src/main/java/cc/fascinated/command/Command.java index 23d18e5..90d2e20 100644 --- a/src/main/java/cc/fascinated/command/Command.java +++ b/src/main/java/cc/fascinated/command/Command.java @@ -13,25 +13,10 @@ import org.jetbrains.annotations.Nullable; import java.util.List; @Getter -public abstract class Command implements CommandExecutor, TabCompleter { +public abstract class Command extends org.bukkit.command.Command { - /** - * The command name - */ - private final String command; - - /** - * The permission required to execute the command. - */ - private String permission; - - public Command(String command, String permissions) { - this.command = command; - this.permission = permissions; - } - - public Command(String command) { - this.command = command; + public Command(@NotNull String name) { + super(name); } /** @@ -54,26 +39,18 @@ public abstract class Command implements CommandExecutor, TabCompleter { } @Override - public final boolean onCommand(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) { - if (this.permission != null && !commandSender.hasPermission(permission)) { - commandSender.sendMessage("§cYou do not have permission to execute this command."); - return true; - } - + public final boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) { Player player = (Player) commandSender; Account account = AccountManager.getAccount(player.getUniqueId()); + this.execute(account, strings); return true; } @Override - public final @Nullable List onTabComplete(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) { - if (this.permission != null && !commandSender.hasPermission(permission)) { - return null; - } - - Player player = (Player) commandSender; + public final @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { + Player player = (Player) sender; Account account = AccountManager.getAccount(player.getUniqueId()); - return this.tabComplete(account, strings); + return this.tabComplete(account, args); } } diff --git a/src/main/java/cc/fascinated/command/CommandManager.java b/src/main/java/cc/fascinated/command/CommandManager.java index 689d7ad..bfb49f8 100644 --- a/src/main/java/cc/fascinated/command/CommandManager.java +++ b/src/main/java/cc/fascinated/command/CommandManager.java @@ -2,16 +2,24 @@ package cc.fascinated.command; import cc.fascinated.Aetheria; import cc.fascinated.command.impl.*; +import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; public class CommandManager { + private static final List COMMANDS = new ArrayList<>(); + public CommandManager() { registerCommand(new TotalJoinsCommand()); registerCommand(new HelpCommand()); registerCommand(new SeedCommand()); registerCommand(new VoteCommand()); registerCommand(new GitCommand()); + registerCommand(new DiscordCommand()); } /** @@ -19,16 +27,18 @@ public class CommandManager { * * @param command The command to register. */ - public static void registerCommand(Command command) { - if (command == null) { - throw new IllegalArgumentException("Command cannot be null."); - } + public static void registerCommand(@NotNull Command command) { + Bukkit.getCommandMap().register(Aetheria.INSTANCE.getName(), command); + COMMANDS.add(command); + } - PluginCommand pluginCommand = Aetheria.INSTANCE.getCommand(command.getCommand()); - if (pluginCommand == null) { - throw new IllegalArgumentException("Command " + command.getCommand() + " does not exist."); - } - pluginCommand.setExecutor(command); - pluginCommand.setTabCompleter(command); + /** + * Unregisters a command. + * + * @param command The command to unregister. + */ + public static void unregisterCommand(@NotNull Command command) { + command.unregister(Bukkit.getCommandMap()); + COMMANDS.remove(command); } } diff --git a/src/main/java/cc/fascinated/command/impl/DiscordCommand.java b/src/main/java/cc/fascinated/command/impl/DiscordCommand.java new file mode 100644 index 0000000..601b3f1 --- /dev/null +++ b/src/main/java/cc/fascinated/command/impl/DiscordCommand.java @@ -0,0 +1,20 @@ +package cc.fascinated.command.impl; + +import cc.fascinated.account.Account; +import cc.fascinated.command.Command; +import cc.fascinated.config.Config; +import cc.fascinated.config.Lang; + +public class DiscordCommand extends Command { + + public DiscordCommand() { + super("discord"); + } + + @Override + public void execute(Account account, String[] args) { + account.sendMessage(Lang.DISCORD_COMMAND.getAsString() + .replace("%invite-url%", Config.DISCORD_INVITE_URL.getAsString()) + ); + } +} diff --git a/src/main/java/cc/fascinated/command/impl/GitCommand.java b/src/main/java/cc/fascinated/command/impl/GitCommand.java index 4c42ca7..62342b5 100644 --- a/src/main/java/cc/fascinated/command/impl/GitCommand.java +++ b/src/main/java/cc/fascinated/command/impl/GitCommand.java @@ -12,7 +12,8 @@ import java.util.List; public class GitCommand extends Command { public GitCommand() { - super("git", "aetheria.command.git"); + super("git"); + super.setPermission("aetheria.command.git"); } @Override diff --git a/src/main/java/cc/fascinated/command/impl/TotalJoinsCommand.java b/src/main/java/cc/fascinated/command/impl/TotalJoinsCommand.java index c0ff2cd..26513fc 100644 --- a/src/main/java/cc/fascinated/command/impl/TotalJoinsCommand.java +++ b/src/main/java/cc/fascinated/command/impl/TotalJoinsCommand.java @@ -7,7 +7,8 @@ import cc.fascinated.config.Lang; public class TotalJoinsCommand extends Command { public TotalJoinsCommand() { - super("totaljoins", "aetheria.command.totaljoins"); + super("totaljoins"); + super.setPermission("aetheria.command.totaljoins"); } @Override diff --git a/src/main/java/cc/fascinated/commandspy/command/CommandSpyCommand.java b/src/main/java/cc/fascinated/commandspy/command/CommandSpyCommand.java index 974a701..6b444f8 100644 --- a/src/main/java/cc/fascinated/commandspy/command/CommandSpyCommand.java +++ b/src/main/java/cc/fascinated/commandspy/command/CommandSpyCommand.java @@ -8,7 +8,8 @@ import cc.fascinated.config.Lang; public class CommandSpyCommand extends Command { public CommandSpyCommand() { - super("commandspy", "aetheria.command.commandspy"); + super("commandspy"); + super.setPermission("aetheria.command.commandspy"); } @Override diff --git a/src/main/java/cc/fascinated/config/Config.java b/src/main/java/cc/fascinated/config/Config.java index 9e5b9ae..2545f31 100644 --- a/src/main/java/cc/fascinated/config/Config.java +++ b/src/main/java/cc/fascinated/config/Config.java @@ -15,11 +15,13 @@ public enum Config { INFLUXDB_ORG("influxdb.org"), INFLUXDB_BUCKET("influxdb.bucket"), DISCORD_TOKEN("discord.token"), + DISCORD_INVITE_URL("discord.invite-url"), MOTD_HEADER("motd.header"), MOTD_FORMAT("motd.format"), MOTD_LIST("motd.motds"), VERSION_WARNING_VERSION("version-warning.min-version"), - VERSION_WARNING_MESSAGE("version-warning.message"); + VERSION_WARNING_MESSAGE("version-warning.message"), + TIP_INTERVAL("tip-interval"); /** * Cache of the config values. diff --git a/src/main/java/cc/fascinated/config/Lang.java b/src/main/java/cc/fascinated/config/Lang.java index 8dcfb31..85a0b7c 100644 --- a/src/main/java/cc/fascinated/config/Lang.java +++ b/src/main/java/cc/fascinated/config/Lang.java @@ -27,6 +27,7 @@ public enum Lang { VOTE_STATS_COMMAND("vote-stats-command"), SAVE_ACCOUNTS_COMMAND_SAVING("save-accounts-command.saving"), SAVE_ACCOUNTS_COMMAND_SAVED("save-accounts-command.saved"), + DISCORD_COMMAND("discord-command"), BLOCKED_MESSAGE("blocked-message"), BLOCKED_MESSAGE_ALERT("blocked-message-alert"), CHAT_FORMAT("chat-format"), @@ -40,7 +41,10 @@ public enum Lang { VOTE_VOTED("vote.voted"), VOTE_BROADCAST("vote.broadcast"), STAFF_CHAT_FORMAT("staff-chat.format"), - STAFF_CHAT_USAGE("staff-chat.usage"); + STAFF_CHAT_USAGE("staff-chat.usage"), + TIPS_FORMAT("tips.format"), + TIPS_LIST("tips.tips"), + TIPS_LOGIN_NOTIFICATION("tips.login-notification"); /** * Cache of the lang values. diff --git a/src/main/java/cc/fascinated/staffchat/StaffChatManager.java b/src/main/java/cc/fascinated/staffchat/StaffChatManager.java index 6d83460..e972400 100644 --- a/src/main/java/cc/fascinated/staffchat/StaffChatManager.java +++ b/src/main/java/cc/fascinated/staffchat/StaffChatManager.java @@ -5,7 +5,6 @@ import cc.fascinated.account.AccountManager; import cc.fascinated.config.Lang; import cc.fascinated.staffchat.command.StaffChatCommand; import cc.fascinated.utils.Manager; -import org.bukkit.Bukkit; public class StaffChatManager extends Manager { diff --git a/src/main/java/cc/fascinated/staffchat/command/StaffChatCommand.java b/src/main/java/cc/fascinated/staffchat/command/StaffChatCommand.java index 5cb201c..65bac13 100644 --- a/src/main/java/cc/fascinated/staffchat/command/StaffChatCommand.java +++ b/src/main/java/cc/fascinated/staffchat/command/StaffChatCommand.java @@ -8,7 +8,8 @@ import cc.fascinated.staffchat.StaffChatManager; public class StaffChatCommand extends Command { public StaffChatCommand() { - super("staffchat", "aetheria.command.staffchat"); + super("staffchat"); + super.setPermission("aetheria.command.staffchat"); } @Override diff --git a/src/main/java/cc/fascinated/tips/TipManager.java b/src/main/java/cc/fascinated/tips/TipManager.java new file mode 100644 index 0000000..df025ce --- /dev/null +++ b/src/main/java/cc/fascinated/tips/TipManager.java @@ -0,0 +1,42 @@ +package cc.fascinated.tips; + +import cc.fascinated.Aetheria; +import cc.fascinated.account.Account; +import cc.fascinated.config.Config; +import cc.fascinated.config.Lang; +import cc.fascinated.utils.Manager; +import cc.fascinated.utils.MessageUtils; +import org.bukkit.Bukkit; +import org.bukkit.event.player.PlayerJoinEvent; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class TipManager extends Manager { + + public TipManager() { + Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> { + MessageUtils.broadcast(Lang.TIPS_FORMAT.getAsString() + .replace("%tip%", this.getRandomTip()) + ); + }, 0, Config.TIP_INTERVAL.getAsInt() , TimeUnit.MINUTES); + } + + /** + * Get a random tip. + * + * @return the random tip + */ + private String getRandomTip() { + List tips = Lang.TIPS_LIST.getAsStringList(); + return tips.get((int) (Math.random() * tips.size())); + } + + @Override + public void onPlayerJoin(Account account, PlayerJoinEvent event) { + account.sendMessage(Lang.TIPS_FORMAT.getAsString() + .replace("%tip%", this.getRandomTip()) + ); + account.sendMessage(Lang.TIPS_LOGIN_NOTIFICATION.getAsString()); + } +} diff --git a/src/main/java/cc/fascinated/worldsize/WorldSizeManager.java b/src/main/java/cc/fascinated/worldsize/WorldSizeManager.java index b2e562c..4ba9919 100644 --- a/src/main/java/cc/fascinated/worldsize/WorldSizeManager.java +++ b/src/main/java/cc/fascinated/worldsize/WorldSizeManager.java @@ -3,7 +3,7 @@ package cc.fascinated.worldsize; import cc.fascinated.Aetheria; import cc.fascinated.command.CommandManager; import cc.fascinated.placeholder.PlaceholderManager; -import cc.fascinated.worldsize.impl.WorldSizeCommand; +import cc.fascinated.worldsize.command.WorldSizeCommand; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.World; diff --git a/src/main/java/cc/fascinated/worldsize/impl/WorldSizeCommand.java b/src/main/java/cc/fascinated/worldsize/command/WorldSizeCommand.java similarity index 97% rename from src/main/java/cc/fascinated/worldsize/impl/WorldSizeCommand.java rename to src/main/java/cc/fascinated/worldsize/command/WorldSizeCommand.java index cfa47c1..0159184 100644 --- a/src/main/java/cc/fascinated/worldsize/impl/WorldSizeCommand.java +++ b/src/main/java/cc/fascinated/worldsize/command/WorldSizeCommand.java @@ -1,4 +1,4 @@ -package cc.fascinated.worldsize.impl; +package cc.fascinated.worldsize.command; import cc.fascinated.account.Account; import cc.fascinated.command.Command; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2b77a15..77da24b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -6,11 +6,14 @@ influxdb: discord: token: "MTEyOTEyMDY0NTk3Mjc2Njc3MA.G7VXPL.8iDzdTxScweAKByKnwY6PFcK07AehFfNvf_2Hk" + invite-url: "https://discord.gg/8qMjSXHGEw" version-warning: min-version: 763 # 1.20 message: "WARNING! You are using an outdated version of Minecraft. Please update to the latest version to avoid any issues." +tip-interval: 30 # in minutes + motd: header: "Aetheria" format: "%motd%" diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index 3c8a5d2..686aa15 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -25,6 +25,18 @@ staff-chat: usage: "Usage: /staffchat " format: "[SC] %player% » %message%" +tips: + format: "TIP! %tip%" + login-notification: "When you login, you will see a random tip." + tips: + - "Make sure to vote for the server to get rewards! /vote" + - "You can change your player name color with /playercolor" + - "You can see the world seed with /seed" + - "You can see the total file size of all worlds with /worldsize" + - "You can kill yourself with /kill" + - "View your vote stats with /votestats" + - "Join our Discord using /discord" + help-command: - "Commands:" - "/kill - Kills you" @@ -61,4 +73,5 @@ save-accounts-command: vote-stats-command: - "Your vote statistics:" - " Total Votes: %total-votes%" - - " Vote Tokens: %vote-tokens%" \ No newline at end of file + - " Vote Tokens: %vote-tokens%" +discord-command: "Join our Discord server: %invite-url%" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ac43ac3..8bb3cea 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,40 +5,4 @@ api-version: 1.20 author: Fascinated depend: - PlaceholderAPI - - ViaVersion -commands: - totaljoins: - description: "Shows the total amount of joins" - usage: "/totaljoins" - worldsize: - description: "Shows the size of all worlds" - usage: "/worldsize" - help: - description: "Shows the help message" - usage: "/help" - playercolor: - description: "Changes your player color" - usage: "/playercolor " - seed: - description: "Shows the seed of the world" - usage: "/seed" - vote: - description: "Shows the vote links" - usage: "/vote" - git: - description: "Shows the git information" - usage: "/git" - saveaccounts: - description: "Saves the accounts" - usage: "/saveaccounts" - commandspy: - description: "Toggles command spy" - usage: "/commandspy" - votestats: - description: "Shows your vote stats" - usage: "/votestats" - staffchat: - description: "Toggles staff chat" - usage: "/staffchat " - aliases: - - sc \ No newline at end of file + - ViaVersion \ No newline at end of file