2013-05-17 23:05:19 +02:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
2013-07-30 02:44:00 +02:00
|
|
|
import java.lang.reflect.Field;
|
2014-06-01 18:06:38 +02:00
|
|
|
import java.lang.reflect.Method;
|
2013-12-03 19:38:10 +01:00
|
|
|
import java.util.Arrays;
|
2014-05-28 02:09:16 +02:00
|
|
|
import java.util.Collection;
|
2013-12-01 16:37:07 +01:00
|
|
|
import java.util.List;
|
2014-04-02 14:49:48 +02:00
|
|
|
import java.util.UUID;
|
2013-12-01 16:37:07 +01:00
|
|
|
|
2014-06-01 22:56:07 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
|
2013-09-25 16:49:24 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
2014-06-01 18:06:38 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
2013-12-01 16:37:07 +01:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
|
2014-06-01 18:06:38 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
|
2013-11-22 21:10:20 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
|
|
|
import me.libraryaddict.disguise.utilities.ReflectionManager;
|
2013-05-29 00:29:36 +02:00
|
|
|
|
2014-06-01 22:56:07 +02:00
|
|
|
import org.bukkit.DyeColor;
|
2014-06-01 18:06:38 +02:00
|
|
|
import org.bukkit.Material;
|
2013-05-29 00:29:36 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
2014-06-15 03:05:13 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.Horse;
|
2014-06-01 18:06:38 +02:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2013-12-01 14:36:42 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2014-06-01 18:06:38 +02:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
2014-06-15 03:05:13 +02:00
|
|
|
import org.bukkit.inventory.HorseInventory;
|
2014-06-01 18:06:38 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.potion.PotionEffect;
|
2013-05-17 23:05:19 +02:00
|
|
|
|
|
|
|
public class DisguiseAPI {
|
2013-08-01 09:56:27 +02:00
|
|
|
|
2014-06-01 18:06:38 +02:00
|
|
|
public static Disguise constructDisguise(Entity entity) {
|
|
|
|
return constructDisguise(entity, true, true, true);
|
|
|
|
}
|
|
|
|
|
2014-06-15 03:09:14 +02:00
|
|
|
public static Disguise constructDisguise(Entity entity, boolean doEquipment, boolean doSneak, boolean doSprint) {
|
2014-06-01 18:06:38 +02:00
|
|
|
DisguiseType disguiseType = DisguiseType.getType(entity);
|
|
|
|
Disguise disguise;
|
|
|
|
if (disguiseType.isMisc()) {
|
|
|
|
disguise = new MiscDisguise(disguiseType);
|
|
|
|
} else if (disguiseType.isMob()) {
|
|
|
|
disguise = new MobDisguise(disguiseType);
|
|
|
|
} else {
|
|
|
|
disguise = new PlayerDisguise(((Player) entity).getName());
|
|
|
|
}
|
|
|
|
FlagWatcher watcher = disguise.getWatcher();
|
|
|
|
if (entity instanceof LivingEntity) {
|
|
|
|
for (PotionEffect effect : ((LivingEntity) entity).getActivePotionEffects()) {
|
|
|
|
((LivingWatcher) watcher).addPotionEffect(effect.getType());
|
|
|
|
if (effect.getType().getName().equals("INVISIBILITY")) {
|
|
|
|
watcher.setInvisible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entity.getFireTicks() > 0) {
|
|
|
|
watcher.setBurning(true);
|
|
|
|
}
|
2014-06-15 03:09:14 +02:00
|
|
|
if (doEquipment && entity instanceof LivingEntity) {
|
|
|
|
EntityEquipment equip = ((LivingEntity) entity).getEquipment();
|
|
|
|
watcher.setArmor(equip.getArmorContents());
|
|
|
|
watcher.setItemInHand(equip.getItemInHand());
|
2014-06-15 03:05:13 +02:00
|
|
|
if (disguiseType.getEntityType() == EntityType.HORSE) {
|
|
|
|
Horse horse = (Horse) entity;
|
|
|
|
HorseInventory horseInventory = horse.getInventory();
|
|
|
|
ItemStack saddle = horseInventory.getSaddle();
|
|
|
|
if (saddle != null && saddle.getType() == Material.SADDLE) {
|
|
|
|
((HorseWatcher) watcher).setSaddled(true);
|
2014-06-01 18:06:38 +02:00
|
|
|
}
|
2014-06-15 03:05:13 +02:00
|
|
|
((HorseWatcher) watcher).setHorseArmor(horseInventory.getArmor());
|
2014-06-01 18:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Method method : entity.getClass().getMethods()) {
|
|
|
|
if ((doSneak || !method.getName().equals("setSneaking")) && (doSprint || !method.getName().equals("setSprinting"))
|
|
|
|
&& method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
|
|
|
|
Class methodReturn = method.getReturnType();
|
|
|
|
if (methodReturn == float.class || methodReturn == Float.class || methodReturn == Double.class) {
|
|
|
|
methodReturn = double.class;
|
|
|
|
}
|
|
|
|
int firstCapitalMethod = firstCapital(method.getName());
|
|
|
|
if (firstCapitalMethod > 0) {
|
|
|
|
for (Method watcherMethod : watcher.getClass().getMethods()) {
|
|
|
|
if (!watcherMethod.getName().startsWith("get") && watcherMethod.getReturnType() == void.class
|
|
|
|
&& watcherMethod.getParameterTypes().length == 1) {
|
|
|
|
int firstCapitalWatcher = firstCapital(watcherMethod.getName());
|
|
|
|
if (firstCapitalWatcher > 0
|
|
|
|
&& method.getName().substring(firstCapitalMethod)
|
|
|
|
.equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) {
|
|
|
|
Class methodParam = watcherMethod.getParameterTypes()[0];
|
|
|
|
if (methodParam == float.class || methodParam == Float.class || methodParam == Double.class) {
|
|
|
|
methodParam = double.class;
|
2014-06-01 22:56:07 +02:00
|
|
|
} else if (methodParam == AnimalColor.class) {
|
|
|
|
methodParam = DyeColor.class;
|
2014-06-01 18:06:38 +02:00
|
|
|
}
|
|
|
|
if (methodReturn == methodParam) {
|
|
|
|
try {
|
|
|
|
Object value = method.invoke(entity);
|
|
|
|
if (value != null) {
|
|
|
|
Class toCast = watcherMethod.getParameterTypes()[0];
|
|
|
|
if (!(toCast.isInstance(value))) {
|
|
|
|
if (toCast == float.class) {
|
|
|
|
if (value instanceof Float) {
|
|
|
|
value = ((Float) value).floatValue();
|
|
|
|
} else {
|
|
|
|
double d = (Double) value;
|
|
|
|
value = (float) d;
|
|
|
|
}
|
|
|
|
} else if (toCast == double.class) {
|
|
|
|
if (value instanceof Double) {
|
|
|
|
value = ((Double) value).doubleValue();
|
|
|
|
} else {
|
|
|
|
float d = (Float) value;
|
|
|
|
value = (double) d;
|
|
|
|
}
|
2014-06-01 22:56:07 +02:00
|
|
|
} else if (toCast == AnimalColor.class) {
|
|
|
|
value = AnimalColor.valueOf(((DyeColor) value).name());
|
2014-06-01 18:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value instanceof Boolean && !(Boolean) value
|
|
|
|
&& watcherMethod.getDeclaringClass() == FlagWatcher.class) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
watcherMethod.invoke(watcher, value);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:21:10 +01:00
|
|
|
public static void disguiseEntity(Entity entity, Disguise disguise) {
|
2013-08-13 05:19:50 +02:00
|
|
|
// If they are trying to disguise a null entity or use a null disguise
|
|
|
|
// Just return.
|
2013-08-08 19:56:13 +02:00
|
|
|
if (entity == null || disguise == null)
|
2013-06-01 08:35:31 +02:00
|
|
|
return;
|
2013-08-13 11:44:54 +02:00
|
|
|
// The event wasn't cancelled.
|
2013-08-13 05:19:50 +02:00
|
|
|
// If the disguise entity isn't the same as the one we are disguising
|
2013-07-28 00:58:21 +02:00
|
|
|
if (disguise.getEntity() != entity) {
|
2013-08-13 05:19:50 +02:00
|
|
|
// If the disguise entity actually exists
|
2013-08-07 12:51:42 +02:00
|
|
|
if (disguise.getEntity() != null) {
|
2013-08-13 05:19:50 +02:00
|
|
|
// Clone the disguise
|
2013-07-28 00:58:21 +02:00
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
// Set the disguise's entity
|
2013-08-07 12:51:42 +02:00
|
|
|
disguise.setEntity(entity);
|
2013-12-01 16:37:07 +01:00
|
|
|
}
|
2014-06-04 14:30:12 +02:00
|
|
|
disguise.startDisguise();
|
2013-12-01 16:37:07 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 02:09:16 +02:00
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Collection playersToNotSeeDisguise) {
|
2014-01-04 10:16:03 +01:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-03 19:38:10 +01:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
2014-05-28 02:09:16 +02:00
|
|
|
for (Object obj : playersToNotSeeDisguise) {
|
|
|
|
if (obj instanceof String) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer((String) obj);
|
|
|
|
} else if (obj instanceof Player) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
|
|
|
|
}
|
2013-12-01 17:10:38 +01:00
|
|
|
}
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
2014-05-28 02:09:16 +02:00
|
|
|
@Deprecated
|
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, List<String> playersToNotSeeDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) playersToNotSeeDisguise);
|
2014-05-28 02:09:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 06:28:35 +02:00
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Player... playersToNotSeeDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) Arrays.asList(playersToNotSeeDisguise));
|
2014-05-09 06:28:35 +02:00
|
|
|
}
|
|
|
|
|
2013-12-03 19:38:10 +01:00
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, String... playersToNotSeeDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) Arrays.asList(playersToNotSeeDisguise));
|
2013-12-03 19:38:10 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 17:10:38 +01:00
|
|
|
/**
|
|
|
|
* Disguise the next entity to spawn with this disguise. This may not work however if the entity doesn't actually spawn.
|
|
|
|
*/
|
2014-04-23 23:50:26 +02:00
|
|
|
public static int disguiseNextEntity(Disguise disguise) {
|
2013-12-01 17:10:38 +01:00
|
|
|
if (disguise == null)
|
2014-04-23 23:50:26 +02:00
|
|
|
return -1;
|
2013-12-01 17:10:38 +01:00
|
|
|
if (disguise.getEntity() != null || DisguiseUtilities.getDisguises().containsValue(disguise)) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
|
|
|
|
field.setAccessible(true);
|
|
|
|
int id = field.getInt(null);
|
2014-04-02 15:02:58 +02:00
|
|
|
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
|
2014-04-23 23:50:26 +02:00
|
|
|
return id;
|
2013-12-01 17:10:38 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2014-04-23 23:50:26 +02:00
|
|
|
return -1;
|
2013-12-01 17:10:38 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 16:37:07 +01:00
|
|
|
/**
|
|
|
|
* Disguise this entity with this disguise
|
|
|
|
*/
|
|
|
|
public static void disguiseToAll(Entity entity, Disguise disguise) {
|
2014-01-04 10:16:03 +01:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-01 16:37:07 +01:00
|
|
|
// You called the disguiseToAll method foolish mortal! Prepare to have your custom settings wiped!!!
|
2013-12-03 19:38:10 +01:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
2013-12-01 16:37:07 +01:00
|
|
|
for (String observer : ((TargetedDisguise) disguise).getObservers())
|
2013-12-03 19:38:10 +01:00
|
|
|
((TargetedDisguise) disguise).removePlayer(observer);
|
2013-12-01 16:37:07 +01:00
|
|
|
disguiseEntity(entity, disguise);
|
2013-05-29 00:52:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 02:09:16 +02:00
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, Collection playersToViewDisguise) {
|
2014-01-04 10:16:03 +01:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-03 19:38:10 +01:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
|
2014-05-28 02:09:16 +02:00
|
|
|
for (Object obj : playersToViewDisguise) {
|
|
|
|
if (obj instanceof String) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer((String) obj);
|
|
|
|
} else if (obj instanceof Player) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
|
|
|
|
}
|
2013-12-01 17:10:38 +01:00
|
|
|
}
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
2014-05-28 02:09:16 +02:00
|
|
|
@Deprecated
|
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> playersToViewDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseToPlayers(entity, disguise, (Collection) playersToViewDisguise);
|
2014-05-28 02:09:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 06:28:35 +02:00
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, Player... playersToViewDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseToPlayers(entity, disguise, (Collection) Arrays.asList(playersToViewDisguise));
|
2014-05-09 06:28:35 +02:00
|
|
|
}
|
|
|
|
|
2013-12-03 19:38:10 +01:00
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, String... playersToViewDisguise) {
|
2014-06-02 13:28:52 +02:00
|
|
|
disguiseToPlayers(entity, disguise, (Collection) Arrays.asList(playersToViewDisguise));
|
2013-12-03 19:38:10 +01:00
|
|
|
}
|
|
|
|
|
2014-06-01 18:06:38 +02:00
|
|
|
private static int firstCapital(String str) {
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
|
|
|
if (Character.isUpperCase(str.charAt(i))) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-29 00:52:54 +02:00
|
|
|
/**
|
2013-08-13 05:19:50 +02:00
|
|
|
* Get the disguise of a entity
|
2013-05-29 00:52:54 +02:00
|
|
|
*/
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Disguise getDisguise(Entity disguised) {
|
|
|
|
if (disguised == null)
|
2013-08-08 19:56:13 +02:00
|
|
|
return null;
|
2014-04-02 14:54:36 +02:00
|
|
|
return DisguiseUtilities.getMainDisguise(disguised.getUniqueId());
|
2013-05-29 00:52:54 +02:00
|
|
|
}
|
2013-05-29 00:29:36 +02:00
|
|
|
|
2013-12-01 15:31:54 +01:00
|
|
|
/**
|
2013-12-01 17:10:38 +01:00
|
|
|
* Get the disguise of a entity
|
2013-12-01 15:31:54 +01:00
|
|
|
*/
|
2013-12-01 17:10:38 +01:00
|
|
|
public static Disguise getDisguise(Player observer, Entity disguised) {
|
2014-02-07 01:50:40 +01:00
|
|
|
if (disguised == null || observer == null)
|
2013-12-01 15:31:54 +01:00
|
|
|
return null;
|
2014-04-02 15:02:58 +02:00
|
|
|
return DisguiseUtilities.getDisguise(observer, disguised);
|
2013-12-01 15:31:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 14:36:42 +01:00
|
|
|
/**
|
2013-12-01 17:10:38 +01:00
|
|
|
* Get the disguises of a entity
|
2013-12-01 14:36:42 +01:00
|
|
|
*/
|
2013-12-01 17:10:38 +01:00
|
|
|
public static Disguise[] getDisguises(Entity disguised) {
|
2013-12-01 14:36:42 +01:00
|
|
|
if (disguised == null)
|
|
|
|
return null;
|
2014-04-02 14:54:36 +02:00
|
|
|
return DisguiseUtilities.getDisguises(disguised.getUniqueId());
|
2013-12-01 14:36:42 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:19:50 +02:00
|
|
|
/**
|
|
|
|
* Get the ID of a fake disguise for a entityplayer
|
|
|
|
*/
|
2014-04-02 14:49:48 +02:00
|
|
|
public static int getFakeDisguise(UUID entityId) {
|
2013-11-22 20:52:15 +01:00
|
|
|
if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId))
|
|
|
|
return DisguiseUtilities.getSelfDisguisesIds().get(entityId);
|
2013-07-30 02:44:00 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-17 23:05:19 +02:00
|
|
|
/**
|
2013-08-13 11:44:54 +02:00
|
|
|
* Is this entity disguised
|
2013-05-17 23:05:19 +02:00
|
|
|
*/
|
2013-11-22 20:52:15 +01:00
|
|
|
public static boolean isDisguised(Entity disguised) {
|
|
|
|
return getDisguise(disguised) != null;
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 14:36:42 +01:00
|
|
|
/**
|
|
|
|
* Is this entity disguised
|
|
|
|
*/
|
|
|
|
public static boolean isDisguised(Player observer, Entity disguised) {
|
|
|
|
return getDisguise(observer, disguised) != null;
|
|
|
|
}
|
|
|
|
|
2013-12-01 17:10:38 +01:00
|
|
|
public static boolean isDisguiseInUse(Disguise disguise) {
|
2014-06-04 14:30:12 +02:00
|
|
|
return disguise.isDisguiseInUse();
|
2013-12-01 17:10:38 +01:00
|
|
|
}
|
|
|
|
|
2013-05-17 23:05:19 +02:00
|
|
|
/**
|
2013-08-13 11:44:54 +02:00
|
|
|
* Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from
|
|
|
|
* the world.
|
2013-05-17 23:05:19 +02:00
|
|
|
*/
|
2013-05-29 00:29:36 +02:00
|
|
|
public static void undisguiseToAll(Entity entity) {
|
2013-12-01 15:31:54 +01:00
|
|
|
Disguise[] disguises = getDisguises(entity);
|
|
|
|
for (Disguise disguise : disguises) {
|
|
|
|
disguise.removeDisguise();
|
|
|
|
}
|
2013-08-13 11:44:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
private DisguiseAPI() {
|
2013-08-14 22:57:30 +02:00
|
|
|
}
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|