TF-Marriage/src/main/java/com/lenis0012/bukkit/marriage2/internal/MarriageCommandExecutor.java

65 lines
2.1 KiB
Java
Raw Normal View History

2014-11-12 19:41:40 +00:00
package com.lenis0012.bukkit.marriage2.internal;
2015-01-09 19:43:37 +00:00
import java.util.Collection;
2014-11-12 19:41:40 +00:00
import java.util.Map;
import java.util.logging.Level;
2015-05-26 21:58:01 +00:00
import com.google.common.collect.Maps;
2014-11-12 19:41:40 +00:00
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
2014-11-13 09:54:23 +00:00
import com.lenis0012.bukkit.marriage2.Marriage;
2014-11-12 19:41:40 +00:00
import com.lenis0012.bukkit.marriage2.commands.Command;
public class MarriageCommandExecutor implements CommandExecutor {
private final MarriageCore core;
private final Map<String, Command> commands = Maps.newHashMap();
public MarriageCommandExecutor(MarriageBase core) {
this.core = (MarriageCore) core;
}
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) {
String subCommand = args.length > 0 ? args[0] : "help";
Command command = commands.get(subCommand.toLowerCase());
if(command == null) {
command = commands.get("marry");
}
// Assuming that the command is not null now, if it is, then that is a mistake on my side.
if(args.length >= command.getMinArgs()) {
if(command.getPermission() == null || sender.hasPermission(command.getPermission())) {
if(command.isAllowConsole() || sender instanceof Player) {
command.prepare(sender, args);
command.execute();
} else {
sender.sendMessage(ChatColor.RED + "You must be a player to execute this command.");
}
} else {
sender.sendMessage(ChatColor.RED + "You are not permitted to use this command.");
}
} else {
2014-11-12 21:27:22 +00:00
sender.sendMessage(ChatColor.RED + "You have not specified enough arguments for this command.");
2014-11-12 19:41:40 +00:00
}
return true;
}
public void regster(Class<? extends Command> commandClass) {
try {
2014-11-13 09:54:23 +00:00
Command command = commandClass.getConstructor(Marriage.class).newInstance(core);
2014-11-12 19:41:40 +00:00
for(String alias : command.getAliases()) {
commands.put(alias.toLowerCase(), command);
}
} catch(Exception e) {
core.getLogger().log(Level.SEVERE, "Failed to register sub command", e);
}
}
2015-01-09 19:43:37 +00:00
public Collection<Command> getSubCommands() {
return commands.values();
}
2014-11-12 19:41:40 +00:00
}