Archived
0

Hotfix for unknown command error on click command

This commit is contained in:
Pepich 2018-01-15 18:47:19 +01:00
parent 3311330bc4
commit eca6013716

View File

@ -12,8 +12,9 @@ import net.nemez.chatapi.click.CallbackCommand;
import net.nemez.chatapi.click.Message; import net.nemez.chatapi.click.Message;
import net.nemez.chatapi.click.PlayerQuitListener; import net.nemez.chatapi.click.PlayerQuitListener;
public class ChatAPI { public class ChatAPI
{
/* message coloring permission */ /* message coloring permission */
public static final String PERMISSION_CHAT_COLOR = "chat.color"; public static final String PERMISSION_CHAT_COLOR = "chat.color";
/* message formatting permission */ /* message formatting permission */
@ -29,104 +30,109 @@ public class ChatAPI {
/* the actual command name for use in click callbacks */ /* the actual command name for use in click callbacks */
private static String internalCommandName; private static String internalCommandName;
/** /** Initializes ChatAPI and registers the required commands for clickable chat to function. */
* Initializes ChatAPI and registers the required commands for clickable chat to function. public static void initialize(JavaPlugin plugin)
*/ {
public static void initialize(JavaPlugin plugin) { if (internalCommandName != null)
if (internalCommandName != null) { {
return; return;
} }
Random rand = new Random(System.currentTimeMillis()); Random rand = new Random(System.currentTimeMillis());
internalCommandName = "chatapi-exec-" + Integer.toHexString(rand.nextInt(0xEFFF) + 0x1000); internalCommandName = "chatapi-exec-" + Integer.toHexString(rand.nextInt(0xEFFF) + 0x1000);
try { try
{
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cmdMap.setAccessible(true); cmdMap.setAccessible(true);
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer()); CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
map.register("net/nemez/chatapi", new CallbackCommand(internalCommandName)); map.register("net/nemez/chatapi", new CallbackCommand(internalCommandName));
} catch (Exception e) { }
catch (Exception e)
{
plugin.getLogger().severe("Failed to register internal command '" + internalCommandName + "'"); plugin.getLogger().severe("Failed to register internal command '" + internalCommandName + "'");
e.printStackTrace(); e.printStackTrace();
} }
internalCommandName = "chatapi:" + internalCommandName; // For some reason the "chatapi:..." cmd is no longer registered. Disabling this until further investigation and fixing.
// internalCommandName = "chatapi:" + internalCommandName;
plugin.getServer().getPluginManager().registerEvents(new PlayerQuitListener(), plugin); plugin.getServer().getPluginManager().registerEvents(new PlayerQuitListener(), plugin);
plugin.getLogger().info("ChatAPI initialized"); plugin.getLogger().info("ChatAPI initialized");
} }
/** /** Colorifies a message using &format codes. Respects permissions.
* Colorifies a message using &format codes. Respects permissions.
* *
* @param sender the command sender whose permissions to use. null if permissions are to be ignored. * @param sender the command sender whose permissions to use. null if permissions are to be ignored.
* @param message the message to color * @param message the message to color
* @return colored message * @return colored message */
*/ public static String colorify(CommandSender sender, String message)
public static String colorify(CommandSender sender, String message) { {
if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR)) { if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR))
{
message = message.replaceAll("&([0-9a-fA-FrR])", "§$1"); message = message.replaceAll("&([0-9a-fA-FrR])", "§$1");
} }
if (sender == null || sender.hasPermission(PERMISSION_CHAT_FORMAT)) { if (sender == null || sender.hasPermission(PERMISSION_CHAT_FORMAT))
{
message = message.replaceAll("&([l-oL-OrR])", "§$1"); message = message.replaceAll("&([l-oL-OrR])", "§$1");
} }
if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC)) { if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC))
{
message = message.replaceAll("&([kKrR])", "§$1"); message = message.replaceAll("&([kKrR])", "§$1");
} }
return message; return message;
} }
/** /** Sends a colorified message to the command sender.
* Sends a colorified message to the command sender.
* *
* @param sender the command sender to whom to send the message. * @param sender the command sender to whom to send the message.
* @param message the message to send. * @param message the message to send. */
*/ public static void send(CommandSender sender, String message)
public static void send(CommandSender sender, String message) { {
if (sender == null) { if (sender == null)
{
return; return;
} }
sender.sendMessage(colorify(null, message)); sender.sendMessage(colorify(null, message));
} }
/** /** Checks if a command sender has the permission node required to send chat messages.
* Checks if a command sender has the permission node required to send chat messages.
* *
* @param sender the command sender to check. * @param sender the command sender to check.
* @return true/false if sender can chat or is null. * @return true/false if sender can chat or is null. */
*/ public static boolean canChat(CommandSender sender)
public static boolean canChat(CommandSender sender) { {
if (sender == null) { if (sender == null)
{
return true; return true;
}else{ }
else
{
return sender.hasPermission(PERMISSION_CHAT_USE); return sender.hasPermission(PERMISSION_CHAT_USE);
} }
} }
/** /** Creates a new message object that will be sent to the given command sender with regards to the second command sender's permissions.
* Creates a new message object that will be sent to the given command sender with regards to the second command sender's permissions.
* *
* @param sender the command sender to whom to send the message. * @param sender the command sender to whom to send the message.
* @param permissionSender the command sender whose permissions to use. * @param permissionSender the command sender whose permissions to use.
* @return message object * @return message object */
*/ public static Message createMessage(CommandSender sender, CommandSender permissionSender)
public static Message createMessage(CommandSender sender, CommandSender permissionSender) { {
return new Message(sender, permissionSender); return new Message(sender, permissionSender);
} }
/** /** Creates a new message object that will be sent to the given command sender.
* Creates a new message object that will be sent to the given command sender.
* *
* @param sender the command sender to whom to send the message. * @param sender the command sender to whom to send the message.
* @return message object. * @return message object. */
*/ public static Message createMessage(CommandSender sender)
public static Message createMessage(CommandSender sender) { {
return createMessage(sender, null); return createMessage(sender, null);
} }
/** /** Gets the name of the internal ChatAPI command used for click callbacks.
* Gets the name of the internal ChatAPI command used for click callbacks.
* This function is used internally and you don't need to worry about it. * This function is used internally and you don't need to worry about it.
* *
* @return callback command name * @return callback command name */
*/ public static String getInternalCallbackCommand()
public static String getInternalCallbackCommand() { {
return internalCommandName; return internalCommandName;
} }
} }