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 me.libraryaddict.disguise.disguisetypes.AnimalColor;
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 org.apache.commons.lang.StringUtils;
@ -161,13 +163,13 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
} else if (int.class == c) {
valueType = "Number";
} else if (float.class == c || double.class == c) {
valueType = "Number.0";
valueType = "Decimal";
} else if (AnimalColor.class == c) {
valueType = "Color";
} else if (ItemStack.class == c) {
valueType = "Item ID with optional :Durability";
valueType = "Item (id:damage)";
} 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")) {
valueType = "Horse Style";
} else if (c.getSimpleName().equals("Color")) {
@ -177,10 +179,17 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
} else if (c.getSimpleName().equals("Profession")) {
valueType = "Villager Profession";
} else if (PotionEffectType.class == c) {
valueType = "Potioneffect";
valueType = "Potion effect";
}
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 + ")");
}
}

View File

@ -5,6 +5,9 @@ import java.lang.reflect.Method;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Zombie;
public enum DisguiseType {
ARROW(60),
@ -133,6 +136,8 @@ public enum DisguiseType {
ZOMBIE_VILLAGER;
private static Method isVillager, getVariant, getSkeletonType;
static {
// 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.
@ -167,6 +172,19 @@ public enum DisguiseType {
// 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) {
@ -174,18 +192,16 @@ public enum DisguiseType {
switch (disguiseType) {
case ZOMBIE:
try {
Method isVillager = entity.getClass().getMethod("isVillager");
if ((Boolean) isVillager.invoke(entity)) {
disguiseType = DisguiseType.ZOMBIE_VILLAGER;
}
} catch (NoSuchMethodException ex) {
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case HORSE:
try {
Object variant = entity.getClass().getMethod("getVariant").invoke(entity);
Object variant = getVariant.invoke(entity);
disguiseType = DisguiseType.valueOf(((Enum) variant).name());
} catch (Exception ex) {
ex.printStackTrace();
@ -193,11 +209,10 @@ public enum DisguiseType {
break;
case SKELETON:
try {
Object type = entity.getClass().getMethod("getSkeletonType").invoke(entity);
if (((Enum) type).name().equals("WITHER")) {
Object type = getSkeletonType.invoke(entity);
if (type == Skeleton.SkeletonType.WITHER) {
disguiseType = DisguiseType.WITHER_SKELETON;
}
} catch (NoSuchMethodException ex) {
} catch (Exception ex) {
ex.printStackTrace();
}

View File

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

View File

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

View File

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

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));