mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-02 22:47:43 +01:00
Started a refactoring for the commands
This commit is contained in:
parent
17c7099cbb
commit
158c7b3465
@ -1,19 +1,12 @@
|
||||
package us.tastybento.bskyblock;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import us.tastybento.bskyblock.commands.AdminCommand;
|
||||
import us.tastybento.bskyblock.commands.IslandCommand;
|
||||
import us.tastybento.bskyblock.config.BSBLocale;
|
||||
import us.tastybento.bskyblock.config.PluginConfig;
|
||||
@ -33,6 +26,13 @@ import us.tastybento.bskyblock.schematics.SchematicsMgr;
|
||||
import us.tastybento.bskyblock.util.FileLister;
|
||||
import us.tastybento.bskyblock.util.VaultHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Main BSkyBlock class - provides an island minigame in the sky
|
||||
* @author Tastybento
|
||||
@ -107,11 +107,12 @@ public class BSkyBlock extends JavaPlugin{
|
||||
};
|
||||
schematicsManager = new SchematicsMgr(plugin);
|
||||
|
||||
getCommand("island").setExecutor(new IslandCommand(plugin));
|
||||
|
||||
Settings.defaultLanguage = "en-US";
|
||||
loadLocales();
|
||||
|
||||
new IslandCommand(BSkyBlock.this);
|
||||
new AdminCommand(BSkyBlock.this);
|
||||
|
||||
// Register Listeners
|
||||
registerListeners();
|
||||
/*
|
||||
|
@ -1,17 +1,20 @@
|
||||
package us.tastybento.bskyblock.commands;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AdminCommand extends BSBCommand{
|
||||
|
||||
BSkyBlock plugin;
|
||||
|
||||
public AdminCommand(BSkyBlock plugin) {
|
||||
super(plugin, true);
|
||||
super(plugin, Settings.ADMINCOMMAND, true);
|
||||
plugin.getCommand(Settings.ADMINCOMMAND).setExecutor(this);
|
||||
plugin.getCommand(Settings.ADMINCOMMAND).setTabCompleter(this);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -20,24 +23,24 @@ public class AdminCommand extends BSBCommand{
|
||||
registerArgument(new String[] {"delete"}, new CommandArgumentHandler() {
|
||||
|
||||
@Override
|
||||
public boolean canExecute(CommandSender sender, String label, String[] args) {
|
||||
public boolean canExecute(CommandSender sender, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(CommandSender sender, String label, String[] args) {
|
||||
public void onExecute(CommandSender sender, String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String label, String[] args) {
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getHelp(CommandSender sender, String label){
|
||||
public String[] getHelp(CommandSender sender){
|
||||
return new String[] {null, plugin.getLocale(sender).get("help.admin.delete")};
|
||||
}
|
||||
});
|
||||
@ -45,13 +48,13 @@ public class AdminCommand extends BSBCommand{
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExecute(CommandSender sender, String label) {
|
||||
public boolean canExecute(CommandSender sender) {
|
||||
// TODO Auto-generated method stub
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecuteDefault(CommandSender sender, String label, String[] args) {
|
||||
public void onExecuteDefault(CommandSender sender, String[] args) {
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,54 +1,58 @@
|
||||
package us.tastybento.bskyblock.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Abstract class that handles commands and tabs. It makes the commands code modular
|
||||
* and allow addons to add their own arguments.
|
||||
* Abstract class that handles commands and tab completion.
|
||||
*
|
||||
* It makes the commands code modular and allow addons to add their own arguments or even remove/edit existing ones.
|
||||
*
|
||||
* @author Poslovitch
|
||||
*/
|
||||
public abstract class BSBCommand implements CommandExecutor, TabCompleter{
|
||||
private Map<String, CommandArgumentHandler> arguments;
|
||||
private boolean help;
|
||||
|
||||
protected BSBCommand(BSkyBlock plugin, boolean help){
|
||||
private String command;
|
||||
|
||||
/** Whether the command has an help list. */
|
||||
private boolean help = true;
|
||||
/** Max subcommands per help page. */
|
||||
private static final int MAX_PER_PAGE = 7; // 10 seems to be the maximum acceptable, 7 is a good number.
|
||||
|
||||
protected BSBCommand(BSkyBlock plugin, String command, boolean help){
|
||||
this.command = command;
|
||||
|
||||
arguments = new HashMap<String, CommandArgumentHandler>();
|
||||
this.help = help;
|
||||
|
||||
// Register a help argument if needed
|
||||
// Register the help argument if needed
|
||||
if(help) {
|
||||
registerArgument(new String[] {"help", "?"}, new CommandArgumentHandler() {
|
||||
|
||||
@Override
|
||||
public boolean canExecute(CommandSender sender, String label, String[] args) {
|
||||
public boolean canExecute(CommandSender sender, String[] args) {
|
||||
return true; // If the player can execute the command, he can receive help
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(CommandSender sender, String label, String[] args) {
|
||||
// TODO send help
|
||||
public void onExecute(CommandSender sender, String[] args) {
|
||||
sender.sendMessage(plugin.getLocale(sender).get("commands." + command + ".help-header"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String label, String[] args) {
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return null; // Doesn't have options for tab-completion
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getHelp(CommandSender sender, String label) {
|
||||
public String[] getHelp(CommandSender sender) {
|
||||
return null; // Obviously, don't send any help message.
|
||||
}
|
||||
|
||||
@ -60,39 +64,40 @@ public abstract class BSBCommand implements CommandExecutor, TabCompleter{
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the command arguments
|
||||
* Registers the command-specific arguments.
|
||||
*
|
||||
* This method is called when BSBCommand has been successfully constructed.
|
||||
*/
|
||||
public abstract void setup();
|
||||
|
||||
/**
|
||||
* Check if the sender can use the command
|
||||
* Asks if the sender can use the command
|
||||
*
|
||||
* @param sender
|
||||
* @param label
|
||||
* @return if the sender can use the command
|
||||
*/
|
||||
public abstract boolean canExecute(CommandSender sender, String label);
|
||||
public abstract boolean canExecute(CommandSender sender);
|
||||
|
||||
/**
|
||||
* This code is executed when no arguments is specified for the command
|
||||
* @param sender
|
||||
* @param label
|
||||
* @param args
|
||||
*/
|
||||
public abstract void onExecuteDefault(CommandSender sender, String label, String[] args);
|
||||
public abstract void onExecuteDefault(CommandSender sender, String[] args);
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
|
||||
if(this.canExecute(sender, label)){
|
||||
if(this.canExecute(sender)){
|
||||
if(args.length >= 1){
|
||||
if(arguments.containsKey(args[0]) && arguments.get(args[0]).canExecute(sender, label, args)){
|
||||
arguments.get(args[0]).onExecute(sender, label, args);
|
||||
if(arguments.containsKey(args[0]) && arguments.get(args[0]).canExecute(sender, args)){
|
||||
arguments.get(args[0]).onExecute(sender, args);
|
||||
} else if(help) {
|
||||
arguments.get("?").onExecute(sender, label, args);
|
||||
arguments.get("?").onExecute(sender, args);
|
||||
} else {
|
||||
this.onExecuteDefault(sender, label, args);
|
||||
this.onExecuteDefault(sender, args);
|
||||
}
|
||||
} else {
|
||||
this.onExecuteDefault(sender, label, args);
|
||||
this.onExecuteDefault(sender, args);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@ -101,60 +106,59 @@ public abstract class BSBCommand implements CommandExecutor, TabCompleter{
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args){
|
||||
List<String> options = new ArrayList<String>();
|
||||
if(this.canExecute(sender, label)){
|
||||
if(this.canExecute(sender)){
|
||||
if(args.length <= 1){
|
||||
// Go through every argument, check if player can use it and if so, add it in tab options
|
||||
for(Entry<String, CommandArgumentHandler> entry : arguments.entrySet()){
|
||||
if(entry.getValue().canExecute(sender, label, args)){
|
||||
if(entry.getValue().canExecute(sender, args)){
|
||||
options.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If player can execute the argument, get its tab-completer options
|
||||
if(getArgumentHandler(args[0]) != null && getArgumentHandler(args[0]).canExecute(sender, label, args)
|
||||
&& getArgumentHandler(args[0]).onTabComplete(sender, label, args) != null){
|
||||
options.addAll(getArgumentHandler(args[0]).onTabComplete(sender, label, args));
|
||||
if(getArgumentHandler(args[0]) != null && getArgumentHandler(args[0]).canExecute(sender, args)
|
||||
&& getArgumentHandler(args[0]).onTabComplete(sender, args) != null){
|
||||
options.addAll(getArgumentHandler(args[0]).onTabComplete(sender, args));
|
||||
}
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the behavior of an argument and its aliases.
|
||||
*/
|
||||
public abstract class CommandArgumentHandler{
|
||||
/**
|
||||
* Check if the sender can use the argument
|
||||
* @param sender
|
||||
* @param label
|
||||
* @param args
|
||||
* @return if the sender can use the argument
|
||||
*/
|
||||
public abstract boolean canExecute(CommandSender sender, String label, String[] args);
|
||||
public abstract boolean canExecute(CommandSender sender, String[] args);
|
||||
|
||||
/**
|
||||
* Code to execute for this argument
|
||||
* @param sender
|
||||
* @param label
|
||||
* @param args
|
||||
*/
|
||||
public abstract void onExecute(CommandSender sender, String label, String[] args);
|
||||
public abstract void onExecute(CommandSender sender, String[] args);
|
||||
|
||||
/**
|
||||
* Request a list of tab-completion options with the argument
|
||||
* @param sender
|
||||
* @param label
|
||||
* @param args
|
||||
* @return the list of options
|
||||
*/
|
||||
public abstract List<String> onTabComplete(CommandSender sender, String label, String[] args);
|
||||
public abstract List<String> onTabComplete(CommandSender sender, String[] args);
|
||||
|
||||
/**
|
||||
* Get help information
|
||||
* <code>new String[] {arguments, description};</code>
|
||||
* @param sender
|
||||
* @param label
|
||||
* @return the help information
|
||||
*/
|
||||
public abstract String[] getHelp(CommandSender sender, String label);
|
||||
public abstract String[] getHelp(CommandSender sender);
|
||||
}
|
||||
|
||||
public void registerArgument(String[] args, CommandArgumentHandler handler){
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,8 @@
|
||||
package us.tastybento.bskyblock.config;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
@ -7,10 +10,6 @@ import java.net.URLClassLoader;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
public class BSBLocale {
|
||||
|
||||
final static String LOCALE_FOLDER = "locales";
|
||||
|
Loading…
Reference in New Issue
Block a user