Added methods and changed internals for flag watchers.

This commit is contained in:
Andrew 2013-07-09 16:08:47 +12:00
parent d181addde9
commit a2e789aac6
5 changed files with 147 additions and 76 deletions

View File

@ -7,12 +7,6 @@ public class HorseWatcher extends AgeableWatcher {
public HorseWatcher(int entityId) {
super(entityId);
setValue(16, 0);
// Horse types (19) are
// Horse
// Donkey
// Mule
// Zombie
// Skeleton
setValue(19, (byte) 0);
setValue(20, new Random().nextInt(7));
setValue(21, "");
@ -27,14 +21,86 @@ public class HorseWatcher extends AgeableWatcher {
return (int) (Byte) getValue(19);
}
public boolean hasChest() {
return isTrue(8);
}
public boolean isBredable() {
return isTrue(16);
}
public boolean isGrazing() {
return isTrue(32);
}
public boolean isMouthOpen() {
return isTrue(128);
}
public boolean isRearing() {
return isTrue(64);
}
public boolean isSaddled() {
return isTrue(4);
}
public boolean isTamed() {
return isTrue(2);
}
private boolean isTrue(int i) {
return ((Integer) getValue(16) & i) != 0;
}
public void setCanBred(boolean bred) {
setFlag(16, bred);
}
public void setCarryingChest(boolean chest) {
setFlag(8, true);
}
public void setColoring(int color) {
setValue(20, color);
sendData(20);
}
private void setFlag(int i, boolean flag) {
if (isTrue(i) != flag) {
int j = (Integer) getValue(16);
if (flag) {
setValue(16, j | i);
} else {
setValue(16, j & ~i);
}
sendData(16);
}
}
public void setGrazing(boolean grazing) {
setFlag(32, grazing);
}
public void setHorseType(int type) {
setValue(19, (byte) type);
sendData(19);
}
public void setMouthOpen(boolean mouthOpen) {
setFlag(128, mouthOpen);
}
public void setRearing(boolean rear) {
setFlag(64, true);
}
public void setSaddled(boolean saddled) {
setFlag(4, saddled);
}
public void setTamed(boolean tamed) {
setFlag(2, tamed);
}
}

View File

@ -4,9 +4,6 @@ import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Ocelot.Type;
public class OcelotWatcher extends AgeableWatcher {
private boolean isSitting;
private boolean isTamed;
private Type type = Ocelot.Type.WILD_OCELOT;
public OcelotWatcher(int entityId) {
super(entityId);
@ -20,7 +17,23 @@ public class OcelotWatcher extends AgeableWatcher {
}
public Type getType() {
return type;
return Ocelot.Type.getType((Byte) getValue(18));
}
private boolean isTrue(int no) {
return ((Byte) getValue(16) & no) != 0;
}
private void setFlag(int no, boolean flag) {
if (isTrue(no) != flag) {
byte b0 = (Byte) getValue(16);
if (flag) {
setValue(16, (byte) (b0 | (no)));
} else {
setValue(16, (byte) (b0 & -(no + 1)));
}
sendData(16);
}
}
public void setOwner(String newOwner) {
@ -28,29 +41,17 @@ public class OcelotWatcher extends AgeableWatcher {
}
public void setSitting(boolean sitting) {
if (isSitting != sitting) {
isSitting = sitting;
updateStatus();
}
setFlag(1, sitting);
}
public void setTamed(boolean tamed) {
if (isTamed != tamed) {
isTamed = tamed;
updateStatus();
}
setFlag(4, tamed);
}
public void setType(Type newType) {
if (type != newType) {
type = newType;
setValue(18, (byte) type.getId());
if (getType() != newType) {
setValue(18, (byte) newType.getId());
sendData(18);
}
}
private void updateStatus() {
setValue(16, (byte) ((isSitting ? 1 : 0) + (isTamed ? 4 : 0)));
sendData(16);
}
}

View File

@ -3,31 +3,37 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers;
import me.libraryaddict.disguise.DisguiseTypes.AnimalColor;
public class SheepWatcher extends AgeableWatcher {
private AnimalColor color = AnimalColor.WHITE;
private boolean isSheared;
public SheepWatcher(int entityId) {
super(entityId);
setValue(16, (byte) 0);
}
public AnimalColor getColor() {
return AnimalColor.values()[(Byte) getValue(16) & 15];
}
public boolean isSheared() {
return isSheared;
return ((Byte) getValue(16) & 16) != 0;
}
public void setColor(AnimalColor newColor) {
if (color != newColor) {
setValue(16, (byte) (newColor.getId() + (isSheared ? 16 : 0)));
public void setColor(AnimalColor color) {
if (getColor() != color) {
byte b0 = (Byte) getValue(16);
setValue(16, (byte) (b0 & 240 | color.getId() & 15));
sendData(16);
}
}
public void setSheared(boolean sheared) {
if (sheared != isSheared) {
isSheared = sheared;
setValue(16, (byte) (color.getId() + (isSheared ? 16 : 0)));
public void setSheared(boolean flag) {
if (isSheared() != flag) {
byte b0 = (Byte) getValue(16);
if (flag) {
setValue(16, (byte) (b0 | 16));
} else {
setValue(16, (byte) (b0 & -17));
}
sendData(16);
}
}
}

View File

@ -5,18 +5,19 @@ import java.util.Random;
import org.bukkit.entity.Villager.Profession;
public class VillagerWatcher extends AgeableWatcher {
private Profession profession;
public VillagerWatcher(int entityId) {
super(entityId);
profession = Profession.values()[new Random().nextInt(Profession.values().length)];
setValue(16, profession.getId());
setValue(16, Profession.values()[new Random().nextInt(Profession.values().length)].getId());
}
public Profession getProfession() {
return Profession.values()[(Integer) getValue(16)];
}
public void setProfession(Profession newProfession) {
if (profession != newProfession) {
profession = newProfession;
setValue(16, profession.getId());
if (getProfession() != newProfession) {
setValue(16, newProfession.getId());
sendData(16);
}
}

View File

@ -3,10 +3,6 @@ package me.libraryaddict.disguise.DisguiseTypes.Watchers;
import me.libraryaddict.disguise.DisguiseTypes.AnimalColor;
public class WolfWatcher extends AgeableWatcher {
private AnimalColor collarColor = AnimalColor.RED;
private boolean isAgressive;
private boolean isSitting;
private boolean isTamed;
public WolfWatcher(int entityId) {
super(entityId);
@ -14,11 +10,11 @@ public class WolfWatcher extends AgeableWatcher {
setValue(17, "");
setValue(18, 8F);
setValue(19, (byte) 0);
setValue(20, (byte) collarColor.getId());
setValue(20, (byte) 14);
}
public AnimalColor getCollarColor() {
return collarColor;
return AnimalColor.values()[(Byte) getValue(20)];
}
public float getHealth() {
@ -29,55 +25,56 @@ public class WolfWatcher extends AgeableWatcher {
return (String) getValue(17);
}
public boolean isAgressive() {
return isAgressive;
public boolean isAngry() {
return isTrue(2);
}
public boolean isSitting() {
return isSitting;
return isTrue(1);
}
public boolean isTamed() {
return isTamed;
return isTrue(4);
}
public void setAgressive(boolean aggressive) {
if (isAgressive != aggressive) {
isAgressive = aggressive;
updateStatus();
private boolean isTrue(int no) {
return ((Byte) getValue(16) & no) != 0;
}
public void setAngry(boolean angry) {
setFlag(2, angry);
}
public void setCollarColor(AnimalColor newColor) {
if (newColor != collarColor) {
collarColor = newColor;
if (newColor != getCollarColor()) {
setValue(20, (byte) newColor.getId());
sendData(20);
}
}
private void setFlag(int no, boolean flag) {
if (isTrue(no) != flag) {
byte b0 = (Byte) getValue(16);
if (flag) {
setValue(16, (byte) (b0 | (no)));
} else {
setValue(16, (byte) (b0 & -(no + 1)));
}
sendData(16);
}
}
public void setHealth(float newHealth) {
setValue(18, newHealth);
sendData(18);
}
public void setSitting(boolean sitting) {
if (isSitting != sitting) {
isSitting = sitting;
updateStatus();
}
setFlag(1, sitting);
}
public void setTamed(boolean tamed) {
if (isTamed != tamed) {
isTamed = tamed;
updateStatus();
}
}
private void updateStatus() {
setValue(16, (byte) ((isTamed ? 4 : 0) + (isSitting ? 1 : 0) + (isAgressive ? 2 : 0)));
sendData(16);
setFlag(4, tamed);
}
}