67 lines
2.4 KiB
Java
67 lines
2.4 KiB
Java
package xyz.etztech.mixtape.commands;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import xyz.etztech.mixtape.Database;
|
|
import xyz.etztech.mixtape.Mixtape;
|
|
import xyz.etztech.mixtape.MixtapeUtil;
|
|
|
|
import java.util.Arrays;
|
|
|
|
public class CommandAlias implements CommandExecutor {
|
|
|
|
Mixtape plugin;
|
|
Database.AliasType aliasType;
|
|
|
|
public CommandAlias(Mixtape plugin, Database.AliasType aliasType) {
|
|
this.plugin = plugin;
|
|
this.aliasType = aliasType;
|
|
plugin.getCommand((aliasType == Database.AliasType.CHAT ? "" : "/") + "alias").setExecutor(this);
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
|
if (!(sender instanceof Player)) {
|
|
sender.sendMessage(ChatColor.RED + "This command is not supported in Console.");
|
|
return true;
|
|
}
|
|
Player player = (Player) sender;
|
|
|
|
boolean isChat = this.aliasType == Database.AliasType.CHAT;
|
|
String aliasType = isChat ? "chat" : "command";
|
|
|
|
if (!player.hasPermission(String.format("mixtape.alias.%s", aliasType))) {
|
|
player.sendMessage(ChatColor.RED + String.format("You do not have permission to create %s aliases.", aliasType));
|
|
return true;
|
|
}
|
|
|
|
if (args.length < 2) {
|
|
player.sendMessage(ChatColor.RED + (isChat ? "/" : "//") + String.format("alias <alias> <%s>", aliasType));
|
|
return true;
|
|
}
|
|
|
|
if (MixtapeUtil.isLimited(player)) {
|
|
player.sendMessage(ChatColor.RED + "You have hit your limit of allowed aliases. Delete one and try again.");
|
|
return true;
|
|
}
|
|
|
|
String alias = MixtapeUtil.stripSlash(args[0]);
|
|
|
|
String command = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
|
|
|
|
if (command.length() > 200) {
|
|
player.sendMessage(ChatColor.RED + "This alias is too large. Aliases are limited to 200 characters.");
|
|
return true;
|
|
}
|
|
|
|
Mixtape.getDatabase().insert(player.getUniqueId().toString(), alias, command, isChat ? Database.AliasType.CHAT : Database.AliasType.COMMAND);
|
|
player.sendMessage(ChatColor.GREEN + String.format("%s alias created.", StringUtils.capitalize(aliasType)));
|
|
return true;
|
|
}
|
|
}
|