From 9c636d2a9b7a6ea766a9ce71f33ef826a41509da Mon Sep 17 00:00:00 2001 From: Dico200 Date: Tue, 30 May 2017 15:15:01 +0200 Subject: [PATCH] Ensure all command aliases are removed in onDisable, and only if they were not replaced. --- .../blockplacemods/BlockPlaceMods.java | 79 +++++++++++-------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/src/com/redstoner/modules/blockplacemods/BlockPlaceMods.java b/src/com/redstoner/modules/blockplacemods/BlockPlaceMods.java index c864f7c..081eabd 100644 --- a/src/com/redstoner/modules/blockplacemods/BlockPlaceMods.java +++ b/src/com/redstoner/modules/blockplacemods/BlockPlaceMods.java @@ -1,16 +1,5 @@ package com.redstoner.modules.blockplacemods; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Map; - -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.Listener; - import com.nemez.cmdmgr.Command; import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.Version; @@ -22,9 +11,15 @@ import com.redstoner.modules.blockplacemods.mods.ModAbstract; import com.redstoner.modules.blockplacemods.mods.ModToggledAbstract; import com.redstoner.utils.CommandException; import com.redstoner.utils.CommandMap; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; + +import java.util.*; @AutoRegisterListener -@Version(major = 3, minor = 2, revision = 6, compatible = 3) +@Version(major = 3, minor = 2, revision = 7, compatible = 3) public final class BlockPlaceMods implements Module, Listener { public static String PREFIX = ChatColor.GRAY + "[" + ChatColor.DARK_GREEN + "BPM" + ChatColor.GRAY + "]" @@ -41,30 +36,15 @@ public final class BlockPlaceMods implements Module, Listener try { Map commandMap = CommandMap.getCommandMap(); - // @noformat - String pluginName = Main.plugin.getName().toLowerCase(); - String[] aliases = {"mod", pluginName + ":mod", - "set", pluginName + ":mod", - "toggle", pluginName + ":toggle" - }; - // @format - org.bukkit.command.Command command = new org.bukkit.command.Command("mod") - { - @Override - public boolean execute(CommandSender sender, String label, String[] args) - { - onModCommand(sender, String.join(" ", args)); - return true; - } - }; - for (String alias : aliases) + org.bukkit.command.Command command = new BlockPlaceModsCommand(); + for (String alias : getCommandAliases()) { commandMap.put(alias, command); } } catch (ReflectiveOperationException ex) { - throw new Error(ex); + return false; } return true; } @@ -76,16 +56,33 @@ public final class BlockPlaceMods implements Module, Listener { mod.unregisterListeners(); } + try { Map commandMap = CommandMap.getCommandMap(); - commandMap.remove("mod"); - commandMap.remove(Main.plugin.getName().toLowerCase() + ":mod"); + for (String alias : getCommandAliases()) + { + if (commandMap.get(alias).getClass() == BlockPlaceModsCommand.class) + { + commandMap.remove(alias); + } + } } catch (Exception ignored) {} } + private static String[] getCommandAliases() + { + String pluginName = Main.plugin.getName().toLowerCase(); + // @noformat + return new String[]{"mod", pluginName + ":mod", + "set", pluginName + ":mod", + "toggle", pluginName + ":toggle" + }; + // @format + } + @Command(hook = "mod_empty") public void onModEmptyCommand(CommandSender sender) { @@ -177,4 +174,22 @@ public final class BlockPlaceMods implements Module, Listener } return modName; } + + private class BlockPlaceModsCommand extends org.bukkit.command.Command + { + public BlockPlaceModsCommand() + { + super("mod"); + String[] aliases = getCommandAliases(); + setAliases(Arrays.asList(Arrays.copyOfRange(aliases, 1, aliases.length))); + } + + @Override + public boolean execute(CommandSender sender, String label, String[] args) + { + onModCommand(sender, String.join(" ", args)); + return true; + } + } + }