Convert disguises commands to use a commonly shared method to parse a string to a disguise including options

This commit is contained in:
Andrew 2013-11-06 04:06:15 +13:00
parent 969fd5e9bf
commit 2ccac87531
6 changed files with 298 additions and 543 deletions

View File

@ -0,0 +1,181 @@
package me.libraryaddict.disguise;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import net.minecraft.v1_6_R3.org.bouncycastle.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public abstract class BaseDisguiseCommand implements CommandExecutor {
protected ArrayList<String> getAllowedDisguises(CommandSender sender, String permissionNode) {
ArrayList<String> names = new ArrayList<String>();
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (sender.hasPermission("libsdisguises." + permissionNode + ".*")
|| sender.hasPermission("libsdisguises." + permissionNode + "." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
protected boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception ex) {
return false;
}
}
protected boolean isDouble(String string) {
try {
Float.parseFloat(string);
return true;
} catch (Exception ex) {
return false;
}
}
protected abstract void sendCommandUsage(CommandSender sender);
/**
* Returns the disguise if it all parsed correctly. Returns a exception with a complete message if it didn't. The
* commandsender is purely used for checking permissions. Would defeat the purpose otherwise. To reach this point, the
* disguise has been feed a proper disguisetype.
*/
protected Disguise parseDisguise(CommandSender sender, String[] args) throws Exception {
String permissionNode = getClass().getSimpleName().replace("Command", "").toLowerCase();
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, permissionNode);
if (allowedDisguises.isEmpty()) {
throw new Exception(ChatColor.RED + "You are forbidden to use this command.");
}
if (args.length == 0) {
sendCommandUsage(sender);
throw new Exception();
}
DisguiseType disguiseType;
try {
disguiseType = DisguiseType.valueOf(args[0].toUpperCase());
} catch (Exception ex) {
throw new Exception(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED
+ " doesn't exist!");
}
if (!allowedDisguises.contains(args[0].toLowerCase())) {
throw new Exception(ChatColor.RED + "You are forbidden to use this disguise!");
}
Disguise disguise = null;
// How many args to skip due to the disugise being constructed
int toSkip = 1;
// Time to start constructing the disguise.
// We will need to check between all 3 kinds of disguises
if (disguiseType.isPlayer()) {// If he is doing a player disguise
toSkip++;
if (args.length == 1) {
// He needs to give the player name
throw new Exception(ChatColor.RED + "Error! You need to give a player name!");
} else {
// Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
}
} else {
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
boolean adult = true;
if (args.length > 1) {
try {
adult = Boolean.valueOf(args[1]);
toSkip++;
} catch (Exception ex) {
// Its not a true/false for adult..
}
}
disguise = new MobDisguise(disguiseType, adult);
} else if (disguiseType.isMisc()) {
// Its a misc, we are going to use the MiscDisguise constructor.
int miscId = -1;
int miscData = -1;
if (args.length > 1) {
// They have defined more arguements!
// If the first arg is a number
if (isNumeric(args[1])) {
miscId = Integer.parseInt(args[1]);
toSkip++;
// If they also defined a data value
if (args.length > 2) {
if (isNumeric(args[2])) {
miscData = Integer.parseInt(args[2]);
toSkip++;
}
}
}
}
// Construct the disguise
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
}
}
// Copy strings to their new range
String[] newArgs = new String[args.length - toSkip];
for (int i = toSkip; i < args.length; i++) {
newArgs[i - toSkip] = args[i];
}
args = newArgs;
// Don't throw a error about uneven methods names and values so we can throw the error about what is unknown later.
for (int i = 0; i < args.length; i += 2) {
String methodName = args[i];
if (i + 1 >= args.length) {
throw new Exception(ChatColor.RED + "No value was given for " + methodName);
}
String valueString = args[i + 1];
Method methodToUse = null;
Object value = null;
for (Method method : disguise.getWatcher().getClass().getMethods()) {
if (method.getName().equalsIgnoreCase(methodName)) {
methodToUse = method;
methodName = method.getName();
Class<?>[] types = method.getParameterTypes();
if (types.length == 1) {
Class param = types[0];
if ((param.isAssignableFrom(Float.class) || param.isAssignableFrom(int.class) || param
.isAssignableFrom(Double.class))) {
if (isDouble(valueString)) {
value = param.cast(Float.parseFloat(valueString));
} else {
throw new Exception(ChatColor.RED + "Expected a number, received " + valueString
+ " instead for " + methodName);
}
} else if (param.isAssignableFrom(boolean.class)) {
try {
Boolean.parseBoolean(valueString);
} catch (Exception ex) {
throw new Exception(ChatColor.RED + "Expected true/false, received " + valueString
+ " instead for " + methodName);
}
value = param.cast(Boolean.parseBoolean(valueString));
} else if (param == String.class) {
value = valueString;
}
}
break;
}
}
if (methodToUse == null) {
throw new Exception(ChatColor.RED + "Cannot find option " + methodName);
}
methodToUse.invoke(disguise.getWatcher(), value);
}
// Alright. We've constructed our disguise.
return disguise;
}
}

View File

@ -28,7 +28,6 @@ public class DisguiseListener implements Listener {
+ ", the new version is " + ChatColor.RED + "%s" + ChatColor.DARK_RED + "!"; + ", the new version is " + ChatColor.RED + "%s" + ChatColor.DARK_RED + "!";
public DisguiseListener(LibsDisguises libsDisguises) { public DisguiseListener(LibsDisguises libsDisguises) {
plugin = libsDisguises; plugin = libsDisguises;
permission = plugin.getConfig().getString("Permission"); permission = plugin.getConfig().getString("Permission");
if (plugin.getConfig().getBoolean("NotifyUpdate")) { if (plugin.getConfig().getBoolean("NotifyUpdate")) {

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
@ -18,41 +18,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class DisguiseCommand extends BaseDisguiseCommand {
public class DisguiseCommand implements CommandExecutor {
private ArrayList<String> allowedDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (sender.hasPermission("libsdisguises.disguise.*") || sender.hasPermission("libsdisguises.disguise." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private ArrayList<String> forbiddenDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
if (sender.hasPermission("libsdisguises.disguise.*"))
return names;
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (!sender.hasPermission("libsdisguises.disguise." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception ex) {
return false;
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
@ -60,102 +26,23 @@ public class DisguiseCommand implements CommandExecutor {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true; return true;
} }
// What disguises can he use try {
ArrayList<String> allowedDisguises = allowedDisguises(sender); Disguise disguise = parseDisguise(sender, args);
// If he owns at least one disguise
if (allowedDisguises.size() > 0) {
// Get his forbidden disguises (Disguises he can't use) for later use
ArrayList<String> forbiddenDisguises = forbiddenDisguises(sender);
// If he is attempting to do something
if (args.length > 0) {
// If he owns the disguise
if (allowedDisguises.contains(args[0].toLowerCase())) {
Disguise disguise = null;
// Time to start constructing the disguise.
// We will need to check between all 3 kinds of disguises
if (args[0].equalsIgnoreCase("player")) {// If he is doing a player disguise
if (args.length == 1) {
// He needs to give the player name
sender.sendMessage(ChatColor.RED + "Error! You need to give a player name!");
return true;
} else {
// Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
}
} else {
// Grab the disguise type so we know what constructor to use
DisguiseType disguiseType = DisguiseType.valueOf(args[0].toUpperCase());
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
boolean adult = true;
if (args.length > 1) {
// Seems they want to make this a baby disguise!
if (!args[1].equalsIgnoreCase("false") && !args[1].equalsIgnoreCase("true")) {
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " isn't true or false!");
return true;
}
adult = args[1].equalsIgnoreCase("false"); // Adult = !arg
}
disguise = new MobDisguise(disguiseType, adult);
} else if (disguiseType.isMisc()) {
// Its a misc, we are going to use the MiscDisguise constructor.
int miscId = -1;
int miscData = -1;
if (args.length > 1) {
// They have defined more arguements!
// If the first arg is a number
if (isNumeric(args[1])) {
miscId = Integer.parseInt(args[1]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " is not a number!");
return true;
}
// If they also defined a data value
if (args.length > 2) {
if (isNumeric(args[1])) {
miscData = Integer.parseInt(args[2]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " is not a number!");
return true;
}
}
}
// Construct the disguise
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
}
}
// Alright. We've constructed our disguise.
// Time to use it!
DisguiseAPI.disguiseToAll((Player) sender, disguise); DisguiseAPI.disguiseToAll((Player) sender, disguise);
sender.sendMessage(ChatColor.RED + "Successfully disguised as a " + toReadable(disguise.getType().name()) sender.sendMessage(ChatColor.RED + "Now disguised as a " + toReadable(disguise.getType().name()));
+ "!"); } catch (Exception ex) {
} else { if (ex.getMessage() != null) {
// He doesn't. Either tell him its incorrect or he isn't allowed to use it sender.sendMessage(ex.getMessage());
if (forbiddenDisguises.contains(args[0].toLowerCase())) {
// He isn't allowed to use it..
sender.sendMessage(ChatColor.RED + "You are forbidden to use this disguise!");
} else {
sender.sendMessage(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED
+ " doesn't exist!");
} }
} }
} else {
// Just send the disguises information.
sendDisguises(sender, allowedDisguises, forbiddenDisguises);
}
} else
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command!");
return true; return true;
} }
/** /**
* Send the player the information * Send the player the information
*/ */
private void sendDisguises(CommandSender sender, ArrayList<String> allowedDisguises, ArrayList<String> forbiddenDisguises) { protected void sendCommandUsage(CommandSender sender) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguise");
sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise to become the disguise!"); sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise to become the disguise!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseListener; import me.libraryaddict.disguise.DisguiseListener;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
@ -17,8 +17,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
public class DisguiseEntityCommand extends BaseDisguiseCommand {
public class DisguiseEntityCommand implements CommandExecutor {
private DisguiseListener listener; private DisguiseListener listener;
@ -26,142 +25,30 @@ public class DisguiseEntityCommand implements CommandExecutor {
this.listener = listener; this.listener = listener;
} }
private ArrayList<String> allowedDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (sender.hasPermission("libsdisguises.disguiseentity.*")
|| sender.hasPermission("libsdisguises.disguiseentity." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private ArrayList<String> forbiddenDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
if (sender.hasPermission("libsdisguises.disguiseentity.*"))
return names;
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (!sender.hasPermission("libsdisguises.disguiseentity." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception ex) {
return false;
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender.getName().equals("CONSOLE")) { if (sender.getName().equals("CONSOLE")) {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true; return true;
} }
// What disguises can he use try {
ArrayList<String> allowedDisguises = allowedDisguises(sender); Disguise disguise = parseDisguise(sender, args);
// If he owns at least one disguise
if (allowedDisguises.size() > 0) {
// Get his forbidden disguises (Disguises he can't use) for later use
ArrayList<String> forbiddenDisguises = forbiddenDisguises(sender);
// If he is attempting to do something
if (args.length > 0) {
// If he owns the disguise
if (allowedDisguises.contains(args[0].toLowerCase())) {
Disguise disguise = null;
// Time to start constructing the disguise.
// We will need to check between all 3 kinds of disguises
if (args[0].equalsIgnoreCase("player")) { // If he is doing a player disguise
if (args.length == 1) {
// He needs to give the player name
sender.sendMessage(ChatColor.RED + "Error! You need to give a player name!");
return true;
} else {
// Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
}
} else {
// Grab the disguise type so we know what constructor to use
DisguiseType disguiseType = DisguiseType.valueOf(args[0].toUpperCase());
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
boolean adult = true;
if (args.length > 1) {
// Seems they want to make this a baby disguise!
if (!args[1].equalsIgnoreCase("false") && !args[1].equalsIgnoreCase("true")) {
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " isn't true or false!");
return true;
}
adult = args[1].equalsIgnoreCase("false"); // Adult = !arg
}
disguise = new MobDisguise(disguiseType, adult);
} else if (disguiseType.isMisc()) {
// Its a misc, we are going to use the MiscDisguise constructor.
int miscId = -1;
int miscData = -1;
if (args.length > 1) {
// They have defined more arguements!
// If the first arg is a number
if (isNumeric(args[1])) {
miscId = Integer.parseInt(args[1]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " is not a number!");
return true;
}
// If they also defined a data value
if (args.length > 2) {
if (isNumeric(args[1])) {
miscData = Integer.parseInt(args[2]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " is not a number!");
return true;
}
}
}
// Construct the disguise
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
}
}
// Alright. We've constructed our disguise.
// Time to use it!
listener.setSlap(sender.getName(), disguise); listener.setSlap(sender.getName(), disguise);
sender.sendMessage(ChatColor.RED + "Right click a entity in the next 10 seconds to disguise it as a " sender.sendMessage(ChatColor.RED + "Right click a entity in the next 10 seconds to disguise it as a "
+ toReadable(disguise.getType().name()) + "!"); + toReadable(disguise.getType().name()) + "!");
} else { } catch (Exception ex) {
// He doesn't. Either tell him its incorrect or he isn't allowed to use it if (ex.getMessage() != null) {
if (forbiddenDisguises.contains(args[0].toLowerCase())) { sender.sendMessage(ex.getMessage());
// He isn't allowed to use it..
sender.sendMessage(ChatColor.RED + "You are forbidden to use this disguise!");
} else {
sender.sendMessage(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED
+ " doesn't exist!");
} }
} }
} else {
// Just send the disguises information.
sendDisguises(sender, allowedDisguises, forbiddenDisguises);
}
} else
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command!");
return true; return true;
} }
/** /**
* Send the player the information * Send the player the information
*/ */
private void sendDisguises(CommandSender sender, ArrayList<String> allowedDisguises, ArrayList<String> forbiddenDisguises) { protected void sendCommandUsage(CommandSender sender) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguiseentity");
sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise then slap a entity to disguise it!"); sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise then slap a entity to disguise it!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

View File

@ -1,169 +1,64 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class DisguisePlayerCommand extends BaseDisguiseCommand {
public class DisguisePlayerCommand implements CommandExecutor {
private ArrayList<String> allowedDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (sender.hasPermission("libsdisguises.disguiseplayer.*")
|| sender.hasPermission("libsdisguises.disguiseplayer." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private ArrayList<String> forbiddenDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
if (sender.hasPermission("libsdisguises.disguiseplayer.*"))
return names;
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (!sender.hasPermission("libsdisguises.disguiseplayer." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception ex) {
return false;
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// What disguises can he use if (sender.getName().equals("CONSOLE")) {
ArrayList<String> allowedDisguises = allowedDisguises(sender); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
// If he owns at least one disguise return true;
if (allowedDisguises.size() > 0) { }
// Get his forbidden disguises (Disguises he can't use) for later use ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguiseplayer");
ArrayList<String> forbiddenDisguises = forbiddenDisguises(sender); if (allowedDisguises.isEmpty()) {
// If he is attempting to do something sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
if (args.length > 0) {// Better go check that the player exists. return true;
}
if (args.length == 0) {
sendCommandUsage(sender);
return true;
}
if (args.length == 1) {
sender.sendMessage(ChatColor.RED + "You need to supply a disguise as well as the player");
return true;
}
Player player = Bukkit.getPlayer(args[0]); Player player = Bukkit.getPlayer(args[0]);
if (player == null) {// Player doesn't exist. Knew it! if (player == null) {
sender.sendMessage(ChatColor.RED + "Error! Player " + ChatColor.GREEN + args[0] + ChatColor.RED sender.sendMessage(ChatColor.RED + "Cannot find the player '" + args[0] + "'");
+ " doesn't exist!");
return true; return true;
} }
if (args.length > 1) { try {
// If he owns the disguise String[] newArgs = new String[args.length - 1];
if (allowedDisguises.contains(args[1].toLowerCase())) { for (int i = 0; i < newArgs.length; i++) {
// He can use the disguise huh. newArgs[i] = args[i + 1];
Disguise disguise = null;
// Time to start constructing the disguise.
// We will need to check between all 3 kinds of disguises
if (args[1].equalsIgnoreCase("player")) { // If he is doing a player disguise
// Did he give enough args?
if (args.length == 2) {
// He needs to give the player name
sender.sendMessage(ChatColor.RED + "Error! You need to give a player name!");
return true;
} else {
// Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[2]));
} }
} else { Disguise disguise = parseDisguise(sender, newArgs);
// Grab the disguise type so we know what constructor to use
DisguiseType disguiseType = DisguiseType.valueOf(args[1].toUpperCase());
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
boolean adult = true;
if (args.length > 2) {
// Seems they want to make this a baby disguise!
if (!args[2].equalsIgnoreCase("false") && !args[2].equalsIgnoreCase("true")) {
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " isn't true or false!");
return true;
}
adult = args[2].equalsIgnoreCase("false"); // Adult = !arg
}
disguise = new MobDisguise(disguiseType, adult);
} else if (disguiseType.isMisc()) {
// Its a misc, we are going to use the MiscDisguise constructor.
int miscId = -1;
int miscData = -1;
if (args.length > 2) {
// They have defined more arguements!
// If the first arg is a number
if (isNumeric(args[2])) {
miscId = Integer.parseInt(args[2]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " is not a number!");
return true;
}
// If they also defined a data value
if (args.length > 3) {
if (isNumeric(args[3])) {
miscData = Integer.parseInt(args[3]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[3]
+ ChatColor.RED + " is not a number!");
return true;
}
}
}
// Construct the disguise
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
}
}
// Alright. We've constructed our disguise.
// Time to use it!
DisguiseAPI.disguiseToAll(player, disguise); DisguiseAPI.disguiseToAll(player, disguise);
sender.sendMessage(ChatColor.RED + "Successfully disguised " + player.getName() + "!"); sender.sendMessage(ChatColor.RED + "Successfully disguised " + player.getName() + " as a "
} else { + toReadable(disguise.getType().name()) + "!");
// He doesn't. Either tell him its incorrect or he isn't allowed to use it } catch (Exception ex) {
if (forbiddenDisguises.contains(args[0].toLowerCase())) { if (ex.getMessage() != null) {
// He isn't allowed to use it.. sender.sendMessage(ex.getMessage());
sender.sendMessage(ChatColor.RED + "You are forbidden to use this disguise!");
} else {
sender.sendMessage(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " doesn't exist!");
} }
} }
} else
sender.sendMessage(ChatColor.RED + "Error! You need to state a disguise!");
} else {
// Just send the disguises information.
sendDisguises(sender, allowedDisguises, forbiddenDisguises);
}
} else
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command!");
return true; return true;
} }
/** /**
* Send the player the information * Send the player the information
*/ */
private void sendDisguises(CommandSender sender, ArrayList<String> allowedDisguises, ArrayList<String> forbiddenDisguises) { protected void sendCommandUsage(CommandSender sender) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguiseplayer");
sender.sendMessage(ChatColor.DARK_GREEN + "Disguise another player!"); sender.sendMessage(ChatColor.DARK_GREEN + "Disguise another player!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));
@ -174,4 +69,11 @@ public class DisguisePlayerCommand implements CommandExecutor {
sender.sendMessage(ChatColor.DARK_GREEN sender.sendMessage(ChatColor.DARK_GREEN
+ "/disguiseplayer <PlayerName> <Dropped_Item/Falling_Block> <Id> <Durability>"); + "/disguiseplayer <PlayerName> <Dropped_Item/Falling_Block> <Id> <Durability>");
} }
private String toReadable(String name) {
String[] split = name.split("_");
for (int i = 0; i < split.length; i++)
split[i] = split[i].substring(0, 1) + split[i].substring(1).toLowerCase();
return StringUtils.join(split, " ");
}
} }

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
@ -12,6 +12,7 @@ import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -19,171 +20,69 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class DisguiseRadiusCommand extends BaseDisguiseCommand {
public class DisguiseRadiusCommand implements CommandExecutor {
private int maxRadius = 30; private int maxRadius = 30;
public DisguiseRadiusCommand(int maxRadius) { public DisguiseRadiusCommand(int maxRadius) {
this.maxRadius = maxRadius; this.maxRadius = maxRadius;
} }
private ArrayList<String> allowedDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (sender.hasPermission("libsdisguises.disguiseradius.*")
|| sender.hasPermission("libsdisguises.disguiseradius." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private ArrayList<String> forbiddenDisguises(CommandSender sender) {
ArrayList<String> names = new ArrayList<String>();
if (sender.hasPermission("libsdisguises.disguiseradius.*"))
return names;
for (DisguiseType type : DisguiseType.values()) {
String name = type.name().toLowerCase();
if (!sender.hasPermission("libsdisguises.disguiseradius." + name))
names.add(name);
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
return names;
}
private boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception ex) {
return false;
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender.getName().equals("CONSOLE")) { if (sender.getName().equals("CONSOLE")) {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true; return true;
} }
// What disguises can he use ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguiseplayer");
ArrayList<String> allowedDisguises = allowedDisguises(sender); if (allowedDisguises.isEmpty()) {
// If he owns at least one disguise sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
if (allowedDisguises.size() > 0) { return true;
// Get his forbidden disguises (Disguises he can't use) for later use }
ArrayList<String> forbiddenDisguises = forbiddenDisguises(sender); if (args.length == 0) {
// If he is attempting to do something sendCommandUsage(sender);
if (args.length > 0) {// Better go check that its a proper radius return true;
if (!isNumeric(args[0])) {// Radius doesn't exist. Knew it! }
sender.sendMessage(ChatColor.RED + "Error! Radius " + ChatColor.GREEN + args[0] + ChatColor.RED if (args.length == 1) {
+ " isn't a number!"); sender.sendMessage(ChatColor.RED + "You need to supply a disguise as well as the radius");
return true;
}
if (!isNumeric(args[0])) {
sender.sendMessage(ChatColor.RED + args[0] + " is not a number");
return true; return true;
} }
if (args.length > 1) {
// If he owns the disguise
if (allowedDisguises.contains(args[1].toLowerCase())) {
// He can use the disguise huh.
int radius = Integer.parseInt(args[0]); int radius = Integer.parseInt(args[0]);
if (radius > maxRadius) { if (radius > maxRadius) {
sender.sendMessage(ChatColor.RED + "Limited radius to " + maxRadius sender.sendMessage(ChatColor.RED + "Limited radius to " + maxRadius + "! Don't want to make too much lag right?");
+ "! Don't want to make too much lag right?");
radius = maxRadius; radius = maxRadius;
} }
Disguise disguise = null; try {
// Time to start constructing the disguise. String[] newArgs = new String[args.length - 1];
// We will need to check between all 3 kinds of disguises for (int i = 0; i < newArgs.length; i++) {
if (args[1].equalsIgnoreCase("player")) { // If he is doing a player disguise newArgs[i] = args[i + 1];
// Did he give enough args?
if (args.length == 2) {
// He needs to give the player name
sender.sendMessage(ChatColor.RED + "Error! You need to give a player name!");
return true;
} else {
// Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[2]));
} }
} else { Disguise disguise = parseDisguise(sender, newArgs);
// Grab the disguise type so we know what constructor to use
DisguiseType disguiseType = DisguiseType.valueOf(args[1].toUpperCase());
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
boolean adult = true;
if (args.length > 2) {
// Seems they want to make this a baby disguise!
if (!args[2].equalsIgnoreCase("false") && !args[2].equalsIgnoreCase("true")) {
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " isn't true or false!");
return true;
}
adult = args[2].equalsIgnoreCase("false"); // Adult = !arg
}
disguise = new MobDisguise(disguiseType, adult);
} else if (disguiseType.isMisc()) {
// Its a misc, we are going to use the MiscDisguise constructor.
int miscId = -1;
int miscData = -1;
if (args.length > 2) {
// They have defined more arguements!
// If the first arg is a number
if (isNumeric(args[2])) {
miscId = Integer.parseInt(args[2]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[2] + ChatColor.RED
+ " is not a number!");
return true;
}
// If they also defined a data value
if (args.length > 3) {
if (isNumeric(args[3])) {
miscData = Integer.parseInt(args[3]);
} else {
// Send them a error
sender.sendMessage(ChatColor.RED + "Error! " + ChatColor.GREEN + args[3]
+ ChatColor.RED + " is not a number!");
return true;
}
}
}
// Construct the disguise
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
}
}
// Alright. We've constructed our disguise.
// Time to use it! // Time to use it!
int disguisedEntitys = 0; int disguisedEntitys = 0;
for (Entity entity : ((Player) sender).getNearbyEntities(radius, radius, radius)) { for (Entity entity : ((Player) sender).getNearbyEntities(radius, radius, radius)) {
if (entity == sender) if (entity == sender)
continue; continue;
DisguiseAPI.disguiseToAll(entity, disguise); DisguiseAPI.disguiseToAll(entity, disguise.clone());
disguisedEntitys++; disguisedEntitys++;
} }
sender.sendMessage(ChatColor.RED + "Successfully disguised " + disguisedEntitys + " entities!"); sender.sendMessage(ChatColor.RED + "Successfully disguised " + disguisedEntitys + " entities!");
} else { } catch (Exception ex) {
// He doesn't. Either tell him its incorrect or he isn't allowed to use it if (ex.getMessage() != null) {
if (forbiddenDisguises.contains(args[0].toLowerCase())) { sender.sendMessage(ex.getMessage());
// He isn't allowed to use it..
sender.sendMessage(ChatColor.RED + "You are forbidden to use this disguise!");
} else {
sender.sendMessage(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[1] + ChatColor.RED
+ " doesn't exist!");
} }
} }
} else
sender.sendMessage(ChatColor.RED + "Error! You need to state a disguise!");
} else {
// Just send the disguises information.
sendDisguises(sender, allowedDisguises, forbiddenDisguises);
}
} else
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command!");
return true; return true;
} }
/** /**
* Send the player the information * Send the player the information
*/ */
private void sendDisguises(CommandSender sender, ArrayList<String> allowedDisguises, ArrayList<String> forbiddenDisguises) { protected void sendCommandUsage(CommandSender sender) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "disguiseradius");
sender.sendMessage(ChatColor.DARK_GREEN + "Disguise all entities in a radius! Caps at 30 blocks!"); sender.sendMessage(ChatColor.DARK_GREEN + "Disguise all entities in a radius! Caps at 30 blocks!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));