CommandCaller

This commit is contained in:
boy0001 2015-07-28 01:14:38 +10:00
parent a39ec22c8c
commit 5a98c5410e
18 changed files with 163 additions and 215 deletions

View File

@ -33,6 +33,7 @@ import com.intellectualcrafters.plot.util.ClusterManager;
import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualsites.commands.CommandCaller;
import com.intellectualsites.commands.CommandDeclaration; import com.intellectualsites.commands.CommandDeclaration;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;

View File

@ -113,9 +113,7 @@ public class Database extends SubCommand {
return sendMessage(plr, "Could not create the required tables and/or load the database") && sendMessage(plr, "Please see the stacktrace for more information"); return sendMessage(plr, "Could not create the required tables and/or load the database") && sendMessage(plr, "Please see the stacktrace for more information");
} }
UUID requester = null; UUID requester = null;
if (plr != null) { requester = UUIDHandler.getUUID(plr);
requester = UUIDHandler.getUUID(plr);
}
insertPlots(manager, requester, n); insertPlots(manager, requester, n);
break; break;
case "sqlite": case "sqlite":

View File

@ -105,10 +105,10 @@ public class Info extends SubCommand {
case "members": case "members":
case "owner": case "owner":
case "rating": case "rating":
plot = MainUtil.getPlotFromString(player, null, player == null); plot = MainUtil.getPlotFromString(player, null, false);
break; break;
default: default:
plot = MainUtil.getPlotFromString(player, arg, player == null); plot = MainUtil.getPlotFromString(player, arg, false);
if (args.length == 2) { if (args.length == 2) {
arg = args[1]; arg = args[1];
} }
@ -119,15 +119,12 @@ public class Info extends SubCommand {
} }
} }
else { else {
plot = MainUtil.getPlotFromString(player, null, player == null); plot = MainUtil.getPlotFromString(player, null, false);
} }
if (plot == null && arg != null) { if (plot == null && arg != null) {
plot = MainUtil.getPlotFromString(player, null, player == null); plot = MainUtil.getPlotFromString(player, null, false);
} }
if (plot == null) { if (plot == null) {
if (player == null) {
return false;
}
MainUtil.sendMessage(player, C.NOT_IN_PLOT); MainUtil.sendMessage(player, C.NOT_IN_PLOT);
return false; return false;
} }
@ -214,7 +211,7 @@ public class Info extends SubCommand {
final String description = descriptionFlag == null ? C.NONE.s() : descriptionFlag.getValueString(); final String description = descriptionFlag == null ? C.NONE.s() : descriptionFlag.getValueString();
final String flags = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "$1, $2") : C.NONE.s()), C.replacements); final String flags = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "$1, $2") : C.NONE.s()), C.replacements);
final boolean build = (player == null) || plot.isAdded(player.getUUID()); final boolean build = plot.isAdded(player.getUUID());
String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners()); String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners());

View File

@ -24,13 +24,11 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.MathMan;
@ -40,19 +38,18 @@ import com.intellectualsites.commands.Argument;
import com.intellectualsites.commands.Command; import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.CommandHandlingOutput; import com.intellectualsites.commands.CommandHandlingOutput;
import com.intellectualsites.commands.CommandManager; import com.intellectualsites.commands.CommandManager;
import com.intellectualcrafters.plot.object.PlotPlayer;
/** /**
* PlotSquared command class * PlotSquared command class
* *
* @author Citymonstret * @author Citymonstret
*/ */
public class MainCommand extends CommandManager { public class MainCommand extends CommandManager<PlotPlayer> {
public static MainCommand instance = new MainCommand(); public static MainCommand instance = new MainCommand();
private MainCommand() { private MainCommand() {
super(null, new ArrayList<Command>()); super(null, new ArrayList<Command<PlotPlayer>>());
List<SubCommand> toAdd = Arrays.asList( List<SubCommand> toAdd = Arrays.asList(
new Buy(), new Save(), new Load(), new Buy(), new Save(), new Load(),
new Template(), new Download(), new Template(), new Download(),
@ -93,10 +90,10 @@ public class MainCommand extends CommandManager {
return false; return false;
} }
public static ArrayList<Command> getCommands(final CommandCategory category, final PlotPlayer player) { public static ArrayList<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
ArrayList<Command> cmds = instance.getCommands(); ArrayList<Command<PlotPlayer>> cmds = instance.getCommands();
for (Iterator<Command> iter = cmds.iterator(); iter.hasNext();){ for (Iterator<Command<PlotPlayer>> iter = cmds.iterator(); iter.hasNext();){
Command cmd = iter.next(); Command<PlotPlayer> cmd = iter.next();
if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) { if ((category != null && (cmd.getCategory().equals(category))) || !player.hasPermission(cmd.getPermission())) {
iter.remove(); iter.remove();
} }
@ -105,7 +102,7 @@ public class MainCommand extends CommandManager {
} }
public static List<String> helpMenu(final PlotPlayer player, final CommandCategory category, int page) { public static List<String> helpMenu(final PlotPlayer player, final CommandCategory category, int page) {
List<Command> commands; List<Command<PlotPlayer>> commands;
commands = getCommands(category, player); commands = getCommands(category, player);
// final int totalPages = ((int) Math.ceil(12 * (commands.size()) / // final int totalPages = ((int) Math.ceil(12 * (commands.size()) /
// 100)); // 100));
@ -122,7 +119,7 @@ public class MainCommand extends CommandManager {
help.add(C.HELP_HEADER.s()); help.add(C.HELP_HEADER.s());
// HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"), // HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"),
help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size())); help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + perPage).replace("%total%", "" + commands.size()));
Command cmd; Command<PlotPlayer> cmd;
final int start = page * perPage; final int start = page * perPage;
for (int x = start; x < max; x++) { for (int x = start; x < max; x++) {
cmd = commands.get(x); cmd = commands.get(x);
@ -230,7 +227,6 @@ public class MainCommand extends CommandManager {
displayHelp(player, category, help_index); displayHelp(player, category, help_index);
return true; return true;
} }
PlotPlayer caller;
StringBuilder builder = new StringBuilder(cmd).append(" "); StringBuilder builder = new StringBuilder(cmd).append(" ");
Iterator<String> iterator = Arrays.asList(args).iterator(); Iterator<String> iterator = Arrays.asList(args).iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
@ -240,31 +236,6 @@ public class MainCommand extends CommandManager {
} }
} }
instance.handle(player, builder.toString()); instance.handle(player, builder.toString());
// for (final SubCommand command : subCommands) {
// if (command.cmd.equalsIgnoreCase(args[0]) || command.alias.contains(args[0].toLowerCase())) {
// final String[] arguments = new String[args.length - 1];
// System.arraycopy(args, 1, arguments, 0, args.length - 1);
// if (command.permission.hasPermissipon(player)) {
// if ((player != null) || !command.isPlayer) {
// return command.execute(player, arguments);
// } else {
// return !MainUtil.sendMessage(null, C.IS_CONSOLE);
// }
// } else {
// return no_permission(player, command.permission.permission.toLowerCase());
// }
// }
// }
// MainUtil.sendMessage(player, C.NOT_VALID_SUBCOMMAND);
// final String[] commands = new String[subCommands.size()];
// for (int x = 0; x < subCommands.size(); x++) {
// commands[x] = subCommands.get(x).cmd;
// }
// /* Let's try to get a proper usage string */
// final String command = new StringComparison(args[0], commands).getBestMatch();
// return MainUtil.sendMessage(player, C.DID_YOU_MEAN, "/plot " + command);
// PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new
// StringComparsion(args[0], commands).getBestMatch());
return true; return true;
} }
@ -272,30 +243,27 @@ public class MainCommand extends CommandManager {
public int handle(PlotPlayer plr, String input) { public int handle(PlotPlayer plr, String input) {
String[] parts = input.split(" "); String[] parts = input.split(" ");
String[] args; String[] args;
String command = parts[0].toLowerCase(); String label;
if (parts.length == 1) { if (parts.length == 1) {
label = null;
args = new String[0]; args = new String[0];
} else { } else {
args = new String[parts.length - 1]; label = parts[1];
System.arraycopy(parts, 1, args, 0, args.length); args = new String[parts.length - 2];
System.arraycopy(parts, 2, args, 0, args.length);
} }
Command cmd = null; Command<PlotPlayer> cmd = null;
System.out.print(command); cmd = this.commands.get(label);
System.out.print(StringMan.join(commands.entrySet(), ", "));
cmd = this.commands.get(command);
if (cmd == null) { if (cmd == null) {
MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND); MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND);
{ {
final String[] commands = new String[this.commands.size()]; ArrayList<Command<PlotPlayer>> cmds = getCommands();
for (int i = 0; i < commands.length; i++) { cmd = new StringComparison<Command<PlotPlayer>>(args[0], cmds).getMatchObject();
commands[i] = this.commands.get(i).getCommand(); MainUtil.sendMessage(plr, C.DID_YOU_MEAN, cmd.getUsage());
}
final String bestMatch = new StringComparison<String>(args[0], commands).getBestMatch();
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, "/plot " + bestMatch);
} }
return CommandHandlingOutput.NOT_FOUND; return CommandHandlingOutput.NOT_FOUND;
} }
if (cmd.getRequiredType().allows(plr)) { if (!cmd.getRequiredType().allows(plr)) {
if (ConsolePlayer.isConsole(plr)) { if (ConsolePlayer.isConsole(plr)) {
MainUtil.sendMessage(plr, C.IS_CONSOLE); MainUtil.sendMessage(plr, C.IS_CONSOLE);
} else { } else {
@ -307,7 +275,7 @@ public class MainCommand extends CommandManager {
MainUtil.sendMessage(plr, C.NO_PERMISSION, cmd.getPermission()); MainUtil.sendMessage(plr, C.NO_PERMISSION, cmd.getPermission());
return CommandHandlingOutput.NOT_PERMITTED; return CommandHandlingOutput.NOT_PERMITTED;
} }
Argument[] requiredArguments = cmd.getRequiredArguments(); Argument<?>[] requiredArguments = cmd.getRequiredArguments();
if (requiredArguments != null && requiredArguments.length > 0) { if (requiredArguments != null && requiredArguments.length > 0) {
boolean success = true; boolean success = true;
if (args.length < requiredArguments.length) { if (args.length < requiredArguments.length) {

View File

@ -1,23 +1,18 @@
package com.intellectualcrafters.plot.commands; package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualsites.commands.CommandCaller;
import com.intellectualcrafters.plot.object.PlotPlayer;
public enum RequiredType { public enum RequiredType {
CONSOLE, CONSOLE,
PLAYER, PLAYER,
NONE; NONE;
public boolean allows(PlotPlayer player) { public boolean allows(CommandCaller player) {
switch (this) { switch (this) {
case NONE: case NONE:
return true; return true;
case PLAYER:
return !ConsolePlayer.isConsole(player);
case CONSOLE:
return ConsolePlayer.isConsole(player);
default: default:
return false; return this == player.getSuperCaller();
} }
} }
} }

View File

@ -27,9 +27,9 @@ import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
@ -142,10 +142,6 @@ public class SchematicCmd extends SubCommand {
break; break;
} }
case "test": { case "test": {
if (plr == null) {
PS.log(C.IS_CONSOLE.s());
return false;
}
if (!Permissions.hasPermission(plr, "plots.schematic.test")) { if (!Permissions.hasPermission(plr, "plots.schematic.test")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test"); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test");
return false; return false;
@ -178,7 +174,7 @@ public class SchematicCmd extends SubCommand {
} }
case "saveall": case "saveall":
case "exportall": { case "exportall": {
if (plr != null) { if (!ConsolePlayer.isConsole(plr)) {
MainUtil.sendMessage(plr, C.NOT_CONSOLE); MainUtil.sendMessage(plr, C.NOT_CONSOLE);
return false; return false;
} }
@ -220,43 +216,21 @@ public class SchematicCmd extends SubCommand {
} }
final String world; final String world;
final Plot p2; final Plot p2;
if (plr != null) { final Location loc = plr.getLocation();
final Location loc = plr.getLocation(); final Plot plot = MainUtil.getPlot(loc);
final Plot plot = MainUtil.getPlot(loc); if (plot == null) {
if (plot == null) { return !sendMessage(plr, C.NOT_IN_PLOT);
return !sendMessage(plr, C.NOT_IN_PLOT);
}
if (!plot.hasOwner()) {
MainUtil.sendMessage(plr, C.PLOT_UNOWNED);
return false;
}
if (!plot.isOwner(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.schematic.save")) {
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
}
p2 = plot;
world = loc.getWorld();
} else {
if (args.length == 3) {
try {
world = args[1];
final String[] split = args[2].split(";");
final PlotId i = new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
if ((PS.get().getPlots(world) == null) || (PS.get().getPlots(world).get(i) == null)) {
MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false;
}
p2 = PS.get().getPlots(world).get(i);
} catch (final Exception e) {
MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false;
}
} else {
MainUtil.sendMessage(null, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false;
}
} }
if (!plot.hasOwner()) {
MainUtil.sendMessage(plr, C.PLOT_UNOWNED);
return false;
}
if (!plot.isOwner(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.schematic.save")) {
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
}
p2 = plot;
world = loc.getWorld();
Collection<Plot> plots = new ArrayList<Plot>(); Collection<Plot> plots = new ArrayList<Plot>();
plots.add(p2); plots.add(p2);
boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() { boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() {

View File

@ -26,6 +26,8 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.intellectualsites.commands.CommandCaller;
import com.intellectualsites.commands.CommandDeclaration; import com.intellectualsites.commands.CommandDeclaration;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
@ -76,13 +78,7 @@ public class Setup extends SubCommand {
@Override @Override
public boolean onCommand(final PlotPlayer plr, final String[] args) { public boolean onCommand(final PlotPlayer plr, final String[] args) {
// going through setup // going through setup
String name; String name = plr.getName();
if (plr == null) {
name = "*";
}
else {
name = plr.getName();
}
if (!SetupUtils.setupMap.containsKey(name)) { if (!SetupUtils.setupMap.containsKey(name)) {
final SetupObject object = new SetupObject(); final SetupObject object = new SetupObject();
SetupUtils.setupMap.put(name, object); SetupUtils.setupMap.put(name, object);
@ -247,9 +243,7 @@ public class Setup extends SubCommand {
world = object.setupManager.setupWorld(object); world = object.setupManager.setupWorld(object);
} }
try { try {
if (plr != null) { plr.teleport(BlockManager.manager.getSpawn(world));
plr.teleport(BlockManager.manager.getSpawn(world));
}
} catch (final Exception e) { } catch (final Exception e) {
plr.sendMessage("&cAn error occured. See console for more information"); plr.sendMessage("&cAn error occured. See console for more information");
e.printStackTrace(); e.printStackTrace();
@ -260,5 +254,4 @@ public class Setup extends SubCommand {
} }
return false; return false;
} }
} }

View File

@ -30,7 +30,7 @@ import com.intellectualcrafters.plot.util.MainUtil;
* @author Citymonstret * @author Citymonstret
*/ */
@SuppressWarnings({ "deprecation", "unused" }) @SuppressWarnings({ "deprecation", "unused" })
public abstract class SubCommand extends com.intellectualsites.commands.Command { public abstract class SubCommand extends com.intellectualsites.commands.Command<PlotPlayer> {
/** /**
* The category * The category
@ -46,9 +46,8 @@ public abstract class SubCommand extends com.intellectualsites.commands.Command
* *
* @see com.intellectualcrafters.plot.util.MainUtil#sendMessage(PlotPlayer, C, String...) * @see com.intellectualcrafters.plot.util.MainUtil#sendMessage(PlotPlayer, C, String...)
*/ */
public boolean sendMessage(final PlotPlayer plr, final C c, final String... args) { public boolean sendMessage(PlotPlayer plr, final C c, final String... args) {
MainUtil.sendMessage(plr, c, args); c.send(plr, args);
return true; return true;
} }
} }

View File

@ -180,9 +180,7 @@ public class Template extends SubCommand {
setup.world = world; setup.world = world;
SetupUtils.manager.setupWorld(setup); SetupUtils.manager.setupWorld(setup);
MainUtil.sendMessage(plr, "Done!"); MainUtil.sendMessage(plr, "Done!");
if (plr != null) { plr.teleport(BlockManager.manager.getSpawn(world));
plr.teleport(BlockManager.manager.getSpawn(world));
}
return true; return true;
} }
case "export": { case "export": {

View File

@ -74,42 +74,35 @@ public class list extends SubCommand {
private String[] getArgumentList(PlotPlayer player) { private String[] getArgumentList(PlotPlayer player) {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
if (player == null) { if (EconHandler.manager != null && player.hasPermission("plots.list.forsale")) {
args.addAll(Arrays.asList("world", "all", "unowned", "unknown", "top", "<player", "<world>")); args.add("forsale");
if (EconHandler.manager != null) { }
args.add("forsale"); if (player.hasPermission("plots.list.mine")) {
} args.add("mine");
} else { }
if (EconHandler.manager != null && player.hasPermission("plots.list.forsale")) { if (player.hasPermission("plots.list.shared")) {
args.add("forsale"); args.add("shared");
} }
if (player.hasPermission("plots.list.mine")) { if (player.hasPermission("plots.list.world")) {
args.add("mine"); args.add("world");
} }
if (player.hasPermission("plots.list.shared")) { if (player.hasPermission("plots.list.top")) {
args.add("shared"); args.add("top");
} }
if (player.hasPermission("plots.list.world")) { if (player.hasPermission("plots.list..all")) {
args.add("world"); args.add("all");
} }
if (player.hasPermission("plots.list.top")) { if (player.hasPermission("plots.list.unowned")) {
args.add("top"); args.add("unowned");
} }
if (player.hasPermission("plots.list..all")) { if (player.hasPermission("plots.list.unknown")) {
args.add("all"); args.add("unknown");
} }
if (player.hasPermission("plots.list.unowned")) { if (player.hasPermission("plots.list.player")) {
args.add("unowned"); args.add("<player>");
} }
if (player.hasPermission("plots.list.unknown")) { if (player.hasPermission("plots.list.world")) {
args.add("unknown"); args.add("<world>");
}
if (player.hasPermission("plots.list.player")) {
args.add("<player>");
}
if (player.hasPermission("plots.list.world")) {
args.add("<world>");
}
} }
return args.toArray(new String[args.size()]); return args.toArray(new String[args.size()]);
} }
@ -139,26 +132,11 @@ public class list extends SubCommand {
List<Plot> plots = null; List<Plot> plots = null;
String world; String world = plr.getLocation().getWorld();
if (plr != null) {
world = plr.getLocation().getWorld();
}
else {
Set<String> worlds = PS.get().getPlotWorlds();
if (worlds.size() == 0) {
world = "world";
}
else {
world = worlds.iterator().next();
}
}
String arg = args[0].toLowerCase(); String arg = args[0].toLowerCase();
boolean sort = true; boolean sort = true;
switch (arg) { switch (arg) {
case "mine": { case "mine": {
if (plr == null) {
break;
}
if (!Permissions.hasPermission(plr, "plots.list.mine")) { if (!Permissions.hasPermission(plr, "plots.list.mine")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.mine"); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.mine");
return false; return false;
@ -167,9 +145,6 @@ public class list extends SubCommand {
break; break;
} }
case "shared": { case "shared": {
if (plr == null) {
break;
}
if (!Permissions.hasPermission(plr, "plots.list.shared")) { if (!Permissions.hasPermission(plr, "plots.list.shared")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.shared"); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.shared");
return false; return false;

View File

@ -36,6 +36,7 @@ import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualsites.commands.CommandCaller;
/** /**
* Captions class. * Captions class.
@ -692,12 +693,12 @@ public enum C {
return cat; return cat;
} }
public void send(PlotPlayer plr, String... args) { public void send(CommandCaller plr, String... args) {
if (plr == null) { if (plr == null) {
MainUtil.sendConsoleMessage(this, args); MainUtil.sendConsoleMessage(this, args);
} }
else { else {
MainUtil.sendMessage(plr, this, args); plr.sendMessage(this, args);
} }
} }
} }

View File

@ -5,7 +5,10 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.util.MainUtil;
import com.plotsquared.bukkit.util.bukkit.BukkitUtil; import com.plotsquared.bukkit.util.bukkit.BukkitUtil;
public class ConsolePlayer implements PlotPlayer { public class ConsolePlayer implements PlotPlayer {
@ -68,6 +71,11 @@ public class ConsolePlayer implements PlotPlayer {
PS.log(message); PS.log(message);
} }
@Override
public void sendMessage(C c, String... args) {
MainUtil.sendMessage(this, c, args);
}
@Override @Override
public void teleport(Location loc) { public void teleport(Location loc) {
this.loc = loc; this.loc = loc;
@ -123,4 +131,9 @@ public class ConsolePlayer implements PlotPlayer {
this.meta.remove(key); this.meta.remove(key);
} }
@Override
public RequiredType getSuperCaller() {
return RequiredType.CONSOLE;
}
} }

View File

@ -2,12 +2,14 @@ package com.intellectualcrafters.plot.object;
import java.util.UUID; import java.util.UUID;
import com.intellectualsites.commands.CommandCaller;
/** /**
* Created 2015-02-20 for PlotSquared * Created 2015-02-20 for PlotSquared
* *
* @author Citymonstret * @author Citymonstret
*/ */
public interface PlotPlayer { public interface PlotPlayer extends CommandCaller {
public long getPreviousLogin(); public long getPreviousLogin();

View File

@ -8,16 +8,14 @@ import java.util.Set;
import com.intellectualcrafters.plot.commands.CommandCategory; import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
public abstract class Command extends CommandManager { public abstract class Command<E extends CommandCaller> extends CommandManager {
private RequiredType requiredType = RequiredType.NONE; private RequiredType requiredType = RequiredType.NONE;
private String command, usage = "", description = "", permission = ""; private String command, usage = "", description = "", permission = "";
private Set<String> aliases = new HashSet<>(); private Set<String> aliases = new HashSet<>();
private CommandCategory category; private CommandCategory category;
protected Argument[] requiredArguments; protected Argument<?>[] requiredArguments;
public Command() { public Command() {
super(null, new ArrayList<Command>()); super(null, new ArrayList<Command>());
@ -97,9 +95,9 @@ public abstract class Command extends CommandManager {
return this.command; return this.command;
} }
public abstract boolean onCommand(PlotPlayer plr, String[] arguments); public abstract boolean onCommand(E plr, String[] arguments);
final public int handle(PlotPlayer plr, String[] args) { final public int handle(E plr, String[] args) {
if (args.length == 0) { if (args.length == 0) {
return super.handle(plr, ""); return super.handle(plr, "");
} }
@ -131,7 +129,7 @@ public abstract class Command extends CommandManager {
return this.aliases; return this.aliases;
} }
final public Argument[] getRequiredArguments() { final public Argument<?>[] getRequiredArguments() {
return this.requiredArguments; return this.requiredArguments;
} }
@ -150,7 +148,7 @@ public abstract class Command extends CommandManager {
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
final Command other = (Command) obj; final Command<?> other = (Command<?>) obj;
if (this.hashCode() != other.hashCode()) { if (this.hashCode() != other.hashCode()) {
return false; return false;
} }

View File

@ -0,0 +1,14 @@
package com.intellectualsites.commands;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C;
public interface CommandCaller {
void sendMessage(final String message);
void sendMessage(C c, String ... args);
boolean hasPermission(final String perm);
RequiredType getSuperCaller();
}

View File

@ -5,40 +5,41 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class CommandManager { public class CommandManager<T extends CommandCaller> {
protected final ConcurrentHashMap<String, Command> commands; protected final ConcurrentHashMap<String, Command<T>> commands;
protected final Character initialCharacter; protected final Character initialCharacter;
public CommandManager() { public CommandManager() {
this('/', new ArrayList<Command>()); this('/', new ArrayList<Command<T>>());
} }
public CommandManager(Character initialCharacter, List<Command> commands) { public CommandManager(Character initialCharacter, List<Command<T>> commands) {
this.commands = new ConcurrentHashMap<>(); this.commands = new ConcurrentHashMap<>();
for (Command command : commands) { for (Command<T> command : commands) {
addCommand(command); addCommand(command);
} }
this.initialCharacter = initialCharacter; this.initialCharacter = initialCharacter;
} }
final public void addCommand(final Command command) { final public void addCommand(final Command<T> command) {
this.commands.put(command.getCommand().toLowerCase(), command); this.commands.put(command.getCommand().toLowerCase(), command);
for (String alias : command.getAliases()) { for (String alias : command.getAliases()) {
this.commands.put(alias.toLowerCase(), command); this.commands.put(alias.toLowerCase(), command);
} }
} }
final public boolean createCommand(final Command command) { final public boolean createCommand(final Command<T> command) {
try { try {
command.create(); command.create();
} catch(final Exception e) { } catch(final Exception e) {
@ -52,11 +53,11 @@ public class CommandManager {
return false; return false;
} }
final public ArrayList<Command> getCommands() { final public ArrayList<Command<T>> getCommands() {
ArrayList<Command> result = new ArrayList<>(this.commands.values()); ArrayList<Command<T>> result = new ArrayList<>(this.commands.values());
Collections.sort(result, new Comparator<Command>() { Collections.sort(result, new Comparator<Command<T>>() {
@Override @Override
public int compare(Command a, Command b) { public int compare(Command<T> a, Command<T> b) {
if (a == b) { if (a == b) {
return 0; return 0;
} }
@ -72,7 +73,15 @@ public class CommandManager {
return result; return result;
} }
public int handle(PlotPlayer plr, String input) { final public ArrayList<String> getCommandLabels(ArrayList<Command<T>> cmds) {
ArrayList<String> labels = new ArrayList<>(cmds.size());
for (Command<T> cmd : cmds) {
labels.add(cmd.getCommand());
}
return labels;
}
public int handle(T plr, String input) {
if (initialCharacter != null && !input.startsWith(initialCharacter + "")) { if (initialCharacter != null && !input.startsWith(initialCharacter + "")) {
return CommandHandlingOutput.NOT_COMMAND; return CommandHandlingOutput.NOT_COMMAND;
} }
@ -86,7 +95,7 @@ public class CommandManager {
args = new String[parts.length - 1]; args = new String[parts.length - 1];
System.arraycopy(parts, 1, args, 0, args.length); System.arraycopy(parts, 1, args, 0, args.length);
} }
Command cmd = null; Command<T> cmd = null;
cmd = commands.get(command); cmd = commands.get(command);
if (cmd == null) { if (cmd == null) {
return CommandHandlingOutput.NOT_FOUND; return CommandHandlingOutput.NOT_FOUND;
@ -97,7 +106,7 @@ public class CommandManager {
if (!plr.hasPermission(cmd.getPermission())) { if (!plr.hasPermission(cmd.getPermission())) {
return CommandHandlingOutput.NOT_PERMITTED; return CommandHandlingOutput.NOT_PERMITTED;
} }
Argument[] requiredArguments = cmd.getRequiredArguments(); Argument<?>[] requiredArguments = cmd.getRequiredArguments();
if (requiredArguments != null && requiredArguments.length > 0) { if (requiredArguments != null && requiredArguments.length > 0) {
boolean success = true; boolean success = true;
if (args.length < requiredArguments.length) { if (args.length < requiredArguments.length) {
@ -121,7 +130,7 @@ public class CommandManager {
if (!a) { if (!a) {
String usage = cmd.getUsage(); String usage = cmd.getUsage();
if (usage != null && !usage.isEmpty()) { if (usage != null && !usage.isEmpty()) {
MainUtil.sendMessage(plr, usage); plr.sendMessage(usage);
} }
return CommandHandlingOutput.WRONG_USAGE; return CommandHandlingOutput.WRONG_USAGE;
} }

View File

@ -53,11 +53,11 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
return null; return null;
} }
final Set<String> tabOptions = new HashSet<>(); final Set<String> tabOptions = new HashSet<>();
ArrayList<Command> commands = MainCommand.instance.getCommands(); ArrayList<Command<PlotPlayer>> commands = MainCommand.instance.getCommands();
String best = new StringComparison(strings[0], commands).getBestMatch(); String best = new StringComparison(strings[0], commands).getBestMatch();
tabOptions.add(best); tabOptions.add(best);
final String arg = strings[0].toLowerCase(); final String arg = strings[0].toLowerCase();
for (final Command cmd : MainCommand.instance.getCommands()) { for (final Command<PlotPlayer> cmd : MainCommand.instance.getCommands()) {
String label = cmd.getCommand(); String label = cmd.getCommand();
if (!label.equalsIgnoreCase(best)) { if (!label.equalsIgnoreCase(best)) {
if (label.startsWith(arg)) { if (label.startsWith(arg)) {

View File

@ -12,8 +12,11 @@ import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.bukkit.util.bukkit.BukkitUtil; import com.plotsquared.bukkit.util.bukkit.BukkitUtil;
@ -82,6 +85,11 @@ public class BukkitPlayer implements PlotPlayer {
this.player.sendMessage(message); this.player.sendMessage(message);
} }
@Override
public void sendMessage(C c, String... args) {
MainUtil.sendMessage(this, c, args);
}
@Override @Override
public void teleport(final Location loc) { public void teleport(final Location loc) {
this.player.teleport(new org.bukkit.Location(BukkitUtil.getWorld(loc.getWorld()), loc.getX() + 0.5, loc.getY(), loc.getZ() + 0.5, loc.getYaw(), loc.getPitch())); this.player.teleport(new org.bukkit.Location(BukkitUtil.getWorld(loc.getWorld()), loc.getX() + 0.5, loc.getY(), loc.getZ() + 0.5, loc.getYaw(), loc.getPitch()));
@ -191,4 +199,9 @@ public class BukkitPlayer implements PlotPlayer {
public void saveData() { public void saveData() {
player.saveData(); player.saveData();
} }
@Override
public RequiredType getSuperCaller() {
return RequiredType.PLAYER;
}
} }