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
commit 676d191c52
7 changed files with 144 additions and 135 deletions

View File

@ -5,6 +5,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -161,13 +163,13 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
} else if (int.class == c) { } else if (int.class == c) {
valueType = "Number"; valueType = "Number";
} else if (float.class == c || double.class == c) { } else if (float.class == c || double.class == c) {
valueType = "Number.0"; valueType = "Decimal";
} else if (AnimalColor.class == c) { } else if (AnimalColor.class == c) {
valueType = "Color"; valueType = "Color";
} else if (ItemStack.class == c) { } else if (ItemStack.class == c) {
valueType = "Item ID with optional :Durability"; valueType = "Item (id:damage)";
} else if (ItemStack[].class == c) { } else if (ItemStack[].class == c) {
valueType = "Item ID,ID,ID,ID with optional :Durability"; valueType = "4 items (id:damage,id,...)";
} else if (c.getSimpleName().equals("Style")) { } else if (c.getSimpleName().equals("Style")) {
valueType = "Horse Style"; valueType = "Horse Style";
} else if (c.getSimpleName().equals("Color")) { } else if (c.getSimpleName().equals("Color")) {
@ -177,10 +179,17 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
} else if (c.getSimpleName().equals("Profession")) { } else if (c.getSimpleName().equals("Profession")) {
valueType = "Villager Profession"; valueType = "Villager Profession";
} else if (PotionEffectType.class == c) { } else if (PotionEffectType.class == c) {
valueType = "Potioneffect"; valueType = "Potion effect";
} }
if (valueType != null) { if (valueType != null) {
methods.add(ChatColor.RED + method.getName() + ChatColor.DARK_RED + "(" + ChatColor.GREEN ChatColor methodColor = ChatColor.YELLOW;
Class<?> declaring = method.getDeclaringClass();
if (declaring == LivingWatcher.class) {
methodColor = ChatColor.AQUA;
} else if (declaring == FlagWatcher.class) {
methodColor = ChatColor.GRAY;
}
methods.add(methodColor + method.getName() + ChatColor.DARK_RED + "(" + ChatColor.GREEN
+ valueType + ChatColor.DARK_RED + ")"); + valueType + ChatColor.DARK_RED + ")");
} }
} }

View File

@ -5,6 +5,9 @@ import java.lang.reflect.Method;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Zombie;
public enum DisguiseType { public enum DisguiseType {
ARROW(60), ARROW(60),
@ -133,6 +136,8 @@ public enum DisguiseType {
ZOMBIE_VILLAGER; ZOMBIE_VILLAGER;
private static Method isVillager, getVariant, getSkeletonType;
static { static {
// We set the entity type in this so that we can safely ignore disguisetypes which don't exist in older versions of MC. // We set the entity type in this so that we can safely ignore disguisetypes which don't exist in older versions of MC.
// Without erroring up everything. // Without erroring up everything.
@ -167,6 +172,19 @@ public enum DisguiseType {
// This version of craftbukkit doesn't have the disguise. // This version of craftbukkit doesn't have the disguise.
} }
} }
try {
isVillager = Zombie.class.getMethod("isVillager");
} catch (Throwable ignored) {
}
try {
getVariant = Horse.class.getMethod("getVariant");
} catch (Throwable ignored) {
// Pre-1.6, but that isn't even supported
}
try {
getSkeletonType = Skeleton.class.getMethod("getSkeletonType");
} catch (Throwable ignored) {
}
} }
public static DisguiseType getType(Entity entity) { public static DisguiseType getType(Entity entity) {
@ -174,18 +192,16 @@ public enum DisguiseType {
switch (disguiseType) { switch (disguiseType) {
case ZOMBIE: case ZOMBIE:
try { try {
Method isVillager = entity.getClass().getMethod("isVillager");
if ((Boolean) isVillager.invoke(entity)) { if ((Boolean) isVillager.invoke(entity)) {
disguiseType = DisguiseType.ZOMBIE_VILLAGER; disguiseType = DisguiseType.ZOMBIE_VILLAGER;
} }
} catch (NoSuchMethodException ex) {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
break; break;
case HORSE: case HORSE:
try { try {
Object variant = entity.getClass().getMethod("getVariant").invoke(entity); Object variant = getVariant.invoke(entity);
disguiseType = DisguiseType.valueOf(((Enum) variant).name()); disguiseType = DisguiseType.valueOf(((Enum) variant).name());
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -193,11 +209,10 @@ public enum DisguiseType {
break; break;
case SKELETON: case SKELETON:
try { try {
Object type = entity.getClass().getMethod("getSkeletonType").invoke(entity); Object type = getSkeletonType.invoke(entity);
if (((Enum) type).name().equals("WITHER")) { if (type == Skeleton.SkeletonType.WITHER) {
disguiseType = DisguiseType.WITHER_SKELETON; disguiseType = DisguiseType.WITHER_SKELETON;
} }
} catch (NoSuchMethodException ex) {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -55,7 +55,7 @@ public class FlagWatcher {
} }
private byte addEntityAnimations(byte originalValue, byte entityValue) { private byte addEntityAnimations(byte originalValue, byte entityValue) {
byte valueByte = (Byte) originalValue; byte valueByte = originalValue;
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
if ((entityValue & 1 << i) != 0 && !modifiedEntityAnimations.contains(i)) { if ((entityValue & 1 << i) != 0 && !modifiedEntityAnimations.contains(i)) {
valueByte = (byte) (valueByte | 1 << i); valueByte = (byte) (valueByte | 1 << i);
@ -66,7 +66,7 @@ public class FlagWatcher {
} }
public FlagWatcher clone(Disguise owningDisguise) { public FlagWatcher clone(Disguise owningDisguise) {
FlagWatcher cloned = null; FlagWatcher cloned;
try { try {
cloned = getClass().getConstructor(Disguise.class).newInstance(getDisguise()); cloned = getClass().getConstructor(Disguise.class).newInstance(getDisguise());
} catch (Exception e) { } catch (Exception e) {

View File

@ -138,7 +138,7 @@ public class HorseWatcher extends AgeableWatcher {
} }
public void setRearing(boolean rear) { public void setRearing(boolean rear) {
setFlag(64, true); setFlag(64, rear);
} }
public void setSaddled(boolean saddled) { public void setSaddled(boolean saddled) {

View File

@ -12,7 +12,7 @@ public class MinecartWatcher extends FlagWatcher {
} }
public ItemStack getBlockInCart() { public ItemStack getBlockInCart() {
int id = (Integer) getValue(20, 0) & '\uffff'; int id = (Integer) getValue(20, 0) & 0xffff;
int data = (Integer) getValue(20, 0) >> 16; int data = (Integer) getValue(20, 0) >> 16;
return new ItemStack(id, 1, (short) data); return new ItemStack(id, 1, (short) data);
} }
@ -32,7 +32,7 @@ public class MinecartWatcher extends FlagWatcher {
public void setBlockInCart(ItemStack item) { public void setBlockInCart(ItemStack item) {
int id = item.getTypeId(); int id = item.getTypeId();
int data = item.getDurability(); int data = item.getDurability();
setValue(20, (int) (id & '\uffff' | data << 16)); setValue(20, (int) (id & 0xffff | data << 16));
sendData(20); sendData(20);
setViewBlockInCart(true); setViewBlockInCart(true);
} }

View File

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

View File

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