Skip to content

Commit

Permalink
Added argument help handling - Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
UndeadScythes committed Dec 1, 2012
1 parent c7e7dad commit 6cb029c
Show file tree
Hide file tree
Showing 73 changed files with 1,046 additions and 1,085 deletions.
Binary file modified Testing/UDSPlugin1.jar
Binary file not shown.
120 changes: 56 additions & 64 deletions src/com/undeadscythes/udsplugin/PlayerCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
public abstract class PlayerCommandExecutor implements CommandExecutor {
private SaveablePlayer player;
private String commandName;
private int argsLength;

/**
Expand All @@ -23,14 +24,11 @@ public abstract class PlayerCommandExecutor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(sender instanceof Player) {
commandName = command.getName();
player = UDSPlugin.getOnlinePlayers().get(sender.getName());
if(hasPerm(Perm.valueOf(command.getName().toUpperCase()))) {
if(args.length == 1 && args[0].equals("help")) {
player.performCommand("help " + command.getName());
} else {
argsLength = args.length;
playerExecute(player, args);
}
if(hasPerm(Perm.valueOf(commandName.toUpperCase()))) {
argsLength = args.length;
playerExecute(player, args);
}
return true;
} else {
Expand Down Expand Up @@ -92,6 +90,57 @@ public Region getShop() {

}

public void subCmdHelp(String[] args) {
if(args.length == 1 && args[0].equalsIgnoreCase("help")) {
sendHelp(1);
} else if(args.length == 2 && args[0].equalsIgnoreCase("help") && args[1].matches("[0-9][0-9]*")) {
sendHelp(Integer.parseInt(args[1]));
} else {
subCmdHelp();
}
}

public void subCmdHelp() {
player.sendMessage(Color.ERROR + "That command is not recognized.");
sendHelp(1);
}

public boolean numArgsHelp(int num) {
if(argsLength == num) {
return true;
} else {
numArgsHelpX();
return false;
}
}

public boolean minArgsHelp(int num) {
if(argsLength >= num) {
return true;
} else {
numArgsHelpX();
return false;
}
}

public boolean maxArgsHelp(int num) {
if(argsLength <= num) {
return true;
} else {
numArgsHelpX();
return false;
}
}

public void numArgsHelpX() {
player.sendMessage(Color.ERROR + "You have made an error using this command.");
sendHelp(1);
}

public void sendHelp(int page) {
player.performCommand("help " + commandName + " " + page);
}

public boolean canRequest(SaveablePlayer target) {
if(noRequests(target) && notIgnored(target)) {
return true;
Expand Down Expand Up @@ -753,63 +802,6 @@ public boolean notIgnored(SaveablePlayer target) {
}
}

/**
* Checks that the command has the correct number of arguments.
* @param length Number of arguments required.
* @return <code>true</code> if there are sufficient arguments, <code>false</code> if there are too few or too many.
*/
public boolean argsEq(int length) {
if(argsLength == length) {
return true;
} else {
player.sendMessage(Message.WRONG_NUM_ARGS);
return false;
}
}

/**
* Checks that the command has the correct number of arguments.
* @param max Maximum number of arguments.
* @return <code>true</code> if there aren't too many arguments, <code>false</code> otherwise.
*/
public boolean argsLessEq(int max) {
if(argsLength <= max) {
return true;
} else {
player.sendMessage(Message.WRONG_NUM_ARGS);
return false;
}
}

/**
* Checks that the command has the correct number of arguments.
* @param min Minimum number of arguments.
* @return <code>true</code> if there aren't enough arguments, <code>false</code> otherwise.
*/
public boolean argsMoreEq(int min) {
if(argsLength >= min) {
return true;
} else {
player.sendMessage(Message.WRONG_NUM_ARGS);
return false;
}
}

/**
* Checks that the command has the correct number of arguments.
* @param min Minimum number of arguments.
* @param max Maximum number of arguments.
* @return <code>true</code> if there the right number of arguments, <code>false</code> otherwise.
*/
public boolean argsMoreLessInc(int min, int max) {
if(argsLength >= min && argsLength <= max) {
return true;
} else {
player.sendMessage(Message.WRONG_NUM_ARGS);
return false;
}
}

/**
* Checks to see if a string is a positive number.
* @param number String to check.
Expand Down
4 changes: 2 additions & 2 deletions src/com/undeadscythes/udsplugin/commands/AcceptRulesCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.bukkit.*;

/**
* Lets a player get build rights and promotes them to member.
* Lets a player get build rights and promotes them to member. Ignores extra arguments.
* @author UndeadScythes
*/
public class AcceptRulesCmd extends PlayerCommandExecutor {
Expand All @@ -14,7 +14,7 @@ public class AcceptRulesCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(canAfford(Config.BUILD_COST) && argsEq(0)) {
if(canAfford(Config.BUILD_COST)) {
player.setRank(PlayerRank.MEMBER);
player.debit(Config.BUILD_COST);
Bukkit.broadcastMessage(Color.BROADCAST + player.getDisplayName() + " has accepted the rules.");
Expand Down
6 changes: 2 additions & 4 deletions src/com/undeadscythes/udsplugin/commands/BackCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public class BackCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsEq(0)) {
if(!player.quietTeleport(player.getBack())) {
player.sendMessage(Color.ERROR + "You can't teleport back at this time.");
}
if(!player.quietTeleport(player.getBack())) {
player.sendMessage(Color.ERROR + "You can't teleport back at this time.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/undeadscythes/udsplugin/commands/BanCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BanCmd extends PlayerCommandExecutor {
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
SaveablePlayer target;
if(argsMoreEq(1) && (target = getMatchingOtherPlayer(args[0])) != null) {
if(minArgsHelp(1) && (target = getMatchingOtherPlayer(args[0])) != null) {
String message = "You have been banned for breaking the rules.";
if(args.length > 1) {
message = StringUtils.join(args, " ", 1, args.length - 1);
Expand Down
24 changes: 11 additions & 13 deletions src/com/undeadscythes/udsplugin/commands/BountyCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ public class BountyCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsLessEq(2)) {
int bounty;
int page;
SaveablePlayer target;
if(args.length == 0) {
sendPage(1, player);
} else if(args.length == 1 && (page = parseInt(args[0])) != -1) {
sendPage(page, player);
} else if(args.length == 2 && (target = getMatchingOtherPlayer(args[0])) != null && (bounty = getAffordablePrice(args[1])) != -1) {
player.debit(bounty);
target.addBounty(bounty);
Bukkit.broadcastMessage(Color.BROADCAST + player.getDisplayName() + " placed a bounty on " + target.getDisplayName() + ".");
}
int bounty;
int page;
SaveablePlayer target;
if(args.length == 0) {
sendPage(1, player);
} else if(args.length == 1 && (page = parseInt(args[0])) != -1) {
sendPage(page, player);
} else if(numArgsHelp(2) && (target = getMatchingOtherPlayer(args[0])) != null && (bounty = getAffordablePrice(args[1])) != -1) {
player.debit(bounty);
target.addBounty(bounty);
Bukkit.broadcastMessage(Color.BROADCAST + player.getDisplayName() + " placed a bounty on " + target.getDisplayName() + ".");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/undeadscythes/udsplugin/commands/BroadcastCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BroadcastCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsMoreEq(1)) {
if(minArgsHelp(1)) {
Bukkit.broadcastMessage(Color.BROADCAST + StringUtils.join(args, " "));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/undeadscythes/udsplugin/commands/ButcherCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ButcherCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsLessEq(1)) {
if(maxArgsHelp(1)) {
boolean all = false;
if(args.length == 1 && (args[0].equals("a") || args[0].equals("all"))) {
all = true;
Expand Down
2 changes: 1 addition & 1 deletion src/com/undeadscythes/udsplugin/commands/CallCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CallCmd extends PlayerCommandExecutor {
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
SaveablePlayer target;
if(argsEq(1) && (target = getMatchingOtherOnlinePlayer(args[0])) != null && canRequest(target) && notJailed(target) && canTP()) {
if(numArgsHelp(1) && (target = getMatchingOtherOnlinePlayer(args[0])) != null && canRequest(target) && notJailed(target) && canTP()) {
UDSPlugin.getRequests().put(target.getName(), new Request(player, Request.RequestType.TP, "", target));
player.sendMessage(Message.REQUEST_SENT);
target.sendMessage(Color.MESSAGE + player.getName() + " wishes to teleport to you.");
Expand Down
2 changes: 1 addition & 1 deletion src/com/undeadscythes/udsplugin/commands/ChallengeCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ChallengeCmd extends PlayerCommandExecutor {
public void playerExecute(SaveablePlayer player, String[] args) {
SaveablePlayer target;
int wager;
if(notJailed() && argsEq(2) && (target = getMatchingPlayer(args[0])) != null && isOnline(target) && notJailed(target) && (wager = parseInt(args[1])) != -1 && canAfford(wager) && noRequests(target) && notDueling(target) && notSelf(target)) {
if(numArgsHelp(2) && notJailed() && (target = getMatchingPlayer(args[0])) != null && isOnline(target) && notJailed(target) && (wager = parseInt(args[1])) != -1 && canAfford(wager) && noRequests(target) && notDueling(target) && notSelf(target)) {
UDSPlugin.getRequests().put(target.getName(), new Request(player, Request.RequestType.PVP, wager, target));
target.sendMessage(Color.MESSAGE + player.getDisplayName() + " has challenged you to a duel for " + wager + " credits.");
target.sendMessage(Message.REQUEST_Y_N);
Expand Down
4 changes: 3 additions & 1 deletion src/com/undeadscythes/udsplugin/commands/CheckCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class CheckCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
player.quietTeleport(player.getCheckPoint());
if(!player.quietTeleport(player.getCheckPoint())) {
player.sendMessage(Color.ERROR + "You do not currently have a checkpoint set.");
}
}
}
6 changes: 2 additions & 4 deletions src/com/undeadscythes/udsplugin/commands/CiCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public class CiCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsEq(0)) {
player.getInventory().clear(-1, -1);
player.sendMessage(Color.MESSAGE + "Inventory cleared.");
}
player.getInventory().clear(-1, -1);
player.sendMessage(Color.MESSAGE + "Inventory cleared.");
}
}
104 changes: 54 additions & 50 deletions src/com/undeadscythes/udsplugin/commands/CityCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,63 @@ public class CityCmd extends PlayerCommandExecutor {
*/
@Override
public void playerExecute(SaveablePlayer player, String[] args) {
if(argsMoreLessInc(1, 3)) {
Region city;
if(args.length == 1) {
if(args[0].equals("set") && (city = getCurrentRegion()).getType() == Region.RegionType.CITY && getMunicipality(city.getName()) != null) {
city.setWarp(player.getLocation());
player.sendMessage(Color.MESSAGE + "City spawn point set.");
} else if(args[0].equals("list")) {
sendPage(1, player);
Region city;
if(args.length == 1) {
if(args[0].equals("set") && (city = getCurrentRegion()).getType() == Region.RegionType.CITY && getMunicipality(city.getName()) != null) {
city.setWarp(player.getLocation());
player.sendMessage(Color.MESSAGE + "City spawn point set.");
} else if(args[0].equals("list")) {
sendPage(1, player);
} else {
subCmdHelp(args);
}
} else if(args.length == 2) {
int page;
if(args[0].equals("new") && canAfford(Config.CITY_COST) && noCensor(args[1]) && notRegion(args[1])) {
Vector min = player.getLocation().add(-100, 0, -100).toVector().setY(0);
Vector max = player.getLocation().add(100, 0, 100).toVector().setY(player.getWorld().getMaxHeight());
city = new Region(args[1], min, max, player.getLocation(), player, "", Region.RegionType.CITY);
if(noOverlaps(city)) {
player.debit(Config.CITY_COST);
UDSPlugin.getRegions().put(args[1], city);
UDSPlugin.getCities().put(args[1], city);
city.placeMoreMarkers();
city.placeTowers();
player.sendMessage(Color.MESSAGE + "City founded.");
Bukkit.broadcastMessage(Color.BROADCAST + player.getDisplayName() + " has just founded " + args[1] + ".");
}
} else if(args[0].equals("leave") && (city = getMatchingCity(args[1])) != null) {
if(city.delMember(player)) {
player.sendMessage(Color.MESSAGE + "You have left " + city.getName() + ".");
} else {
player.sendMessage(Color.ERROR + "You are not a citizen of " + city.getName() + ".");
}
} else if(args.length == 2) {
int page;
if(args[0].equals("new") && canAfford(Config.CITY_COST) && noCensor(args[1]) && notRegion(args[1])) {
Vector min = player.getLocation().add(-100, 0, -100).toVector().setY(0);
Vector max = player.getLocation().add(100, 0, 100).toVector().setY(player.getWorld().getMaxHeight());
city = new Region(args[1], min, max, player.getLocation(), player, "", Region.RegionType.CITY);
if(noOverlaps(city)) {
player.debit(Config.CITY_COST);
UDSPlugin.getRegions().put(args[1], city);
UDSPlugin.getCities().put(args[1], city);
city.placeMoreMarkers();
city.placeTowers();
player.sendMessage(Color.MESSAGE + "City founded.");
Bukkit.broadcastMessage(Color.BROADCAST + player.getDisplayName() + " has just founded " + args[1] + ".");
}
} else if(args[0].equals("leave") && (city = getMatchingCity(args[1])) != null) {
if(city.delMember(player)) {
player.sendMessage(Color.MESSAGE + "You have left " + city.getName() + ".");
} else {
player.sendMessage(Color.ERROR + "You are not a citizen of " + city.getName() + ".");
}
} else if(args[0].equals("warp") && (city = getMatchingCity(args[1])) != null && notJailed() && notPinned()) {
player.quietTeleport(city.getWarp());
} else if(args[0].equals("list") && (page = parseInt(args[1])) != -1) {
sendPage(page, player);
} else if(args[0].equals("warp") && (city = getMatchingCity(args[1])) != null && notJailed() && notPinned()) {
player.quietTeleport(city.getWarp());
} else if(args[0].equals("list") && (page = parseInt(args[1])) != -1) {
sendPage(page, player);
} else {
subCmdHelp(args);
}
} else if(numArgsHelp(3)) {
SaveablePlayer target;
if(args[0].equals("invite") && (city = getMunicipality(args[1])) != null && (target = getMatchingPlayer(args[2])) != null) {
if(city.addMember(target)) {
player.sendMessage(Color.MESSAGE + target.getDisplayName() + " was added as a citizen of " + city.getName() + ".");
target.sendMessage(Color.MESSAGE + "You have been added as a citizen of " + city.getName());
} else {
player.sendMessage(Color.ERROR + target.getDisplayName() + " is already a citizen of " + city.getName() + ".");
}
} else if(args.length == 3) {
SaveablePlayer target;
if(args[0].equals("invite") && (city = getMunicipality(args[1])) != null && (target = getMatchingPlayer(args[2])) != null) {
if(city.addMember(target)) {
player.sendMessage(Color.MESSAGE + target.getDisplayName() + " was added as a citizen of " + city.getName() + ".");
target.sendMessage(Color.MESSAGE + "You have been added as a citizen of " + city.getName());
} else {
player.sendMessage(Color.ERROR + target.getDisplayName() + " is already a citizen of " + city.getName() + ".");
}
} else if(args[0].equals("banish") && (city = getMunicipality(args[1])) != null && (target = getMatchingPlayer(args[2])) != null) {
if(city.delMember(target)) {
player.sendMessage(Color.MESSAGE + target.getDisplayName() + " has been banished from " + city.getName() + ".");
target.sendMessage(Color.MESSAGE + "You have been banished from " + city.getName() + ".");
target.quietTeleport(city.getWarp());
} else {
player.sendMessage(Color.ERROR + target.getDisplayName() + " is not a citizen of " + city.getName() + ".");
}
} else if(args[0].equals("banish") && (city = getMunicipality(args[1])) != null && (target = getMatchingPlayer(args[2])) != null) {
if(city.delMember(target)) {
player.sendMessage(Color.MESSAGE + target.getDisplayName() + " has been banished from " + city.getName() + ".");
target.sendMessage(Color.MESSAGE + "You have been banished from " + city.getName() + ".");
target.quietTeleport(city.getWarp());
} else {
player.sendMessage(Color.ERROR + target.getDisplayName() + " is not a citizen of " + city.getName() + ".");
}
} else {
subCmdHelp();
}
}
}
Expand Down

0 comments on commit 6cb029c

Please sign in to comment.