diff --git a/bukkit/src/main/java/net/leaderos/plugin/helpers/CommandHelper.java b/bukkit/src/main/java/net/leaderos/plugin/helpers/CommandHelper.java new file mode 100644 index 0000000000..5c93429f2c --- /dev/null +++ b/bukkit/src/main/java/net/leaderos/plugin/helpers/CommandHelper.java @@ -0,0 +1,65 @@ +package net.leaderos.plugin.helpers; + +import dev.triumphteam.cmd.core.exceptions.CommandRegistrationException; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; + +/** + * Unload mechanism for commands + * @author WaterArchery, hyperion + * @since 1.0.0 + */ +public class CommandHelper { + + /** + * Unload all cached commands + * + * @since 1.0 + * @author WaterArchery + */ + public static void unregisterCommands(List commands) { + commands.forEach(command -> getBukkitCommands(getCommandMap()).remove(command)); + } + + /** + * Get command map from server + * + * @since 1.0 + * @author hyperion + */ + @NotNull + private static CommandMap getCommandMap() { + try { + final Server server = Bukkit.getServer(); + final Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap"); + getCommandMap.setAccessible(true); + + return (CommandMap) getCommandMap.invoke(server); + } catch (final Exception ignored) { + throw new CommandRegistrationException("Unable get Command Map. Commands will not be registered!"); + } + } + + // copied from triumph-cmd, credit goes to triumph-team + @NotNull + private static Map getBukkitCommands(@NotNull final CommandMap commandMap) { + try { + final Field bukkitCommands = SimpleCommandMap.class.getDeclaredField("knownCommands"); + bukkitCommands.setAccessible(true); + //noinspection unchecked + return (Map) bukkitCommands.get(commandMap); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new CommandRegistrationException("Unable get Bukkit commands. Commands might not be registered correctly!"); + } + } + +} diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/auth/AuthModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/auth/AuthModule.java index 79320326d7..cbce289168 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/auth/AuthModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/auth/AuthModule.java @@ -1,9 +1,13 @@ package net.leaderos.plugin.modules.auth; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.auth.commands.AuthCommand; import net.leaderos.shared.modules.LeaderOSModule; +import java.util.Arrays; +import java.util.Collections; + /** * Auth module of leaderos-plugin * @@ -23,7 +27,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new AuthCommand()); + CommandHelper.unregisterCommands(Collections.singletonList("auth")); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/BazaarModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/BazaarModule.java index 8c5f1c7681..f0f1de8c07 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/BazaarModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/BazaarModule.java @@ -2,9 +2,12 @@ import lombok.Getter; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.bazaar.commands.BazaarCommand; import net.leaderos.shared.modules.LeaderOSModule; +import java.util.Arrays; + /** * Bazaar module of leaderos-plugin * @@ -31,7 +34,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new BazaarCommand()); + CommandHelper.unregisterCommands(Arrays.asList("bazaar", "webbazaar", "pazar")); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java index 0f4097d17b..2afe8f5d13 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java @@ -1,6 +1,7 @@ package net.leaderos.plugin.modules.cache; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.cache.commands.CacheCommand; import net.leaderos.plugin.modules.cache.listeners.CacheUpdateEvent; import net.leaderos.plugin.modules.cache.listeners.LoginListener; @@ -9,6 +10,9 @@ import net.leaderos.shared.modules.LeaderOSModule; import org.bukkit.event.HandlerList; +import java.util.Arrays; +import java.util.Collections; + /** * Cache module of leaderos-plugin * @@ -61,7 +65,7 @@ public void onDisable() { // Removes cache User.getUserList().clear(); // Unregister Command - Bukkit.getCommandManager().unregisterCommand(new CacheCommand()); + CommandHelper.unregisterCommands(Collections.singletonList("leaderos-cache")); // Placeholder unloader if( org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) new Placeholders().unregister(); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/credit/CreditModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/credit/CreditModule.java index 652ef2359d..e1df1ebad8 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/credit/CreditModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/credit/CreditModule.java @@ -1,9 +1,12 @@ package net.leaderos.plugin.modules.credit; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.credit.commands.CreditCommand; import net.leaderos.shared.modules.LeaderOSModule; +import java.util.Arrays; + /** * Credit module of leaderos-plugin * @@ -23,7 +26,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new CreditCommand()); + CommandHelper.unregisterCommands(Arrays.asList("credit", "credits", "kredi")); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/discord/DiscordModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/discord/DiscordModule.java index 17bc370456..e1c6bf9d62 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/discord/DiscordModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/discord/DiscordModule.java @@ -1,9 +1,12 @@ package net.leaderos.plugin.modules.discord; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.discord.commands.SyncCommand; import net.leaderos.shared.modules.LeaderOSModule; +import java.util.Arrays; + /** * Discord module of leaderos-plugin * @@ -23,7 +26,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new SyncCommand()); + CommandHelper.unregisterCommands(Arrays.asList("discord-sync", "discord-link")); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/donations/DonationsModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/donations/DonationsModule.java index 0ef02f4272..607b3f54ec 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/donations/DonationsModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/donations/DonationsModule.java @@ -5,6 +5,7 @@ import com.cryptomorin.xseries.profiles.objects.Profileable; import net.leaderos.plugin.Bukkit; import net.leaderos.plugin.helpers.ChatUtil; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.helpers.ItemUtil; import net.leaderos.plugin.modules.donations.commands.DonationsCommand; import net.leaderos.plugin.modules.donations.model.Donation; @@ -16,8 +17,8 @@ import org.bukkit.inventory.meta.SkullMeta; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; import java.util.Objects; -import java.util.UUID; /** * Donations module main class @@ -40,7 +41,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new DonationsCommand()); + CommandHelper.unregisterCommands(Arrays.asList("donations", "recentdonations", "krediyukleyenler")); Timer.taskid.cancel(); if( org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) new Placeholders().unregister(); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/VoucherModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/VoucherModule.java index 9083b3a4f3..3e889aebc5 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/VoucherModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/VoucherModule.java @@ -2,12 +2,15 @@ import lombok.Getter; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.voucher.commands.VoucherCommand; import net.leaderos.plugin.modules.voucher.listeners.VoucherListener; import net.leaderos.plugin.modules.voucher.data.Data; import net.leaderos.shared.modules.LeaderOSModule; import org.bukkit.event.HandlerList; +import java.util.Arrays; + /** * Voucher module of leaderos plugin * @author poyrazinan @@ -41,7 +44,7 @@ public void onEnable() { */ public void onDisable() { HandlerList.unregisterAll(voucherListener); - Bukkit.getCommandManager().unregisterCommand(new VoucherCommand()); + CommandHelper.unregisterCommands(Arrays.asList("creditvoucher", "creditsvoucher", "creditsvouchers", "kredikagidi")); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/WebStoreModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/WebStoreModule.java index 69093655ea..b84725760d 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/WebStoreModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/WebStoreModule.java @@ -1,12 +1,14 @@ package net.leaderos.plugin.modules.webstore; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.helpers.CommandHelper; import net.leaderos.plugin.modules.webstore.commands.WebStoreCommand; import net.leaderos.plugin.modules.webstore.model.Category; import net.leaderos.shared.exceptions.RequestException; import net.leaderos.shared.modules.LeaderOSModule; import java.io.IOException; +import java.util.Arrays; /** * Web-store module of leaderos-plugin @@ -27,7 +29,7 @@ public void onEnable() { * onDisable method of module */ public void onDisable() { - Bukkit.getCommandManager().unregisterCommand(new WebStoreCommand()); + CommandHelper.unregisterCommands(Arrays.asList("webstore", "webshop", "sitemarket", "webmarket")); } /**