Now can clone a player without clicking on them

This commit is contained in:
libraryaddict 2017-01-21 07:58:51 +13:00
parent 5bc64bdae3
commit d84ec1fd2c
5 changed files with 514 additions and 475 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>LibsDisguises</groupId> <groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId> <artifactId>LibsDisguises</artifactId>
<version>9.2.4</version> <version>9.2.4-SNAPSHOT</version>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>

View File

@ -1,434 +1,435 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse; import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.HorseInventory; import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise; import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager;
public class DisguiseAPI { public class DisguiseAPI {
public static Disguise constructDisguise(Entity entity) { public static Disguise constructDisguise(Entity entity) {
return constructDisguise(entity, true, true, true); return constructDisguise(entity, true, true, true);
} }
public static Disguise constructDisguise(Entity entity, boolean doEquipment, boolean doSneak, boolean doSprint) { public static Disguise constructDisguise(Entity entity, boolean doEquipment, boolean doSneak, boolean doSprint) {
DisguiseType disguiseType = DisguiseType.getType(entity); DisguiseType disguiseType = DisguiseType.getType(entity);
Disguise disguise; Disguise disguise;
if (disguiseType.isMisc()) { if (disguiseType.isMisc()) {
disguise = new MiscDisguise(disguiseType); disguise = new MiscDisguise(disguiseType);
} }
else if (disguiseType.isMob()) { else if (disguiseType.isMob()) {
disguise = new MobDisguise(disguiseType); disguise = new MobDisguise(disguiseType);
} }
else { else {
disguise = new PlayerDisguise(entity.getName()); disguise = new PlayerDisguise(entity.getName());
} }
FlagWatcher watcher = disguise.getWatcher(); FlagWatcher watcher = disguise.getWatcher();
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
for (PotionEffect effect : ((LivingEntity) entity).getActivePotionEffects()) { for (PotionEffect effect : ((LivingEntity) entity).getActivePotionEffects()) {
((LivingWatcher) watcher).addPotionEffect(effect.getType()); ((LivingWatcher) watcher).addPotionEffect(effect.getType());
if (effect.getType() == PotionEffectType.INVISIBILITY) { if (effect.getType() == PotionEffectType.INVISIBILITY) {
watcher.setInvisible(true); watcher.setInvisible(true);
} }
else if (effect.getType() == PotionEffectType.GLOWING) { else if (effect.getType() == PotionEffectType.GLOWING) {
watcher.setGlowing(true); watcher.setGlowing(true);
} }
} }
} }
if (entity.getFireTicks() > 0) { if (entity.getFireTicks() > 0) {
watcher.setBurning(true); watcher.setBurning(true);
} }
if (doEquipment && entity instanceof LivingEntity) { if (doEquipment && entity instanceof LivingEntity) {
EntityEquipment equip = ((LivingEntity) entity).getEquipment(); EntityEquipment equip = ((LivingEntity) entity).getEquipment();
watcher.setArmor(equip.getArmorContents()); watcher.setArmor(equip.getArmorContents());
watcher.setItemInMainHand(equip.getItemInMainHand()); watcher.setItemInMainHand(equip.getItemInMainHand());
if (disguiseType.getEntityType() == EntityType.HORSE) { if (disguiseType.getEntityType() == EntityType.HORSE) {
Horse horse = (Horse) entity; Horse horse = (Horse) entity;
HorseInventory horseInventory = horse.getInventory(); HorseInventory horseInventory = horse.getInventory();
ItemStack saddle = horseInventory.getSaddle(); ItemStack saddle = horseInventory.getSaddle();
if (saddle != null && saddle.getType() == Material.SADDLE) { if (saddle != null && saddle.getType() == Material.SADDLE) {
((AbstractHorseWatcher) watcher).setSaddled(true); ((AbstractHorseWatcher) watcher).setSaddled(true);
} }
if (watcher instanceof HorseWatcher) if (watcher instanceof HorseWatcher)
((HorseWatcher) watcher).setHorseArmor(horseInventory.getArmor()); ((HorseWatcher) watcher).setHorseArmor(horseInventory.getArmor());
} }
} }
for (Method method : entity.getClass().getMethods()) { for (Method method : entity.getClass().getMethods()) {
if ((doSneak || !method.getName().equals("setSneaking")) && (doSprint || !method.getName().equals("setSprinting")) if ((doSneak || !method.getName().equals("setSneaking")) && (doSprint || !method.getName().equals("setSprinting"))
&& method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { && method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
Class methodReturn = method.getReturnType(); Class methodReturn = method.getReturnType();
if (methodReturn == float.class || methodReturn == Float.class || methodReturn == Double.class) { if (methodReturn == float.class || methodReturn == Float.class || methodReturn == Double.class) {
methodReturn = double.class; methodReturn = double.class;
} }
int firstCapitalMethod = firstCapital(method.getName()); int firstCapitalMethod = firstCapital(method.getName());
if (firstCapitalMethod > 0) { if (firstCapitalMethod > 0) {
for (Method watcherMethod : watcher.getClass().getMethods()) { for (Method watcherMethod : watcher.getClass().getMethods()) {
if (!watcherMethod.getName().startsWith("get") && watcherMethod.getReturnType() == void.class if (!watcherMethod.getName().startsWith("get") && watcherMethod.getReturnType() == void.class
&& watcherMethod.getParameterTypes().length == 1) { && watcherMethod.getParameterTypes().length == 1) {
int firstCapitalWatcher = firstCapital(watcherMethod.getName()); int firstCapitalWatcher = firstCapital(watcherMethod.getName());
if (firstCapitalWatcher > 0 && method.getName().substring(firstCapitalMethod) if (firstCapitalWatcher > 0 && method.getName().substring(firstCapitalMethod)
.equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) { .equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) {
Class methodParam = watcherMethod.getParameterTypes()[0]; Class methodParam = watcherMethod.getParameterTypes()[0];
if (methodParam == float.class || methodParam == Float.class || methodParam == Double.class) { if (methodParam == float.class || methodParam == Float.class || methodParam == Double.class) {
methodParam = double.class; methodParam = double.class;
} }
else if (methodParam == AnimalColor.class) { else if (methodParam == AnimalColor.class) {
methodParam = DyeColor.class; methodParam = DyeColor.class;
} }
if (methodReturn == methodParam) { if (methodReturn == methodParam) {
try { try {
Object value = method.invoke(entity); Object value = method.invoke(entity);
if (value != null) { if (value != null) {
Class toCast = watcherMethod.getParameterTypes()[0]; Class toCast = watcherMethod.getParameterTypes()[0];
if (!(toCast.isInstance(value))) { if (!(toCast.isInstance(value))) {
if (toCast == float.class) { if (toCast == float.class) {
if (!(value instanceof Float)) { if (!(value instanceof Float)) {
double d = (Double) value; double d = (Double) value;
value = (float) d; value = (float) d;
} }
} }
else if (toCast == double.class) { else if (toCast == double.class) {
if (!(value instanceof Double)) { if (!(value instanceof Double)) {
float d = (Float) value; float d = (Float) value;
value = (double) d; value = (double) d;
} }
} }
else if (toCast == AnimalColor.class) { else if (toCast == AnimalColor.class) {
value = AnimalColor.valueOf(((DyeColor) value).name()); value = AnimalColor.valueOf(((DyeColor) value).name());
} }
} }
if (value instanceof Boolean && !(Boolean) value if (value instanceof Boolean && !(Boolean) value
&& watcherMethod.getDeclaringClass() == FlagWatcher.class) { && watcherMethod.getDeclaringClass() == FlagWatcher.class) {
continue; continue;
} }
} }
watcherMethod.invoke(watcher, value); watcherMethod.invoke(watcher, value);
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
} }
} }
} }
} }
} }
} }
return disguise;
} return disguise;
}
public static void disguiseEntity(Entity entity, Disguise disguise) {
// If they are trying to disguise a null entity or use a null disguise public static void disguiseEntity(Entity entity, Disguise disguise) {
// Just return. // If they are trying to disguise a null entity or use a null disguise
if (entity == null || disguise == null) { // Just return.
return; if (entity == null || disguise == null) {
} return;
// The event wasn't cancelled. }
// If the disguise entity isn't the same as the one we are disguising // The event wasn't cancelled.
if (disguise.getEntity() != entity) { // If the disguise entity isn't the same as the one we are disguising
// If the disguise entity actually exists if (disguise.getEntity() != entity) {
if (disguise.getEntity() != null) { // If the disguise entity actually exists
// Clone the disguise if (disguise.getEntity() != null) {
disguise = disguise.clone(); // Clone the disguise
} disguise = disguise.clone();
// Set the disguise's entity }
disguise.setEntity(entity); // Set the disguise's entity
} disguise.setEntity(entity);
}
if (Disguise.getViewSelf().contains(disguise.getEntity().getUniqueId())) {
disguise.setViewSelfDisguise(true); if (Disguise.getViewSelf().contains(disguise.getEntity().getUniqueId())) {
} disguise.setViewSelfDisguise(true);
}
disguise.startDisguise();
} disguise.startDisguise();
}
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Collection playersToNotSeeDisguise) {
if (disguise.getEntity() != null) { public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Collection playersToNotSeeDisguise) {
disguise = disguise.clone(); if (disguise.getEntity() != null) {
} disguise = disguise.clone();
}
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
for (Object obj : playersToNotSeeDisguise) {
if (obj instanceof String) { for (Object obj : playersToNotSeeDisguise) {
((TargetedDisguise) disguise).addPlayer((String) obj); if (obj instanceof String) {
} ((TargetedDisguise) disguise).addPlayer((String) obj);
else if (obj instanceof Player) { }
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName()); else if (obj instanceof Player) {
} ((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
} }
}
disguiseEntity(entity, disguise);
} disguiseEntity(entity, disguise);
}
@Deprecated
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, List<String> playersToNotSeeDisguise) { @Deprecated
disguiseIgnorePlayers(entity, disguise, (Collection) playersToNotSeeDisguise); 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, 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)); 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. /**
* * Disguise the next entity to spawn, this means you need to spawn an entity immediately after calling this.
* @param disguise *
* @return * @param disguise
*/ * @return
public static int disguiseNextEntity(Disguise disguise) { */
if (disguise == null) { public static int disguiseNextEntity(Disguise disguise) {
return -1; if (disguise == null) {
} return -1;
}
if (disguise.getEntity() != null || DisguiseUtilities.getDisguises().containsValue(disguise)) {
disguise = disguise.clone(); if (disguise.getEntity() != null || DisguiseUtilities.getDisguises().containsValue(disguise)) {
} disguise = disguise.clone();
}
try {
int id = ReflectionManager.getNmsField("Entity", "entityCount").getInt(null); try {
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise); int id = ReflectionManager.getNmsField("Entity", "entityCount").getInt(null);
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
return id;
} return id;
catch (IllegalAccessException e) { }
e.printStackTrace(); catch (IllegalAccessException e) {
} e.printStackTrace();
}
return -1;
} return -1;
}
/**
* Disguise this entity with this disguise /**
* * Disguise this entity with this disguise
* @param entity *
* @param disguise * @param entity
*/ * @param disguise
public static void disguiseToAll(Entity entity, Disguise disguise) { */
if (disguise.getEntity() != null) { public static void disguiseToAll(Entity entity, Disguise disguise) {
disguise = disguise.clone(); 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); // 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); for (String observer : ((TargetedDisguise) disguise).getObservers()) {
} ((TargetedDisguise) disguise).removePlayer(observer);
disguiseEntity(entity, disguise); }
} disguiseEntity(entity, disguise);
}
public static void disguiseToPlayers(Entity entity, Disguise disguise, Collection playersToViewDisguise) {
if (disguise.getEntity() != null) { public static void disguiseToPlayers(Entity entity, Disguise disguise, Collection playersToViewDisguise) {
disguise = disguise.clone(); if (disguise.getEntity() != null) {
} disguise = disguise.clone();
}
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
for (Object obj : playersToViewDisguise) {
if (obj instanceof String) { for (Object obj : playersToViewDisguise) {
((TargetedDisguise) disguise).addPlayer((String) obj); if (obj instanceof String) {
} ((TargetedDisguise) disguise).addPlayer((String) obj);
else if (obj instanceof Player) { }
((TargetedDisguise) disguise).addPlayer(((Player) obj).getName()); else if (obj instanceof Player) {
} ((TargetedDisguise) disguise).addPlayer(((Player) obj).getName());
} }
}
disguiseEntity(entity, disguise);
} disguiseEntity(entity, disguise);
}
@Deprecated
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> playersToViewDisguise) { @Deprecated
disguiseToPlayers(entity, disguise, (Collection) playersToViewDisguise); 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, 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)); 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++) { private static int firstCapital(String str) {
if (Character.isUpperCase(str.charAt(i))) { for (int i = 0; i < str.length(); i++) {
return i; if (Character.isUpperCase(str.charAt(i))) {
} return i;
} }
}
return -1;
} return -1;
}
/**
* Get the disguise of a entity /**
* * Get the disguise of a entity
* @param disguised *
* @return * @param disguised
*/ * @return
public static Disguise getDisguise(Entity disguised) { */
if (disguised == null) { public static Disguise getDisguise(Entity disguised) {
return null; if (disguised == null) {
} return null;
}
return DisguiseUtilities.getMainDisguise(disguised.getUniqueId());
} return DisguiseUtilities.getMainDisguise(disguised.getUniqueId());
}
/**
* Get the disguise of a entity /**
* * Get the disguise of a entity
* @param observer *
* @param disguised * @param observer
* @return * @param disguised
*/ * @return
public static Disguise getDisguise(Player observer, Entity disguised) { */
if (disguised == null || observer == null) { public static Disguise getDisguise(Player observer, Entity disguised) {
return null; if (disguised == null || observer == null) {
} return null;
}
return DisguiseUtilities.getDisguise(observer, disguised);
} return DisguiseUtilities.getDisguise(observer, disguised);
}
/**
* Get the disguises of a entity /**
* * Get the disguises of a entity
* @param disguised *
* @return * @param disguised
*/ * @return
public static Disguise[] getDisguises(Entity disguised) { */
if (disguised == null) { public static Disguise[] getDisguises(Entity disguised) {
return null; if (disguised == null) {
} return null;
}
return DisguiseUtilities.getDisguises(disguised.getUniqueId());
} return DisguiseUtilities.getDisguises(disguised.getUniqueId());
}
public static int getSelfDisguiseId() {
return -10; public static int getSelfDisguiseId() {
} return -10;
}
/**
* Is this entity disguised /**
* * Is this entity disguised
* @param disguised *
* @return * @param disguised
*/ * @return
public static boolean isDisguised(Entity disguised) { */
return getDisguise(disguised) != null; public static boolean isDisguised(Entity disguised) {
} return getDisguise(disguised) != null;
}
/**
* Is this entity disguised /**
* * Is this entity disguised
* @param observer *
* @param disguised * @param observer
* @return * @param disguised
*/ * @return
public static boolean isDisguised(Player observer, Entity disguised) { */
return getDisguise(observer, disguised) != null; 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 isDisguiseInUse(Disguise disguise) {
} return disguise.isDisguiseInUse();
}
public static boolean isSelfDisguised(Player player) {
return DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()); public static boolean isSelfDisguised(Player player) {
} return DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId());
}
/**
* Returns true if the entitiy has /disguiseviewself toggled on. /**
* * Returns true if the entitiy has /disguiseviewself toggled on.
* @param entity *
* @return * @param entity
*/ * @return
public static boolean isViewSelfToggled(Entity entity) { */
return isDisguised(entity) ? getDisguise(entity).isSelfDisguiseVisible() public static boolean isViewSelfToggled(Entity entity) {
: Disguise.getViewSelf().contains(entity.getUniqueId()); return isDisguised(entity) ? getDisguise(entity).isSelfDisguiseVisible()
} : Disguise.getViewSelf().contains(entity.getUniqueId());
}
/**
* Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from /**
* the world. * Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from
* * the world.
* @param entity *
*/ * @param entity
public static void undisguiseToAll(Entity entity) { */
Disguise[] disguises = getDisguises(entity); public static void undisguiseToAll(Entity entity) {
Disguise[] disguises = getDisguises(entity);
for (Disguise disguise : disguises) {
disguise.removeDisguise(); for (Disguise disguise : disguises) {
} disguise.removeDisguise();
} }
}
/**
* Set whether this player can see his own disguise or not. /**
* * Set whether this player can see his own disguise or not.
* @param entity *
* @param toggled * @param entity
*/ * @param toggled
public static void setViewDisguiseToggled(Entity entity, boolean toggled) { */
if (isDisguised(entity)) { public static void setViewDisguiseToggled(Entity entity, boolean toggled) {
Disguise disguise = getDisguise(entity); if (isDisguised(entity)) {
disguise.setViewSelfDisguise(toggled); Disguise disguise = getDisguise(entity);
} disguise.setViewSelfDisguise(toggled);
}
if (toggled) {
if (!Disguise.getViewSelf().contains(entity.getUniqueId())) { if (toggled) {
Disguise.getViewSelf().add(entity.getUniqueId()); if (!Disguise.getViewSelf().contains(entity.getUniqueId())) {
} Disguise.getViewSelf().add(entity.getUniqueId());
} }
else { }
Disguise.getViewSelf().remove(entity.getUniqueId()); else {
} Disguise.getViewSelf().remove(entity.getUniqueId());
} }
}
private DisguiseAPI() {
} private DisguiseAPI() {
} }
}

View File

@ -329,41 +329,7 @@ public class DisguiseListener implements Listener {
if (disguiseClone.containsKey(p.getName())) { if (disguiseClone.containsKey(p.getName())) {
Boolean[] options = disguiseClone.remove(p.getName()); Boolean[] options = disguiseClone.remove(p.getName());
Disguise disguise = DisguiseAPI.getDisguise(p, entity); DisguiseUtilities.createClonedDisguise(p, entity, options);
if (disguise == null) {
disguise = DisguiseAPI.constructDisguise(entity, options[0], options[1], options[2]);
}
else {
disguise = disguise.clone();
}
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
String reference = null;
int referenceLength = Math.max(2, (int) Math.ceil((0.1D + DisguiseConfig.getMaxClonedDisguises()) / 26D));
int attempts = 0;
while (reference == null && attempts++ < 1000) {
reference = "@";
for (int i = 0; i < referenceLength; i++) {
reference += alphabet[DisguiseUtilities.random.nextInt(alphabet.length)];
}
if (DisguiseUtilities.getClonedDisguise(reference) != null) {
reference = null;
}
}
if (reference != null && DisguiseUtilities.addClonedDisguise(reference, disguise)) {
p.sendMessage(ChatColor.RED + "Constructed a " + entityName + " disguise! Your reference is " + reference);
p.sendMessage(ChatColor.RED + "Example usage: /disguise " + reference);
}
else {
p.sendMessage(
ChatColor.RED + "Failed to store the reference due to lack of size. Please set this in the config");
}
} }
else if (disguiseEntity.containsKey(p.getName())) { else if (disguiseEntity.containsKey(p.getName())) {
Disguise disguise = disguiseEntity.remove(p.getName()); Disguise disguise = disguiseEntity.remove(p.getName());

View File

@ -5,14 +5,17 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm; import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabCompleter { public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabCompleter {
@Override @Override
@ -26,8 +29,14 @@ public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabComp
boolean doEquipment = true; boolean doEquipment = true;
boolean doSneak = false; boolean doSneak = false;
boolean doSprint = false; boolean doSprint = false;
Player player = null;
for (String option : args) { if (args.length > 0) {
player = Bukkit.getPlayerExact(args[0]);
}
for (int i = player == null ? 0 : 1; i < args.length; i++) {
String option = args[i];
if (StringUtils.startsWithIgnoreCase(option, "ignoreEquip") if (StringUtils.startsWithIgnoreCase(option, "ignoreEquip")
|| StringUtils.startsWithIgnoreCase(option, "ignoreEnquip")) { || StringUtils.startsWithIgnoreCase(option, "ignoreEnquip")) {
doEquipment = false; doEquipment = false;
@ -49,12 +58,19 @@ public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabComp
} }
} }
LibsDisguises.getInstance().getListener().setDisguiseClone(sender.getName(), new Boolean[] { Boolean[] options = new Boolean[] {
doEquipment, doSneak, doSprint doEquipment, doSneak, doSprint
}); };
sender.sendMessage(ChatColor.RED + "Right click a entity in the next " + DisguiseConfig.getDisguiseCloneExpire() if (player != null) {
+ " seconds to grab the disguise reference!"); DisguiseUtilities.createClonedDisguise((Player) sender, player, options);
}
else {
LibsDisguises.getInstance().getListener().setDisguiseClone(sender.getName(), options);
sender.sendMessage(ChatColor.RED + "Right click a entity in the next " + DisguiseConfig.getDisguiseCloneExpire()
+ " seconds to grab the disguise reference!");
}
} }
else { else {
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
@ -67,6 +83,14 @@ public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabComp
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<String>(); ArrayList<String> tabs = new ArrayList<String>();
String[] args = getArgs(origArgs);
if (args.length == 0) {
for (Player player : Bukkit.getOnlinePlayers()) {
tabs.add(player.getName());
}
}
tabs.add("ignoreEquip"); tabs.add("ignoreEquip");
tabs.add("doSneakSprint"); tabs.add("doSneakSprint");
tabs.add("doSneak"); tabs.add("doSneak");

View File

@ -17,6 +17,7 @@ import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -58,6 +59,9 @@ import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets; import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ClickEvent.Action;
import net.md_5.bungee.api.chat.ComponentBuilder;
public class DisguiseUtilities { public class DisguiseUtilities {
public static final Random random = new Random(); public static final Random random = new Random();
@ -143,6 +147,50 @@ public class DisguiseUtilities {
} }
} }
public static void createClonedDisguise(Player player, Entity toClone, Boolean[] options) {
Disguise disguise = DisguiseAPI.getDisguise(player, toClone);
if (disguise == null) {
disguise = DisguiseAPI.constructDisguise(toClone, options[0], options[1], options[2]);
}
else {
disguise = disguise.clone();
}
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
String reference = null;
int referenceLength = Math.max(2, (int) Math.ceil((0.1D + DisguiseConfig.getMaxClonedDisguises()) / 26D));
int attempts = 0;
while (reference == null && attempts++ < 1000) {
reference = "@";
for (int i = 0; i < referenceLength; i++) {
reference += alphabet[DisguiseUtilities.random.nextInt(alphabet.length)];
}
if (DisguiseUtilities.getClonedDisguise(reference) != null) {
reference = null;
}
}
if (reference != null && DisguiseUtilities.addClonedDisguise(reference, disguise)) {
String entityName = DisguiseType.getType(toClone).toReadable();
ComponentBuilder text = new ComponentBuilder(
ChatColor.RED + "Constructed a " + entityName + " disguise! Your reference is ");
text.append(reference).event(new ClickEvent(Action.SUGGEST_COMMAND, reference));
player.sendRawMessage(text.toString());
player.sendMessage(ChatColor.RED + "Example usage: /disguise " + reference);
}
else {
player.sendMessage(
ChatColor.RED + "Failed to store the reference, too many cloned disguises. Please set this in the config");
}
}
public static boolean addClonedDisguise(String key, Disguise disguise) { public static boolean addClonedDisguise(String key, Disguise disguise) {
if (DisguiseConfig.getMaxClonedDisguises() > 0) { if (DisguiseConfig.getMaxClonedDisguises() > 0) {
if (clonedDisguises.containsKey(key)) { if (clonedDisguises.containsKey(key)) {