LibsDisguises/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java

1357 lines
59 KiB
Java
Raw Normal View History

2016-05-11 23:47:08 +02:00
package me.libraryaddict.disguise.utilities;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2016-05-11 23:47:08 +02:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus;
import org.bukkit.util.Vector;
2016-06-13 22:55:30 +02:00
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server;
2016-05-11 23:47:08 +02:00
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
2016-06-13 22:55:30 +02:00
import com.comphenix.protocol.wrappers.BlockPosition;
2016-05-11 23:47:08 +02:00
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
2016-12-21 04:25:28 +01:00
import me.libraryaddict.disguise.DisguiseConfig.DisguisePushing;
2016-05-11 23:47:08 +02:00
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
public class DisguiseUtilities {
2016-05-11 23:47:08 +02:00
public static final Random random = new Random();
/**
* This is a list of names which was called by other plugins. As such, don't remove from the gameProfiles as its the duty of
* the plugin to do that.
*/
private static HashSet<String> addedByPlugins = new HashSet<>();
private static LinkedHashMap<String, Disguise> clonedDisguises = new LinkedHashMap<>();
/**
* A hashmap of the uuid's of entitys, alive and dead. And their disguises in use
*/
private static HashMap<UUID, HashSet<TargetedDisguise>> disguisesInUse = new HashMap<>();
/**
* Disguises which are stored ready for a entity to be seen by a player Preferably, disguises in this should only stay in for
* a max of a second.
*/
private static HashMap<Integer, HashSet<TargetedDisguise>> futureDisguises = new HashMap<>();
/**
* A hashmap storing the uuid and skin of a playername
*/
private static HashMap<String, WrappedGameProfile> gameProfiles = new HashMap<>();
private static LibsDisguises libsDisguises;
private static HashMap<String, ArrayList<Object>> runnables = new HashMap<>();
private static HashSet<UUID> selfDisguised = new HashSet<>();
2016-06-05 15:31:34 +02:00
private static Thread mainThread;
2016-06-13 22:55:30 +02:00
private static PacketContainer spawnChunk;
2017-02-23 13:42:13 +01:00
private static HashMap<UUID, String> preDisguiseTeam = new HashMap<UUID, String>();
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
static {
try {
2016-05-11 23:47:08 +02:00
Object server = ReflectionManager.getNmsMethod("MinecraftServer", "getServer").invoke(null);
Object world = ((List) server.getClass().getField("worlds").get(server)).get(0);
Object bedChunk = ReflectionManager.getNmsClass("Chunk").getConstructor(
ReflectionManager.getNmsClass("World"), int.class, int.class).newInstance(world, 0, 0);
2016-05-11 23:47:08 +02:00
Field cSection = bedChunk.getClass().getDeclaredField("sections");
cSection.setAccessible(true);
Object chunkSection = ReflectionManager.getNmsClass("ChunkSection").getConstructor(int.class,
boolean.class).newInstance(0, true);
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
Object block = ReflectionManager.getNmsClass("Block").getMethod("getById", int.class).invoke(null,
Material.BED_BLOCK.getId());
2016-05-11 23:47:08 +02:00
Method fromLegacyData = block.getClass().getMethod("fromLegacyData", int.class);
Method setType = chunkSection.getClass().getMethod("setType", int.class, int.class, int.class,
ReflectionManager.getNmsClass("IBlockData"));
Method setSky = chunkSection.getClass().getMethod("a", int.class, int.class, int.class, int.class);
Method setEmitted = chunkSection.getClass().getMethod("b", int.class, int.class, int.class, int.class);
for (BlockFace face : new BlockFace[]{BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH}) {
2016-05-11 23:47:08 +02:00
int x = 1 + face.getModX();
int z = 1 + face.getModZ();
setType.invoke(chunkSection, x, 0, z, fromLegacyData.invoke(block, face.ordinal()));
setSky.invoke(chunkSection, x, 0, z, 0);
setEmitted.invoke(chunkSection, x, 0, z, 0);
}
Object[] array = (Object[]) Array.newInstance(chunkSection.getClass(), 16);
array[0] = chunkSection;
cSection.set(bedChunk, array);
spawnChunk = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.MAP_CHUNK,
bedChunk, 65535).createPacket(bedChunk, 65535);
2016-06-05 15:31:34 +02:00
Field threadField = ReflectionManager.getNmsField("MinecraftServer", "primaryThread");
threadField.setAccessible(true);
mainThread = (Thread) threadField.get(ReflectionManager.getMinecraftServer());
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
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();
player.sendMessage(
ChatColor.RED + "Constructed a " + entityName + " disguise! Your reference is " + reference);
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");
}
}
2016-11-28 22:47:48 +01:00
public static boolean addClonedDisguise(String key, Disguise disguise) {
if (DisguiseConfig.getMaxClonedDisguises() > 0) {
if (clonedDisguises.containsKey(key)) {
2016-05-11 23:47:08 +02:00
clonedDisguises.remove(key);
} else if (DisguiseConfig.getMaxClonedDisguises() == clonedDisguises.size()) {
2016-05-11 23:47:08 +02:00
clonedDisguises.remove(clonedDisguises.keySet().iterator().next());
}
2016-11-28 22:47:48 +01:00
if (DisguiseConfig.getMaxClonedDisguises() > clonedDisguises.size()) {
2016-05-11 23:47:08 +02:00
clonedDisguises.put(key, disguise);
return true;
}
}
return false;
}
2016-11-28 22:47:48 +01:00
public static void addDisguise(UUID entityId, TargetedDisguise disguise) {
if (!getDisguises().containsKey(entityId)) {
2016-05-11 23:47:08 +02:00
getDisguises().put(entityId, new HashSet<TargetedDisguise>());
}
getDisguises().get(entityId).add(disguise);
checkConflicts(disguise, null);
2016-11-28 22:47:48 +01:00
if (disguise.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS && disguise.isModifyBoundingBox()) {
2016-05-11 23:47:08 +02:00
doBoundingBox(disguise);
}
}
2016-11-28 22:47:48 +01:00
public static void addFutureDisguise(final int entityId, final TargetedDisguise disguise) {
if (!futureDisguises.containsKey(entityId)) {
2016-05-11 23:47:08 +02:00
futureDisguises.put(entityId, new HashSet<TargetedDisguise>());
}
futureDisguises.get(entityId).add(disguise);
2016-11-28 22:47:48 +01:00
final BukkitRunnable runnable = new BukkitRunnable() {
2016-05-11 23:47:08 +02:00
@Override
2016-11-28 22:47:48 +01:00
public void run() {
if (futureDisguises.containsKey(entityId) && futureDisguises.get(entityId).contains(disguise)) {
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (entity.getEntityId() == entityId) {
2016-05-11 23:47:08 +02:00
UUID uniqueId = entity.getUniqueId();
2016-11-28 22:47:48 +01:00
for (TargetedDisguise disguise : futureDisguises.remove(entityId)) {
2016-05-11 23:47:08 +02:00
addDisguise(uniqueId, disguise);
}
return;
}
}
}
futureDisguises.get(entityId).remove(disguise);
2016-11-28 22:47:48 +01:00
if (futureDisguises.get(entityId).isEmpty()) {
2016-05-11 23:47:08 +02:00
futureDisguises.remove(entityId);
}
}
}
};
runnable.runTaskLater(libsDisguises, 20);
}
2016-11-28 22:47:48 +01:00
public static void addGameProfile(String string, WrappedGameProfile gameProfile) {
2016-05-11 23:47:08 +02:00
getGameProfiles().put(string, gameProfile);
getAddedByPlugins().add(string.toLowerCase());
}
/**
* If name isn't null. Make sure that the name doesn't see any other disguise. Else if name is null. Make sure that the
* observers in the disguise don't see any other disguise.
*/
2016-11-28 22:47:48 +01:00
public static void checkConflicts(TargetedDisguise disguise, String name) {
2016-05-11 23:47:08 +02:00
// If the disguise is being used.. Else we may accidentally undisguise something else
2016-11-28 22:47:48 +01:00
if (DisguiseAPI.isDisguiseInUse(disguise)) {
2016-05-11 23:47:08 +02:00
Iterator<TargetedDisguise> disguiseItel = getDisguises().get(disguise.getEntity().getUniqueId()).iterator();
// Iterate through the disguises
2016-11-28 22:47:48 +01:00
while (disguiseItel.hasNext()) {
2016-05-11 23:47:08 +02:00
TargetedDisguise d = disguiseItel.next();
// Make sure the disguise isn't the same thing
2016-11-28 22:47:48 +01:00
if (d != disguise) {
2016-05-11 23:47:08 +02:00
// If the loop'd disguise is hiding the disguise to everyone in its list
2016-11-28 22:47:48 +01:00
if (d.getDisguiseTarget() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// If player is a observer in the loop
2016-11-28 22:47:48 +01:00
if (disguise.getDisguiseTarget() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// If player is a observer in the disguise
// Remove them from the loop
2016-11-28 22:47:48 +01:00
if (name != null) {
2016-05-11 23:47:08 +02:00
d.removePlayer(name);
} else {
2016-11-28 22:47:48 +01:00
for (String playername : disguise.getObservers()) {
2016-05-11 23:47:08 +02:00
d.silentlyRemovePlayer(playername);
}
}
} else if (disguise.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// If player is not a observer in the loop
2016-11-28 22:47:48 +01:00
if (name != null) {
if (!disguise.getObservers().contains(name)) {
2016-05-11 23:47:08 +02:00
d.removePlayer(name);
}
} else {
2016-11-28 22:47:48 +01:00
for (String playername : new ArrayList<>(d.getObservers())) {
if (!disguise.getObservers().contains(playername)) {
2016-05-11 23:47:08 +02:00
d.silentlyRemovePlayer(playername);
}
}
}
}
} else if (d.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// Here you add it to the loop if they see the disguise
2016-11-28 22:47:48 +01:00
if (disguise.getDisguiseTarget() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// Everyone who is in the disguise needs to be added to the loop
2016-11-28 22:47:48 +01:00
if (name != null) {
2016-05-11 23:47:08 +02:00
d.addPlayer(name);
} else {
2016-11-28 22:47:48 +01:00
for (String playername : disguise.getObservers()) {
2016-05-11 23:47:08 +02:00
d.silentlyAddPlayer(playername);
}
}
} else if (disguise.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
// This here is a paradox.
// If fed a name. I can do this.
// But the rest of the time.. Its going to conflict.
// The below is debug output. Most people wouldn't care for it.
disguiseItel.remove();
d.removeDisguise();
}
}
}
}
}
}
/**
* Sends entity removal packets, as this disguise was removed
*/
2016-11-28 22:47:48 +01:00
public static void destroyEntity(TargetedDisguise disguise) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
2016-11-28 22:47:48 +01:00
try {
2016-05-11 23:47:08 +02:00
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
2016-06-12 16:22:56 +02:00
if (entityTrackerEntry == null)
return;
2016-05-11 23:47:08 +02:00
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
// If the tracker exists. Remove himself from his tracker
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
PacketContainer destroyPacket = new PacketContainer(Server.ENTITY_DESTROY);
2016-05-11 23:47:08 +02:00
destroyPacket.getIntegerArrays().write(0, new int[]{disguise.getEntity().getEntityId()});
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
for (Object p : trackedPlayers) {
2016-06-12 16:22:56 +02:00
Player player = (Player) ReflectionManager.getBukkitEntity(p);
2016-11-28 22:47:48 +01:00
if (player == disguise.getEntity() || disguise.canSee(player)) {
2016-06-12 16:22:56 +02:00
ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket);
2016-05-11 23:47:08 +02:00
}
}
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
2016-11-28 22:47:48 +01:00
public static void doBoundingBox(TargetedDisguise disguise) {
2016-05-11 23:47:08 +02:00
Entity entity = disguise.getEntity();
2016-11-28 22:47:48 +01:00
if (entity != null) {
if (isDisguiseInUse(disguise)) {
2016-05-11 23:47:08 +02:00
DisguiseValues disguiseValues = DisguiseValues.getDisguiseValues(disguise.getType());
FakeBoundingBox disguiseBox = disguiseValues.getAdultBox();
2016-11-28 22:47:48 +01:00
if (disguiseValues.getBabyBox() != null) {
if ((disguise.getWatcher() instanceof AgeableWatcher && ((AgeableWatcher) disguise.getWatcher()).isBaby()) || (disguise.getWatcher() instanceof ZombieWatcher && ((ZombieWatcher) disguise.getWatcher()).isBaby())) {
2016-05-11 23:47:08 +02:00
disguiseBox = disguiseValues.getBabyBox();
}
}
ReflectionManager.setBoundingBox(entity, disguiseBox);
} else {
2016-05-11 23:47:08 +02:00
DisguiseValues entityValues = DisguiseValues.getDisguiseValues(DisguiseType.getType(entity.getType()));
FakeBoundingBox entityBox = entityValues.getAdultBox();
2016-11-28 22:47:48 +01:00
if (entityValues.getBabyBox() != null) {
if ((entity instanceof Ageable && !((Ageable) entity).isAdult()) || (entity instanceof Zombie && ((Zombie) entity).isBaby())) {
2016-05-11 23:47:08 +02:00
entityBox = entityValues.getBabyBox();
}
}
ReflectionManager.setBoundingBox(entity, entityBox);
}
}
}
2016-11-28 22:47:48 +01:00
public static HashSet<String> getAddedByPlugins() {
2016-05-11 23:47:08 +02:00
return addedByPlugins;
}
2016-11-28 22:47:48 +01:00
public static int getChunkCord(int blockCord) {
2016-06-13 22:55:30 +02:00
int cord = (int) Math.floor(blockCord / 16D) - 17;
cord -= (cord % 8);
return cord;
}
2016-11-28 22:47:48 +01:00
public static PacketContainer[] getBedChunkPacket(Location newLoc, Location oldLoc) {
2016-05-11 23:47:08 +02:00
int i = 0;
2016-06-13 22:55:30 +02:00
PacketContainer[] packets = new PacketContainer[(newLoc != null ? 1 : 0) + (oldLoc != null ? 1 : 0)];
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
if (oldLoc != null) {
2016-06-13 22:55:30 +02:00
PacketContainer despawn = new PacketContainer(Server.UNLOAD_CHUNK);
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
StructureModifier<Object> modifier = despawn.getModifier();
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
modifier.write(0, getChunkCord(oldLoc.getBlockX()));
modifier.write(1, getChunkCord(oldLoc.getBlockZ()));
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
packets[i++] = despawn;
}
2016-05-12 22:27:53 +02:00
2016-11-28 22:47:48 +01:00
if (newLoc != null) {
2016-06-13 22:55:30 +02:00
PacketContainer spawn = spawnChunk.shallowClone();
2016-05-12 22:27:53 +02:00
2016-06-13 22:55:30 +02:00
StructureModifier<Object> modifier = spawn.getModifier();
2016-05-12 22:27:53 +02:00
2016-06-13 22:55:30 +02:00
modifier.write(0, getChunkCord(newLoc.getBlockX()));
modifier.write(1, getChunkCord(newLoc.getBlockZ()));
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
packets[i++] = spawn;
2016-05-11 23:47:08 +02:00
}
return packets;
}
public static PacketContainer[] getBedPackets(Location sleepingLocation, Location playerLocation,
PlayerDisguise disguise) {
2016-06-13 22:55:30 +02:00
int entity = disguise.getEntity().getEntityId();
PlayerWatcher watcher = disguise.getWatcher();
2016-05-11 23:47:08 +02:00
PacketContainer setBed = new PacketContainer(Server.BED);
2016-06-13 22:55:30 +02:00
int bX = (getChunkCord(playerLocation.getBlockX()) * 16) + 1 + watcher.getSleepingDirection().getModX();
int bZ = (getChunkCord(playerLocation.getBlockZ()) * 16) + 1 + watcher.getSleepingDirection().getModZ();
2016-05-11 23:47:08 +02:00
2016-06-13 22:55:30 +02:00
setBed.getIntegers().write(0, entity);
setBed.getBlockPositionModifier().write(0, new BlockPosition(bX, 0, bZ));
2016-05-11 23:47:08 +02:00
PacketContainer teleport = new PacketContainer(Server.ENTITY_TELEPORT);
StructureModifier<Double> doubles = teleport.getDoubles();
2016-06-13 22:55:30 +02:00
teleport.getIntegers().write(0, entity);
doubles.write(0, sleepingLocation.getX());
doubles.write(1, PacketsManager.getYModifier(disguise.getEntity(), disguise) + sleepingLocation.getY());
doubles.write(2, sleepingLocation.getZ());
2016-05-11 23:47:08 +02:00
return new PacketContainer[]{setBed, teleport};
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
public static Disguise getClonedDisguise(String key) {
if (clonedDisguises.containsKey(key)) {
2016-05-11 23:47:08 +02:00
return clonedDisguises.get(key).clone();
}
return null;
}
2016-11-28 22:47:48 +01:00
public static PacketContainer getDestroyPacket(int... ids) {
2016-05-11 23:47:08 +02:00
PacketContainer destroyPacket = new PacketContainer(Server.ENTITY_DESTROY);
destroyPacket.getIntegerArrays().write(0, ids);
return destroyPacket;
}
2016-11-28 22:47:48 +01:00
public static TargetedDisguise getDisguise(Player observer, Entity entity) {
2016-05-11 23:47:08 +02:00
UUID entityId = entity.getUniqueId();
2016-11-28 22:47:48 +01:00
if (futureDisguises.containsKey(entity.getEntityId())) {
for (TargetedDisguise disguise : futureDisguises.remove(entity.getEntityId())) {
2016-05-11 23:47:08 +02:00
addDisguise(entityId, disguise);
}
}
2016-11-28 22:47:48 +01:00
if (getDisguises().containsKey(entityId)) {
for (TargetedDisguise disguise : getDisguises().get(entityId)) {
if (disguise.canSee(observer)) {
2016-05-11 23:47:08 +02:00
return disguise;
}
}
}
return null;
}
2016-11-28 22:47:48 +01:00
public static HashMap<UUID, HashSet<TargetedDisguise>> getDisguises() {
2016-05-11 23:47:08 +02:00
return disguisesInUse;
}
2016-11-28 22:47:48 +01:00
public static TargetedDisguise[] getDisguises(UUID entityId) {
if (getDisguises().containsKey(entityId)) {
2016-05-11 23:47:08 +02:00
HashSet<TargetedDisguise> disguises = getDisguises().get(entityId);
return disguises.toArray(new TargetedDisguise[disguises.size()]);
}
return new TargetedDisguise[0];
}
2016-11-28 22:47:48 +01:00
public static HashMap<Integer, HashSet<TargetedDisguise>> getFutureDisguises() {
2016-05-11 23:47:08 +02:00
return futureDisguises;
}
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile getGameProfile(String playerName) {
2016-05-11 23:47:08 +02:00
return gameProfiles.get(playerName.toLowerCase());
}
2016-11-28 22:47:48 +01:00
public static HashMap<String, WrappedGameProfile> getGameProfiles() {
2016-05-11 23:47:08 +02:00
return gameProfiles;
}
2016-11-28 22:47:48 +01:00
public static TargetedDisguise getMainDisguise(UUID entityId) {
2016-05-11 23:47:08 +02:00
TargetedDisguise toReturn = null;
2016-11-28 22:47:48 +01:00
if (getDisguises().containsKey(entityId)) {
for (TargetedDisguise disguise : getDisguises().get(entityId)) {
if (disguise.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
2016-05-11 23:47:08 +02:00
return disguise;
}
toReturn = disguise;
}
}
return toReturn;
}
/**
* Get all EntityPlayers who have this entity in their Entity Tracker And they are in the targeted disguise.
*
* @param disguise
* @return
*/
2016-11-28 22:47:48 +01:00
public static List<Player> getPerverts(Disguise disguise) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (disguise.getEntity() == null)
throw new IllegalStateException(
"The entity for the disguisetype " + disguise.getType().name() + " is null!");
2016-05-11 23:47:08 +02:00
List<Player> players = new ArrayList<>();
2016-11-28 22:47:48 +01:00
try {
2016-05-11 23:47:08 +02:00
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
2016-11-28 22:47:48 +01:00
if (entityTrackerEntry != null) {
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-05-11 23:47:08 +02:00
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
2016-11-28 22:47:48 +01:00
for (Object p : trackedPlayers) {
2016-05-11 23:47:08 +02:00
Player player = (Player) ReflectionManager.getBukkitEntity(p);
2016-06-12 16:22:56 +02:00
2016-11-28 22:47:48 +01:00
if (((TargetedDisguise) disguise).canSee(player)) {
2016-05-11 23:47:08 +02:00
players.add(player);
}
}
}
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
return players;
}
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile getProfileFromMojang(final PlayerDisguise disguise) {
2016-05-11 23:47:08 +02:00
final String nameToFetch = disguise.getSkin() != null ? disguise.getSkin() : disguise.getName();
2016-06-14 11:08:53 +02:00
2016-05-11 23:47:08 +02:00
final boolean remove = getAddedByPlugins().contains(nameToFetch.toLowerCase());
2016-11-28 22:47:48 +01:00
return getProfileFromMojang(nameToFetch, new LibsProfileLookup() {
2016-05-11 23:47:08 +02:00
@Override
2016-11-28 22:47:48 +01:00
public void onLookup(WrappedGameProfile gameProfile) {
if (remove) {
2016-05-11 23:47:08 +02:00
getAddedByPlugins().remove(nameToFetch.toLowerCase());
}
if (DisguiseAPI.isDisguiseInUse(disguise) && (!gameProfile.getName().equals(
disguise.getSkin() != null ? disguise.getSkin() : disguise.getName()) || !gameProfile.getProperties().isEmpty())) {
2016-05-11 23:47:08 +02:00
disguise.setGameProfile(gameProfile);
DisguiseUtilities.refreshTrackers(disguise);
}
}
2016-06-14 11:08:53 +02:00
}, LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true));
2016-05-11 23:47:08 +02:00
}
/**
* Pass in a set, check if it's a hashset. If it's not, return false. If you pass in something else, you failed.
*
* @param obj
* @return
*/
2016-11-28 22:47:48 +01:00
private static boolean isHashSet(Object obj) {
2016-05-11 23:47:08 +02:00
if (obj instanceof HashSet)
return true; // It's Spigot/Bukkit
if (obj instanceof Set)
return false; // It's PaperSpigot/SportsBukkit
throw new IllegalArgumentException("Object passed was not either a hashset or set!");
}
/**
* Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup
* using schedulers. The runnable is run once the GameProfile has been successfully dealt with
*/
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile getProfileFromMojang(String playerName, LibsProfileLookup runnableIfCantReturn) {
2016-06-14 11:08:53 +02:00
return getProfileFromMojang(playerName, (Object) runnableIfCantReturn, true);
2016-05-11 23:47:08 +02:00
}
2016-06-14 11:08:53 +02:00
/**
* Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup
* using schedulers. The runnable is run once the GameProfile has been successfully dealt with
*/
public static WrappedGameProfile getProfileFromMojang(String playerName, LibsProfileLookup runnableIfCantReturn,
2016-11-28 22:47:48 +01:00
boolean contactMojang) {
2016-06-14 11:08:53 +02:00
return getProfileFromMojang(playerName, (Object) runnableIfCantReturn, contactMojang);
}
private static WrappedGameProfile getProfileFromMojang(final String origName, final Object runnable,
boolean contactMojang) {
2016-05-11 23:47:08 +02:00
final String playerName = origName.toLowerCase();
2016-11-28 22:47:48 +01:00
if (gameProfiles.containsKey(playerName)) {
if (gameProfiles.get(playerName) != null) {
2016-05-11 23:47:08 +02:00
return gameProfiles.get(playerName);
}
} else if (Pattern.matches("([A-Za-z0-9_]){1,16}", origName)) {
2016-05-11 23:47:08 +02:00
getAddedByPlugins().add(playerName);
Player player = Bukkit.getPlayerExact(playerName);
2016-11-28 22:47:48 +01:00
if (player != null) {
2016-05-11 23:47:08 +02:00
WrappedGameProfile gameProfile = ReflectionManager.getGameProfile(player);
2016-11-28 22:47:48 +01:00
if (!gameProfile.getProperties().isEmpty()) {
2016-05-11 23:47:08 +02:00
gameProfiles.put(playerName, gameProfile);
2016-06-14 11:08:53 +02:00
2016-05-11 23:47:08 +02:00
return gameProfile;
}
}
if (runnable != null && (contactMojang || gameProfiles.containsKey(playerName))) {
if (!runnables.containsKey(playerName)) {
runnables.put(playerName, new ArrayList<>());
}
runnables.get(playerName).add(runnable);
}
2016-11-28 22:47:48 +01:00
if (contactMojang) {
// Add null so that if this is called again. I already know I'm doing something about it
gameProfiles.put(playerName, null);
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().runTaskAsynchronously(libsDisguises, new Runnable() {
@Override
2016-11-28 22:47:48 +01:00
public void run() {
try {
final WrappedGameProfile gameProfile = lookupGameProfile(origName);
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().runTask(libsDisguises, new Runnable() {
@Override
2016-11-28 22:47:48 +01:00
public void run() {
if (gameProfile.getProperties().isEmpty()) {
return;
}
2016-06-14 11:08:53 +02:00
2016-11-28 22:47:48 +01:00
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
gameProfiles.put(playerName, gameProfile);
}
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
if (runnables.containsKey(playerName)) {
for (Object obj : runnables.remove(playerName)) {
if (obj instanceof Runnable) {
((Runnable) obj).run();
} else if (obj instanceof LibsProfileLookup) {
((LibsProfileLookup) obj).onLookup(gameProfile);
}
2016-05-11 23:47:08 +02:00
}
}
}
});
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
catch (Exception e) {
if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) {
gameProfiles.remove(playerName);
getAddedByPlugins().remove(playerName);
}
2016-05-11 23:47:08 +02:00
System.out.print(
"[LibsDisguises] Error when fetching " + playerName + "'s uuid from mojang: " + e.getMessage());
}
2016-05-11 23:47:08 +02:00
}
});
return null;
}
2016-05-11 23:47:08 +02:00
}
return ReflectionManager.getGameProfile(null, origName);
2016-05-11 23:47:08 +02:00
}
/**
* Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup
* using schedulers. The runnable is run once the GameProfile has been successfully dealt with
*/
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile getProfileFromMojang(String playerName, Runnable runnableIfCantReturn) {
2016-06-14 11:08:53 +02:00
return getProfileFromMojang(playerName, (Object) runnableIfCantReturn, true);
}
/**
* Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup
* using schedulers. The runnable is run once the GameProfile has been successfully dealt with
*/
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile getProfileFromMojang(String playerName, Runnable runnableIfCantReturn,
boolean contactMojang) {
2016-06-14 11:08:53 +02:00
return getProfileFromMojang(playerName, (Object) runnableIfCantReturn, contactMojang);
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
public static HashSet<UUID> getSelfDisguised() {
2016-05-11 23:47:08 +02:00
return selfDisguised;
}
2016-11-28 22:47:48 +01:00
public static boolean hasGameProfile(String playerName) {
2016-05-11 23:47:08 +02:00
return getGameProfile(playerName) != null;
}
2016-11-28 22:47:48 +01:00
public static void init(LibsDisguises disguises) {
2016-05-11 23:47:08 +02:00
libsDisguises = disguises;
}
2016-11-28 22:47:48 +01:00
public static boolean isDisguiseInUse(Disguise disguise) {
return disguise.getEntity() != null && getDisguises().containsKey(
disguise.getEntity().getUniqueId()) && getDisguises().get(disguise.getEntity().getUniqueId()).contains(
disguise);
2016-05-11 23:47:08 +02:00
}
/**
* This is called on a thread as it is thread blocking
*/
2016-11-28 22:47:48 +01:00
public static WrappedGameProfile lookupGameProfile(String playerName) {
2016-05-11 23:47:08 +02:00
return ReflectionManager.getSkullBlob(ReflectionManager.grabProfileAddUUID(playerName));
}
/**
* Resends the entity to this specific player
*/
2016-11-28 22:47:48 +01:00
public static void refreshTracker(final TargetedDisguise disguise, String player) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (disguise.getEntity() == null || !disguise.getEntity().isValid())
return;
2016-11-28 22:47:48 +01:00
try {
PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId());
if (disguise.isDisguiseInUse() && disguise.getEntity() instanceof Player && disguise.getEntity().getName().equalsIgnoreCase(
player)) {
removeSelfDisguise((Player) disguise.getEntity());
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
if (disguise.isSelfDisguiseVisible()) {
selfDisguised.add(disguise.getEntity().getUniqueId());
}
2016-05-11 23:47:08 +02:00
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) disguise.getEntity(), destroyPacket);
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
@Override
2016-11-28 22:47:48 +01:00
public void run() {
try {
DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
}
}
}, 2);
} else {
final Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
2016-06-12 16:22:56 +02:00
if (entityTrackerEntry == null)
return;
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-06-12 16:22:56 +02:00
Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
ReflectionManager.getNmsClass("EntityPlayer"));
final Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
2016-11-28 22:47:48 +01:00
for (final Object p : trackedPlayers) {
2016-06-12 16:22:56 +02:00
Player pl = (Player) ReflectionManager.getBukkitEntity(p);
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
if (!player.equalsIgnoreCase((pl).getName()))
continue;
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
clear.invoke(entityTrackerEntry, p);
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
ProtocolLibrary.getProtocolManager().sendServerPacket(pl, destroyPacket);
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
2016-05-11 23:47:08 +02:00
2016-06-12 16:22:56 +02:00
@Override
2016-11-28 22:47:48 +01:00
public void run() {
try {
2016-06-12 16:22:56 +02:00
updatePlayer.invoke(entityTrackerEntry, p);
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-06-12 16:22:56 +02:00
}
2016-05-11 23:47:08 +02:00
}
2016-06-12 16:22:56 +02:00
}, 2);
break;
2016-05-11 23:47:08 +02:00
}
}
}
catch (
Exception ex)
{
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
/**
* A convenience method for me to refresh trackers in other plugins
*/
2016-11-28 22:47:48 +01:00
public static void refreshTrackers(Entity entity) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
2016-11-28 22:47:48 +01:00
if (entity.isValid()) {
try {
2016-05-11 23:47:08 +02:00
PacketContainer destroyPacket = getDestroyPacket(entity.getEntityId());
final Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(entity);
2016-11-28 22:47:48 +01:00
if (entityTrackerEntry != null) {
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry",
"trackedPlayers").get(entityTrackerEntry);
2016-05-11 23:47:08 +02:00
Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
ReflectionManager.getNmsClass("EntityPlayer"));
final Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
2016-11-28 22:47:48 +01:00
for (final Object p : trackedPlayers) {
2016-05-11 23:47:08 +02:00
Player player = (Player) ReflectionManager.getBukkitEntity(p);
2016-11-28 22:47:48 +01:00
if (player != entity) {
2016-05-11 23:47:08 +02:00
clear.invoke(entityTrackerEntry, p);
ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket);
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
2016-05-11 23:47:08 +02:00
@Override
2016-11-28 22:47:48 +01:00
public void run() {
try {
2016-05-11 23:47:08 +02:00
updatePlayer.invoke(entityTrackerEntry, p);
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
}, 2);
}
}
}
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
}
/**
* Resends the entity to all the watching players, which is where the magic begins
*/
2016-11-28 22:47:48 +01:00
public static void refreshTrackers(final TargetedDisguise disguise) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (!disguise.getEntity().isValid()) {
return;
}
2016-11-28 22:47:48 +01:00
PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId());
2016-05-11 23:47:08 +02:00
try {
if (selfDisguised.contains(disguise.getEntity().getUniqueId()) && disguise.isDisguiseInUse()) {
removeSelfDisguise((Player) disguise.getEntity());
2016-05-11 23:47:08 +02:00
selfDisguised.add(disguise.getEntity().getUniqueId());
2016-05-11 23:47:08 +02:00
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) disguise.getEntity(), destroyPacket);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
@Override
public void run() {
try {
DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
2016-05-11 23:47:08 +02:00
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}, 2);
}
2016-05-11 23:47:08 +02:00
final Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
2016-05-11 23:47:08 +02:00
if (entityTrackerEntry != null) {
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-05-11 23:47:08 +02:00
final Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
ReflectionManager.getNmsClass("EntityPlayer"));
2016-05-11 23:47:08 +02:00
final Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
2016-05-11 23:47:08 +02:00
trackedPlayers = (Set) new HashSet(trackedPlayers).clone();
2016-05-11 23:47:08 +02:00
for (final Object p : trackedPlayers) {
Player player = (Player) ReflectionManager.getBukkitEntity(p);
2016-05-11 23:47:08 +02:00
if (disguise.getEntity() != player && disguise.canSee(player)) {
clear.invoke(entityTrackerEntry, p);
2016-05-11 23:47:08 +02:00
ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket);
2016-05-11 23:47:08 +02:00
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
@Override
public void run() {
try {
updatePlayer.invoke(entityTrackerEntry, p);
2016-05-11 23:47:08 +02:00
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}, 2);
2016-05-11 23:47:08 +02:00
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
2016-11-28 22:47:48 +01:00
public static boolean removeDisguise(TargetedDisguise disguise) {
2016-05-11 23:47:08 +02:00
UUID entityId = disguise.getEntity().getUniqueId();
2016-11-28 22:47:48 +01:00
if (getDisguises().containsKey(entityId) && getDisguises().get(entityId).remove(disguise)) {
if (getDisguises().get(entityId).isEmpty()) {
2016-05-11 23:47:08 +02:00
getDisguises().remove(entityId);
}
2016-11-28 22:47:48 +01:00
if (disguise.getDisguiseTarget() == TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS && disguise.isModifyBoundingBox()) {
2016-05-11 23:47:08 +02:00
doBoundingBox(disguise);
}
return true;
}
return false;
}
2016-11-28 22:47:48 +01:00
public static void removeGameProfile(String string) {
2016-05-11 23:47:08 +02:00
gameProfiles.remove(string.toLowerCase());
}
2016-11-28 22:47:48 +01:00
public static void removeSelfDisguise(Player player) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
2016-11-28 22:47:48 +01:00
if (!selfDisguised.contains(player.getUniqueId())) {
return;
}
2016-05-11 23:47:08 +02:00
// Send a packet to destroy the fake entity
PacketContainer packet = getDestroyPacket(DisguiseAPI.getSelfDisguiseId());
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
}
2016-05-11 23:47:08 +02:00
2017-02-23 13:42:13 +01:00
String originalTeam = preDisguiseTeam.remove(player.getUniqueId());
2016-12-21 04:25:28 +01:00
if (DisguiseConfig.getPushingOption() != DisguisePushing.IGNORE_SCOREBOARD) {
2017-02-23 13:42:13 +01:00
// Code to stop player pushing
2016-12-15 21:23:44 +01:00
Scoreboard scoreboard = player.getScoreboard();
2017-02-23 13:42:13 +01:00
Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
2016-12-21 04:25:28 +01:00
Team ldTeam = scoreboard.getEntryTeam(player.getName());
if (ldTeam != null) {
2017-03-28 21:24:21 +02:00
if (!ldTeam.getName().equals("LD Pushing") && !ldTeam.getName().endsWith("_LDP")) {
2017-02-23 13:42:13 +01:00
// Its not a team assigned by me
2016-12-21 04:25:28 +01:00
ldTeam = null;
2017-02-23 13:42:13 +01:00
}
2016-12-21 04:25:28 +01:00
}
if (team != null) {
team.addEntry(player.getName());
} else if (ldTeam != null) {
2016-12-21 04:25:28 +01:00
ldTeam.removeEntry(player.getName());
}
2016-05-11 23:47:08 +02:00
2016-12-21 04:25:28 +01:00
if (ldTeam != null && ldTeam.getEntries().isEmpty()) {
ldTeam.unregister();
2016-12-15 21:23:44 +01:00
}
}
2016-05-11 23:47:08 +02:00
// player.spigot().setCollidesWithEntities(true);
// Finish up
// Remove the fake entity ID from the disguise bin
selfDisguised.remove(player.getUniqueId());
// Get the entity tracker
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
try {
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
if (entityTrackerEntry != null) {
Object trackedPlayersObj = ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-05-11 23:47:08 +02:00
// If the tracker exists. Remove himself from his tracker
2016-11-28 22:47:48 +01:00
if (isHashSet(trackedPlayersObj)) {
((Set<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry)).remove(ReflectionManager.getNmsEntity(player));
} else {
((Map<Object, Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayerMap").get(
entityTrackerEntry)).remove(ReflectionManager.getNmsEntity(player));
}
2016-05-11 23:47:08 +02:00
}
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
}
2016-05-11 23:47:08 +02:00
// Resend entity metadata else he will be invisible to himself until its resent
2016-11-28 22:47:48 +01:00
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player,
ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.ENTITY_METADATA,
player.getEntityId(), WrappedDataWatcher.getEntityWatcher(player), true).createPacket(
player.getEntityId(), WrappedDataWatcher.getEntityWatcher(player), true));
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
player.updateInventory();
2016-05-11 23:47:08 +02:00
}
/**
* Sends the self disguise to the player
*/
2016-11-28 22:47:48 +01:00
public static void sendSelfDisguise(final Player player, final TargetedDisguise disguise) {
2016-06-05 15:31:34 +02:00
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
2016-11-28 22:47:48 +01:00
try {
if (!disguise.isDisguiseInUse() || !player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible() || !disguise.canSee(
player)) {
2016-05-11 23:47:08 +02:00
return;
}
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
2016-11-28 22:47:48 +01:00
if (entityTrackerEntry == null) {
2016-05-11 23:47:08 +02:00
// A check incase the tracker is null.
// If it is, then this method will be run again in one tick. Which is when it should be constructed.
// Else its going to run in a infinite loop hue hue hue..
// At least until this disguise is discarded
2016-11-28 22:47:48 +01:00
Bukkit.getScheduler().runTask(libsDisguises, new Runnable() {
2016-05-11 23:47:08 +02:00
@Override
2016-11-28 22:47:48 +01:00
public void run() {
if (DisguiseAPI.getDisguise(player, player) == disguise) {
2016-05-11 23:47:08 +02:00
sendSelfDisguise(player, disguise);
}
}
});
return;
}
2016-12-21 04:25:28 +01:00
DisguisePushing pOption = DisguiseConfig.getPushingOption();
if (pOption != DisguisePushing.IGNORE_SCOREBOARD) {
2016-12-15 21:23:44 +01:00
// Code to stop player pushing
Scoreboard scoreboard = player.getScoreboard();
2016-12-21 04:25:28 +01:00
Team prevTeam = scoreboard.getEntryTeam(player.getName());
2017-02-23 13:42:13 +01:00
Team ldTeam = null;
2017-03-29 14:34:42 +02:00
String ldTeamName = "LD Pushing";
2017-02-23 13:42:13 +01:00
// If the player is in a team already
2017-03-29 14:34:42 +02:00
if (prevTeam != null && !prevTeam.getName().equals("LD Pushing")) {
2017-02-23 13:42:13 +01:00
// If we're creating a scoreboard
if (pOption == DisguisePushing.CREATE_SCOREBOARD) {
// Remember his old team so we can give him it back later
preDisguiseTeam.put(player.getUniqueId(), prevTeam.getName());
} else {
2017-02-23 13:42:13 +01:00
// We're modifying the scoreboard
ldTeam = prevTeam;
}
2017-03-29 14:34:42 +02:00
} else {
prevTeam = null;
2016-12-21 04:25:28 +01:00
}
2017-02-23 13:42:13 +01:00
// If we are creating a new scoreboard because the current one must not be modified
if (pOption == DisguisePushing.CREATE_SCOREBOARD) {
2017-02-23 13:42:13 +01:00
// If they have a team, we'll reuse that name. Otherwise go for another name
ldTeamName = (prevTeam == null ? "No Team" : prevTeam.getName());
2017-02-23 13:42:13 +01:00
// Give the teamname a custom name
ldTeamName = ldTeamName.substring(0, Math.min(12, ldTeamName.length())) + "_LDP";
2016-12-21 04:25:28 +01:00
}
2017-03-26 15:06:17 +02:00
if (ldTeam == null && (ldTeam = scoreboard.getTeam(ldTeamName)) == null) {
2017-02-23 13:42:13 +01:00
ldTeam = scoreboard.registerNewTeam(ldTeamName);
2016-12-15 21:23:44 +01:00
}
2017-02-23 13:42:13 +01:00
if (ldTeam.getOption(Option.COLLISION_RULE) != OptionStatus.NEVER) {
ldTeam.setOption(Option.COLLISION_RULE, OptionStatus.NEVER);
2016-12-21 04:25:28 +01:00
}
2017-02-23 13:42:13 +01:00
if (ldTeam.canSeeFriendlyInvisibles()) {
ldTeam.setCanSeeFriendlyInvisibles(false);
2016-12-15 21:23:44 +01:00
}
2017-02-23 13:42:13 +01:00
if (!ldTeam.hasEntry(player.getName()))
ldTeam.addEntry(player.getName());
2016-12-21 04:25:28 +01:00
if (pOption == DisguisePushing.CREATE_SCOREBOARD && prevTeam != null) {
2017-02-23 13:42:13 +01:00
ldTeam.setAllowFriendlyFire(prevTeam.allowFriendlyFire());
ldTeam.setCanSeeFriendlyInvisibles(prevTeam.canSeeFriendlyInvisibles());
ldTeam.setDisplayName(prevTeam.getDisplayName());
ldTeam.setPrefix(prevTeam.getPrefix());
ldTeam.setSuffix(prevTeam.getSuffix());
2016-12-21 04:25:28 +01:00
for (Option option : Team.Option.values()) {
2017-02-23 13:42:13 +01:00
ldTeam.setOption(option, prevTeam.getOption(option));
2016-12-21 04:25:28 +01:00
}
}
2016-12-15 21:23:44 +01:00
}
2016-05-11 23:47:08 +02:00
// Add himself to his own entity tracker
Object trackedPlayersObj = ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry);
2016-05-11 23:47:08 +02:00
// Check for code differences in PaperSpigot vs Spigot
2016-11-28 22:47:48 +01:00
if (isHashSet(trackedPlayersObj)) {
((Set<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(
entityTrackerEntry)).add(ReflectionManager.getNmsEntity(player));
} else {
((Map<Object, Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayerMap").get(
entityTrackerEntry)).put(ReflectionManager.getNmsEntity(player), true);
2016-05-11 23:47:08 +02:00
}
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
// Send the player a packet with himself being spawned
manager.sendServerPacket(player,
manager.createPacketConstructor(Server.NAMED_ENTITY_SPAWN, player).createPacket(player));
WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(player);
sendSelfPacket(player,
manager.createPacketConstructor(Server.ENTITY_METADATA, player.getEntityId(), dataWatcher,
true).createPacket(player.getEntityId(), dataWatcher, true));
2016-05-11 23:47:08 +02:00
boolean isMoving = false;
2016-11-28 22:47:48 +01:00
try {
2016-05-11 23:47:08 +02:00
Field field = ReflectionManager.getNmsClass("EntityTrackerEntry").getDeclaredField("isMoving");
field.setAccessible(true);
isMoving = field.getBoolean(entityTrackerEntry);
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
// Send the velocity packets
2016-11-28 22:47:48 +01:00
if (isMoving) {
2016-05-11 23:47:08 +02:00
Vector velocity = player.getVelocity();
sendSelfPacket(player,
manager.createPacketConstructor(Server.ENTITY_VELOCITY, player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()).createPacket(player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()));
2016-05-11 23:47:08 +02:00
}
// Why the hell would he even need this. Meh.
2016-11-28 22:47:48 +01:00
if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) {
sendSelfPacket(player, manager.createPacketConstructor(Server.ATTACH_ENTITY, 0, player,
player.getVehicle()).createPacket(0, player, player.getVehicle()));
} else if (player.getPassenger() != null && player.getEntityId() > player.getPassenger().getEntityId()) {
sendSelfPacket(player, manager.createPacketConstructor(Server.ATTACH_ENTITY, 0, player.getPassenger(),
player).createPacket(0, player.getPassenger(), player));
2016-05-11 23:47:08 +02:00
}
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(player.getInventory().getHelmet())));
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.CHEST),
ReflectionManager.getNmsItem(player.getInventory().getChestplate())));
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.LEGS),
ReflectionManager.getNmsItem(player.getInventory().getLeggings())));
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.FEET),
ReflectionManager.getNmsItem(player.getInventory().getBoots())));
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.HAND),
ReflectionManager.getNmsItem(player.getInventory().getItemInMainHand())));
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EQUIPMENT, 0,
ReflectionManager.createEnumItemSlot(EquipmentSlot.HEAD),
ReflectionManager.getNmsItem(new ItemStack(Material.STONE))).createPacket(player.getEntityId(),
ReflectionManager.createEnumItemSlot(EquipmentSlot.OFF_HAND),
ReflectionManager.getNmsItem(player.getInventory().getItemInOffHand())));
2016-05-11 23:47:08 +02:00
Location loc = player.getLocation();
// If the disguised is sleeping for w/e reason
2016-11-28 22:47:48 +01:00
if (player.isSleeping()) {
sendSelfPacket(player, manager.createPacketConstructor(Server.BED, player,
ReflectionManager.getBlockPosition(0, 0, 0)).createPacket(player,
ReflectionManager.getBlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())));
2016-05-11 23:47:08 +02:00
}
// Resend any active potion effects
2016-11-28 22:47:48 +01:00
for (PotionEffect potionEffect : player.getActivePotionEffects()) {
2016-05-11 23:47:08 +02:00
Object mobEffect = ReflectionManager.createMobEffect(potionEffect);
sendSelfPacket(player, manager.createPacketConstructor(Server.ENTITY_EFFECT, player.getEntityId(),
mobEffect).createPacket(player.getEntityId(), mobEffect));
2016-05-11 23:47:08 +02:00
}
}
2016-11-28 22:47:48 +01:00
catch (Exception ex) {
ex.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
2016-11-28 22:47:48 +01:00
public static LibsDisguises getPlugin() {
return libsDisguises;
}
2016-05-11 23:47:08 +02:00
/**
* Method to send a packet to the self disguise, translate his entity ID to the fake id.
*/
2016-11-28 22:47:48 +01:00
private static void sendSelfPacket(final Player player, final PacketContainer packet) {
final Disguise disguise = DisguiseAPI.getDisguise(player, player);
2016-05-11 23:47:08 +02:00
// If disguised.
2016-11-28 22:47:48 +01:00
if (disguise == null) {
return;
}
2016-05-11 23:47:08 +02:00
LibsPackets transformed = PacketsManager.transformPacket(packet, disguise, player, player);
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
try {
if (transformed.isUnhandled())
transformed.addPacket(packet);
transformed.setPacketType(packet.getType());
2016-05-11 23:47:08 +02:00
2016-11-28 22:47:48 +01:00
for (PacketContainer p : transformed.getPackets()) {
2016-05-11 23:47:08 +02:00
p = p.deepClone();
p.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
ProtocolLibrary.getProtocolManager().sendServerPacket(player, p, false);
}
transformed.sendDelayed(player);
2016-05-11 23:47:08 +02:00
}
2016-11-28 22:47:48 +01:00
catch (InvocationTargetException e) {
e.printStackTrace();
2016-05-11 23:47:08 +02:00
}
}
/**
* Setup it so he can see himself when disguised
*
* @param disguise
*/
2016-11-28 22:47:48 +01:00
public static void setupFakeDisguise(final Disguise disguise) {
2016-05-11 23:47:08 +02:00
Entity e = disguise.getEntity();
// If the disguises entity is null, or the disguised entity isn't a player return
if (e == null || !(e instanceof Player) || !getDisguises().containsKey(e.getUniqueId()) || !getDisguises().get(
e.getUniqueId()).contains(disguise)) {
2016-05-11 23:47:08 +02:00
return;
}
Player player = (Player) e;
// Check if he can even see this..
2016-11-28 22:47:48 +01:00
if (!((TargetedDisguise) disguise).canSee(player)) {
2016-05-11 23:47:08 +02:00
return;
}
// Remove the old disguise, else we have weird disguises around the place
DisguiseUtilities.removeSelfDisguise(player);
// If the disguised player can't see himself. Return
if (!disguise.isSelfDisguiseVisible() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null) {
2016-05-11 23:47:08 +02:00
return;
}
// player.spigot().setCollidesWithEntities(false);
// Finish up
selfDisguised.add(player.getUniqueId());
sendSelfDisguise(player, (TargetedDisguise) disguise);
2016-11-28 22:47:48 +01:00
if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) {
if (PacketsManager.isInventoryListenerEnabled()) {
2016-05-11 23:47:08 +02:00
player.updateInventory();
}
}
}
}