Skip to content

Commit

Permalink
Merge pull request #74 from CraftLuna/feat/command-unregister
Browse files Browse the repository at this point in the history
Unregister commands onDisable
  • Loading branch information
poyrazinan authored Aug 20, 2024
2 parents c16c027 + e0aa1f1 commit 75d87dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/xyz/geik/farmer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import xyz.geik.farmer.database.SQL;
import xyz.geik.farmer.database.SQLite;
import xyz.geik.farmer.helpers.CacheLoader;
import xyz.geik.farmer.helpers.CommandHelper;
import xyz.geik.farmer.helpers.WorldHelper;
import xyz.geik.farmer.integrations.Integrations;
import xyz.geik.farmer.listeners.ListenerRegister;
Expand Down Expand Up @@ -148,6 +149,7 @@ public void onEnable() {
*/
public void onDisable() {
getSql().updateAllFarmers();
CommandHelper.unregisterCommands();
}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/xyz/geik/farmer/helpers/CommandHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package xyz.geik.farmer.helpers;

import com.google.common.collect.Lists;
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 xyz.geik.glib.shades.triumphteam.cmd.core.exceptions.CommandRegistrationException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

/**
* Utility class for unregistering commands
*
* @author Efe Kurban (hyperion), CraftLuna, TriumphTeam
*/
public class CommandHelper {

/**
* Unregisters every command related to Farmer v6
*/
public static void unregisterCommands() {
Lists.newArrayList("çiftçi", "farmer", "farm", "fm", "ciftci").forEach(CommandHelper::unregisterCommand);
}

/**
* Unregisters a command
*
* @param name Command's name
*/
private static void unregisterCommand(String name) {
getBukkitCommands(getCommandMap()).remove(name);
}

/**
* @return CommandMap object from Server
*/
@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!");
}
}

/**
* @return Map of Registered Commands, directly from Bukkit
*/
@NotNull
private static Map<String, Command> getBukkitCommands(@NotNull final CommandMap commandMap) {
try {
final Field bukkitCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
bukkitCommands.setAccessible(true);
//noinspection unchecked
return (Map<String, Command>) bukkitCommands.get(commandMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new CommandRegistrationException("Unable get Bukkit commands. Commands might not be registered correctly!");
}
}

}

0 comments on commit 75d87dc

Please sign in to comment.