diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java index f905c110..17423e09 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java @@ -6,6 +6,7 @@ import java.util.Map; import java.util.Random; import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher; +import me.libraryaddict.disguise.DisguiseTypes.Watchers.LivingWatcher; import net.minecraft.server.v1_5_R3.Entity; import net.minecraft.server.v1_5_R3.EntityLiving; import net.minecraft.server.v1_5_R3.EntityTypes; @@ -217,6 +218,10 @@ public class Disguise { } } catch (Exception ex) { // There is no watcher for this entity + if (type.isAlive()) + watcher = new LivingWatcher(entityId); + else + watcher = new FlagWatcher(entityId); } } @@ -228,10 +233,6 @@ public class Disguise { return watcher; } - public boolean hasWatcher() { - return watcher != null; - } - public boolean replaceSounds() { return replaceSounds; } diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java index c530aea3..fa201305 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java @@ -14,7 +14,7 @@ import net.minecraft.server.v1_5_R3.ItemStack; import net.minecraft.server.v1_5_R3.Packet40EntityMetadata; import net.minecraft.server.v1_5_R3.WatchableObject; -public abstract class FlagWatcher { +public class FlagWatcher { private static HashMap classTypes = new HashMap(); static { @@ -29,8 +29,6 @@ public abstract class FlagWatcher { protected FlagWatcher(int entityId) { this.entityId = entityId; - setValue(6, (byte) 0); - setValue(5, ""); } public List convert(List list) { @@ -44,27 +42,13 @@ public abstract class FlagWatcher { if (watch.a() == 1) sendAllCustom = true; if (entityValues.containsKey(watch.a())) { + if (entityValues.get(watch.a()) == null) + continue; boolean doD = watch.d(); - watch = new WatchableObject(watch.c(), watch.a(), watch.b()); + Object value = entityValues.get(watch.a()); + watch = new WatchableObject(classTypes.get(value.getClass()), watch.a(), value); if (!doD) watch.a(false); - if (entityValues.get(watch.a()) == null) { - continue; - } else { - Object value = entityValues.get(watch.a()); - if (watch.b().getClass() != value.getClass()) { - watch.a(value); - try { - Field field = WatchableObject.class.getDeclaredField("a"); - field.setAccessible(true); - field.set(watch, classTypes.get(value.getClass())); - } catch (Exception ex) { - ex.printStackTrace(); - } - } else { - watch.a(value); - } - } } newList.add(watch); } @@ -83,17 +67,38 @@ public abstract class FlagWatcher { return newList; } - public void displayName(boolean display) { - if ((Byte) getValue(6) != (display ? 1 : 0)) { - setValue(6, (byte) (display ? 1 : 0)); - sendData(6); - } + private boolean getFlag(int i) { + return ((Byte) getValue(0) & 1 << i) != 0; } protected Object getValue(int no) { return entityValues.get(no); } + public boolean isBurning() { + return getFlag(0); + } + + public boolean isInvisible() { + return getFlag(5); + } + + public boolean isRiding() { + return getFlag(2); + } + + public boolean isRightClicking() { + return getFlag(4); + } + + public boolean isSneaking() { + return getFlag(1); + } + + public boolean isSprinting() { + return getFlag(3); + } + protected void sendData(int data) { Packet40EntityMetadata packet = new Packet40EntityMetadata(); try { @@ -112,8 +117,55 @@ public abstract class FlagWatcher { } } - public void setName(String name) { - setValue(5, name); + public void setBurning(boolean setBurning) { + if (isSneaking() != setBurning) { + setFlag(0, true); + sendData(0); + } + } + + private void setFlag(int i, boolean flag) { + byte currentValue = (Byte) getValue(0); + if (flag) { + setValue(0, Byte.valueOf((byte) (currentValue | 1 << i))); + } else { + setValue(0, Byte.valueOf((byte) (currentValue & ~(1 << i)))); + } + } + + public void setInvisible(boolean setInvis) { + if (isInvisible() != setInvis) { + setFlag(5, true); + sendData(5); + } + } + + public void setRiding(boolean setRiding) { + if (isSprinting() != setRiding) { + setFlag(2, true); + sendData(2); + } + } + + public void setRightClicking(boolean setRightClicking) { + if (isRightClicking() != setRightClicking) { + setFlag(4, true); + sendData(4); + } + } + + public void setSneaking(boolean setSneaking) { + if (isSneaking() != setSneaking) { + setFlag(1, true); + sendData(1); + } + } + + public void setSprinting(boolean setSprinting) { + if (isSprinting() != setSprinting) { + setFlag(3, true); + sendData(3); + } } protected void setValue(int no, Object value) { diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/AgeableWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/AgeableWatcher.java index 969acd20..a9944ab8 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/AgeableWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/AgeableWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public abstract class AgeableWatcher extends FlagWatcher { +public abstract class AgeableWatcher extends LivingWatcher { public AgeableWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BatWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BatWatcher.java index 6f000d90..c2f2ed77 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BatWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BatWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class BatWatcher extends FlagWatcher { +public class BatWatcher extends LivingWatcher { protected BatWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BlazeWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BlazeWatcher.java index 6cbaedf9..1af8a9b0 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BlazeWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/BlazeWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class BlazeWatcher extends FlagWatcher { +public class BlazeWatcher extends LivingWatcher { public BlazeWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/CreeperWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/CreeperWatcher.java index 1f6d80fa..315cace8 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/CreeperWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/CreeperWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class CreeperWatcher extends FlagWatcher { +public class CreeperWatcher extends LivingWatcher { public CreeperWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EnderDragonWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EnderDragonWatcher.java index 0dd3cc70..83b4cb62 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EnderDragonWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EnderDragonWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class EnderDragonWatcher extends FlagWatcher { +public class EnderDragonWatcher extends LivingWatcher { public EnderDragonWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EndermanWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EndermanWatcher.java index b4bb25c6..6906351e 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EndermanWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/EndermanWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class EndermanWatcher extends FlagWatcher { +public class EndermanWatcher extends LivingWatcher { public EndermanWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/GhastWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/GhastWatcher.java index f7512340..3c454338 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/GhastWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/GhastWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class GhastWatcher extends FlagWatcher { +public class GhastWatcher extends LivingWatcher { public GhastWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/LivingWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/LivingWatcher.java new file mode 100644 index 00000000..22b6fc2e --- /dev/null +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/LivingWatcher.java @@ -0,0 +1,98 @@ +package me.libraryaddict.disguise.DisguiseTypes.Watchers; + +import java.util.HashSet; +import java.util.Iterator; + +import net.minecraft.server.v1_5_R3.MobEffect; +import net.minecraft.server.v1_5_R3.PotionBrewer; + +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; + +public class LivingWatcher extends FlagWatcher { + private HashSet potionEffects = new HashSet(); + + public LivingWatcher(int entityId) { + super(entityId); + setValue(5, ""); + setValue(6, (byte) 0); + } + + public void addPotionEffect(PotionEffect potionEffect) { + if (hasPotionEffect(potionEffect.getType())) + removePotionEffect(potionEffect.getType()); + new MobEffect(potionEffect.getType().getId(), potionEffect.getDuration(), potionEffect.getAmplifier()); + sendPotionEffects(); + } + + public int getArrowsSticking() { + return (Byte) getValue(10); + } + + public String getCustomName() { + return (String) getValue(5); + } + + public boolean getPotionParticlesRemoved() { + return (Byte) getValue(9) == 1; + } + + public boolean hasCustomName() { + return getCustomName().length() > 0; + } + + public boolean hasPotionEffect(PotionEffectType type) { + for (MobEffect effect : potionEffects) + if (effect.getEffectId() == type.getId()) + return true; + return false; + } + + public void removePotionEffect(PotionEffectType type) { + Iterator itel = potionEffects.iterator(); + while (itel.hasNext()) { + MobEffect effect = itel.next(); + if (effect.getEffectId() == type.getId()) { + itel.remove(); + sendPotionEffects(); + return; + } + } + } + + public void removePotionParticles(boolean particles) { + if (particles != getPotionParticlesRemoved()) { + setValue(9, (byte) (particles ? 1 : 0)); + sendData(9); + } + } + + private void sendPotionEffects() { + setValue(8, PotionBrewer.a(potionEffects)); + sendData(8); + } + + public void setArrowsSticking(int arrowsNo) { + if (arrowsNo != getArrowsSticking()) { + setValue(10, (byte) arrowsNo); + sendData(10); + } + } + + public void setCustomName(String name) { + if (!getCustomName().equals(name)) { + setValue(5, name); + sendData(5); + } + } + + public void setCustomNameVisible(boolean display) { + if ((Byte) getValue(6) != (display ? 1 : 0)) { + setValue(6, (byte) (display ? 1 : 0)); + sendData(6); + } + } + +} diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PigZombieWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PigZombieWatcher.java index 56cb314f..e4176297 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PigZombieWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PigZombieWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class PigZombieWatcher extends FlagWatcher { +public class PigZombieWatcher extends LivingWatcher { public PigZombieWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PlayerWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PlayerWatcher.java deleted file mode 100644 index 97406e46..00000000 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/PlayerWatcher.java +++ /dev/null @@ -1,14 +0,0 @@ -package me.libraryaddict.disguise.DisguiseTypes.Watchers; - -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class PlayerWatcher extends FlagWatcher { - - protected PlayerWatcher(int entityId) { - super(entityId); - setValue(8, 0); - setValue(9, (byte) 0); - setValue(10, (byte) 0); - } - -} diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SkeletonWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SkeletonWatcher.java index 14a4d19a..e8489890 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SkeletonWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SkeletonWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class SkeletonWatcher extends FlagWatcher { +public class SkeletonWatcher extends LivingWatcher { protected SkeletonWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SlimeWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SlimeWatcher.java index b4e4bafe..fa08e6ed 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SlimeWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/SlimeWatcher.java @@ -2,9 +2,7 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; import java.util.Random; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class SlimeWatcher extends FlagWatcher { +public class SlimeWatcher extends LivingWatcher { public SlimeWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/WitherSkeletonWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/WitherSkeletonWatcher.java index 667594b7..b47d0f47 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/WitherSkeletonWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Watchers/WitherSkeletonWatcher.java @@ -1,8 +1,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers; -import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; - -public class WitherSkeletonWatcher extends FlagWatcher { +public class WitherSkeletonWatcher extends LivingWatcher { protected WitherSkeletonWatcher(int entityId) { super(entityId); diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index cf78d3bd..2089ac2d 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -58,9 +58,7 @@ public class LibsDisguises extends JavaPlugin { if (DisguiseAPI.isDisguised(entity)) { Disguise disguise = DisguiseAPI.getDisguise(entity); if (event.getPacketID() == Packets.Server.ENTITY_METADATA) { - if (!(entity instanceof Player && disguise.getType().isPlayer())) - if (disguise.hasWatcher()) - mods.write(1, disguise.getWatcher().convert((List) mods.read(1))); + mods.write(1, disguise.getWatcher().convert((List) mods.read(1))); } else if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) { if (disguise.getType().isPlayer()) { String name = (String) mods.read(1);