Implemented command arguments and tabcomplete changes

Args no longer include the sub-command, just the args after the sub
command.

Improved tab complete so that it completes half-written commands

Fixed deprecation issue with visitor guard.
This commit is contained in:
tastybento 2017-08-05 10:23:45 -07:00
parent 3a10230824
commit f515623e26
7 changed files with 140 additions and 128 deletions

Binary file not shown.

View File

@ -64,7 +64,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12-pre5-SNAPSHOT</version>
<version>1.12.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -1,13 +1,22 @@
package us.tastybento.bskyblock.api.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import java.util.*;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.util.Util;
/**
*
@ -134,32 +143,22 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
checkForPlayer(sender);
if (this.canUse(sender)) {
// Check if the command sender is a player or not
if (sender instanceof Player) {
isPlayer = true;
player = (Player)sender;
playerUUID = player.getUniqueId();
}
// Check if the player is in a team or not and if so, grab the team leader's UUID
if (plugin.getPlayers().inTeam(playerUUID)) {
inTeam = true;
teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID);
teamMembers = plugin.getIslands().getMembers(teamLeaderUUID);
}
if(args.length >= 1) {
ArgumentHandler handler = getHandler(args[0]); // Store the handler to save some calculations
if (handler != null && handler.canUse(sender)) {
handler.execute(sender, args);
handler.execute(sender, clean(Arrays.copyOfRange(args, 1, args.length)));
} else if (help) {
if (argumentsMap.containsKey("help")) {
argumentsMap.get("help").execute(sender, args);
argumentsMap.get("help").execute(sender, clean(Arrays.copyOfRange(args, 1, args.length)));
}
} else {
// Unknown handler
this.execute(sender, args);
}
} else {
// No args
this.execute(sender, args);
}
}
@ -169,6 +168,8 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args){
List<String> options = new ArrayList<String>();
checkForPlayer(sender);
String lastArg = (args.length != 0 ? args[args.length - 1] : "");
if (canUse(sender)) {
if (args.length <= 1) {
// Go through every argument, check if player can use it and if so, add it in tab options
@ -179,11 +180,38 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
// If player can execute the argument, get its tab-completer options
ArgumentHandler handler = getHandler(args[0]);
if (handler != null && handler.canUse(sender)) {
List<String> tabOptions = handler.tabComplete(sender, args);
// We remove the 1st arg - and remove any blank args caused by hitting space before the tab
List<String> tabOptions = handler.tabComplete(sender, clean(Arrays.copyOfRange(args, 1, args.length)));
if (tabOptions != null) options.addAll(tabOptions);
}
}
}
return options;
return Util.tabLimit(options, lastArg);
}
private static String[] clean(final String[] v) {
List<String> list = new ArrayList<String>(Arrays.asList(v));
list.removeAll(Collections.singleton(""));
return list.toArray(new String[list.size()]);
}
/**
* Sets some variables and flags if this is a player
* @param sender
*/
private void checkForPlayer(CommandSender sender) {
// Check if the command sender is a player or not
if (sender instanceof Player) {
isPlayer = true;
player = (Player)sender;
playerUUID = player.getUniqueId();
}
// Check if the player is in a team or not and if so, grab the team leader's UUID
if (plugin.getPlayers().inTeam(playerUUID)) {
inTeam = true;
teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID);
teamMembers = plugin.getIslands().getMembers(teamLeaderUUID);
}
}
}

View File

@ -1,11 +1,17 @@
package us.tastybento.bskyblock.commands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.AbstractCommand;
import us.tastybento.bskyblock.config.Settings;
@ -14,10 +20,6 @@ import us.tastybento.bskyblock.schematics.Schematic;
import us.tastybento.bskyblock.util.Util;
import us.tastybento.bskyblock.util.VaultHelper;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
/**
* "/island" command
*
@ -502,17 +504,6 @@ public class IslandCommand extends AbstractCommand {
@Override
public void execute(CommandSender sender, String[] args) {
plugin.getLogger().info("DEBUG: executing team command");
}
@Override
public List<String> tabComplete(CommandSender sender, String[] args) {
return null;
}
@Override
public String[] usage(CommandSender sender){
plugin.getLogger().info("DEBUG: executing team help");
if (inTeam) {
if (teamLeaderUUID.equals(playerUUID)) {
int maxSize = Settings.maxTeamSize;
@ -553,6 +544,18 @@ public class IslandCommand extends AbstractCommand {
} else {
Util.sendMessage(sender, plugin.getLocale(sender).get("general.errors.no-team"));
}
}
@Override
public List<String> tabComplete(CommandSender sender, String[] args) {
return null;
}
@Override
public String[] usage(CommandSender sender){
plugin.getLogger().info("DEBUG: executing team help");
return new String[] {null, plugin.getLocale(sender).get("help.island.team")};
}
});
@ -562,19 +565,24 @@ public class IslandCommand extends AbstractCommand {
@Override
public boolean canUse(CommandSender sender) {
plugin.getLogger().info("DEBUG: invite player command canUse check");
if (isPlayer) {
plugin.getLogger().info("DEBUG: is player");
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) {
plugin.getLogger().info("DEBUG: has perm");
plugin.getLogger().info("DEBUG: " + player.getName() + " has perm");
return true;
} else {
plugin.getLogger().info("DEBUG: " + player.getName() + " does not have perm");
}
} else {
plugin.getLogger().info("DEBUG: is not a player");
}
plugin.getLogger().info("DEBUG: does not have perm");
plugin.getLogger().info("DEBUG: does not have perm: " + Settings.PERMPREFIX + "team.create");
return false;
}
@Override
public void execute(CommandSender sender, String[] args) {
plugin.getLogger().info("DEBUG: executing invite");
if (args.length == 0) {
// Invite label with no name, i.e., /island invite - tells the player who has invited them so far
//TODO
@ -583,7 +591,7 @@ public class IslandCommand extends AbstractCommand {
if (args.length == 1) {
// Only online players can be invited
@SuppressWarnings("deprecation")
Player invitedPlayer = plugin.getServer().getPlayer(args[1]);
Player invitedPlayer = plugin.getServer().getPlayer(args[0]);
if (invitedPlayer == null) {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(sender).get("general.errors.offline-player"));
return;
@ -595,7 +603,7 @@ public class IslandCommand extends AbstractCommand {
return;
}
// Player cannot invite themselves
if (player.getName().equalsIgnoreCase(args[1])) {
if (player.getName().equalsIgnoreCase(args[0])) {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(sender).get("invite.errorYouCannotInviteYourself"));
return;
}
@ -704,8 +712,7 @@ public class IslandCommand extends AbstractCommand {
@Override
public List<String> tabComplete(CommandSender sender, String[] args) {
return null;
return Util.getOnlinePlayerList(player);
}
@Override

View File

@ -1,66 +0,0 @@
package us.tastybento.bskyblock.database.managers;
import java.util.UUID;
import org.bukkit.Location;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter;
import us.tastybento.bskyblock.database.objects.Island;
public class RunTest {
public RunTest(BSkyBlock plugin) {
try {
DatabaseConnecter connecter = new FlatFileDatabaseConnecter(plugin, null);
/*
new DatabaseConnectionSettingsImpl(
"127.0.0.1", 3306, "exampleDatabase","user", "pass"));
*/
Island test = new Island();
test.setName("testname");
test.setOwner(UUID.randomUUID());
test.addMember(UUID.randomUUID());
test.addToBanList(UUID.randomUUID());
test.setCenter(new Location(plugin.getServer().getWorld("world"), 1, 2, 3, 1, 1));
test.setLocked(true);
/*
HashMap<Integer, Location> homes = new HashMap<Integer, Location>();
homes.put(1, new Location(plugin.getServer().getWorld("world"), 1, 2, 3, 1, 1));
homes.put(2, new Location(plugin.getServer().getWorld("world"), 3, 3, 3, 3, 3));
test.setHomeLocations(homes);
List<ItemStack> items = new ArrayList<ItemStack>();
items.add(new ItemStack(Material.ACTIVATOR_RAIL, 2));
items.add(new ItemStack(Material.FEATHER,5));
test.setInventory(items);
*/
BSBDatabase database = BSBDatabase.getDatabase();
AbstractDatabaseHandler<Island> handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class);
handler.saveObject(test);
plugin.getLogger().info("DEBUG: ALL WRITTEN! Now reading...");
test = handler.loadObject(test.getUniqueId());
plugin.getLogger().info("DEBUG: name = " + test.getName());
plugin.getLogger().info("DEBUG: owner = " + test.getOwner());
/*
homes = test.getHomeLocations();
plugin.getLogger().info("DEBUG: homes = " + homes);
items = test.getInventory();
plugin.getLogger().info("DEBUG: items = " + items);
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -8,10 +8,10 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import us.tastybento.bskyblock.BSkyBlock;
@ -73,25 +73,6 @@ public class VisitorGuard implements Listener {
InventorySave.getInstance().clearSavedInventory(e.getPlayer());
}
}
/*
* Prevent item pickup by visitors
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onVisitorPickup(final PlayerPickupItemEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
}
if (!Util.inWorld(e.getPlayer())) {
return;
}
Island island = plugin.getIslands().getIslandAt(e.getItem().getLocation());
if ((island != null && island.getFlag(SettingsFlag.ITEM_PICKUP))
|| e.getPlayer().isOp() || VaultHelper.hasPerm(e.getPlayer(), Settings.PERMPREFIX + "mod.bypassprotect")
|| plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getItem().getLocation())) {
return;
}
e.setCancelled(true);
}
/*
* Prevent item drop by visitors
@ -182,4 +163,27 @@ public class VisitorGuard implements Listener {
}
}
/*
* Prevent item pickup by visitors
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onVisitorPickup(final EntityPickupItemEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
}
if (e.getEntity() instanceof Player) {
Player player = (Player)e.getEntity();
if (!Util.inWorld(player)) {
return;
}
Island island = plugin.getIslands().getIslandAt(e.getItem().getLocation());
if ((island != null && island.getFlag(SettingsFlag.ITEM_PICKUP))
|| player.isOp() || VaultHelper.hasPerm(player, Settings.PERMPREFIX + "mod.bypassprotect")
|| plugin.getIslands().locationIsOnIsland(player, e.getItem().getLocation())) {
return;
}
e.setCancelled(true);
}
}
}

View File

@ -18,6 +18,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -299,4 +300,42 @@ public class Util {
}
return null;
}
/**
* Return a list of online players this player can see, i.e. are not invisible
* @param player - if null, all player names on the server are shown
* @return
*/
public static List<String> getOnlinePlayerList(Player player) {
final List<String> returned = new ArrayList<String>();
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (player == null) {
returned.add(p.getName());
} else if (player.canSee(p)) {
returned.add(p.getName());
}
}
return returned;
}
/**
* Returns all of the items that begin with the given start,
* ignoring case. Intended for tabcompletion.
*
* @param list
* @param start
* @return List of items that start with the letters
*/
public static List<String> tabLimit(final List<String> list, final String start) {
final List<String> returned = new ArrayList<String>();
for (String s : list) {
if (s == null)
continue;
if (s.toLowerCase().startsWith(start.toLowerCase())) {
returned.add(s);
}
}
return returned;
}
}