First commit, converted to a truely maven project and switching over to my own repository for better management.
This commit is contained in:
31
src/main/java/com/graywolf336/jail/command/Command.java
Normal file
31
src/main/java/com/graywolf336/jail/command/Command.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.graywolf336.jail.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
|
||||
/**
|
||||
* The base of all the commands.
|
||||
*
|
||||
* @author graywolf336
|
||||
* @since 3.0.0
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public interface Command {
|
||||
/**
|
||||
* Execute the command given the arguments, returning whether the command handled it or not.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* When the method returns false, the usage message is printed to the sender. If the method
|
||||
* handles the command in any way, such as sending a message to the sender or actually doing
|
||||
* something, then it should return true so that the sender of the command doesn't get the
|
||||
* usage message.
|
||||
*
|
||||
* @param jm An instance of the {@link JailManager}
|
||||
* @param sender The {@link CommandSender sender} of the command
|
||||
* @param args The args, in an array
|
||||
* @return True if the method handled it in any way, false if we should send the usage message.
|
||||
*/
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args);
|
||||
}
|
157
src/main/java/com/graywolf336/jail/command/CommandHandler.java
Normal file
157
src/main/java/com/graywolf336/jail/command/CommandHandler.java
Normal file
@ -0,0 +1,157 @@
|
||||
package com.graywolf336.jail.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.graywolf336.jail.JailMain;
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.commands.CellCreateCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCheckCommand;
|
||||
import com.graywolf336.jail.command.commands.JailClearCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCreateCommand;
|
||||
import com.graywolf336.jail.command.commands.JailRemoveCellCommand;
|
||||
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.
|
||||
*
|
||||
* @author graywolf336
|
||||
* @since 3.0.0
|
||||
* @version 1.0.2
|
||||
*
|
||||
*/
|
||||
public class CommandHandler {
|
||||
private LinkedHashMap<String, Command> commands;
|
||||
|
||||
public CommandHandler(JailMain plugin) {
|
||||
commands = new LinkedHashMap<String, Command>();
|
||||
loadCommands();
|
||||
|
||||
plugin.getLogger().info("Loaded " + commands.size() + " commands.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the given command and checks that the command is in valid form.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* It checks in the following order:
|
||||
* <ol>
|
||||
* <li>If the command is registered or not.</li>
|
||||
* <li>If more than one command matches the command's name and sends the usage for each one.</li>
|
||||
* <li>If they have permission for it, if they don't then we send them a message stating so.</li>
|
||||
* <li>If the command needs a player instance, if so we send a message stating that.</li>
|
||||
* <li>If the required minimum arguments have been passed, if not sends the usage.</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>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 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);
|
||||
|
||||
if(matches.size() == 0) {
|
||||
sender.sendMessage("No commands registered by the name of " + command + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
if(matches.size() > 1) {
|
||||
for(Command c : matches)
|
||||
showUsage(sender, c);
|
||||
return;
|
||||
}
|
||||
|
||||
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())) {
|
||||
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)) {
|
||||
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()) {
|
||||
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) {
|
||||
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.
|
||||
if(!c.execute(jailmanager, sender, args)) {
|
||||
showUsage(sender, c);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Command> getMatches(String command) {
|
||||
List<Command> result = new ArrayList<Command>();
|
||||
|
||||
for(Entry<String, Command> entry : commands.entrySet()) {
|
||||
if(command.matches(entry.getKey())) {
|
||||
result.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private void showUsage(CommandSender sender, Command command) {
|
||||
CommandInfo info = command.getClass().getAnnotation(CommandInfo.class);
|
||||
if(!sender.hasPermission(info.permission())) return;
|
||||
|
||||
sender.sendMessage(info.usage());
|
||||
}
|
||||
|
||||
/** Loads all the commands into the hashmap. */
|
||||
private void loadCommands() {
|
||||
load(CellCreateCommand.class);
|
||||
load(JailCheckCommand.class);
|
||||
load(JailClearCommand.class);
|
||||
load(JailCommand.class);
|
||||
load(JailCreateCommand.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;
|
||||
|
||||
try {
|
||||
commands.put(info.pattern(), c.newInstance());
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
75
src/main/java/com/graywolf336/jail/command/CommandInfo.java
Normal file
75
src/main/java/com/graywolf336/jail/command/CommandInfo.java
Normal file
@ -0,0 +1,75 @@
|
||||
package com.graywolf336.jail.command;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Contains a definition of all the annotations {@link Command commands} should have, if a {@link Command command} doesn't have any then it isn't registered.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* This helps make loading and registering and verifying commands a lot
|
||||
* easier, makes sure that before we really process a command that it
|
||||
* is structured correctly and all the right information is passed. If
|
||||
* the command takes a variety of options, then that command obviously
|
||||
* has to handle it. We just max sure that the minimum amount of
|
||||
* arguments is met and same with the maximum amount, if there is a max.
|
||||
* We also check if the commands needs a player or not, if so and the
|
||||
* sender is something other than a player we send a message stating that
|
||||
* a player context is required. The pattern is just used to match up
|
||||
* the command used to it's registered value, in regex form. We check
|
||||
* the permission stated and determine if the sender has it or not, if
|
||||
* they don't then we send a message stating they don't have permission
|
||||
* for that command. Finally we have the usage string, which is sent
|
||||
* when the sender of the command sends an incorrectly formatted
|
||||
* command. The order of checking is as defined in {@link CommandHandler#handleCommand(com.graywolf336.jail.JailManager, org.bukkit.command.CommandSender, String, String[]) CommandHandler.handleCommand}.
|
||||
*
|
||||
* @author graywolf336
|
||||
* @since 3.0.0
|
||||
* @version 1.0.0
|
||||
*
|
||||
*/
|
||||
@Retention (RetentionPolicy.RUNTIME)
|
||||
public @interface CommandInfo {
|
||||
/**
|
||||
* Gets the maximum amount of arguments required, -1 if no maximum (ex: Jailing someone with a reason or editing a reason).
|
||||
*
|
||||
* @return The maximum number of arguments required, -1 if no maximum.
|
||||
*/
|
||||
public int maxArgs();
|
||||
|
||||
/**
|
||||
* Gets the minimum amount of arguments required.
|
||||
*
|
||||
* @return The minimum number of arguments required.
|
||||
*/
|
||||
public int minimumArgs();
|
||||
|
||||
/**
|
||||
* Whether the command needs a player context or not.
|
||||
*
|
||||
* @return True if requires a player, false if not.
|
||||
*/
|
||||
public boolean needsPlayer();
|
||||
|
||||
/**
|
||||
* A regex pattern that allows for alternatives to the command (ex: /jail or /j, /jailstatus or /js).
|
||||
*
|
||||
* @return The regex pattern to match.
|
||||
*/
|
||||
public String pattern();
|
||||
|
||||
/**
|
||||
* Gets the permission required to execute this command.
|
||||
*
|
||||
* @return The permission required.
|
||||
*/
|
||||
public String permission();
|
||||
|
||||
/**
|
||||
* Gets the usage message for this command.
|
||||
*
|
||||
* @return The usage message.
|
||||
*/
|
||||
public String usage();
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.beans.Cell;
|
||||
import com.graywolf336.jail.beans.Jail;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 2,
|
||||
minimumArgs = 2,
|
||||
needsPlayer = true,
|
||||
pattern = "jailcreatecells|jcc",
|
||||
permission = "jail.command.jailcreatecells",
|
||||
usage = "/jailcellcreate [jail] [cellname]"
|
||||
)
|
||||
public class CellCreateCommand implements Command {
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
Player player = (Player) sender;
|
||||
String name = player.getName();
|
||||
String jail = args[0].toLowerCase();
|
||||
String cell = args[1];
|
||||
|
||||
//Check if the player is currently creating something else
|
||||
if(jm.isCreatingSomething(name)) {
|
||||
String message = jm.getStepMessage(name);
|
||||
if(!message.isEmpty()) {
|
||||
player.sendMessage(ChatColor.RED + message);
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "You're already creating something else, please finish it or cancel.");
|
||||
}
|
||||
}else {
|
||||
//Not creating anything, so let them create some cells.
|
||||
if(jm.isValidJail(jail)) {
|
||||
Jail j = jm.getJail(jail);
|
||||
Cell c = j.getCell(cell);
|
||||
|
||||
//No cell found
|
||||
if(c == null) {
|
||||
if(jm.addCreatingCell(name, jail, cell)) {
|
||||
jm.getCellCreationSteps().startStepping(player);
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "Appears you're creating a cell or something went wrong on our side.");
|
||||
}
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "There's already a cell with the name '" + cell + "', please pick a new one or remove that cell.");
|
||||
}
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "No such jail found by the name of '" + jail + "'.");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 1,
|
||||
minimumArgs = 0,
|
||||
needsPlayer = true,
|
||||
pattern = "jailcheck|jcheck",
|
||||
permission = "jail.command.jailcheck",
|
||||
usage = "/jailcheck (Player name)"
|
||||
)
|
||||
public class JailCheckCommand implements Command{
|
||||
|
||||
// Checks the status of the specified prisoner, if no args, will display all players currently jailed
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
return true; //If they made it this far, the command is intact and ready to be processed. :)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 1,
|
||||
minimumArgs = 0,
|
||||
needsPlayer = false,
|
||||
pattern = "jailclear|jclear",
|
||||
permission = "jail.command.jailclear",
|
||||
usage = "/jailclear (Jail name)"
|
||||
)
|
||||
public class JailClearCommand implements Command {
|
||||
|
||||
// If Jail is specified clear all prisoners from that Jail (new feature) else, clear all players from all jails
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
return true; //If they made it this far, the command is intact and ready to be processed. :)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = -1,
|
||||
minimumArgs = 1,
|
||||
needsPlayer = false,
|
||||
pattern = "jail|j",
|
||||
permission = "jail.command.jail",
|
||||
usage = "/jail [name] (time) (j:Jail name) (c:Cell name) (r:Reason) (m:Muted)"
|
||||
)
|
||||
public class JailCommand implements Command {
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
return true; //If they made it this far, the command is intact and ready to be processed. :)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 1,
|
||||
minimumArgs = 1,
|
||||
needsPlayer = true,
|
||||
pattern = "jailcreate|jc",
|
||||
permission = "jail.command.jailcreate",
|
||||
usage = "/jailcreate [name]"
|
||||
)
|
||||
public class JailCreateCommand implements Command {
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
Player player = (Player) sender;
|
||||
String name = player.getName();
|
||||
String jail = args[0];
|
||||
|
||||
//Check if the player is currently creating something else
|
||||
if(jm.isCreatingSomething(name)) {
|
||||
String message = jm.getStepMessage(name);
|
||||
if(!message.isEmpty()) {
|
||||
player.sendMessage(ChatColor.RED + message);
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "You're already creating something else, please finish it or cancel.");
|
||||
}
|
||||
}else {
|
||||
if(jm.isValidJail(jail)) {
|
||||
player.sendMessage(ChatColor.RED + "Jail by the name of '" + jail + "' already exist!");
|
||||
}else {
|
||||
if(jm.addCreatingJail(name, jail)) {
|
||||
jm.getJailCreationSteps().startStepping(player);
|
||||
}else {
|
||||
player.sendMessage(ChatColor.RED + "Seems like you're already creating a Jail or something went wrong on our side.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 2,
|
||||
minimumArgs = 1,
|
||||
needsPlayer = true,
|
||||
pattern = "jailremovecell|jrcell",
|
||||
permission = "jail.command.jailremovecell",
|
||||
usage = "/jailremovecell [Jail Name] (Cell Name)"
|
||||
)
|
||||
public class JailRemoveCellCommand implements Command{
|
||||
|
||||
// Remove the specified Cell from the Specified Jail, if no cell specified will delete nearest cell
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
return true; //If they made it this far, the command is intact and ready to be processed. :)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 0,
|
||||
minimumArgs = 0,
|
||||
needsPlayer = true,
|
||||
pattern = "jailstop",
|
||||
permission = "jail.command.jailstop",
|
||||
usage = "/jailstop"
|
||||
)
|
||||
public class JailStopCommand implements Command {
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
jm.removeJailCreationPlayer(sender.getName());
|
||||
jm.removeCellCreationPlayer(sender.getName());
|
||||
|
||||
sender.sendMessage("Any creations, jail or cell, have been stopped.");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
|
||||
@CommandInfo(
|
||||
maxArgs = 0,
|
||||
minimumArgs = 0,
|
||||
needsPlayer = false,
|
||||
pattern = "jailversion|jv",
|
||||
permission = "jail.command.jailversion",
|
||||
usage = "/jailversion"
|
||||
)
|
||||
public class JailVersionCommand implements Command{
|
||||
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
// Sends the version number to the sender
|
||||
sender.sendMessage("Jail Version: " + jm.getPlugin().getDescription().getVersion());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user