Add jail list command, outputs the amount of prisoners in there.
This commit is contained in:
parent
11f9e94f40
commit
b3ccf5e82a
@ -1,157 +1,159 @@
|
|||||||
package com.graywolf336.jail.command;
|
package com.graywolf336.jail.command;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import com.graywolf336.jail.JailMain;
|
import com.graywolf336.jail.JailMain;
|
||||||
import com.graywolf336.jail.JailManager;
|
import com.graywolf336.jail.JailManager;
|
||||||
import com.graywolf336.jail.command.commands.CellCreateCommand;
|
import com.graywolf336.jail.command.commands.CellCreateCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailCheckCommand;
|
import com.graywolf336.jail.command.commands.JailCheckCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailClearCommand;
|
import com.graywolf336.jail.command.commands.JailClearCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailCommand;
|
import com.graywolf336.jail.command.commands.JailCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailCreateCommand;
|
import com.graywolf336.jail.command.commands.JailCreateCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailRemoveCellCommand;
|
import com.graywolf336.jail.command.commands.JailListCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailStopCommand;
|
import com.graywolf336.jail.command.commands.JailRemoveCellCommand;
|
||||||
import com.graywolf336.jail.command.commands.JailVersionCommand;
|
import com.graywolf336.jail.command.commands.JailStopCommand;
|
||||||
|
import com.graywolf336.jail.command.commands.JailVersionCommand;
|
||||||
/**
|
|
||||||
* Where all the commands are registered at and handled, processed, at.
|
/**
|
||||||
*
|
* Where all the commands are registered at and handled, processed, at.
|
||||||
* @author graywolf336
|
*
|
||||||
* @since 3.0.0
|
* @author graywolf336
|
||||||
* @version 1.0.2
|
* @since 3.0.0
|
||||||
*
|
* @version 1.0.2
|
||||||
*/
|
*
|
||||||
public class CommandHandler {
|
*/
|
||||||
private LinkedHashMap<String, Command> commands;
|
public class CommandHandler {
|
||||||
|
private LinkedHashMap<String, Command> commands;
|
||||||
public CommandHandler(JailMain plugin) {
|
|
||||||
commands = new LinkedHashMap<String, Command>();
|
public CommandHandler(JailMain plugin) {
|
||||||
loadCommands();
|
commands = new LinkedHashMap<String, Command>();
|
||||||
|
loadCommands();
|
||||||
plugin.getLogger().info("Loaded " + commands.size() + " commands.");
|
|
||||||
}
|
plugin.getLogger().info("Loaded " + commands.size() + " commands.");
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Handles the given command and checks that the command is in valid form.
|
/**
|
||||||
*
|
* Handles the given command and checks that the command is in valid form.
|
||||||
* <p>
|
*
|
||||||
*
|
* <p>
|
||||||
* It checks in the following order:
|
*
|
||||||
* <ol>
|
* It checks in the following order:
|
||||||
* <li>If the command is registered or not.</li>
|
* <ol>
|
||||||
* <li>If more than one command matches the command's name and sends the usage for each one.</li>
|
* <li>If the command is registered or not.</li>
|
||||||
* <li>If they have permission for it, if they don't then we send them a message stating so.</li>
|
* <li>If more than one command matches the command's name and sends the usage for each one.</li>
|
||||||
* <li>If the command needs a player instance, if so we send a message stating that.</li>
|
* <li>If they have permission for it, if they don't then we send them a message stating so.</li>
|
||||||
* <li>If the required minimum arguments have been passed, if not sends the usage.</li>
|
* <li>If the command needs a player instance, if so we send a message stating that.</li>
|
||||||
* <li>If the required maximum arguments have been passed (if there is a max, -1 if no max), if not sends the usage.</li>
|
* <li>If the required minimum arguments have been passed, if not sends the usage.</li>
|
||||||
* <li>Then executes, upon failed execution it sends the usage command.</li>
|
* <li>If the required maximum arguments have been passed (if there is a max, -1 if no max), if not sends the usage.</li>
|
||||||
* </ol>
|
* <li>Then executes, upon failed execution it sends the usage command.</li>
|
||||||
*
|
* </ol>
|
||||||
* @param jailmanager The instance of {@link JailManager}.
|
*
|
||||||
* @param sender The sender of the command.
|
* @param jailmanager The instance of {@link JailManager}.
|
||||||
* @param command The name of the command.
|
* @param sender The sender of the command.
|
||||||
* @param args The arguments passed to the command.
|
* @param command The name of the command.
|
||||||
*/
|
* @param args The arguments passed to the command.
|
||||||
public void handleCommand(JailManager jailmanager, CommandSender sender, String command, String[] args) {
|
*/
|
||||||
List<Command> matches = getMatches(command);
|
public void handleCommand(JailManager jailmanager, CommandSender sender, String command, String[] args) {
|
||||||
|
List<Command> matches = getMatches(command);
|
||||||
if(matches.size() == 0) {
|
|
||||||
sender.sendMessage("No commands registered by the name of " + command + ".");
|
if(matches.size() == 0) {
|
||||||
return;
|
sender.sendMessage("No commands registered by the name of " + command + ".");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if(matches.size() > 1) {
|
|
||||||
for(Command c : matches)
|
if(matches.size() > 1) {
|
||||||
showUsage(sender, c);
|
for(Command c : matches)
|
||||||
return;
|
showUsage(sender, c);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
Command c = matches.get(0);
|
|
||||||
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
|
Command c = matches.get(0);
|
||||||
|
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
|
||||||
// First, let's check if the sender has permission for the command.
|
|
||||||
if(!sender.hasPermission(i.permission())) {
|
// First, let's check if the sender has permission for the command.
|
||||||
sender.sendMessage("No permission to use that command.");//TODO: Make this configurable
|
if(!sender.hasPermission(i.permission())) {
|
||||||
return;
|
sender.sendMessage("No permission to use that command.");//TODO: Make this configurable
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Next, let's check if we need a player and then if the sender is actually a player
|
|
||||||
if(i.needsPlayer() && !(sender instanceof Player)) {
|
// Next, let's check if we need a player and then if the sender is actually a player
|
||||||
sender.sendMessage("A player context is required for this command.");
|
if(i.needsPlayer() && !(sender instanceof Player)) {
|
||||||
return;
|
sender.sendMessage("A player context is required for this command.");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Now, let's check the size of the arguments passed. If it is shorter than the minimum required args, let's show the usage.
|
|
||||||
if(args.length < i.minimumArgs()) {
|
// Now, let's check the size of the arguments passed. If it is shorter than the minimum required args, let's show the usage.
|
||||||
showUsage(sender, c);
|
if(args.length < i.minimumArgs()) {
|
||||||
return;
|
showUsage(sender, c);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Then, if the maximumArgs doesn't equal -1, we need to check if the size of the arguments passed is greater than the maximum args.
|
|
||||||
if(i.maxArgs() != -1 && i.maxArgs() < args.length) {
|
// Then, if the maximumArgs doesn't equal -1, we need to check if the size of the arguments passed is greater than the maximum args.
|
||||||
showUsage(sender, c);
|
if(i.maxArgs() != -1 && i.maxArgs() < args.length) {
|
||||||
return;
|
showUsage(sender, c);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Since everything has been checked and we're all clear, let's execute it.
|
|
||||||
// But if get back false, let's show the usage message.
|
// Since everything has been checked and we're all clear, let's execute it.
|
||||||
if(!c.execute(jailmanager, sender, args)) {
|
// But if get back false, let's show the usage message.
|
||||||
showUsage(sender, c);
|
if(!c.execute(jailmanager, sender, args)) {
|
||||||
return;
|
showUsage(sender, c);
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private List<Command> getMatches(String command) {
|
|
||||||
List<Command> result = new ArrayList<Command>();
|
private List<Command> getMatches(String command) {
|
||||||
|
List<Command> result = new ArrayList<Command>();
|
||||||
for(Entry<String, Command> entry : commands.entrySet()) {
|
|
||||||
if(command.matches(entry.getKey())) {
|
for(Entry<String, Command> entry : commands.entrySet()) {
|
||||||
result.add(entry.getValue());
|
if(command.matches(entry.getKey())) {
|
||||||
}
|
result.add(entry.getValue());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
|
||||||
}
|
return result;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Shows the usage information to the sender, if they have permission.
|
/**
|
||||||
*
|
* Shows the usage information to the sender, if they have permission.
|
||||||
* @param sender The sender of the command
|
*
|
||||||
* @param command The command to send usage of.
|
* @param sender The sender of the command
|
||||||
*/
|
* @param command The command to send usage of.
|
||||||
private void showUsage(CommandSender sender, Command command) {
|
*/
|
||||||
CommandInfo info = command.getClass().getAnnotation(CommandInfo.class);
|
private void showUsage(CommandSender sender, Command command) {
|
||||||
if(!sender.hasPermission(info.permission())) return;
|
CommandInfo info = command.getClass().getAnnotation(CommandInfo.class);
|
||||||
|
if(!sender.hasPermission(info.permission())) return;
|
||||||
sender.sendMessage(info.usage());
|
|
||||||
}
|
sender.sendMessage(info.usage());
|
||||||
|
}
|
||||||
/** Loads all the commands into the hashmap. */
|
|
||||||
private void loadCommands() {
|
/** Loads all the commands into the hashmap. */
|
||||||
load(CellCreateCommand.class);
|
private void loadCommands() {
|
||||||
load(JailCheckCommand.class);
|
load(CellCreateCommand.class);
|
||||||
load(JailClearCommand.class);
|
load(JailCheckCommand.class);
|
||||||
load(JailCommand.class);
|
load(JailClearCommand.class);
|
||||||
load(JailCreateCommand.class);
|
load(JailCommand.class);
|
||||||
load(JailRemoveCellCommand.class);
|
load(JailCreateCommand.class);
|
||||||
load(JailStopCommand.class);
|
load(JailListCommand.class);
|
||||||
load(JailVersionCommand.class);
|
load(JailRemoveCellCommand.class);
|
||||||
}
|
load(JailStopCommand.class);
|
||||||
|
load(JailVersionCommand.class);
|
||||||
private void load(Class<? extends Command> c) {
|
}
|
||||||
CommandInfo info = c.getAnnotation(CommandInfo.class);
|
|
||||||
if(info == null) return;
|
private void load(Class<? extends Command> c) {
|
||||||
|
CommandInfo info = c.getAnnotation(CommandInfo.class);
|
||||||
try {
|
if(info == null) return;
|
||||||
commands.put(info.pattern(), c.newInstance());
|
|
||||||
}catch(Exception e) {
|
try {
|
||||||
e.printStackTrace();
|
commands.put(info.pattern(), c.newInstance());
|
||||||
}
|
}catch(Exception e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.graywolf336.jail.command.commands;
|
||||||
|
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import com.graywolf336.jail.JailManager;
|
||||||
|
import com.graywolf336.jail.beans.Jail;
|
||||||
|
import com.graywolf336.jail.command.Command;
|
||||||
|
import com.graywolf336.jail.command.CommandInfo;
|
||||||
|
|
||||||
|
@CommandInfo(
|
||||||
|
maxArgs = 0,
|
||||||
|
minimumArgs = 0,
|
||||||
|
needsPlayer = false,
|
||||||
|
pattern = "jaillist|jc",
|
||||||
|
permission = "jail.command.jaillist",
|
||||||
|
usage = "/jaillist"
|
||||||
|
)
|
||||||
|
public class JailListCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||||
|
sender.sendMessage(ChatColor.AQUA + "----------Jails----------");
|
||||||
|
|
||||||
|
if(!jm.getJails().isEmpty()) {
|
||||||
|
for(Jail j : jm.getJails()) {
|
||||||
|
sender.sendMessage(ChatColor.BLUE + " " + j.getName() + " (" + j.getAllPrisoners().size() + ")");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
sender.sendMessage(ChatColor.RED + " There are no jails.");
|
||||||
|
}
|
||||||
|
|
||||||
|
sender.sendMessage(ChatColor.AQUA + "-------------------------");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user