Merge pull request #30 from riking/stuff

Code cleanup
Fix horse rearing
DisguiseHelp improved
This commit is contained in:
libraryaddict
2014-06-05 10:47:56 +12:00
7 changed files with 144 additions and 135 deletions

View File

@@ -279,130 +279,105 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName)
&& method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) {
methodToUse = method;
methodName = method.getName();
Class<?>[] types = method.getParameterTypes();
if (types.length == 1) {
Class param = types[0];
// Parse to number
if (int.class == param) {
if (isNumeric(valueString)) {
value = (int) Integer.parseInt(valueString);
} else {
throw parseToException("number", valueString, methodName);
}
// Parse to boolean
} else if (float.class == param || double.class == param) {
if (isDouble(valueString)) {
float obj = Float.parseFloat(valueString);
if (param == float.class) {
value = (float) obj;
} else if (param == int.class) {
value = (int) obj;
} else if (param == double.class) {
value = (double) obj;
}
} else {
throw parseToException("number.0", valueString, methodName);
}
// Parse to boolean
} else if (boolean.class == param) {
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
throw parseToException("true/false", valueString, methodName);
value = (boolean) "true".equalsIgnoreCase(valueString);
// Parse to string
} else if (param == String.class) {
value = ChatColor.translateAlternateColorCodes('&', valueString);
// Parse to animal color
} else if (param == AnimalColor.class) {
try {
value = AnimalColor.valueOf(valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("animal color", valueString, methodName);
}
// Parse to itemstack
} else if (param == ItemStack.class) {
try {
value = parseToItemstack(valueString);
} catch (Exception ex) {
throw new Exception(String.format(ex.getMessage(), methodName));
}
// Parse to itemstack array
} else if (param == ItemStack[].class) {
ItemStack[] items = new ItemStack[4];
String[] split = valueString.split(",");
if (split.length == 4) {
for (int a = 0; a < 4; a++) {
try {
ItemStack item = parseToItemstack(split[a]);
items[a] = item;
} catch (Exception ex) {
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
}
}
} else {
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
}
value = items;
// Parse to horse color
} else if (param.getSimpleName().equals("Color")) {
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("a horse color", valueString, methodName);
}
// Parse to horse style
} else if (param.getSimpleName().equals("Style")) {
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("a horse style", valueString, methodName);
}
// Parse to villager profession
} else if (param.getSimpleName().equals("Profession")) {
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("a villager profession", valueString, methodName);
}
// Parse to ocelot type
} else if (param.getSimpleName().equals("Art")) {
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
ex.printStackTrace();
throw parseToException("a painting art", valueString, methodName);
}
// Parse to ocelot type
} else if (param.getSimpleName().equals("Type")) {
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("a ocelot type", valueString, methodName);
}
// Parse to potion effect
} else if (param == PotionEffectType.class) {
try {
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
if (potionType == null && isNumeric(valueString)) {
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
}
if (potionType == null)
throw new Exception();
value = potionType;
} catch (Exception ex) {
throw parseToException("a potioneffect type", valueString, methodName);
}
}
}
break;
}
}
if (methodToUse == null) {
throw new Exception(ChatColor.RED + "Cannot find the option " + methodName);
}
methodName = methodToUse.getName();
Class<?>[] types = methodToUse.getParameterTypes();
if (types.length == 1) {
Class param = types[0];
if (int.class == param) {
// Parse to integer
if (isNumeric(valueString)) {
value = (int) Integer.parseInt(valueString);
} else {
throw parseToException("number", valueString, methodName);
}
} else if (float.class == param || double.class == param) {
// Parse to number
if (isDouble(valueString)) {
float obj = Float.parseFloat(valueString);
if (param == float.class) {
value = (float) obj;
} else if (param == double.class) {
value = (double) obj;
}
} else {
throw parseToException("number.0", valueString, methodName);
}
} else if (boolean.class == param) {
// Parse to boolean
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
throw parseToException("true/false", valueString, methodName);
value = (boolean) "true".equalsIgnoreCase(valueString);
} else if (param == String.class) {
// Parse to string
value = ChatColor.translateAlternateColorCodes('&', valueString);
} else if (param == AnimalColor.class) {
// Parse to animal color
try {
value = AnimalColor.valueOf(valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException("animal color", valueString, methodName);
}
} else if (param == ItemStack.class) {
// Parse to itemstack
try {
value = parseToItemstack(valueString);
} catch (Exception ex) {
throw new Exception(String.format(ex.getMessage(), methodName));
}
} else if (param == ItemStack[].class) {
// Parse to itemstack array
ItemStack[] items = new ItemStack[4];
String[] split = valueString.split(",");
if (split.length == 4) {
for (int a = 0; a < 4; a++) {
try {
items[a] = parseToItemstack(split[a]);
} catch (Exception ex) {
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
}
}
} else {
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
}
value = items;
} else if (param.getSimpleName().equals("Color")) {
// Parse to horse color
value = callValueOf(param, valueString, methodName, "a horse color");
} else if (param.getSimpleName().equals("Style")) {
// Parse to horse style
value = callValueOf(param, valueString, methodName, "a horse style");
} else if (param.getSimpleName().equals("Profession")) {
// Parse to villager profession
value = callValueOf(param, valueString, methodName, "a villager profession");
} else if (param.getSimpleName().equals("Art")) {
// Parse to art type
value = callValueOf(param, valueString, methodName, "a painting art");
} else if (param.getSimpleName().equals("Type")) {
// Parse to ocelot type
value = callValueOf(param, valueString, methodName, "a ocelot type");
} else if (param == PotionEffectType.class) {
// Parse to potion effect
try {
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
if (potionType == null && isNumeric(valueString)) {
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
}
if (potionType == null)
throw new Exception();
value = potionType;
} catch (Exception ex) {
throw parseToException("a potioneffect type", valueString, methodName);
}
}
}
if (!usedOptions.contains(methodName.toLowerCase())) {
usedOptions.add(methodName.toLowerCase());
}
@@ -413,6 +388,16 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
return disguise;
}
private Object callValueOf(Class<?> param, String valueString, String methodName, String description) throws Exception {
Object value;
try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) {
throw parseToException(description, valueString, methodName);
}
return value;
}
private void doCheck(HashSet<HashSet<String>> optionPermissions, ArrayList<String> usedOptions) throws Exception {
if (!optionPermissions.isEmpty()) {
boolean hasPermission = true;

View File

@@ -749,7 +749,7 @@ public class PacketsManager {
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, observer.getEntityId());
List<WrappedWatchableObject> watchableList = new ArrayList<WrappedWatchableObject>();
byte b = (byte) (0 | 1 << 5);
byte b = (byte) 1 << 5;
if (observer.isSprinting())
b = (byte) (b | 1 << 3);
watchableList.add(new WrappedWatchableObject(0, b));