Read desc

Watcher is never null now
You can now change potion particles etc.
Added LivingWatcher
Cleaned up code
This commit is contained in:
Andrew 2013-05-31 03:36:59 +12:00
parent 39ca52ae8d
commit d1ccc40c59
16 changed files with 195 additions and 82 deletions

View File

@ -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;
}

View File

@ -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<Class, Integer> classTypes = new HashMap<Class, Integer>();
static {
@ -29,8 +29,6 @@ public abstract class FlagWatcher {
protected FlagWatcher(int entityId) {
this.entityId = entityId;
setValue(6, (byte) 0);
setValue(5, "");
}
public List<WatchableObject> convert(List<WatchableObject> 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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<MobEffect> potionEffects = new HashSet<MobEffect>();
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<MobEffect> 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);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<WatchableObject>) mods.read(1)));
mods.write(1, disguise.getWatcher().convert((List<WatchableObject>) mods.read(1)));
} else if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) {
if (disguise.getType().isPlayer()) {
String name = (String) mods.read(1);