2013-05-17 21:05:19 +00:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
2013-07-30 00:44:00 +00:00
|
|
|
import java.lang.reflect.Field;
|
2013-12-03 18:38:10 +00:00
|
|
|
import java.util.Arrays;
|
2013-12-01 15:37:07 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2013-09-25 14:49:24 +00:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
2013-12-01 15:37:07 +00:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
|
2013-09-25 14:49:24 +00:00
|
|
|
import me.libraryaddict.disguise.events.DisguiseEvent;
|
|
|
|
import me.libraryaddict.disguise.events.UndisguiseEvent;
|
2013-11-22 20:10:20 +00:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
|
|
|
import me.libraryaddict.disguise.utilities.ReflectionManager;
|
2013-05-28 22:29:36 +00:00
|
|
|
|
2013-07-24 00:16:54 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2013-05-28 22:29:36 +00:00
|
|
|
import org.bukkit.entity.Entity;
|
2013-12-01 13:36:42 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2013-05-17 21:05:19 +00:00
|
|
|
|
|
|
|
public class DisguiseAPI {
|
2013-08-01 07:56:27 +00:00
|
|
|
|
2013-12-02 10:21:10 +00:00
|
|
|
public static void disguiseEntity(Entity entity, Disguise disguise) {
|
2013-08-13 03:19:50 +00:00
|
|
|
// If they are trying to disguise a null entity or use a null disguise
|
|
|
|
// Just return.
|
2013-08-08 17:56:13 +00:00
|
|
|
if (entity == null || disguise == null)
|
2013-06-01 06:35:31 +00:00
|
|
|
return;
|
2013-08-13 03:19:50 +00:00
|
|
|
// Fire a disguise event
|
2013-08-11 18:32:51 +00:00
|
|
|
DisguiseEvent event = new DisguiseEvent(entity, disguise);
|
2013-07-31 07:39:16 +00:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
2013-08-13 03:19:50 +00:00
|
|
|
// If they cancelled this disguise event. No idea why.
|
|
|
|
// Just return.
|
|
|
|
if (event.isCancelled())
|
2013-07-31 07:39:16 +00:00
|
|
|
return;
|
2013-08-13 09:44:54 +00:00
|
|
|
// The event wasn't cancelled.
|
2013-08-13 03:19:50 +00:00
|
|
|
// If the disguise entity isn't the same as the one we are disguising
|
2013-07-27 22:58:21 +00:00
|
|
|
if (disguise.getEntity() != entity) {
|
2013-08-13 03:19:50 +00:00
|
|
|
// If the disguise entity actually exists
|
2013-08-07 10:51:42 +00:00
|
|
|
if (disguise.getEntity() != null) {
|
2013-08-13 03:19:50 +00:00
|
|
|
// Clone the disguise
|
2013-07-27 22:58:21 +00:00
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-08-13 03:19:50 +00:00
|
|
|
// Set the disguise's entity
|
2013-08-07 10:51:42 +00:00
|
|
|
disguise.setEntity(entity);
|
2013-12-01 15:37:07 +00:00
|
|
|
}
|
2013-08-13 03:19:50 +00:00
|
|
|
// Stick the disguise in the disguises bin
|
2013-12-01 15:37:07 +00:00
|
|
|
DisguiseUtilities.addDisguise(entity.getEntityId(), (TargetedDisguise) disguise);
|
2013-08-13 03:19:50 +00:00
|
|
|
// Resend the disguised entity's packet
|
2013-12-01 15:37:07 +00:00
|
|
|
DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise);
|
2013-08-13 03:19:50 +00:00
|
|
|
// If he is a player, then self disguise himself
|
2013-11-22 19:52:15 +00:00
|
|
|
DisguiseUtilities.setupFakeDisguise(disguise);
|
2013-12-01 15:37:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 18:38:10 +00:00
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, List<String> playersToNotSeeDisguise) {
|
2014-01-04 09:16:03 +00:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-03 18:38:10 +00:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
|
|
|
for (String name : playersToNotSeeDisguise) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer(name);
|
2013-12-01 16:10:38 +00:00
|
|
|
}
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:38:10 +00:00
|
|
|
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, String... playersToNotSeeDisguise) {
|
|
|
|
disguiseIgnorePlayers(entity, disguise, Arrays.asList(playersToNotSeeDisguise));
|
|
|
|
}
|
|
|
|
|
2013-12-01 16:10:38 +00:00
|
|
|
/**
|
|
|
|
* Disguise the next entity to spawn with this disguise. This may not work however if the entity doesn't actually spawn.
|
|
|
|
*/
|
|
|
|
public static void disguiseNextEntity(Disguise disguise) {
|
|
|
|
if (disguise == null)
|
|
|
|
return;
|
|
|
|
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);
|
|
|
|
DisguiseUtilities.addDisguise(id, (TargetedDisguise) disguise);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-01 15:37:07 +00:00
|
|
|
/**
|
|
|
|
* Disguise this entity with this disguise
|
|
|
|
*/
|
|
|
|
public static void disguiseToAll(Entity entity, Disguise disguise) {
|
2014-01-04 09:16:03 +00:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-01 15:37:07 +00:00
|
|
|
// You called the disguiseToAll method foolish mortal! Prepare to have your custom settings wiped!!!
|
2013-12-03 18:38:10 +00:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS);
|
2013-12-01 15:37:07 +00:00
|
|
|
for (String observer : ((TargetedDisguise) disguise).getObservers())
|
2013-12-03 18:38:10 +00:00
|
|
|
((TargetedDisguise) disguise).removePlayer(observer);
|
2013-12-01 15:37:07 +00:00
|
|
|
disguiseEntity(entity, disguise);
|
2013-05-28 22:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 18:38:10 +00:00
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> playersToViewDisguise) {
|
2014-01-04 09:16:03 +00:00
|
|
|
if (disguise.getEntity() != null) {
|
|
|
|
disguise = disguise.clone();
|
|
|
|
}
|
2013-12-03 18:38:10 +00:00
|
|
|
((TargetedDisguise) disguise).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
|
|
|
|
for (String name : playersToViewDisguise) {
|
|
|
|
((TargetedDisguise) disguise).addPlayer(name);
|
2013-12-01 16:10:38 +00:00
|
|
|
}
|
|
|
|
disguiseEntity(entity, disguise);
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:38:10 +00:00
|
|
|
public static void disguiseToPlayers(Entity entity, Disguise disguise, String... playersToViewDisguise) {
|
|
|
|
disguiseToPlayers(entity, disguise, Arrays.asList(playersToViewDisguise));
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:52:54 +00:00
|
|
|
/**
|
2013-08-13 03:19:50 +00:00
|
|
|
* Get the disguise of a entity
|
2013-05-28 22:52:54 +00:00
|
|
|
*/
|
2013-12-01 13:36:42 +00:00
|
|
|
@Deprecated
|
2013-11-22 19:52:15 +00:00
|
|
|
public static Disguise getDisguise(Entity disguised) {
|
|
|
|
if (disguised == null)
|
2013-08-08 17:56:13 +00:00
|
|
|
return null;
|
2014-01-20 17:35:42 +00:00
|
|
|
return DisguiseUtilities.getMainDisguise(disguised.getEntityId());
|
2013-05-28 22:52:54 +00:00
|
|
|
}
|
2013-05-28 22:29:36 +00:00
|
|
|
|
2013-12-01 14:31:54 +00:00
|
|
|
/**
|
2013-12-01 16:10:38 +00:00
|
|
|
* Get the disguise of a entity
|
2013-12-01 14:31:54 +00:00
|
|
|
*/
|
2013-12-01 16:10:38 +00:00
|
|
|
public static Disguise getDisguise(Player observer, Entity disguised) {
|
2013-12-01 14:31:54 +00:00
|
|
|
if (disguised == null)
|
|
|
|
return null;
|
2013-12-01 16:10:38 +00:00
|
|
|
return DisguiseUtilities.getDisguise(observer, disguised.getEntityId());
|
2013-12-01 14:31:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 13:36:42 +00:00
|
|
|
/**
|
2013-12-01 16:10:38 +00:00
|
|
|
* Get the disguises of a entity
|
2013-12-01 13:36:42 +00:00
|
|
|
*/
|
2013-12-01 16:10:38 +00:00
|
|
|
public static Disguise[] getDisguises(Entity disguised) {
|
2013-12-01 13:36:42 +00:00
|
|
|
if (disguised == null)
|
|
|
|
return null;
|
2013-12-01 16:10:38 +00:00
|
|
|
return DisguiseUtilities.getDisguises(disguised.getEntityId());
|
2013-12-01 13:36:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 03:19:50 +00:00
|
|
|
/**
|
|
|
|
* Get the ID of a fake disguise for a entityplayer
|
|
|
|
*/
|
2013-11-22 19:52:15 +00:00
|
|
|
public static int getFakeDisguise(int entityId) {
|
|
|
|
if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId))
|
|
|
|
return DisguiseUtilities.getSelfDisguisesIds().get(entityId);
|
2013-07-30 00:44:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-17 21:05:19 +00:00
|
|
|
/**
|
2013-08-13 09:44:54 +00:00
|
|
|
* Is this entity disguised
|
2013-05-17 21:05:19 +00:00
|
|
|
*/
|
2013-12-01 13:36:42 +00:00
|
|
|
@Deprecated
|
2013-11-22 19:52:15 +00:00
|
|
|
public static boolean isDisguised(Entity disguised) {
|
|
|
|
return getDisguise(disguised) != null;
|
2013-05-17 21:05:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 13:36:42 +00:00
|
|
|
/**
|
|
|
|
* Is this entity disguised
|
|
|
|
*/
|
|
|
|
public static boolean isDisguised(Player observer, Entity disguised) {
|
|
|
|
return getDisguise(observer, disguised) != null;
|
|
|
|
}
|
|
|
|
|
2013-12-01 16:10:38 +00:00
|
|
|
public static boolean isDisguiseInUse(Disguise disguise) {
|
|
|
|
return DisguiseUtilities.isDisguiseInUse(disguise);
|
|
|
|
}
|
|
|
|
|
2013-05-17 21:05:19 +00:00
|
|
|
/**
|
2013-08-13 09:44:54 +00: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 21:05:19 +00:00
|
|
|
*/
|
2013-05-28 22:29:36 +00:00
|
|
|
public static void undisguiseToAll(Entity entity) {
|
2013-12-01 14:31:54 +00:00
|
|
|
Disguise[] disguises = getDisguises(entity);
|
|
|
|
for (Disguise disguise : disguises) {
|
|
|
|
UndisguiseEvent event = new UndisguiseEvent(entity, disguise);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if (event.isCancelled())
|
|
|
|
continue;
|
|
|
|
disguise.removeDisguise();
|
|
|
|
}
|
2013-08-13 09:44:54 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:52:15 +00:00
|
|
|
private DisguiseAPI() {
|
2013-08-14 20:57:30 +00:00
|
|
|
}
|
2013-05-17 21:05:19 +00:00
|
|
|
}
|