From de63a2549606f09d69b83950f0535d0c0f2dcba3 Mon Sep 17 00:00:00 2001 From: HypherionMC Date: Sun, 6 Aug 2023 12:19:33 +0200 Subject: [PATCH] [CHORE] Code Cleanup --- build.gradle | 5 +- gradle.properties | 2 +- .../core/accounts/MinecraftAccount.java | 40 +++---- .../sdlink/core/config/SDLinkConfig.java | 6 +- .../core/config/impl/BotCommandsConfig.java | 28 ----- .../impl/LinkAndWhitelistConfigSettings.java | 4 + .../config/impl/LinkedCommandsConfig.java | 36 ------ .../core/discord/commands/CommandManager.java | 16 +-- .../commands/slash/SDLinkSlashCommand.java | 7 -- .../commands/slash/mc/MCSlashCommand.java | 104 ------------------ .../ViewWhitelistedAccountsSlashCommand.java | 14 ++- .../sdlink/core/managers/RoleManager.java | 12 -- 12 files changed, 40 insertions(+), 234 deletions(-) delete mode 100644 src/main/java/com/hypherionmc/sdlink/core/config/impl/BotCommandsConfig.java delete mode 100644 src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkedCommandsConfig.java delete mode 100644 src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/mc/MCSlashCommand.java diff --git a/build.gradle b/build.gradle index 8f814af..de35aaf 100644 --- a/build.gradle +++ b/build.gradle @@ -4,8 +4,9 @@ plugins { id 'maven-publish' id "com.diffplug.spotless" version "6.13.0" } + apply plugin: 'java' -java.toolchain.languageVersion = JavaLanguageVersion.of(16) +java.toolchain.languageVersion = JavaLanguageVersion.of(17) group 'com.hypherionmc.sdlink' version = "${version_major}.${version_minor}.${version_patch}" @@ -144,7 +145,7 @@ spotless { tasks.withType(JavaCompile).configureEach { it.options.encoding = 'UTF-8' - it.options.release = 16 + it.options.release = 17 } tasks.withType(GenerateModuleMetadata) { diff --git a/gradle.properties b/gradle.properties index 7067b06..4c174c9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ version_major=0 version_minor=0 -version_patch=16 +version_patch=17 shade_group=com.hypherionmc.sdlink.shaded. diff --git a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java index 9ff885e..0b37188 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java +++ b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java @@ -102,22 +102,6 @@ public class MinecraftAccount { return sdlinkDatabase.findById(uuid, SDLinkAccount.class); } - public String getUsername() { - return username; - } - - public UUID getUuid() { - return uuid; - } - - public boolean isValid() { - return isValid; - } - - public boolean isOffline() { - return isOffline; - } - /** * Link a Minecraft account to a discord account * @param member The discord user @@ -336,11 +320,11 @@ public class MinecraftAccount { System.out.println("[AutoWhiteList] Has Auto Role: " + hasAutoRole); } - return hasAutoRole; + return hasAutoRole && !account.getDiscordID().isEmpty(); } } - return !SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.accountLinking; + return false; } /** @@ -388,7 +372,23 @@ public class MinecraftAccount { return BotController.INSTANCE.getJDA().getUserById(storedAccount.getDiscordID()); } - // Helper Methods + public String getUsername() { + return username; + } + + public UUID getUuid() { + return uuid; + } + + public boolean isValid() { + return isValid; + } + + public boolean isOffline() { + return isOffline; + } + + // private static Pair fetchPlayer(String name) { try { BufferedReader read = new BufferedReader(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream())); @@ -425,5 +425,5 @@ public class MinecraftAccount { private static Pair offlinePlayer(String offlineName) { return Pair.of(offlineName, UUID.nameUUIDFromBytes(("OfflinePlayer:" + offlineName).getBytes(StandardCharsets.UTF_8))); } - + // } diff --git a/src/main/java/com/hypherionmc/sdlink/core/config/SDLinkConfig.java b/src/main/java/com/hypherionmc/sdlink/core/config/SDLinkConfig.java index 4d911ad..2578a30 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/config/SDLinkConfig.java +++ b/src/main/java/com/hypherionmc/sdlink/core/config/SDLinkConfig.java @@ -24,7 +24,7 @@ public class SDLinkConfig extends ModuleConfig { // DO NOT REMOVE TRANSIENT HERE... OTHERWISE, THE STUPID CONFIG LIBRARY // WILL TRY TO WRITE THESE TO THE CONFIG public transient static SDLinkConfig INSTANCE; - public transient static int configVer = 8; + public transient static int configVer = 9; @Path("general") @SpecComment("General Mod Config") @@ -54,10 +54,6 @@ public class SDLinkConfig extends ModuleConfig { @SpecComment("Configure Whitelisting and Account Linking through the bot") public LinkAndWhitelistConfigSettings whitelistingAndLinking = new LinkAndWhitelistConfigSettings(); - @Path("botCommands") - @SpecComment("Enable or Disable certain bot commands") - public BotCommandsConfig botCommands = new BotCommandsConfig(); - @Path("minecraftCommands") @SpecComment("Execute Minecraft commands in Discord") public MinecraftCommands linkedCommands = new MinecraftCommands(); diff --git a/src/main/java/com/hypherionmc/sdlink/core/config/impl/BotCommandsConfig.java b/src/main/java/com/hypherionmc/sdlink/core/config/impl/BotCommandsConfig.java deleted file mode 100644 index df2cc40..0000000 --- a/src/main/java/com/hypherionmc/sdlink/core/config/impl/BotCommandsConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of sdlink-core, licensed under the MIT License (MIT). - * Copyright HypherionSA and Contributors - */ -package com.hypherionmc.sdlink.core.config.impl; - -import me.hypherionmc.moonconfig.core.conversion.Path; -import me.hypherionmc.moonconfig.core.conversion.SpecComment; - -/** - * @author HypherionSA - * Config Structure to allow disabling some bot commands - */ -public class BotCommandsConfig { - - @Path("allowPlayerList") - @SpecComment("Enable/Disable the Player List command") - public boolean allowPlayerList = true; - - @Path("allowServerStatus") - @SpecComment("Enable/Disable the Server Status command") - public boolean allowServerStatus = true; - - @Path("allowHelpCommand") - @SpecComment("Enable/Disable the Help command") - public boolean allowHelpCommand = true; - -} diff --git a/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkAndWhitelistConfigSettings.java b/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkAndWhitelistConfigSettings.java index b255186..95992c5 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkAndWhitelistConfigSettings.java +++ b/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkAndWhitelistConfigSettings.java @@ -55,6 +55,10 @@ public class LinkAndWhitelistConfigSettings { @SpecComment("Automatically link Minecraft and Discord Accounts when a user is whitelisted") public boolean linkedWhitelist = false; + @Path("autoWhitelist") + @SpecComment("Allow automatically whitelisting and de-whitelisting users with autoWhitelistRoles") + public boolean autoWhitelist = false; + @Path("autoWhitelistRoles") @SpecComment("Users with linked discord accounts, that contain any of these roles will be automatically whitelisted") public List autoWhitelistRoles = new ArrayList<>(); diff --git a/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkedCommandsConfig.java b/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkedCommandsConfig.java deleted file mode 100644 index 62affea..0000000 --- a/src/main/java/com/hypherionmc/sdlink/core/config/impl/LinkedCommandsConfig.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of sdlink-core, licensed under the MIT License (MIT). - * Copyright HypherionSA and Contributors - */ -package com.hypherionmc.sdlink.core.config.impl; - -/** - * @author HypherionSA - * Main Config Structure to control Discord -> MC Commands - */ -/*public class LinkedCommandsConfig { - - @Path("enabled") - @SpecComment("Should linked commands be enabled") - public boolean enabled = false; - - @Path("commands") - @SpecComment("Commands to be linked") - public List commands = new ArrayList<>(); - - public static class Command { - @Path("mcCommand") - @SpecComment("The Minecraft Command. Use %args% to pass everything after the discordCommand to Minecraft") - public String mcCommand; - - @Path("discordCommand") - @SpecComment("The command slug in discord. To be used as /mc slug") - public String discordCommand; - - @Path("discordRole") - @SpecComment("Discord Role Name of ID of the role that is allowed to execute this command. If empty, all players will be allowed to use this command") - public String discordRole; - } - -} -*/ \ No newline at end of file diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java index c37673c..5563391 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java @@ -54,23 +54,13 @@ public class CommandManager { } // Enable the Server Status command - if (SDLinkConfig.INSTANCE.botCommands.allowServerStatus) { - commands.add(new ServerStatusSlashCommand()); - } + commands.add(new ServerStatusSlashCommand()); // Enable the Player List command - if (SDLinkConfig.INSTANCE.botCommands.allowPlayerList) { - commands.add(new PlayerListSlashCommand()); - } + commands.add(new PlayerListSlashCommand()); // Enable the Help command - if (SDLinkConfig.INSTANCE.botCommands.allowHelpCommand) { - commands.add(new HelpSlashCommand()); - } - - if (SDLinkConfig.INSTANCE.linkedCommands.enabled) { - //commands.add(new MCSlashCommand()); - } + commands.add(new HelpSlashCommand()); } /** diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/SDLinkSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/SDLinkSlashCommand.java index 26de6e5..2a78efb 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/SDLinkSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/SDLinkSlashCommand.java @@ -13,13 +13,6 @@ import com.jagrosh.jdautilities.command.SlashCommand; public abstract class SDLinkSlashCommand extends SlashCommand { public SDLinkSlashCommand(boolean requiresPerms) { - /*if (requiresPerms) { - if (RoleManager.getStaffRole() != null) { - this.requiredRole = RoleManager.getStaffRole().getName(); - } else { - this.userPermissions = new Permission[] { Permission.ADMINISTRATOR, Permission.KICK_MEMBERS }; - } - }*/ this.guildOnly = true; } diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/mc/MCSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/mc/MCSlashCommand.java deleted file mode 100644 index 80057f7..0000000 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/mc/MCSlashCommand.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * This file is part of sdlink-core, licensed under the MIT License (MIT). - * Copyright HypherionSA and Contributors - */ -package com.hypherionmc.sdlink.core.discord.commands.slash.mc; - -/*import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; -import com.hypherionmc.sdlink.core.config.SDLinkConfig; -import com.hypherionmc.sdlink.core.config.impl.LinkedCommandsConfig; -import com.hypherionmc.sdlink.core.database.SDLinkAccount; -import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; -import com.hypherionmc.sdlink.core.managers.RoleManager; -import com.hypherionmc.sdlink.core.messaging.Result; -import com.hypherionmc.sdlink.core.services.SDLinkPlatform; -import com.jagrosh.jdautilities.command.SlashCommandEvent; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.OptionData; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabase; - -public class MCSlashCommand extends SDLinkSlashCommand { - - public MCSlashCommand() { - super(false); - this.name = "mc"; - this.help = "Execute Minecraft Command from Discord"; - - this.options = new ArrayList<>() {{ - add(new OptionData(OptionType.STRING, "slug", "The discordCommand slug defined in the config").setRequired(true)); - add(new OptionData(OptionType.STRING, "args0", "Additional arguments to pass to the %args% variable").setRequired(false)); - add(new OptionData(OptionType.STRING, "args1", "Additional arguments to pass to the %args% variable").setRequired(false)); - add(new OptionData(OptionType.STRING, "args2", "Additional arguments to pass to the %args% variable").setRequired(false)); - add(new OptionData(OptionType.STRING, "args3", "Additional arguments to pass to the %args% variable").setRequired(false)); - add(new OptionData(OptionType.STRING, "args4", "Additional arguments to pass to the %args% variable").setRequired(false)); - add(new OptionData(OptionType.STRING, "args5", "Additional arguments to pass to the %args% variable").setRequired(false)); - }}; - } - - @Override - protected void execute(SlashCommandEvent event) { - if (SDLinkConfig.INSTANCE.linkedCommands.enabled) { - String slug = event.getOption("slug") != null ? event.getOption("slug").getAsString() : ""; - String args0 = event.getOption("args0") != null ? event.getOption("args0").getAsString() : ""; - String args1 = event.getOption("args1") != null ? event.getOption("args1").getAsString() : ""; - String args2 = event.getOption("args2") != null ? event.getOption("args2").getAsString() : ""; - String args3 = event.getOption("args3") != null ? event.getOption("args3").getAsString() : ""; - String args4 = event.getOption("args4") != null ? event.getOption("args4").getAsString() : ""; - String args5 = event.getOption("args5") != null ? event.getOption("args5").getAsString() : ""; - - Optional linkedCommand = SDLinkConfig.INSTANCE.linkedCommands.commands.stream().filter(c -> c.discordCommand.equalsIgnoreCase(slug)).findFirst(); - - StringBuilder args = new StringBuilder(); - if (!args0.isEmpty()) - args.append(args0); - if (!args1.isEmpty()) - args.append(" ").append(args1); - if (!args2.isEmpty()) - args.append(" ").append(args2); - if (!args3.isEmpty()) - args.append(" ").append(args3); - if (!args4.isEmpty()) - args.append(" ").append(args4); - if (!args5.isEmpty()) - args.append(" ").append(args5); - - sdlinkDatabase.reloadCollection("accounts"); - List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); - Optional account = accounts.stream().filter(u -> u.getDiscordID().equals(event.getMember().getId())).findFirst(); - - linkedCommand.ifPresent(command -> { - if (!command.discordRole.isEmpty()) { - Role role = RoleManager.getCommandRoles().isEmpty() ? null : RoleManager.getCommandRoles().get(command.discordCommand); - - boolean userRole = role != null && event.getMember().getRoles().stream().anyMatch(r -> r.getIdLong() == role.getIdLong()); - if (userRole) { - executeCommand(event, command, args.toString(), event.getMember(), account.orElse(null)); - } else { - event.reply("You need the " + role.getName() + " role to perform this action").setEphemeral(true).queue(); - } - } else { - executeCommand(event, command, args.toString(), event.getMember(), account.orElse(null)); - } - }); - - if (linkedCommand.isEmpty()) { - event.reply("Cannot find linked command " + slug).setEphemeral(true).queue(); - } - - } else { - event.reply("Linked commands are not enabled!").setEphemeral(true).queue(); - } - } - - private void executeCommand(SlashCommandEvent event, LinkedCommandsConfig.Command mcCommand, String args, Member member, SDLinkAccount account) { - Result result = SDLinkPlatform.minecraftHelper.executeMinecraftCommand(mcCommand.mcCommand, args, member, account); - event.reply(result.getMessage()).setEphemeral(true).queue(); - } -}*/ diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/ViewWhitelistedAccountsSlashCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/ViewWhitelistedAccountsSlashCommand.java index 7123692..d02f5df 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/ViewWhitelistedAccountsSlashCommand.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/ViewWhitelistedAccountsSlashCommand.java @@ -4,8 +4,10 @@ */ package com.hypherionmc.sdlink.core.discord.commands.slash.whitelist; +import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; import com.hypherionmc.sdlink.core.database.SDLinkAccount; import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.services.SDLinkPlatform; import com.hypherionmc.sdlink.core.util.MessageUtil; import com.jagrosh.jdautilities.command.SlashCommandEvent; import com.jagrosh.jdautilities.menu.EmbedPaginator; @@ -38,29 +40,29 @@ public class ViewWhitelistedAccountsSlashCommand extends SDLinkSlashCommand { protected void execute(SlashCommandEvent event) { EmbedPaginator.Builder paginator = MessageUtil.defaultPaginator(event); sdlinkDatabase.reloadCollection("accounts"); - List accounts = sdlinkDatabase.findAll(SDLinkAccount.class).stream().filter(SDLinkAccount::isWhitelisted).toList(); + List players = SDLinkPlatform.minecraftHelper.getWhitelistedPlayers(); EmbedBuilder builder = new EmbedBuilder(); ArrayList pages = new ArrayList<>(); AtomicInteger count = new AtomicInteger(); - if (accounts.isEmpty()) { + if (players.isEmpty()) { event.reply("There are no whitelisted accounts for this discord").setEphemeral(true).queue(); return; } - MessageUtil.listBatches(accounts, 10).forEach(itm -> { + MessageUtil.listBatches(players, 10).forEach(itm -> { count.getAndIncrement(); builder.clear(); - builder.setTitle("Whitelisted Accounts - Page " + count + "/" + (int)Math.ceil(((float)accounts.size() / 10))); + builder.setTitle("Whitelisted Accounts - Page " + count + "/" + (int)Math.ceil(((float)players.size() / 10))); builder.setColor(Color.GREEN); StringBuilder sBuilder = new StringBuilder(); itm.forEach(v -> { Member member = null; - if (v.getDiscordID() != null && !v.getDiscordID().isEmpty()) { - member = event.getGuild().getMemberById(v.getDiscordID()); + if (v.getDiscordUser() != null) { + member = event.getGuild().getMemberById(v.getDiscordUser().getId()); } sBuilder.append(v.getUsername()).append(" -> ").append(member == null ? "Unlinked" : member.getAsMention()).append("\r\n"); diff --git a/src/main/java/com/hypherionmc/sdlink/core/managers/RoleManager.java b/src/main/java/com/hypherionmc/sdlink/core/managers/RoleManager.java index cd86ab4..927bfe5 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/managers/RoleManager.java +++ b/src/main/java/com/hypherionmc/sdlink/core/managers/RoleManager.java @@ -47,18 +47,6 @@ public class RoleManager { autoWhitelistRoles.add(role); }); } - - /*if (SDLinkConfig.INSTANCE.linkedCommands.enabled) { - commandRoles.clear(); - SDLinkConfig.INSTANCE.linkedCommands.permissions.forEach(cmd -> { - if (!cmd.role.isEmpty() && !cmd.role.equals("0")) { - Role role = getRole(errCount, builder, "Command Usage", cmd.role); - if (role != null) { - cmd.commands.forEach(c -> commandRoles.putIfAbsent(c, role)); - } - } - }); - }*/ } /**