LibsDisguises/src/me/libraryaddict/disguise/DisguiseAPI.java

225 lines
8.4 KiB
Java
Raw Normal View History

2013-05-17 23:05:19 +02:00
package me.libraryaddict.disguise;
import java.lang.reflect.Field;
import java.util.ArrayList;
2013-12-03 19:38:10 +01:00
import java.util.Arrays;
import java.util.Collection;
2013-12-01 16:37:07 +01:00
import java.util.List;
import java.util.UUID;
2013-12-01 16:37:07 +01:00
import me.libraryaddict.disguise.disguisetypes.Disguise;
2013-12-01 16:37:07 +01:00
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;
import me.libraryaddict.disguise.utilities.ReflectionManager;
2013-07-24 02:16:54 +02:00
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
2013-05-17 23:05:19 +02:00
public class DisguiseAPI {
public static void disguiseEntity(Entity entity, Disguise disguise) {
// 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;
// Fire a disguise event
DisguiseEvent event = new DisguiseEvent(entity, disguise);
2013-07-31 09:39:16 +02:00
Bukkit.getPluginManager().callEvent(event);
// If they cancelled this disguise event. No idea why.
// Just return.
if (event.isCancelled())
2013-07-31 09:39:16 +02:00
return;
// 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);
2013-12-01 16:37:07 +01:00
}
// Stick the disguise in the disguises bin
DisguiseUtilities.addDisguise(entity.getUniqueId(), (TargetedDisguise) disguise);
// Resend the disguised entity's packet
2013-12-01 16:37:07 +01:00
DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise);
// If he is a player, then self disguise himself
DisguiseUtilities.setupFakeDisguise(disguise);
2013-12-01 16:37:07 +01:00
}
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Collection playersToNotSeeDisguise) {
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);
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);
}
@Deprecated
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, List<String> playersToNotSeeDisguise) {
disguiseIgnorePlayers(entity, disguise, playersToNotSeeDisguise);
}
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, Player... playersToNotSeeDisguise) {
ArrayList<String> names = new ArrayList<String>();
for (Player p : playersToNotSeeDisguise) {
names.add(p.getName());
}
disguiseIgnorePlayers(entity, disguise, names);
}
2013-12-03 19:38:10 +01:00
public static void disguiseIgnorePlayers(Entity entity, Disguise disguise, String... playersToNotSeeDisguise) {
disguiseIgnorePlayers(entity, disguise, Arrays.asList(playersToNotSeeDisguise));
}
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.
*/
public static int disguiseNextEntity(Disguise disguise) {
2013-12-01 17:10:38 +01:00
if (disguise == null)
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);
return id;
2013-12-01 17:10:38 +01:00
} catch (Exception ex) {
ex.printStackTrace();
}
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) {
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
}
public static void disguiseToPlayers(Entity entity, Disguise disguise, Collection playersToViewDisguise) {
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);
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);
}
@Deprecated
public static void disguiseToPlayers(Entity entity, Disguise disguise, List<String> playersToViewDisguise) {
disguiseToPlayers(entity, disguise, playersToViewDisguise);
}
public static void disguiseToPlayers(Entity entity, Disguise disguise, Player... playersToViewDisguise) {
ArrayList<String> names = new ArrayList<String>();
for (Player p : playersToViewDisguise) {
names.add(p.getName());
}
disguiseToPlayers(entity, disguise, names);
}
2013-12-03 19:38:10 +01:00
public static void disguiseToPlayers(Entity entity, Disguise disguise, String... playersToViewDisguise) {
disguiseToPlayers(entity, disguise, Arrays.asList(playersToViewDisguise));
}
2013-05-29 00:52:54 +02:00
/**
* Get the disguise of a entity
2013-05-29 00:52:54 +02:00
*/
public static Disguise getDisguise(Entity disguised) {
if (disguised == null)
2013-08-08 19:56:13 +02:00
return null;
return DisguiseUtilities.getMainDisguise(disguised.getUniqueId());
2013-05-29 00:52:54 +02:00
}
/**
2013-12-01 17:10:38 +01:00
* Get the disguise of a entity
*/
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)
return null;
2014-04-02 15:02:58 +02:00
return DisguiseUtilities.getDisguise(observer, disguised);
}
/**
2013-12-01 17:10:38 +01:00
* Get the disguises of a entity
*/
2013-12-01 17:10:38 +01:00
public static Disguise[] getDisguises(Entity disguised) {
if (disguised == null)
return null;
return DisguiseUtilities.getDisguises(disguised.getUniqueId());
}
/**
* Get the ID of a fake disguise for a entityplayer
*/
public static int getFakeDisguise(UUID entityId) {
if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId))
return DisguiseUtilities.getSelfDisguisesIds().get(entityId);
return -1;
}
2013-05-17 23:05:19 +02:00
/**
* Is this entity disguised
2013-05-17 23:05:19 +02:00
*/
public static boolean isDisguised(Entity disguised) {
return getDisguise(disguised) != null;
2013-05-17 23:05:19 +02: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) {
return DisguiseUtilities.isDisguiseInUse(disguise);
}
2013-05-17 23:05:19 +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
*/
public static void undisguiseToAll(Entity entity) {
Disguise[] disguises = getDisguises(entity);
for (Disguise disguise : disguises) {
UndisguiseEvent event = new UndisguiseEvent(entity, disguise);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
continue;
disguise.removeDisguise();
}
}
private DisguiseAPI() {
}
2013-05-17 23:05:19 +02:00
}