From 9e4ad0ba8e44fa33cec106bf14695ffed5b05bb8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 23 May 2014 16:40:22 +1200 Subject: [PATCH] Added tameable watcher --- .../libraryaddict/disguise/LibsDisguises.java | 6 +- .../disguisetypes/watchers/OcelotWatcher.java | 41 +----------- .../watchers/TameableWatcher.java | 62 +++++++++++++++++++ .../disguisetypes/watchers/WolfWatcher.java | 52 +--------------- 4 files changed, 69 insertions(+), 92 deletions(-) create mode 100644 src/me/libraryaddict/disguise/disguisetypes/watchers/TameableWatcher.java diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 8e284796..d3297033 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -14,6 +14,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.MinecartWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.SlimeWatcher; +import me.libraryaddict.disguise.disguisetypes.watchers.TameableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.utilities.DisguiseSound; import me.libraryaddict.disguise.utilities.DisguiseUtilities; @@ -30,6 +31,7 @@ import org.bukkit.entity.Ageable; import org.bukkit.entity.Damageable; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Tameable; import org.bukkit.entity.Zombie; import org.bukkit.plugin.java.JavaPlugin; @@ -171,7 +173,9 @@ public class LibsDisguises extends JavaPlugin { } catch (ClassNotFoundException ex) { // There is no explicit watcher for this entity. Class entityClass = disguiseType.getEntityType().getEntityClass(); - if (Ageable.class.isAssignableFrom(entityClass)) { + if (Tameable.class.isAssignableFrom(entityClass)) { + watcherClass = TameableWatcher.class; + } else if (Ageable.class.isAssignableFrom(entityClass)) { watcherClass = AgeableWatcher.class; } else if (LivingEntity.class.isAssignableFrom(entityClass)) { watcherClass = LivingWatcher.class; diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/OcelotWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/OcelotWatcher.java index e8e27243..27a0085b 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/OcelotWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/OcelotWatcher.java @@ -5,55 +5,16 @@ import me.libraryaddict.disguise.disguisetypes.Disguise; import org.bukkit.entity.Ocelot; import org.bukkit.entity.Ocelot.Type; -public class OcelotWatcher extends AgeableWatcher { +public class OcelotWatcher extends TameableWatcher { public OcelotWatcher(Disguise disguise) { super(disguise); } - public String getOwner() { - return (String) getValue(17, ""); - } - public Type getType() { return Ocelot.Type.getType((Byte) getValue(18, (byte) 0)); } - public boolean isSitting() { - return isTrue(1); - } - - public boolean isTamed() { - return isTrue(4); - } - - private boolean isTrue(int no) { - return ((Byte) getValue(16, (byte) 0) & no) != 0; - } - - private void setFlag(int no, boolean flag) { - byte b0 = (Byte) getValue(16, (byte) 0); - if (flag) { - setValue(16, (byte) (b0 | no)); - } else { - setValue(16, (byte) (b0 & -(no + 1))); - } - sendData(16); - } - - public void setOwner(String newOwner) { - setValue(17, newOwner); - sendData(17); - } - - public void setSitting(boolean sitting) { - setFlag(1, sitting); - } - - public void setTamed(boolean tamed) { - setFlag(4, tamed); - } - public void setType(Type newType) { setValue(18, (byte) newType.getId()); sendData(18); diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/TameableWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/TameableWatcher.java new file mode 100644 index 00000000..2f1b7ee4 --- /dev/null +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/TameableWatcher.java @@ -0,0 +1,62 @@ +package me.libraryaddict.disguise.disguisetypes.watchers; + +import me.libraryaddict.disguise.disguisetypes.Disguise; + +public class TameableWatcher extends AgeableWatcher { + + public TameableWatcher(Disguise disguise) { + super(disguise); + } + + @Override + public float getHealth() { + return (Float) getValue(18, 8F); + } + + public String getOwner() { + return (String) getValue(17, ""); + } + + public boolean isSitting() { + return isTrue(1); + } + + public boolean isTamed() { + return isTrue(4); + } + + protected boolean isTrue(int no) { + return ((Byte) getValue(16, (byte) 0) & no) != 0; + } + + protected void setFlag(int no, boolean flag) { + byte b0 = (Byte) getValue(16, (byte) 0); + if (flag) { + setValue(16, (byte) (b0 | no)); + } else { + setValue(16, (byte) (b0 & -(no + 1))); + } + sendData(16); + } + + @Override + public void setHealth(float newHealth) { + setValue(18, newHealth); + sendData(18); + super.setHealth(newHealth); + } + + public void setOwner(String owner) { + setValue(17, owner); + sendData(17); + } + + public void setSitting(boolean sitting) { + setFlag(1, sitting); + } + + public void setTamed(boolean tamed) { + setFlag(4, tamed); + } + +} diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/WolfWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/WolfWatcher.java index 7ee038f4..a579e51d 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/WolfWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/WolfWatcher.java @@ -3,7 +3,7 @@ package me.libraryaddict.disguise.disguisetypes.watchers; import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.Disguise; -public class WolfWatcher extends AgeableWatcher { +public class WolfWatcher extends TameableWatcher { public WolfWatcher(Disguise disguise) { super(disguise); @@ -13,31 +13,10 @@ public class WolfWatcher extends AgeableWatcher { return AnimalColor.values()[(Byte) getValue(20, (byte) 14)]; } - @Override - public float getHealth() { - return (Float) getValue(18, 8F); - } - - public String getOwner() { - return (String) getValue(17, ""); - } - public boolean isAngry() { return isTrue(2); } - public boolean isSitting() { - return isTrue(1); - } - - public boolean isTamed() { - return isTrue(4); - } - - private boolean isTrue(int no) { - return ((Byte) getValue(16, (byte) 0) & no) != 0; - } - public void setAngry(boolean angry) { setFlag(2, angry); } @@ -52,33 +31,4 @@ public class WolfWatcher extends AgeableWatcher { } } - private void setFlag(int no, boolean flag) { - byte b0 = (Byte) getValue(16, (byte) 0); - if (flag) { - setValue(16, (byte) (b0 | no)); - } else { - setValue(16, (byte) (b0 & -(no + 1))); - } - sendData(16); - } - - @Override - public void setHealth(float newHealth) { - setValue(18, newHealth); - sendData(18); - } - - public void setOwner(String owner) { - setValue(17, owner); - sendData(17); - } - - public void setSitting(boolean sitting) { - setFlag(1, sitting); - } - - public void setTamed(boolean tamed) { - setFlag(4, tamed); - } - }