More stuff. Self disguises no work :(

This commit is contained in:
libraryaddict 2013-12-02 04:37:07 +13:00
parent 4dc6853945
commit efa75e677f
12 changed files with 214 additions and 111 deletions

View File

@ -1,9 +1,11 @@
package me.libraryaddict.disguise;
import java.lang.reflect.Field;
import java.util.List;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.events.DisguiseEvent;
import me.libraryaddict.disguise.events.UndisguiseEvent;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
@ -26,6 +28,18 @@ public class DisguiseAPI {
return hearSelfDisguise;
}
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> players) {
((TargetedDisguise) disguise).setTargetType(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
((TargetedDisguise) disguise).getObservers().addAll(players);
disguiseEntity(entity, disguise);
}
public static void disguiseToEveryoneButThese(Entity entity, Disguise disguise, List<String> players) {
((TargetedDisguise) disguise).setTargetType(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
((TargetedDisguise) disguise).getObservers().addAll(players);
disguiseEntity(entity, disguise);
}
/**
* Disguise the next entity to spawn with this disguise. This may not work however if the entity doesn't actually spawn.
*/
@ -39,23 +53,17 @@ public class DisguiseAPI {
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
field.setAccessible(true);
int id = field.getInt(null);
DisguiseUtilities.addDisguise(id, (TargettedDisguise) disguise);
DisguiseUtilities.addDisguise(id, (TargetedDisguise) disguise);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Disguise this entity with this disguise
*/
public static void disguiseToAll(Entity entity, Disguise disguise) {
private static void disguiseEntity(Entity entity, Disguise disguise) {
// If they are trying to disguise a null entity or use a null disguise
// Just return.
if (entity == null || disguise == null)
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
DisguiseEvent event = new DisguiseEvent(entity, disguise);
Bukkit.getPluginManager().callEvent(event);
@ -73,20 +81,24 @@ public class DisguiseAPI {
}
// Set the disguise's entity
disguise.setEntity(entity);
} // If there was a old disguise
Disguise[] oldDisguises = getDisguises(entity);
}
// Stick the disguise in the disguises bin
DisguiseUtilities.addDisguise(entity.getEntityId(), (TargettedDisguise) disguise);
DisguiseUtilities.addDisguise(entity.getEntityId(), (TargetedDisguise) disguise);
// Resend the disguised entity's packet
DisguiseUtilities.refreshTrackers(entity);
DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise);
// If he is a player, then self disguise himself
DisguiseUtilities.setupFakeDisguise(disguise);
// Discard the disguise
for (Disguise oldDisguise : oldDisguises) {
oldDisguise.removeDisguise();
// Make everyone see this disguise. Remove any old disguises.
DisguiseUtilities.getDisguises().remove(entity.getEntityId());
}
}
/**
* Disguise this entity with this disguise
*/
public static void disguiseToAll(Entity entity, Disguise disguise) {
// You called the disguiseToAll method foolish mortal! Prepare to have your custom settings wiped!!!
((TargetedDisguise) disguise).setTargetType(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
for (String observer : ((TargetedDisguise) disguise).getObservers())
((TargetedDisguise) disguise).unsetViewDisguise(observer);
disguiseEntity(entity, disguise);
}
/**

View File

@ -175,6 +175,7 @@ public abstract class Disguise {
final boolean sendMovementPacket = movement;
final double vectorY = fallSpeed;
final boolean alwaysSendVelocity = alwaysSend;
final TargetedDisguise disguise=(TargetedDisguise) this;
// A scheduler to clean up any unused disguises.
velocityRunnable = new BukkitRunnable() {
private int i = 0;
@ -189,7 +190,7 @@ public abstract class Disguise {
i++;
if (i % 40 == 0) {
i = 0;
DisguiseUtilities.refreshTrackers(getEntity());
DisguiseUtilities.refreshTrackers(disguise);
if (getEntity() instanceof Player && isSelfDisguiseVisible()) {
DisguiseUtilities.sendSelfDisguise((Player) getEntity());
}
@ -298,7 +299,7 @@ public abstract class Disguise {
.get(entityTrackerEntry);
for (Object p : trackedPlayers) {
Player player = (Player) ReflectionManager.getBukkitEntity(p);
if (((TargettedDisguise) this).canSee(player)) {
if (((TargetedDisguise) this).canSee(player)) {
players.add(player);
}
}
@ -371,19 +372,19 @@ public abstract class Disguise {
velocityRunnable.cancel();
} catch (Exception ex) {
}
HashMap<Integer, HashSet<TargettedDisguise>> disguises = DisguiseUtilities.getDisguises();
HashMap<Integer, HashSet<TargetedDisguise>> disguises = DisguiseUtilities.getDisguises();
// If this disguise has a entity set
if (getEntity() != null) {
// If the entity is valid
if (getEntity().isValid()) {
// If this disguise is active
// Remove the disguise from the current disguises.
if (DisguiseUtilities.removeDisguise((TargettedDisguise) this)) {
if (DisguiseUtilities.removeDisguise((TargetedDisguise) this)) {
if (getEntity() instanceof Player) {
DisguiseUtilities.removeSelfDisguise((Player) getEntity());
}
// Better refresh the entity to undisguise it
DisguiseUtilities.refreshTrackers(getEntity());
DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
}
}
} else {

View File

@ -42,14 +42,14 @@ public class FlagWatcher {
* This is the entity values I need to add else it could crash them..
*/
private HashMap<Integer, Object> backupEntityValues = new HashMap<Integer, Object>();
private Disguise disguise;
private TargetedDisguise disguise;
private HashMap<Integer, Object> entityValues = new HashMap<Integer, Object>();
private boolean hasDied;
private org.bukkit.inventory.ItemStack[] items = new org.bukkit.inventory.ItemStack[5];
private HashSet<Integer> modifiedEntityAnimations = new HashSet<Integer>();
public FlagWatcher(Disguise disguise) {
this.disguise = disguise;
this.disguise = (TargetedDisguise) disguise;
}
@Override
@ -158,7 +158,7 @@ public class FlagWatcher {
return armor;
}
protected Disguise getDisguise() {
protected TargetedDisguise getDisguise() {
return disguise;
}

View File

@ -11,7 +11,7 @@ import org.bukkit.Art;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
public class MiscDisguise extends TargettedDisguise {
public class MiscDisguise extends TargetedDisguise {
private int data = -1;
private int id = -1;

View File

@ -5,7 +5,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import org.bukkit.entity.EntityType;
public class MobDisguise extends TargettedDisguise {
public class MobDisguise extends TargetedDisguise {
private boolean isAdult;

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.disguisetypes;
public class PlayerDisguise extends TargettedDisguise {
public class PlayerDisguise extends TargetedDisguise {
private String playerName;
public PlayerDisguise(String name) {

View File

@ -0,0 +1,60 @@
package me.libraryaddict.disguise.disguisetypes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.entity.Player;
public abstract class TargetedDisguise extends Disguise {
public enum TargetType {
HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS, SHOW_TO_EVERYONE_BUT_THESE_PLAYERS;
}
public void setTargetType(TargetType newTargetType) {
if (DisguiseUtilities.isDisguiseInUse(this)) {
throw new RuntimeException("Cannot set the disguise target after the entity has been disguised");
}
targetType = newTargetType;
}
private List<String> disguiseViewers = new ArrayList<String>();
private TargetType targetType = TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS;
public boolean canSee(Player player) {
return canSee(player.getName());
}
public List<String> getObservers() {
return Collections.unmodifiableList(disguiseViewers);
}
public boolean canSee(String playername) {
boolean contains = disguiseViewers.contains(playername);
if (targetType == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
return !contains;
}
return contains;
}
public void setViewDisguise(String playername) {
if (!disguiseViewers.contains(playername)) {
disguiseViewers.add(playername);
DisguiseUtilities.checkConflicts(this, playername);
}
}
public void unsetViewDisguise(String playername) {
if (disguiseViewers.contains(playername)) {
disguiseViewers.remove(playername);
DisguiseUtilities.checkConflicts(this, playername);
}
}
public TargetType getTargetType() {
return targetType;
}
}

View File

@ -1,51 +0,0 @@
package me.libraryaddict.disguise.disguisetypes;
import java.util.HashSet;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.entity.Player;
public abstract class TargettedDisguise extends Disguise {
public enum TargetType {
SHOW_TO_THESE, HIDE_FROM_THESE;
}
public void setTargetType(TargetType newTargetType) {
if (DisguiseUtilities.isDisguiseInUse(this)) {
throw new RuntimeException("Cannot set the disguise target after the entity has been disguised");
}
targetType = newTargetType;
}
private HashSet<String> disguiseViewers = new HashSet<String>();
private TargetType targetType = TargetType.HIDE_FROM_THESE;
public boolean canSee(Player player) {
return canSee(player.getName());
}
public HashSet<String> getObservers() {
return disguiseViewers;
}
public boolean canSee(String playername) {
boolean contains = disguiseViewers.contains(playername);
if (targetType == TargetType.HIDE_FROM_THESE) {
return !contains;
}
return contains;
}
public void setViewDisguise(Player player) {
setViewDisguise(player.getName());
}
public void setViewDisguise(String playername) {
disguiseViewers.add(playername);
}
public TargetType getTargetType() {
return targetType;
}
}

View File

@ -27,7 +27,7 @@ public class FallingBlockWatcher extends FlagWatcher {
public void setBlock(ItemStack block) {
this.block = block;
if (getDisguise().getEntity() != null && getDisguise().getWatcher() == this) {
DisguiseUtilities.refreshTrackers(getDisguise().getEntity());
DisguiseUtilities.refreshTrackers(getDisguise());
}
}
}

View File

@ -26,7 +26,7 @@ public class PaintingWatcher extends FlagWatcher {
public void setArt(Art newPainting) {
this.painting = newPainting;
if (getDisguise().getEntity() != null && getDisguise().getWatcher() == this) {
DisguiseUtilities.refreshTrackers(getDisguise().getEntity());
DisguiseUtilities.refreshTrackers(getDisguise());
}
}

View File

@ -25,7 +25,7 @@ public class SplashPotionWatcher extends FlagWatcher {
public void setPotionId(int newPotionId) {
this.potionId = newPotionId;
if (getDisguise().getEntity() != null && getDisguise().getWatcher() == this) {
DisguiseUtilities.refreshTrackers(getDisguise().getEntity());
DisguiseUtilities.refreshTrackers(getDisguise());
}
}

View File

@ -6,10 +6,11 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargettedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -28,13 +29,13 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisguiseUtilities {
// Store the entity IDs instead of entitys because then I can disguise entitys even before they exist
private static HashMap<Integer, HashSet<TargettedDisguise>> targetedDisguises = new HashMap<Integer, HashSet<TargettedDisguise>>();
private static HashMap<Integer, HashSet<TargetedDisguise>> targetedDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>();
private static LibsDisguises libsDisguises;
// A internal storage of fake entity ID's I can use.
// Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID
private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>();
public static HashMap<Integer, HashSet<TargettedDisguise>> getDisguises() {
public static HashMap<Integer, HashSet<TargetedDisguise>> getDisguises() {
return targetedDisguises;
}
@ -47,7 +48,7 @@ public class DisguiseUtilities {
}
public static boolean isDisguiseInUse(Disguise disguise) {
if (getDisguises().containsKey(disguise.getEntity().getEntityId())
if (disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getEntityId())
&& getDisguises().get(disguise.getEntity().getEntityId()).contains(disguise)) {
return true;
}
@ -58,13 +59,13 @@ public class DisguiseUtilities {
* @param Resends
* the entity to all the watching players, which is where the magic begins
*/
public static void refreshTrackers(Entity entity) {
public static void refreshTrackers(TargetedDisguise disguise) {
try {
Object world = ReflectionManager.getWorld(entity.getWorld());
Object world = ReflectionManager.getWorld(disguise.getEntity().getWorld());
Object tracker = world.getClass().getField("tracker").get(world);
Object trackedEntities = tracker.getClass().getField("trackedEntities").get(tracker);
Object entityTrackerEntry = trackedEntities.getClass().getMethod("get", int.class)
.invoke(trackedEntities, entity.getEntityId());
.invoke(trackedEntities, disguise.getEntity().getEntityId());
if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry);
@ -72,11 +73,15 @@ public class DisguiseUtilities {
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object player : cloned) {
if (entity instanceof Player && !((Player) ReflectionManager.getBukkitEntity(player)).canSee((Player) entity))
continue;
clear.invoke(entityTrackerEntry, player);
updatePlayer.invoke(entityTrackerEntry, player);
for (Object p : cloned) {
Player player = (Player) ReflectionManager.getBukkitEntity(p);
// if (entity instanceof Player && !((Player) ReflectionManager.getBukkitEntity(player)).canSee((Player)
// entity))
// continue;
if (disguise.canSee(player.getName())) {
clear.invoke(entityTrackerEntry, p);
updatePlayer.invoke(entityTrackerEntry, p);
}
}
}
} catch (Exception ex) {
@ -232,14 +237,89 @@ public class DisguiseUtilities {
}
}
public static void addDisguise(int entityId, TargettedDisguise disguise) {
public static void addDisguise(int entityId, TargetedDisguise disguise) {
// TODO Make sure that the disguised entity doesn't have the player looking at other girls
// ^ Done?
if (!getDisguises().containsKey(entityId)) {
getDisguises().put(entityId, new HashSet<TargettedDisguise>());
getDisguises().put(entityId, new HashSet<TargetedDisguise>());
}
getDisguises().get(entityId).add(disguise);
checkConflicts(disguise, null);
}
public static boolean removeDisguise(TargettedDisguise disguise) {
/**
* If name isn't null. Make sure that the name doesn't see any other disguise. Else if name is null. Make sure that the
* observers in the disguise don't see any other disguise.
*/
public static void checkConflicts(TargetedDisguise disguise, String name) {
if (DisguiseAPI.isDisguiseInUse(disguise)) {
Iterator<TargetedDisguise> disguiseItel = getDisguises().get(disguise.getEntity().getEntityId()).iterator();
while (disguiseItel.hasNext()) {
TargetedDisguise d = disguiseItel.next();
if (d != disguise) {
if (d.getTargetType() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
// If player is a observer in the loop
if (disguise.getTargetType() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
// If player is a observer in the disguise
// Remove them from the loop
if (name != null) {
d.unsetViewDisguise(name);
} else {
for (String playername : disguise.getObservers()) {
d.unsetViewDisguise(playername);
}
}
} else if (disguise.getTargetType() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
// If player is not a observer in the loop
if (name != null) {
if (!disguise.getObservers().contains(name)) {
d.unsetViewDisguise(name);
}
} else {
for (String playername : d.getObservers()) {
if (!disguise.getObservers().contains(playername)) {
d.unsetViewDisguise(playername);
}
}
}
}
} else if (d.getTargetType() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
// Here you add it to the loop if they see the disguise
if (disguise.getTargetType() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
// Everyone who is in the disguise needs to be added to the loop
if (name != null) {
d.setViewDisguise(name);
} else {
for (String playername : disguise.getObservers()) {
d.setViewDisguise(playername);
}
}
} else if (disguise.getTargetType() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
// This here is a paradox.
// If fed a name. I can do this.
// But the rest of the time.. Its going to conflict.
System.out.print("Cannot set more than one " + TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS
+ " on a entity. Removed the old disguise.");
disguiseItel.remove();
/* if (name != null) {
if (!disguise.getObservers().contains(name)) {
d.setViewDisguise(name);
}
} else {
for (String playername : d.getObservers()) {
if (!disguise.getObservers().contains(playername)) {
d.setViewDisguise(playername);
}
}
}*/
}
}
}
}
}
}
public static boolean removeDisguise(TargetedDisguise disguise) {
int entityId = disguise.getEntity().getEntityId();
if (getDisguises().containsKey(entityId) && getDisguises().get(entityId).remove(disguise)) {
if (getDisguises().get(entityId).isEmpty()) {
@ -251,11 +331,12 @@ public class DisguiseUtilities {
}
@Deprecated
public static TargettedDisguise getDisguise(int entityId) {
TargettedDisguise toReturn = null;
public static TargetedDisguise getDisguise(int entityId) {
TargetedDisguise toReturn = null;
if (getDisguises().containsKey(entityId)) {
for (TargettedDisguise disguise : getDisguises().get(entityId)) {
if (disguise.getTargetType() == TargetType.HIDE_FROM_THESE && disguise.getObservers().isEmpty()) {
for (TargetedDisguise disguise : getDisguises().get(entityId)) {
if (disguise.getTargetType() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS
&& disguise.getObservers().isEmpty()) {
return disguise;
}
if (toReturn == null) {
@ -266,16 +347,16 @@ public class DisguiseUtilities {
return toReturn;
}
public static TargettedDisguise[] getDisguises(int entityId) {
public static TargetedDisguise[] getDisguises(int entityId) {
if (getDisguises().containsKey(entityId)) {
return getDisguises().get(entityId).toArray(new TargettedDisguise[getDisguises().get(entityId).size()]);
return getDisguises().get(entityId).toArray(new TargetedDisguise[getDisguises().get(entityId).size()]);
}
return new TargettedDisguise[0];
return new TargetedDisguise[0];
}
public static TargettedDisguise getDisguise(Player observer, int entityId) {
public static TargetedDisguise getDisguise(Player observer, int entityId) {
if (getDisguises().containsKey(entityId)) {
for (TargettedDisguise disguise : getDisguises().get(entityId)) {
for (TargetedDisguise disguise : getDisguises().get(entityId)) {
if (disguise.canSee(observer)) {
return disguise;
}
@ -291,7 +372,7 @@ public class DisguiseUtilities {
Entity e = disguise.getEntity();
// If the disguises entity is null, or the disguised entity isn't a player return
if (e == null || !(e instanceof Player) || !getDisguises().containsKey(e.getEntityId())
|| !getDisguises().get(e).contains(disguise))
|| !getDisguises().get(e.getEntityId()).contains(disguise))
return;
Player player = (Player) e;
// Remove the old disguise, else we have weird disguises around the place