@EventHandler
  public void onAsyncChat(AsyncPlayerChatEvent event) {
    String message = event.getMessage();
    Player player = event.getPlayer();
    // Chat commands start with a '!' delimiter, so check if it's a chat command
    if (message.startsWith("!")) {
      event.setCancelled(true);
      String command = message;
      String[] args = PATTERN_ON_SPACE.split(message);
      if (args.length > 0) {
        command = args[0];
        try {
          args = Arrays_copyOfRange(args, 1, args.length);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }

      command = StringUtils.remove(command, "!");
      if (ChatCommand.isValidCommand(command)) {
        ChatCommand chatCommand = ChatCommand.getCommand(command);
        int argsRequired = chatCommand.getMinArgs();
        if (args.length >= argsRequired) {
          chatCommand.doCommand(player, args);
        } else {
          Players.sendMessage(
              player, GameMessages.INSUFFICIENT_CHAT_COMMAND_ARGUMENTS(command, argsRequired));
        }
      } else {
        Players.sendMessage(player, GameMessages.INVALID_CHAT_COMMAND(command));
      }
    }
  }
 @EventHandler(priority = EventPriority.HIGH)
 public void onPlayerJoin(PlayerJoinEvent event) {
   File datafile =
       new File(
           plugin.getDataFolder()
               + File.separator
               + "userdata"
               + File.separator
               + event.getPlayer().getName().toLowerCase()
               + ".yml");
   if (!datafile.exists()) {
     log.info("[RoyalCommands] Creating userdata for user " + event.getPlayer().getName() + ".");
     try {
       FileConfiguration out = YamlConfiguration.loadConfiguration(datafile);
       out.set("name", event.getPlayer().getName());
       String dispname = event.getPlayer().getDisplayName();
       if (dispname == null || dispname.equals("")) dispname = event.getPlayer().getName();
       out.set("dispname", dispname);
       out.set("ip", event.getPlayer().getAddress().getAddress().toString().replace("/", ""));
       out.set("banreason", "");
       out.set("allow-tp", true);
       out.save(datafile);
       log.info("[RoyalCommands] Userdata creation finished.");
     } catch (Exception e) {
       log.severe(
           "[RoyalCommands] Could not create userdata for user "
               + event.getPlayer().getName()
               + "!");
       e.printStackTrace();
     }
     if (plugin.useWelcome) {
       String welcomemessage = plugin.welcomeMessage;
       welcomemessage = welcomemessage.replace("{name}", event.getPlayer().getName());
       String dispname = event.getPlayer().getDisplayName();
       if (dispname == null || dispname.equals("")) dispname = event.getPlayer().getName();
       welcomemessage = welcomemessage.replace("{dispname}", dispname);
       welcomemessage = welcomemessage.replace("{world}", event.getPlayer().getWorld().getName());
       plugin.getServer().broadcastMessage(welcomemessage);
     }
     if (plugin.stsNew)
       event.getPlayer().teleport(event.getPlayer().getWorld().getSpawnLocation());
   } else {
     log.info("[RoyalCommands] Updating the IP for " + event.getPlayer().getName() + ".");
     String playerip = event.getPlayer().getAddress().getAddress().toString();
     playerip = playerip.replace("/", "");
     PConfManager.setPValString(event.getPlayer(), playerip, "ip");
   }
   if (plugin.motdLogin) CmdMotd.showMotd(event.getPlayer());
   if (plugin.sendToSpawn) {
     if (plugin.stsBack) CmdBack.backdb.put(event.getPlayer(), event.getPlayer().getLocation());
     event.getPlayer().teleport(event.getPlayer().getWorld().getSpawnLocation());
   }
 }