package net.piratjsk.nocmds;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Utils {

    public static boolean isTabCompleteEventSupported() {
        return classExist("org.bukkit.event.server.TabCompleteEvent");
    }

    public static boolean isSpigotConfigSupported() {
        return classExist("org.spigotmc.SpigotConfig");
    }

    private static boolean classExist(final String clazz) {
        try {
            Class.forName(clazz);
            return true;
        } catch (final ClassNotFoundException e) {
            return false;
        }
    }

    public static String colorize(final String uncoloredString) {
        return ChatColor.translateAlternateColorCodes('&', uncoloredString);
    }

    public static boolean commandExists(final String command) {
        final String cmd = command.split(" ")[0];
        return Bukkit.getHelpMap().getHelpTopic(cmd) != null;
    }

    public static void sendMsg(String message, final String command, final CommandSender sender) {
        message = message
                .replaceAll("(%name%|%player%)", sender.getName())
                .replaceAll("%command%", command);
        if (sender instanceof Player)
            message = message.replaceAll("%displayname%", ((Player) sender).getDisplayName());
        else
            message = message.replaceAll("%displayname%", sender.getName());

        sender.sendMessage(message);
    }

}

@@ -78,16 +78,54 @@
 tColor;%0A
+import org.bukkit.command.CommandMap;%0A
 import o
@@ -191,16 +191,49 @@
 layer;%0A%0A
+import java.lang.reflect.Field;%0A%0A
 public c
@@ -508,14 +508,13 @@
    p
-rivate
+ublic
  sta
@@ -991,70 +991,447 @@
 )%5B0%5D
-;%0A        return Bukkit.getHelpMap().getHelpTopic(cmd) != null
+.replaceFirst(%22/%22,%22%22);%0A        try %7B%0A            final Field field = Bukkit.getServer().getClass().getDeclaredField(%22commandMap%22);%0A            field.setAccessible(true);%0A            final CommandMap cmdMap = (CommandMap) field.get(Bukkit.getServer());%0A            return cmdMap.getCommand(cmd) != null;%0A        %7D catch (final NoSuchFieldException %7C IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return true
 ;%0A  

fix cmds detection