From 15c2ee630562086eab007776d09de7023931448d Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Jul 2013 11:01:09 +1200 Subject: [PATCH] Move calls elsewhere and rename some methods and remove old code. --- .../disguise/DisguiseTypes/Disguise.java | 104 +++++------------- 1 file changed, 29 insertions(+), 75 deletions(-) diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java index 2b59e0c0..22a785a2 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java @@ -1,7 +1,5 @@ package me.libraryaddict.disguise.DisguiseTypes; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; @@ -10,9 +8,7 @@ import java.util.Random; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher; -import me.libraryaddict.disguise.DisguiseTypes.Watchers.LivingWatcher; import me.libraryaddict.disguise.DisguiseTypes.Watchers.ZombieWatcher; -import net.minecraft.server.v1_6_R2.DataWatcher; import net.minecraft.server.v1_6_R2.Entity; import net.minecraft.server.v1_6_R2.EntityAgeable; import net.minecraft.server.v1_6_R2.EntityInsentient; @@ -22,7 +18,6 @@ import net.minecraft.server.v1_6_R2.EntityTrackerEntry; import net.minecraft.server.v1_6_R2.ItemStack; import net.minecraft.server.v1_6_R2.MathHelper; import net.minecraft.server.v1_6_R2.EnumArt; -import net.minecraft.server.v1_6_R2.WatchableObject; import net.minecraft.server.v1_6_R2.WorldServer; import org.bukkit.Location; @@ -57,16 +52,16 @@ public class Disguise { return disguise; } - public PacketContainer[] constructPacket(org.bukkit.entity.Entity e) { + public PacketContainer[] constructPacket(org.bukkit.entity.Entity disguisedEntity) { PacketContainer[] spawnPackets = new PacketContainer[2]; ProtocolManager manager = ProtocolLibrary.getProtocolManager(); - Entity entity = ((CraftEntity) e).getHandle(); - Location loc = e.getLocation(); + Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle(); + Location loc = disguisedEntity.getLocation(); if (getType() == DisguiseType.EXPERIENCE_ORB) { spawnPackets[0] = manager.createPacket(Packets.Server.ADD_EXP_ORB); StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, (int) Math.floor(loc.getX() * 32)); mods.write(2, (int) Math.floor(loc.getY() * 32) + 2); mods.write(3, (int) Math.floor(loc.getZ() * 32)); @@ -75,7 +70,7 @@ public class Disguise { } else if (getType() == DisguiseType.PAINTING) { spawnPackets[0] = manager.createPacket(Packets.Server.ENTITY_PAINTING); StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, loc.getBlockX()); mods.write(2, loc.getBlockY()); mods.write(3, loc.getBlockZ()); @@ -88,7 +83,7 @@ public class Disguise { // Make the teleport packet to make it visible.. spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_TELEPORT); mods = spawnPackets[1].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, (int) Math.floor(loc.getX() * 32D)); mods.write(2, (int) Math.floor(loc.getY() * 32D)); mods.write(3, (int) Math.floor(loc.getZ() * 32D)); @@ -101,25 +96,25 @@ public class Disguise { spawnPackets[0] = manager.createPacket(Packets.Server.NAMED_ENTITY_SPAWN); StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, ((PlayerDisguise) this).getName()); mods.write(2, (int) Math.floor(loc.getX() * 32)); mods.write(3, (int) Math.floor(loc.getY() * 32)); mods.write(4, (int) Math.floor(loc.getZ() * 32)); - mods.write(5, (byte) (int) (entity.yaw * 256F / 360F)); - mods.write(6, (byte) (int) (entity.pitch * 256F / 360F)); + mods.write(5, (byte) (int) (nmsEntity.yaw * 256F / 360F)); + mods.write(6, (byte) (int) (nmsEntity.pitch * 256F / 360F)); ItemStack item = null; - if (e instanceof Player && ((Player) e).getItemInHand() != null) - item = CraftItemStack.asNMSCopy(((Player) e).getItemInHand()); + if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) + item = CraftItemStack.asNMSCopy(((Player) disguisedEntity).getItemInHand()); mods.write(7, (item == null ? 0 : item.id)); - mods.write(8, entity.getDataWatcher()); + mods.write(8, nmsEntity.getDataWatcher()); } else if (getType().isMob()) { - Vector vec = e.getVelocity(); + Vector vec = disguisedEntity.getVelocity(); spawnPackets[0] = manager.createPacket(Packets.Server.MOB_SPAWN); StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, (int) getType().getEntityType().getTypeId()); double d1 = 3.9D; double d2 = vec.getX(); @@ -137,32 +132,20 @@ public class Disguise { d3 = d1; if (d4 > d1) d4 = d1; - mods.write(2, entity.at.a(loc.getX())); + mods.write(2, nmsEntity.at.a(loc.getX())); mods.write(3, (int) Math.floor(loc.getY() * 32D)); - mods.write(4, entity.at.a(loc.getZ())); + mods.write(4, nmsEntity.at.a(loc.getZ())); mods.write(5, (int) (d2 * 8000.0D)); mods.write(6, (int) (d3 * 8000.0D)); mods.write(7, (int) (d4 * 8000.0D)); - byte yawValue = (byte) (int) (entity.yaw * 256.0F / 360.0F); + byte yawValue = (byte) (int) (nmsEntity.yaw * 256.0F / 360.0F); if (getType() == DisguiseType.ENDER_DRAGON) yawValue -= 128; mods.write(8, yawValue); - mods.write(9, (byte) (int) (entity.pitch * 256.0F / 360.0F)); - if (entity instanceof EntityLiving) - mods.write(10, (byte) (int) (((EntityLiving) entity).aA * 256.0F / 360.0F)); - DataWatcher newWatcher = new DataWatcher(); - try { - Field map = newWatcher.getClass().getDeclaredField("c"); - map.setAccessible(true); - HashMap c = (HashMap) map.get(newWatcher); - List list = entity.getDataWatcher().c(); - int i = 0; - for (Object obj : watcher.convert(list)) - c.put(i++, obj); - } catch (Exception ex) { - ex.printStackTrace(); - } - mods.write(11, newWatcher); + mods.write(9, (byte) (int) (nmsEntity.pitch * 256.0F / 360.0F)); + if (nmsEntity instanceof EntityLiving) + mods.write(10, (byte) (int) (((EntityLiving) nmsEntity).aA * 256.0F / 360.0F)); + mods.write(11, nmsEntity.getDataWatcher()); // Theres a list sometimes written with this. But no problems have appeared! // Probably just the metadata to be sent. But the next meta packet after fixes that anyways. @@ -182,12 +165,12 @@ public class Disguise { data = (int) Math.abs(loc.getYaw() % 4); spawnPackets[0] = manager.createPacket(Packets.Server.VEHICLE_SPAWN); StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, (int) Math.floor(loc.getX() * 32D)); mods.write(2, (int) Math.floor(loc.getY() * 32D)); mods.write(3, (int) Math.floor(loc.getZ() * 32D)); if (data > 0) { - Vector vec = e.getVelocity(); + Vector vec = disguisedEntity.getVelocity(); double d1 = vec.getX(); double d2 = vec.getY(); double d3 = vec.getZ(); @@ -208,8 +191,8 @@ public class Disguise { mods.write(5, (int) (d2 * 8000.0D)); mods.write(6, (int) (d3 * 8000.0D)); } - mods.write(7, (int) MathHelper.floor(entity.pitch * 256.0F / 360.0F)); - mods.write(8, (int) MathHelper.floor(entity.yaw * 256.0F / 360.0F) - 64); + mods.write(7, (int) MathHelper.floor(nmsEntity.pitch * 256.0F / 360.0F)); + mods.write(8, (int) MathHelper.floor(nmsEntity.yaw * 256.0F / 360.0F) - 64); mods.write(9, id); mods.write(10, data); @@ -218,7 +201,7 @@ public class Disguise { // Make a packet to turn his head! spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_HEAD_ROTATION); StructureModifier mods = spawnPackets[1].getModifier(); - mods.write(0, e.getEntityId()); + mods.write(0, disguisedEntity.getEntityId()); mods.write(1, (byte) (int) Math.floor(loc.getYaw() * 256.0F / 360.0F)); } return spawnPackets; @@ -228,32 +211,11 @@ public class Disguise { if (this.entity != null) throw new RuntimeException("This disguise is already in use! Try .clone()"); this.entity = entity; - FlagWatcher tempWatcher; + FlagWatcher tempWatcher = null; try { - String name; - if (getType() == DisguiseType.MINECART_FURNACE || getType() == DisguiseType.MINECART_HOPPER - || getType() == DisguiseType.MINECART_MOB_SPAWNER || getType() == DisguiseType.MINECART_TNT - || getType() == DisguiseType.MINECART_CHEST) { - name = "Minecart"; - } else if (getType() == DisguiseType.DONKEY || getType() == DisguiseType.MULE - || getType() == DisguiseType.UNDEAD_HORSE || getType() == DisguiseType.SKELETON_HORSE) { - name = "Horse"; - } else if (getType() == DisguiseType.ZOMBIE_VILLAGER) { - name = "Zombie"; - } else if (getType() == DisguiseType.WITHER_SKELETON) { - name = "Skeleton"; - } else { - name = toReadable(getType().name()); - } - Class watcherClass = Class.forName("me.libraryaddict.disguise.DisguiseTypes.Watchers." + name + "Watcher"); - Constructor contructor = watcherClass.getDeclaredConstructor(Disguise.class); - tempWatcher = (FlagWatcher) contructor.newInstance(this); + tempWatcher = (FlagWatcher) getType().getWatcherClass().getConstructor(Disguise.class).newInstance(this); } catch (Exception ex) { - // There is no watcher for this entity, or a error was thrown. - if (entity.getType().isAlive()) - tempWatcher = new LivingWatcher(this); - else - tempWatcher = new FlagWatcher(this); + ex.printStackTrace(); } if (this instanceof MobDisguise && !((MobDisguise) this).isAdult()) { if (tempWatcher instanceof AgeableWatcher) @@ -523,12 +485,4 @@ public class Disguise { public void setWatcher(FlagWatcher newWatcher) { watcher = newWatcher; } - - private String toReadable(String string) { - String[] strings = string.split("_"); - string = ""; - for (String s : strings) - string += s.substring(0, 1) + s.substring(1).toLowerCase(); - return string; - } } \ No newline at end of file