Clean up code. Add in code. Forgot what I did.

This commit is contained in:
libraryaddict 2013-12-02 03:31:54 +13:00
parent a969811c64
commit 4dc6853945
3 changed files with 38 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise; import me.libraryaddict.disguise.disguisetypes.TargettedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise.TargetType;
import me.libraryaddict.disguise.events.DisguiseEvent; import me.libraryaddict.disguise.events.DisguiseEvent;
import me.libraryaddict.disguise.events.UndisguiseEvent; import me.libraryaddict.disguise.events.UndisguiseEvent;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
@ -48,11 +49,13 @@ public class DisguiseAPI {
* Disguise this entity with this disguise * Disguise this entity with this disguise
*/ */
public static void disguiseToAll(Entity entity, Disguise disguise) { public static void disguiseToAll(Entity entity, Disguise disguise) {
// TODO Make everyone see this disguise. Remove any old disguises.
// If they are trying to disguise a null entity or use a null disguise // If they are trying to disguise a null entity or use a null disguise
// Just return. // Just return.
if (entity == null || disguise == null) if (entity == null || disguise == null)
return; return;
// You called the disguiseToAll method foolish mortal! Prepare to have your custom settings wiped!!!
((TargettedDisguise) disguise).setTargetType(TargetType.HIDE_FROM_THESE);
((TargettedDisguise) disguise).getObservers().clear();
// Fire a disguise event // Fire a disguise event
DisguiseEvent event = new DisguiseEvent(entity, disguise); DisguiseEvent event = new DisguiseEvent(entity, disguise);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
@ -71,7 +74,7 @@ public class DisguiseAPI {
// Set the disguise's entity // Set the disguise's entity
disguise.setEntity(entity); disguise.setEntity(entity);
} // If there was a old disguise } // If there was a old disguise
Disguise oldDisguise = getDisguise(entity); Disguise[] oldDisguises = getDisguises(entity);
// Stick the disguise in the disguises bin // Stick the disguise in the disguises bin
DisguiseUtilities.addDisguise(entity.getEntityId(), (TargettedDisguise) disguise); DisguiseUtilities.addDisguise(entity.getEntityId(), (TargettedDisguise) disguise);
// Resend the disguised entity's packet // Resend the disguised entity's packet
@ -79,8 +82,11 @@ public class DisguiseAPI {
// If he is a player, then self disguise himself // If he is a player, then self disguise himself
DisguiseUtilities.setupFakeDisguise(disguise); DisguiseUtilities.setupFakeDisguise(disguise);
// Discard the disguise // Discard the disguise
if (oldDisguise != null) for (Disguise oldDisguise : oldDisguises) {
oldDisguise.removeDisguise(); oldDisguise.removeDisguise();
// Make everyone see this disguise. Remove any old disguises.
DisguiseUtilities.getDisguises().remove(entity.getEntityId());
}
} }
/** /**
@ -93,6 +99,15 @@ public class DisguiseAPI {
return DisguiseUtilities.getDisguise(disguised.getEntityId()); return DisguiseUtilities.getDisguise(disguised.getEntityId());
} }
/**
* Get the disguises of a entity
*/
public static Disguise[] getDisguises(Entity disguised) {
if (disguised == null)
return null;
return DisguiseUtilities.getDisguises(disguised.getEntityId());
}
/** /**
* Get the disguise of a entity * Get the disguise of a entity
*/ */
@ -237,16 +252,15 @@ public class DisguiseAPI {
* the world. * the world.
*/ */
public static void undisguiseToAll(Entity entity) { public static void undisguiseToAll(Entity entity) {
// TODO Make all of these disguises be removed Disguise[] disguises = getDisguises(entity);
Disguise disguise = getDisguise(entity); for (Disguise disguise : disguises) {
if (disguise == null)
return;
UndisguiseEvent event = new UndisguiseEvent(entity, disguise); UndisguiseEvent event = new UndisguiseEvent(entity, disguise);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; continue;
disguise.removeDisguise(); disguise.removeDisguise();
} }
}
private DisguiseAPI() { private DisguiseAPI() {
} }

View File

@ -129,7 +129,8 @@ public class FlagWatcher {
} }
} }
// Here we check for if there is a health packet that says they died. // Here we check for if there is a health packet that says they died.
if (disguise.isSelfDisguiseVisible() && getDisguise().getEntity() != null && getDisguise().getEntity() instanceof Player) { if (getDisguise().isSelfDisguiseVisible() && getDisguise().getEntity() != null
&& getDisguise().getEntity() instanceof Player) {
for (WrappedWatchableObject watch : newList) { for (WrappedWatchableObject watch : newList) {
// Its a health packet // Its a health packet
if (watch.getIndex() == 6) { if (watch.getIndex() == 6) {
@ -217,7 +218,7 @@ public class FlagWatcher {
} }
protected void sendData(int data) { protected void sendData(int data) {
if (disguise.getWatcher() == null || DisguiseAPI.getDisguise(disguise.getEntity()) != disguise) if (getDisguise().getWatcher() == null || !DisguiseAPI.isDisguiseInUse(getDisguise()))
return; return;
if (!entityValues.containsKey(data) || entityValues.get(data) == null) if (!entityValues.containsKey(data) || entityValues.get(data) == null)
return; return;
@ -283,7 +284,7 @@ public class FlagWatcher {
// Itemstack which is null means that its not replacing the disguises itemstack. // Itemstack which is null means that its not replacing the disguises itemstack.
if (itemStack == null) { if (itemStack == null) {
// Find the item to replace it with // Find the item to replace it with
if (disguise.getEntity() instanceof LivingEntity) { if (getDisguise().getEntity() instanceof LivingEntity) {
EntityEquipment enquipment = ((LivingEntity) getDisguise().getEntity()).getEquipment(); EntityEquipment enquipment = ((LivingEntity) getDisguise().getEntity()).getEquipment();
if (slot == 0) { if (slot == 0) {
itemStack = enquipment.getItemInHand(); itemStack = enquipment.getItemInHand();
@ -299,7 +300,7 @@ public class FlagWatcher {
if (itemStack != null && itemStack.getTypeId() != 0) if (itemStack != null && itemStack.getTypeId() != 0)
itemToSend = ReflectionManager.getNmsItem(itemStack); itemToSend = ReflectionManager.getNmsItem(itemStack);
items[slot] = itemStack; items[slot] = itemStack;
if (DisguiseAPI.getDisguise(disguise.getEntity()) != disguise) if (!DisguiseAPI.isDisguiseInUse(getDisguise()))
return; return;
slot++; slot++;
if (slot > 4) if (slot > 4)

View File

@ -266,6 +266,13 @@ public class DisguiseUtilities {
return toReturn; return toReturn;
} }
public static TargettedDisguise[] getDisguises(int entityId) {
if (getDisguises().containsKey(entityId)) {
return getDisguises().get(entityId).toArray(new TargettedDisguise[getDisguises().get(entityId).size()]);
}
return new TargettedDisguise[0];
}
public static TargettedDisguise getDisguise(Player observer, int entityId) { public static TargettedDisguise getDisguise(Player observer, int entityId) {
if (getDisguises().containsKey(entityId)) { if (getDisguises().containsKey(entityId)) {
for (TargettedDisguise disguise : getDisguises().get(entityId)) { for (TargettedDisguise disguise : getDisguises().get(entityId)) {