Lots of work done on the command API.

Sub commands work, tab complete works. See the TestIslandCommand.java
test command for how to use the API and unit tests.

Still need to do the auto-help.
This commit is contained in:
Tastybento 2017-12-23 19:14:35 -08:00
parent c5262e8faa
commit eeb4e2a9ee
28 changed files with 481 additions and 276 deletions

View File

@ -1,12 +1,12 @@
package us.tastybento.bskyblock.api.addons;
import java.io.File;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.BSModule;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import java.io.File;
public abstract class BSAddon implements BSModule {
private File folder;

View File

@ -0,0 +1,40 @@
package us.tastybento.bskyblock.api.commands;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
/**
* Interface for BSkyBlock Commands
* @author ben
*
*/
public interface BSBCommand {
/**
* Anything that needs to be set up for this command.
* This is where you register subcommands. This will be run if it exists.
*/
default void setup() {};
/**
* What will be executed when this command is run
* @param user
* @param list
* @return true or false - true if the command executed successfully
*/
public abstract boolean execute(User user, List<String> list);
/**
* Tab Completer for CompositeCommands. Note that any registered sub-commands will be automatically
* added to the list must not be manually added. Use this to add tab-complete for things like names.
* @param sender
* @param alias
* @param args
* @return List of strings that could be used to complete this command.
*/
default Optional<List<String>> tabComplete(User user, String alias, LinkedList<String> args) {
return Optional.empty();
}
}

View File

@ -3,6 +3,7 @@ package us.tastybento.bskyblock.api.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -24,11 +25,12 @@ import us.tastybento.bskyblock.util.Util;
* @author ben, poslovich
*
*/
public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand {
public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand, BSBCommand {
private static final boolean DEBUG = true;
private boolean onlyPlayer = false;
private final CompositeCommand parent;
private final int level;
private String permission = "";
public BSkyBlock plugin = BSkyBlock.getPlugin();
@ -43,6 +45,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
public CompositeCommand(CompositeCommand parent, String label, String... aliases) {
super(label);
this.parent = parent;
this.level = parent.getLevel() + 1;
// Add this sub-command to the parent
parent.getSubCommands().put(label, this);
this.setAliases(new ArrayList<>(Arrays.asList(aliases)));
@ -54,6 +57,15 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
/**
* @return this command's sub-level. Top level is 0.
* Every time a command registers with a parent, their level will be set.
*/
protected int getLevel() {
return level;
}
/**
* This is the top-level command constructor for commands that have no parent.
* @param label - string for this command
@ -64,6 +76,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
this.setDescription(description);
this.setAliases(new ArrayList<>(Arrays.asList(aliases)));
this.parent = null;
this.level = 0; // Top level
this.subCommands = new LinkedHashMap<>();
this.setup();
@ -79,84 +92,23 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
Bukkit.getLogger().info("DEBUG: executing command " + label);
// Get the User instance for this sender
User user = User.getInstance(sender);
// Check permissions
if (user.hasPermission(permission)) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: sender has permission");
// Do the player part
if (onlyPlayer && user.isPlayer()) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: sender is a player and command requires players");
// Find the subCommand
CompositeCommand subCommand = this;
// Run through any arguments
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Running through args: " + args.toString());
if (args.length > 0) {
for (int i = 0; i <= args.length; i++) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Argument " + i);
// get the subcommand corresponding to the arg
if (subCommand.hasSubCommmands()) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: This command has subcommands");
if (subCommand.hasSubCommand(args[i])) {
// Step down one
subCommand = subCommand.getSubCommand(args[i]);
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Moved to " + subCommand.getLabel());
} else {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Unknown command");
// Unknown command
user.sendMessage("general.errors.unknown-command");
return true;
}
} else {
// We are at the end of the walk
if (DEBUG)
Bukkit.getLogger().info("DEBUG: End of traversal, checking perms");
// Check permission
if (user.hasPermission(subCommand.getPermission())) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: player has perm");
if (onlyPlayer && user.isPlayer()) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: subcommand is for player's only - executing with args " + Arrays.copyOfRange(args, i, args.length));
// Execute the subcommand with trimmed args
subCommand.execute(user, Arrays.copyOfRange(args, i, args.length));
} else {
user.sendMessage("general.errors.use-in-game");
}
} else {
user.sendMessage("general.errors.no-permission");
}
}
// else continue the loop
}
} else {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: no args, just execute");
execute(user, args);
}
} else {
user.sendMessage("general.errors.use-in-game");
}
} else {
user.sendMessage("general.errors.no-permission");
CompositeCommand cmd = getCommandFromArgs(args);
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: Command = " + cmd.getLabel() + " onlyplayer = " + cmd.isOnlyPlayer() + " permission = " + cmd.getPermission());
}
return true;
// Check for console and permissions
if (cmd.onlyPlayer && !(sender instanceof Player)) {
user.sendMessage("general.errors.use-in-game");
return true;
}
if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission())) {
user.sendMessage("general.errors.no-permission");
return true;
}
// Execute and trim args
return cmd.execute(user, Arrays.asList(args).subList(cmd.level, args.length));
}
/**
* What will be executed when this command is run
* @param user
* @param args
* @return true or false
*/
public abstract boolean execute(User user, String[] args);
/**
* @return IslandsManager
*/
@ -295,15 +247,98 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
return (user.getPlayer() instanceof Player);
}
@Override
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) {
plugin.getLogger().info("DEBUG: tab complete " + subCommands.keySet().toString());
final List<String> options = new ArrayList<>(subCommands.keySet());
List<String> options = new ArrayList<>();
// Get command object based on args entered so far
CompositeCommand cmd = getCommandFromArgs(args);
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: subCommand = " + cmd.getLabel() + " onlyplayer = " + cmd.isOnlyPlayer() + " permission = " + cmd.getPermission());
}
// Check for console and permissions
if (cmd.onlyPlayer && !(sender instanceof Player)) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: returning, only for player");
return options;
}
if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission())) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: failed perm check");
return options;
}
// Add any tab completion from the subcommand
options.addAll(cmd.tabComplete(User.getInstance(sender), alias, new LinkedList<String>(Arrays.asList(args))).orElse(new ArrayList<>()));
// Add any sub-commands automatically
if (cmd.hasSubCommmands()) {
// Check if subcommands are visible to this sender
for (CompositeCommand subCommand: cmd.getSubCommands().values()) {
if ((sender instanceof Player)) {
// Player
if (subCommand.getPermission().isEmpty() || sender.hasPermission(subCommand.getPermission())) {
// Permission is okay
options.add(subCommand.getLabel());
}
} else {
// Console
if (!subCommand.onlyPlayer) {
// Not a player command
options.add(subCommand.getLabel());
}
}
}
}
String lastArg = (args.length != 0 ? args[args.length - 1] : "");
if (DEBUG) {
String arguments = "";
for (String arg : args) {
arguments += "'" + arg + "' ";
}
Bukkit.getLogger().info("DEBUG: tab complete for " + arguments);
Bukkit.getLogger().info("DEBUG: result = " + Util.tabLimit(options, lastArg));
}
return Util.tabLimit(options, lastArg);
}
/**
* Get the current composite command based on the arguments
* @param args
* @return the current composite command based on the arguments
*/
private CompositeCommand getCommandFromArgs(String[] args) {
CompositeCommand subCommand = this;
// Run through any arguments
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Running through args: " + args.toString());
if (args.length > 0) {
for (int i = 0; i <= args.length; i++) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Argument " + i);
// get the subcommand corresponding to the arg
if (subCommand.hasSubCommmands()) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: This command has subcommands");
if (subCommand.hasSubCommand(args[i])) {
// Step down one
subCommand = subCommand.getSubCommand(args[i]);
if (DEBUG)
Bukkit.getLogger().info("DEBUG: Moved to " + subCommand.getLabel());
} else {
return subCommand;
}
} else {
// We are at the end of the walk
if (DEBUG)
Bukkit.getLogger().info("DEBUG: End of traversal");
return subCommand;
}
// else continue the loop
}
}
return subCommand;
}
public void setOnlyPlayer(boolean onlyPlayer) {
this.onlyPlayer = onlyPlayer;
}
@ -312,11 +347,5 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
public void setPermission(String permission) {
this.permission = permission;
}
/**
* Anything that needs to be set up for this command.
* This is where you register subcommands.
*/
public abstract void setup();
}

View File

@ -1,13 +1,13 @@
package us.tastybento.bskyblock.api.localization;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
/**
* @author Poslovitch, Tastybento
*/

View File

@ -1,5 +1,7 @@
package us.tastybento.bskyblock.commands;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.admin.AdminVersionCommand;
@ -19,7 +21,8 @@ public class AdminCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
return false;
}
}

View File

@ -1,5 +1,7 @@
package us.tastybento.bskyblock.commands;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.island.IslandAboutCommand;
@ -22,7 +24,6 @@ public class IslandCommand extends CompositeCommand {
public IslandCommand() {
super(Settings.ISLANDCOMMAND, "is");
this.setOnlyPlayer(true);
}
/* (non-Javadoc)
@ -30,6 +31,8 @@ public class IslandCommand extends CompositeCommand {
*/
@Override
public void setup() {
// Permission
this.setPermission(Settings.PERMPREFIX + "island");
// Set up subcommands
new IslandAboutCommand(this);
new IslandCreateCommand(this);
@ -50,9 +53,13 @@ public class IslandCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
user.sendLegacyMessage("You successfully did /is !");
return true;
if (!plugin.getIslands().hasIsland(user.getUniqueId())) {
return this.getSubCommand("create").execute(user, args);
}
// Currently, just go home
return this.getSubCommand("go").execute(user, args);
}
}

View File

@ -1,22 +1,22 @@
package us.tastybento.bskyblock.commands.admin;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
public class AdminVersionCommand extends CompositeCommand {
public AdminVersionCommand(CompositeCommand adminCommand) {
super(adminCommand, "version");
// Permission
this.setPermission(Settings.PERMPREFIX + "admin.version");
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
return false;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,7 @@
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
@ -15,7 +17,7 @@ public class IslandAboutCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
user.sendLegacyMessage("About " + BSkyBlock.getPlugin().getDescription().getName() + " v" + BSkyBlock.getPlugin().getDescription().getVersion() + ":");
user.sendLegacyMessage("Copyright (c) 2017 - 2018 tastybento, Poslovitch");
user.sendLegacyMessage("All rights reserved.");
@ -48,9 +50,4 @@ public class IslandAboutCommand extends CompositeCommand {
return false;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -4,6 +4,7 @@
package us.tastybento.bskyblock.commands.island;
import java.io.IOException;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
@ -28,7 +29,7 @@ public class IslandCreateCommand extends CompositeCommand {
* @see us.tastybento.bskyblock.api.commands.CommandArgument#execute(org.bukkit.command.CommandSender, java.lang.String[])
*/
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
if (getIslands().hasIsland(user.getUniqueId())) {
user.sendMessage("general.errors.already-have-island");
}
@ -58,11 +59,4 @@ public class IslandCreateCommand extends CompositeCommand {
e.printStackTrace();
}
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -3,6 +3,8 @@
*/
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.ChatColor;
@ -28,13 +30,13 @@ public class IslandGoCommand extends CompositeCommand {
* @see us.tastybento.bskyblock.api.commands.CommandArgument#execute(org.bukkit.command.CommandSender, java.lang.String[])
*/
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
if (!getIslands().hasIsland(user.getUniqueId())) {
user.sendMessage(ChatColor.RED + "general.errors.no-island");
return true;
}
if (args.length == 1 && NumberUtils.isDigits(args[0])) {
int homeValue = Integer.valueOf(args[0]);
if (!args.isEmpty() && NumberUtils.isDigits(args.get(0))) {
int homeValue = Integer.valueOf(args.get(0));
int maxHomes = Util.getPermValue(user.getPlayer(), Settings.PERMPREFIX + "island.maxhomes", Settings.maxHomes);
if (homeValue > 1 && homeValue <= maxHomes) {
getIslands().homeTeleport(user.getPlayer(), homeValue);
@ -45,8 +47,4 @@ public class IslandGoCommand extends CompositeCommand {
return true;
}
@Override
public void setup() {
}
}

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.commands.island;
import java.io.IOException;
import java.util.List;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
@ -24,7 +25,7 @@ public class IslandResetCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
if (!getIslands().hasIsland(user.getUniqueId())) {
user.sendMessage("general.errors.no-island");
return true;
@ -63,9 +64,4 @@ public class IslandResetCommand extends CompositeCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -3,7 +3,9 @@
*/
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@ -30,7 +32,7 @@ public class IslandResetnameCommand extends CompositeCommand {
* @see us.tastybento.bskyblock.api.commands.CommandArgument#execute(org.bukkit.command.CommandSender, java.lang.String[])
*/
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
Player player = user.getPlayer();
UUID playerUUID = player.getUniqueId();
@ -44,14 +46,13 @@ public class IslandResetnameCommand extends CompositeCommand {
return true;
}
// Explain command
if (args.length == 1) {
//TODO Util.sendMessage(player, getHelpMessage(player, label, args[0], usage(sender, label)));
if (args.isEmpty()) {
user.sendMessage(getUsage());
return true;
}
// Naming the island
String name = args[1];
for (int i = 2; i < args.length; i++) name += " " + args[i];
// Naming the island - join all the arguments with spaces.
String name = args.stream().collect(Collectors.joining( " " ));
// Check if the name isn't too short or too long
if (name.length() < Settings.nameMinLength) {
@ -72,10 +73,4 @@ public class IslandResetnameCommand extends CompositeCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,6 @@
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import java.util.UUID;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
@ -16,7 +17,7 @@ public class IslandSethomeCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Check island
if (plugin.getIslands().getIsland(user.getUniqueId()) == null) {
@ -27,18 +28,18 @@ public class IslandSethomeCommand extends CompositeCommand {
user.sendMessage("sethome.error.NotOnIsland");
return true;
}
if (args.length == 0) {
if (args.isEmpty()) {
// island sethome
plugin.getPlayers().setHomeLocation(playerUUID, user.getLocation());
user.sendMessage("sethome.homeSet");
} else if (args.length == 1) {
} else {
// Dynamic home sizes with permissions
int maxHomes = Util.getPermValue(user.getPlayer(), Settings.PERMPREFIX + "island.maxhomes", Settings.maxHomes);
if (maxHomes > 1) {
// Check the number given is a number
int number = 0;
try {
number = Integer.valueOf(args[0]);
number = Integer.valueOf(args.get(0));
if (number < 1 || number > maxHomes) {
user.sendMessage("sethome.error.NumHomes", "[max]", String.valueOf(maxHomes));
} else {
@ -55,9 +56,4 @@ public class IslandSethomeCommand extends CompositeCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -3,10 +3,10 @@
*/
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
@ -29,9 +29,8 @@ public class IslandSetnameCommand extends CompositeCommand {
* @see us.tastybento.bskyblock.api.commands.CommandArgument#execute(org.bukkit.command.CommandSender, java.lang.String[])
*/
@Override
public boolean execute(User user, String[] args) {
Player player = user.getPlayer();
UUID playerUUID = player.getUniqueId();
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
if (!getIslands().hasIsland(playerUUID)) {
user.sendMessage(ChatColor.RED + "general.errors.no-island");
@ -49,10 +48,4 @@ public class IslandSetnameCommand extends CompositeCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -23,11 +23,11 @@ import us.tastybento.bskyblock.config.Settings;
public abstract class AbstractIslandTeamCommand extends CompositeCommand {
protected final static boolean DEBUG = false;
protected BiMap<UUID, UUID> inviteList = HashBiMap.create();
protected static BiMap<UUID, UUID> inviteList = HashBiMap.create();
// The time a player has to wait until they can reset their island again
protected HashMap<UUID, Long> resetWaitTime = new HashMap<>();
protected Set<UUID> leavingPlayers = new HashSet<>();
protected Set<UUID> kickingPlayers = new HashSet<>();
protected static HashMap<UUID, Long> resetWaitTime = new HashMap<>();
protected static Set<UUID> leavingPlayers = new HashSet<>();
protected static Set<UUID> kickingPlayers = new HashSet<>();
// TODO: It would be good if these could be auto-provided
protected User user;

View File

@ -1,7 +1,9 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -22,7 +24,10 @@ public class IslandInviteAcceptCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
Bukkit.getLogger().info("DEBUG: accept - " + inviteList.toString());
UUID playerUUID = user.getUniqueId();
if(!inviteList.containsKey(playerUUID))
return true;
@ -97,9 +102,4 @@ public class IslandInviteAcceptCommand extends AbstractIslandTeamCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,6 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.List;
import java.util.UUID;
import us.tastybento.bskyblock.api.commands.User;
@ -11,7 +12,6 @@ import us.tastybento.bskyblock.config.Settings;
public class IslandInviteRejectCommand extends AbstractIslandTeamCommand {
public IslandInviteRejectCommand(IslandCommand islandCommand) {
super(islandCommand, "reject");
this.setPermission(Settings.PERMPREFIX + "island.team");
@ -19,7 +19,7 @@ public class IslandInviteRejectCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Reject /island reject
if (inviteList.containsKey(playerUUID)) {
@ -50,9 +50,4 @@ public class IslandInviteRejectCommand extends AbstractIslandTeamCommand {
return true;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,7 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
@ -14,17 +16,9 @@ public class IslandLeaveCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
// TODO Auto-generated method stub
return false;
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,6 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.List;
import java.util.Set;
import java.util.UUID;
@ -26,7 +27,7 @@ public class IslandTeamCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
if (DEBUG)
plugin.getLogger().info("DEBUG: executing team command for " + playerUUID);
@ -84,7 +85,4 @@ public class IslandTeamCommand extends AbstractIslandTeamCommand {
return true;
}
@Override
public void setup() {
}
}

View File

@ -1,14 +1,14 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissionAttachmentInfo;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
@ -28,53 +28,52 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Player issuing the command must have an island
if (!getPlayers().hasIsland(playerUUID)) {
// If the player is in a team, they are not the leader
if (getPlayers().inTeam(playerUUID)) {
user.sendMessage(ChatColor.RED + "general.errors.not-leader");
user.sendMessage("general.errors.not-leader");
}
user.sendMessage(ChatColor.RED + "invite.error.YouMustHaveIslandToInvite");
user.sendMessage("invite.error.YouMustHaveIslandToInvite");
}
if (args.length == 0 || args.length > 1) {
if (args.isEmpty() || args.size() > 1) {
// Invite label with no name, i.e., /island invite - tells the player who has invited them so far
if (inviteList.containsKey(playerUUID)) {
OfflinePlayer inviter = plugin.getServer().getOfflinePlayer(inviteList.get(playerUUID));
user.sendMessage(ChatColor.GOLD + "invite.nameHasInvitedYou", "[name]", inviter.getName());
user.sendMessage("invite.nameHasInvitedYou", "[name]", inviter.getName());
} else {
user.sendMessage(ChatColor.GOLD + "help.island.invite");
user.sendMessage("help.island.invite");
}
return true;
}
if (args.length == 1) {
} else {
// Only online players can be invited
UUID invitedPlayerUUID = getPlayers().getUUID(args[0]);
UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));
if (invitedPlayerUUID == null) {
user.sendMessage(ChatColor.RED + "general.errors.offline-player");
user.sendMessage("general.errors.offline-player");
return true;
}
User invitedPlayer = User.getInstance(invitedPlayerUUID);
if (!invitedPlayer.isOnline()) {
user.sendMessage(ChatColor.RED + "general.errors.offline-player");
user.sendMessage("general.errors.offline-player");
return true;
}
// Player cannot invite themselves
if (playerUUID.equals(invitedPlayerUUID)) {
user.sendMessage(ChatColor.RED + "invite.error.YouCannotInviteYourself");
user.sendMessage("invite.error.YouCannotInviteYourself");
return true;
}
// Check if this player can be invited to this island, or
// whether they are still on cooldown
long time = getPlayers().getInviteCoolDownTime(invitedPlayerUUID, getIslands().getIslandLocation(playerUUID));
if (time > 0 && !user.isOp()) {
user.sendMessage(ChatColor.RED + "invite.error.CoolDown", "[time]", String.valueOf(time));
user.sendMessage("invite.error.CoolDown", "[time]", String.valueOf(time));
return true;
}
// Player cannot invite someone already on a team
if (getPlayers().inTeam(invitedPlayerUUID)) {
user.sendMessage(ChatColor.RED + "invite.error.ThatPlayerIsAlreadyInATeam");
user.sendMessage("invite.error.ThatPlayerIsAlreadyInATeam");
return true;
}
Set<UUID> teamMembers = getMembers(user);
@ -106,7 +105,7 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
// Players can only have one invite one at a time - interesting
if (inviteList.containsValue(playerUUID)) {
inviteList.inverse().remove(playerUUID);
user.sendMessage(ChatColor.RED + "invite.removingInvite");
user.sendMessage("invite.removingInvite");
}
// Fire event so add-ons can run commands, etc.
IslandBaseEvent event = TeamEvent.builder()
@ -119,7 +118,7 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
// Put the invited player (key) onto the list with inviter (value)
// If someone else has invited a player, then this invite will overwrite the previous invite!
inviteList.put(invitedPlayerUUID, playerUUID);
user.sendMessage("invite.inviteSentTo", "[name]", args[0]);
user.sendMessage("invite.inviteSentTo", "[name]", args.get(0));
// Send message to online player
invitedPlayer.sendMessage("invite.nameHasInvitedYou", "[name]", user.getName());
invitedPlayer.sendMessage("invite.toAcceptOrReject", "[label]", getLabel());
@ -134,18 +133,15 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
}
@Override
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) {
User user = User.getInstance(sender);
if (args.length == 0 || !isPlayer(user)) {
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {
List<String> options = new ArrayList<>();
String lastArg = (!args.isEmpty() ? args.getLast() : "");
if (args.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return null;
return Optional.empty();
}
return new ArrayList<>(Util.getOnlinePlayerList(user));
options.addAll(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,11 +1,12 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissionAttachmentInfo;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
@ -15,6 +16,7 @@ import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util;
public class IslandTeamPromoteCommand extends AbstractIslandTeamCommand {
@ -25,7 +27,7 @@ public class IslandTeamPromoteCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Can use if in a team
boolean inTeam = plugin.getPlayers().inTeam(playerUUID);
@ -33,8 +35,8 @@ public class IslandTeamPromoteCommand extends AbstractIslandTeamCommand {
if (!(inTeam && teamLeaderUUID.equals(playerUUID))) {
return true;
}
plugin.getLogger().info("DEBUG: arg[0] = " + args[0]);
UUID targetUUID = getPlayers().getUUID(args[0]);
plugin.getLogger().info("DEBUG: arg[0] = " + args.get(0));
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player");
return true;
@ -130,18 +132,13 @@ public class IslandTeamPromoteCommand extends AbstractIslandTeamCommand {
}
@Override
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) {
User user = User.getInstance(sender);
List<String> result = new ArrayList<>();
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {
List<String> options = new ArrayList<>();
String lastArg = (!args.isEmpty() ? args.getLast() : "");
for (UUID member : plugin.getIslands().getMembers(user.getUniqueId())) {
result.add(plugin.getServer().getOfflinePlayer(member).getName());
options.add(plugin.getServer().getOfflinePlayer(member).getName());
}
return result;
return Optional.of(Util.tabLimit(options, lastArg));
}
@Override
public void setup() {
// TODO Auto-generated method stub
}
}

View File

@ -1,5 +1,9 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
@ -8,9 +12,10 @@ import us.tastybento.bskyblock.api.events.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.util.Util;
public class IslandTeamUninviteCommand extends AbstractIslandTeamCommand {
public IslandTeamUninviteCommand(CompositeCommand islandCommand) {
super(islandCommand, "uninvite");
this.setPermission(Settings.PERMPREFIX + "island.team");
@ -18,7 +23,7 @@ public class IslandTeamUninviteCommand extends AbstractIslandTeamCommand {
}
@Override
public boolean execute(User user, String[] args) {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Can only use if you have an invite out there
if(!inviteList.inverse().containsKey(playerUUID)) {
@ -28,12 +33,13 @@ public class IslandTeamUninviteCommand extends AbstractIslandTeamCommand {
// Fire event so add-ons can run commands, etc.
IslandBaseEvent event = TeamEvent.builder()
.island(getIslands()
.getIsland(playerUUID))
.getIsland(playerUUID))
.reason(TeamReason.UNINVITE)
.involvedPlayer(playerUUID)
.build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return true;
// Perform uninvite
if (inviteList.inverse().containsKey(playerUUID)) {
User invitee = User.getInstance(inviteList.inverse().get(playerUUID));
if (invitee != null) {
@ -47,9 +53,17 @@ public class IslandTeamUninviteCommand extends AbstractIslandTeamCommand {
return false;
}
@Override
public void setup() {
// TODO Auto-generated method stub
// TODO: FIX THIS
@Override
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {
List<String> options = new ArrayList<>();
String lastArg = (!args.isEmpty() ? args.getLast() : "");
if (inviteList.inverse().containsKey(user.getUniqueId())) {
User invitee = User.getInstance(inviteList.inverse().get(user.getUniqueId()));
if (invitee != null) {
options.add(invitee.getName());
}
}
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -15,7 +15,6 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;

View File

@ -97,10 +97,11 @@ public class NewIsland {
plugin.getLogger().info("DEBUG: new island");
//long time = System.nanoTime();
final UUID playerUUID = player.getUniqueId();
/*
boolean firstTime = false;
if (!plugin.getPlayers().hasIsland(playerUUID)) {
firstTime = true;
}
}*/
if (DEBUG)
plugin.getLogger().info("DEBUG: finding island location");
Location next = getNextIsland(player.getUniqueId());
@ -149,8 +150,10 @@ public class NewIsland {
.setChestItems(Settings.chestItems)
.setType(IslandType.END)
.build();
}
}
}
// Teleport player to their island
plugin.getIslands().homeTeleport(player);
// Fire exit event
Reason reasonDone = Reason.CREATED;
switch (reason) {

View File

@ -16,7 +16,6 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Chest;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.config.Settings.GameType;

View File

@ -1,17 +1,18 @@
package us.tastybento.bskyblock.managers;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.BSModule;
import us.tastybento.bskyblock.api.localization.BSLocale;
import us.tastybento.bskyblock.config.Settings;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.BSModule;
import us.tastybento.bskyblock.api.localization.BSLocale;
import us.tastybento.bskyblock.config.Settings;
/**
* @author Poslovitch
*/

View File

@ -1,26 +1,42 @@
package bskyblock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.util.Util;
public class TestIslandCommand {
private final UUID playerUUID = UUID.randomUUID();
private CommandSender sender;
private Player player;
@Before
public void setUp() {
@ -40,30 +56,188 @@ public class TestIslandCommand {
Mockito.when(server.getVersion()).thenReturn("TestTestMocking");
Bukkit.setServer(server);
Mockito.when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
sender = mock(CommandSender.class);
player = mock(Player.class);
Mockito.when(player.hasPermission(Settings.PERMPREFIX + "default.permission")).thenReturn(true);
//Mockito.doReturn(Logger.getAnonymousLogger()).when(plugin.getLogger());
}
@Test
public void command() {
IslandCommand islandCommand = new IslandCommand();
public void test() {
// Test island events
IslandBaseEvent event = TeamEvent.builder()
//.island(getIslands().getIsland(playerUUID))
.reason(TeamReason.INFO)
.involvedPlayer(playerUUID)
.build();
assertEquals(playerUUID, event.getPlayerUUID());
// Test command
User user = User.getInstance(playerUUID);
CompositeCommand testCommand = new TestCommand();
testCommand.setOnlyPlayer(true);
testCommand.setPermission(Settings.PERMPREFIX + "default.permission");
// Test basic execution
assertEquals(islandCommand.execute(user, null), true);
assertEquals(islandCommand.getLabel(), Settings.ISLANDCOMMAND);
assertEquals(islandCommand.getAliases().size(), 1);
assertEquals(islandCommand.getAliases().get(0), "is");
assertEquals(islandCommand.isOnlyPlayer(), true);
assertEquals(islandCommand.getParent(), null);
//TODO: assertEquals(islandCommand.getPermission(), "");
assertTrue(testCommand.execute(user, new ArrayList<>()));
assertEquals("test",testCommand.getLabel());
assertEquals(2, testCommand.getAliases().size());
assertEquals("t", testCommand.getAliases().get(0));
assertTrue(testCommand.isOnlyPlayer());
assertNull(testCommand.getParent());
assertEquals(Settings.PERMPREFIX + "default.permission", testCommand.getPermission());
// Check commands and aliases match to correct class
for (Entry<String, CompositeCommand> command : islandCommand.getSubCommands().entrySet()) {
assertEquals(islandCommand.getSubCommand(command.getKey()), command.getValue());
for (Entry<String, CompositeCommand> command : testCommand.getSubCommands().entrySet()) {
assertEquals(testCommand.getSubCommand(command.getKey()), command.getValue());
// Check aliases
for (String alias : command.getValue().getAliases()) {
assertEquals(islandCommand.getSubCommand(alias), command.getValue());
assertEquals(testCommand.getSubCommand(alias), command.getValue());
}
}
String[] args = {""};
assertEquals(Arrays.asList("sub1","sub2"), testCommand.tabComplete(player, "test", args));
assertNotSame(Arrays.asList("sub1","sub2"), testCommand.tabComplete(sender, "test", args));
args[0] = "su";
assertEquals(Arrays.asList("sub1","sub2"), testCommand.tabComplete(player, "test", args));
args[0] = "d";
assertNotSame(Arrays.asList("sub1","sub2"), testCommand.tabComplete(player, "test", args));
args[0] = "sub1";
assertEquals(Arrays.asList(), testCommand.tabComplete(player, "test", args));
String[] args2 = {"sub2",""};
assertEquals(Arrays.asList("subsub"), testCommand.tabComplete(player, "test", args2));
args2[1] = "s";
assertEquals(Arrays.asList("subsub"), testCommand.tabComplete(player, "test", args2));
String[] args3 = {"sub2","subsub", ""};
assertEquals(Arrays.asList("subsubsub"), testCommand.tabComplete(player, "test", args3));
// Test for overridden tabcomplete
assertEquals(Arrays.asList(new String[] {"Florian", "Ben", "Bill", "Ted"}),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", ""}));
// Test for partial word
assertEquals(Arrays.asList(new String[] {"Ben", "Bill"}),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", "b"}));
// Test command arguments
CompositeCommand argCmd = new Test3ArgsCommand();
argCmd.setOnlyPlayer(true);
argCmd.setPermission(Settings.PERMPREFIX + "default.permission");
assertTrue(argCmd.execute(player, "args", new String[]{"give", "100", "ben"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100"}));
assertTrue(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100", "today"}));
}
private class TestCommand extends CompositeCommand {
public TestCommand() {
super("test", "t", "tt");
}
@Override
public boolean execute(User user, List<String> args) {
return true;
}
@Override
public void setup() {
// Set up sub commands
new TestSubCommand(this); // Level 1
new TestSubCommand2(this); // Has sub command
}
}
private class TestSubCommand extends CompositeCommand {
public TestSubCommand(CompositeCommand parent) {
super(parent, "sub1", "subone");
}
@Override
public boolean execute(User user, List<String> args) {
return true;
}
}
private class TestSubCommand2 extends CompositeCommand {
public TestSubCommand2(CompositeCommand parent) {
super(parent, "sub2", "level1");
}
@Override
public boolean execute(User user, List<String> args) {
return true;
}
@Override
public void setup() {
// Set up sub commands
new TestSubSubCommand(this); // Level 2
}
}
private class TestSubSubCommand extends CompositeCommand {
public TestSubSubCommand(CompositeCommand parent) {
super(parent, "subsub", "level2", "subsubby");
}
@Override
public boolean execute(User user, List<String> args) {
return true;
}
@Override
public void setup() {
// Set up sub commands
new TestSubSubSubCommand(this); // Level 3
}
}
private class TestSubSubSubCommand extends CompositeCommand {
public TestSubSubSubCommand(CompositeCommand parent) {
super(parent, "subsubsub", "level3", "subsubsubby");
}
@Override
public boolean execute(User user, List<String> args) {
Bukkit.getLogger().info("args are " + args.toString());
if (args.size() == 3) return true;
return false;
}
@Override
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {
List<String> options = new ArrayList<>();
String lastArg = (!args.isEmpty() ? args.getLast() : "");
options.addAll(Arrays.asList(new String[] {"Florian", "Ben", "Bill", "Ted"}));
return Optional.of(Util.tabLimit(options, lastArg));
}
}
private class Test3ArgsCommand extends CompositeCommand {
public Test3ArgsCommand() {
super("args", "");
}
@Override
public boolean execute(User user, List<String> args) {
Bukkit.getLogger().info("args are " + args.toString());
if (args.size() == 3) return true;
return false;
}
}
}

View File

@ -1,21 +1,8 @@
package bskyblock;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason;
public class TestTest {
private final UUID playerUUID = UUID.randomUUID();