2017-01-20 19:58:51 +01:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.*;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
|
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2019-03-05 05:46:47 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
|
2019-01-03 03:13:03 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2017-01-20 19:58:51 +01:00
|
|
|
import org.bukkit.DyeColor;
|
|
|
|
import org.bukkit.Material;
|
2017-12-05 20:08:24 +01:00
|
|
|
import org.bukkit.entity.*;
|
2017-01-20 19:58:51 +01:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
|
|
|
import org.bukkit.inventory.HorseInventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.potion.PotionEffect;
|
|
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-01-20 19:58:51 +01:00
|
|
|
|
|
|
|
public class DisguiseAPI {
|
2017-12-05 20:08:24 +01:00
|
|
|
private static int selfDisguiseId = ReflectionManager.getNewEntityId(true);
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
public static String getCustomDisguise(String disguiseName) {
|
|
|
|
Map.Entry<DisguisePerm, String> entry = DisguiseConfig.getCustomDisguise(disguiseName);
|
2017-07-20 09:23:43 +02:00
|
|
|
|
|
|
|
if (entry == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:58:51 +01:00
|
|
|
public static Disguise constructDisguise(Entity entity) {
|
|
|
|
return constructDisguise(entity, true, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Disguise constructDisguise(Entity entity, boolean doEquipment, boolean doSneak, boolean doSprint) {
|
|
|
|
DisguiseType disguiseType = DisguiseType.getType(entity);
|
|
|
|
Disguise disguise;
|
|
|
|
|
|
|
|
if (disguiseType.isMisc()) {
|
|
|
|
disguise = new MiscDisguise(disguiseType);
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (disguiseType.isMob()) {
|
2017-01-20 19:58:51 +01:00
|
|
|
disguise = new MobDisguise(disguiseType);
|
2017-06-21 21:20:12 +02:00
|
|
|
} else {
|
2017-01-20 19:58:51 +01:00
|
|
|
disguise = new PlayerDisguise(entity.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
FlagWatcher watcher = disguise.getWatcher();
|
|
|
|
|
|
|
|
if (entity instanceof LivingEntity) {
|
|
|
|
for (PotionEffect effect : ((LivingEntity) entity).getActivePotionEffects()) {
|
|
|
|
((LivingWatcher) watcher).addPotionEffect(effect.getType());
|
|
|
|
|
|
|
|
if (effect.getType() == PotionEffectType.INVISIBILITY) {
|
|
|
|
watcher.setInvisible(true);
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (effect.getType() == PotionEffectType.GLOWING) {
|
2017-01-20 19:58:51 +01:00
|
|
|
watcher.setGlowing(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity.getFireTicks() > 0) {
|
|
|
|
watcher.setBurning(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doEquipment && entity instanceof LivingEntity) {
|
|
|
|
EntityEquipment equip = ((LivingEntity) entity).getEquipment();
|
|
|
|
|
|
|
|
watcher.setArmor(equip.getArmorContents());
|
|
|
|
watcher.setItemInMainHand(equip.getItemInMainHand());
|
|
|
|
|
|
|
|
if (disguiseType.getEntityType() == EntityType.HORSE) {
|
|
|
|
Horse horse = (Horse) entity;
|
|
|
|
HorseInventory horseInventory = horse.getInventory();
|
|
|
|
ItemStack saddle = horseInventory.getSaddle();
|
|
|
|
|
|
|
|
if (saddle != null && saddle.getType() == Material.SADDLE) {
|
|
|
|
((AbstractHorseWatcher) watcher).setSaddled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (watcher instanceof HorseWatcher)
|
|
|
|
((HorseWatcher) watcher).setHorseArmor(horseInventory.getArmor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Method method : entity.getClass().getMethods()) {
|
2017-12-05 20:08:24 +01:00
|
|
|
if ((doSneak || !method.getName().equals("setSneaking")) &&
|
|
|
|
(doSprint || !method.getName().equals("setSprinting")) && method.getParameterTypes().length == 0 &&
|
|
|
|
method.getReturnType() != void.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
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()) {
|
2017-12-05 20:08:24 +01:00
|
|
|
if (!watcherMethod.getName().startsWith("get") && watcherMethod.getReturnType() == void.class &&
|
|
|
|
watcherMethod.getParameterTypes().length == 1) {
|
2017-01-20 19:58:51 +01:00
|
|
|
int firstCapitalWatcher = firstCapital(watcherMethod.getName());
|
|
|
|
|
|
|
|
if (firstCapitalWatcher > 0 && method.getName().substring(firstCapitalMethod)
|
|
|
|
.equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) {
|
|
|
|
Class methodParam = watcherMethod.getParameterTypes()[0];
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
if (methodParam == float.class || methodParam == Float.class ||
|
|
|
|
methodParam == Double.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
methodParam = double.class;
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (methodParam == AnimalColor.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
methodParam = DyeColor.class;
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
double d = (Double) value;
|
|
|
|
value = (float) d;
|
|
|
|
}
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (toCast == double.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
if (!(value instanceof Double)) {
|
|
|
|
float d = (Float) value;
|
|
|
|
value = (double) d;
|
|
|
|
}
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (toCast == AnimalColor.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
value = AnimalColor.valueOf(((DyeColor) value).name());
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 20:08:24 +01:00
|
|
|
if (value instanceof Boolean && !(Boolean) value &&
|
|
|
|
watcherMethod.getDeclaringClass() == FlagWatcher.class) {
|
2017-01-20 19:58:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
watcherMethod.invoke(watcher, value);
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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;
|
|
|
|
}
|
2017-07-11 07:38:27 +02:00
|
|
|
|
2017-01-20 19:58:51 +01:00
|
|
|
// The event wasn't cancelled.
|
|
|
|
// If the disguise entity isn't the same as the one we are disguising
|
|
|
|
if (disguise.getEntity() != entity) {
|
|
|
|
// If the disguise entity actually exists
|
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
// Clone the disguise
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
// Set the disguise's entity
|
|
|
|
disguise.setEntity(entity);
|
|
|
|
}
|
|
|
|
|
2018-02-14 06:49:10 +01:00
|
|
|
// They prefer to have the opposite of whatever the view disguises option is
|
|
|
|
if (hasSelfDisguisePreference(entity) && disguise.isSelfDisguiseVisible() == DisguiseConfig.isViewDisguises())
|
|
|
|
disguise.setViewSelfDisguise(!disguise.isSelfDisguiseVisible());
|
2017-01-20 19:58:51 +01:00
|
|
|
|
|
|
|
disguise.startDisguise();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Collection playersToNotSeeDisguise) {
|
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
|
|
|
|
|
|
|
for (Object obj : playersToNotSeeDisguise) {
|
|
|
|
if (obj instanceof String) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer((String) obj);
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (obj instanceof Player) {
|
2017-01-20 19:58:51 +01:00
|
|
|
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, List<String> playersToNotSeeDisguise) {
|
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) playersToNotSeeDisguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Player... playersToNotSeeDisguise) {
|
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) Arrays.asList(playersToNotSeeDisguise));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, String... playersToNotSeeDisguise) {
|
|
|
|
disguiseIgnorePlayers(entity, disguise, (Collection) Arrays.asList(playersToNotSeeDisguise));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disguise the next entity to spawn, this means you need to spawn an entity immediately after calling this.
|
|
|
|
*
|
|
|
|
* @param disguise
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static int disguiseNextEntity(Disguise disguise) {
|
|
|
|
if (disguise == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguise.getEntity() != null || DisguiseUtilities.getDisguises().containsValue(disguise)) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
int id = ReflectionManager.getNewEntityId(false);
|
|
|
|
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
|
2017-01-20 19:58:51 +01:00
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
return id;
|
2017-01-20 19:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disguise this entity with this disguise
|
|
|
|
*
|
|
|
|
* @param entity
|
|
|
|
* @param disguise
|
|
|
|
*/
|
|
|
|
public static void disguiseToAll(Entity entity, Disguise disguise) {
|
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// You called the disguiseToAll method foolish mortal! Prepare to have your custom settings wiped!!!
|
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
|
|
|
|
|
|
|
for (String observer : ((TargetedDisguise) disguise).getObservers()) {
|
|
|
|
((TargetedDisguise) disguise).removePlayer(observer);
|
|
|
|
}
|
2017-06-22 18:14:19 +02:00
|
|
|
|
2017-01-20 19:58:51 +01:00
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, Collection playersToViewDisguise) {
|
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
|
|
|
|
|
|
|
|
for (Object obj : playersToViewDisguise) {
|
|
|
|
if (obj instanceof String) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer((String) obj);
|
2017-06-21 21:20:12 +02:00
|
|
|
} else if (obj instanceof Player) {
|
2017-01-20 19:58:51 +01:00
|
|
|
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> playersToViewDisguise) {
|
|
|
|
disguiseToPlayers(entity, disguise, (Collection) playersToViewDisguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, Player... playersToViewDisguise) {
|
|
|
|
disguiseToPlayers(entity, disguise, (Collection) Arrays.asList(playersToViewDisguise));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, String... playersToViewDisguise) {
|
|
|
|
disguiseToPlayers(entity, disguise, (Collection) Arrays.asList(playersToViewDisguise));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int firstCapital(String str) {
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
|
|
|
if (Character.isUpperCase(str.charAt(i))) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the disguise of a entity
|
|
|
|
*
|
|
|
|
* @param disguised
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Disguise getDisguise(Entity disguised) {
|
|
|
|
if (disguised == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DisguiseUtilities.getMainDisguise(disguised.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the disguise of a entity
|
|
|
|
*
|
|
|
|
* @param observer
|
|
|
|
* @param disguised
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Disguise getDisguise(Player observer, Entity disguised) {
|
|
|
|
if (disguised == null || observer == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DisguiseUtilities.getDisguise(observer, disguised);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the disguises of a entity
|
|
|
|
*
|
|
|
|
* @param disguised
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Disguise[] getDisguises(Entity disguised) {
|
|
|
|
if (disguised == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DisguiseUtilities.getDisguises(disguised.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getSelfDisguiseId() {
|
2017-12-05 20:08:24 +01:00
|
|
|
return selfDisguiseId;
|
2017-01-20 19:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this entity disguised
|
|
|
|
*
|
|
|
|
* @param disguised
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isDisguised(Entity disguised) {
|
|
|
|
return getDisguise(disguised) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this entity disguised
|
|
|
|
*
|
|
|
|
* @param observer
|
|
|
|
* @param disguised
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isDisguised(Player observer, Entity disguised) {
|
|
|
|
return getDisguise(observer, disguised) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isDisguiseInUse(Disguise disguise) {
|
|
|
|
return disguise.isDisguiseInUse();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isSelfDisguised(Player player) {
|
|
|
|
return DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the entitiy has /disguiseviewself toggled on.
|
|
|
|
*
|
|
|
|
* @param entity
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isViewSelfToggled(Entity entity) {
|
2018-02-14 06:49:10 +01:00
|
|
|
return hasSelfDisguisePreference(entity) != DisguiseConfig.isViewDisguises();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasSelfDisguisePreference(Entity entity) {
|
|
|
|
return Disguise.getViewSelf().contains(entity.getUniqueId());
|
2017-01-20 19:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-05 20:08:24 +01:00
|
|
|
* Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka
|
|
|
|
* removed from
|
2017-01-20 19:58:51 +01:00
|
|
|
* the world.
|
|
|
|
*
|
|
|
|
* @param entity
|
|
|
|
*/
|
|
|
|
public static void undisguiseToAll(Entity entity) {
|
|
|
|
Disguise[] disguises = getDisguises(entity);
|
|
|
|
|
|
|
|
for (Disguise disguise : disguises) {
|
|
|
|
disguise.removeDisguise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this player can see his own disguise or not.
|
|
|
|
*
|
|
|
|
* @param entity
|
2017-07-23 13:47:00 +02:00
|
|
|
* @param canSeeSelfDisguises
|
2017-01-20 19:58:51 +01:00
|
|
|
*/
|
2017-07-23 13:47:00 +02:00
|
|
|
public static void setViewDisguiseToggled(Entity entity, boolean canSeeSelfDisguises) {
|
2017-01-20 19:58:51 +01:00
|
|
|
if (isDisguised(entity)) {
|
2017-06-21 21:20:12 +02:00
|
|
|
Disguise[] disguises = getDisguises(entity);
|
|
|
|
|
|
|
|
for (Disguise disguise : disguises) {
|
2017-07-23 13:47:00 +02:00
|
|
|
disguise.setViewSelfDisguise(canSeeSelfDisguises);
|
2017-06-21 21:20:12 +02:00
|
|
|
}
|
2017-01-20 19:58:51 +01:00
|
|
|
}
|
|
|
|
|
2017-07-23 13:47:00 +02:00
|
|
|
if (!canSeeSelfDisguises == DisguiseConfig.isViewDisguises()) {
|
2018-02-14 06:49:10 +01:00
|
|
|
if (!hasSelfDisguisePreference(entity)) {
|
2017-01-20 19:58:51 +01:00
|
|
|
Disguise.getViewSelf().add(entity.getUniqueId());
|
|
|
|
}
|
2017-06-21 21:20:12 +02:00
|
|
|
} else {
|
2017-01-20 19:58:51 +01:00
|
|
|
Disguise.getViewSelf().remove(entity.getUniqueId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DisguiseAPI() {
|
|
|
|
}
|
|
|
|
}
|