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

2197 lines
95 KiB
Java
Raw Normal View History

package me.libraryaddict.disguise.utilities;
2016-05-09 17:28:38 +02:00
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.bukkit.Art;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedAttribute;
import com.comphenix.protocol.wrappers.WrappedAttribute.Builder;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
2016-05-09 17:28:38 +02:00
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher;
import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType;
2016-05-09 17:28:38 +02:00
public class PacketsManager
{
2013-12-16 20:03:59 +01:00
/**
* This is a fix for the stupidity that is
* "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard"
2013-12-16 20:03:59 +01:00
*/
private static boolean cancelSound;
private static PacketListener clientInteractEntityListener;
private static PacketListener inventoryListener;
private static boolean inventoryModifierEnabled;
private static LibsDisguises libsDisguises;
private static PacketListener mainListener;
private static PacketListener soundsListener;
private static boolean soundsListenerEnabled;
private static PacketListener viewDisguisesListener;
private static boolean viewDisguisesListenerEnabled;
2016-05-09 17:28:38 +02:00
public static void addPacketListeners()
{
// Add a client listener to cancel them interacting with uninteractable disguised entitys.
// You ain't supposed to be allowed to 'interact' with a item that cannot be clicked.
// Because it kicks you for hacking.
2016-05-09 17:28:38 +02:00
2014-05-23 00:30:56 +02:00
clientInteractEntityListener = new PacketAdapter(libsDisguises, ListenerPriority.NORMAL,
2016-05-09 17:28:38 +02:00
PacketType.Play.Client.USE_ENTITY)
{
@Override
2016-05-09 17:28:38 +02:00
public void onPacketReceiving(PacketEvent event)
{
if (event.isCancelled())
return;
2016-05-09 17:28:38 +02:00
try
{
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return;
2016-05-09 17:28:38 +02:00
Player observer = event.getPlayer();
2016-05-09 17:28:38 +02:00
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
2016-05-09 17:28:38 +02:00
Entity entity = entityModifer.read(0);
2016-05-09 17:28:38 +02:00
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow
2016-05-09 17:28:38 +02:00
|| entity == observer)
{
event.setCancelled(true);
}
2016-05-09 17:28:38 +02:00
ItemStack item = observer.getItemInHand();
2016-05-09 17:28:38 +02:00
if (item != null && item.getType() == Material.INK_SACK)
{
Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
2016-05-09 17:28:38 +02:00
if (disguise != null
2016-05-09 17:28:38 +02:00
&& (disguise.getType() == DisguiseType.SHEEP || disguise.getType() == DisguiseType.WOLF))
{
AnimalColor color = AnimalColor.getColor(item.getDurability());
2016-05-09 17:28:38 +02:00
if (disguise.getType() == DisguiseType.SHEEP)
{
SheepWatcher watcher = (SheepWatcher) disguise.getWatcher();
2016-05-09 17:28:38 +02:00
watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor());
2016-05-09 17:28:38 +02:00
}
else
{
WolfWatcher watcher = (WolfWatcher) disguise.getWatcher();
2016-05-09 17:28:38 +02:00
watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor());
}
}
}
2016-05-09 17:28:38 +02:00
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
2016-05-09 17:28:38 +02:00
ProtocolLibrary.getProtocolManager().addPacketListener(clientInteractEntityListener);
2016-05-09 17:28:38 +02:00
// Now I call this and the main listener is registered!
setupMainPacketsListener();
}
/**
* Construct the packets I need to spawn in the disguise
*/
2016-05-09 17:28:38 +02:00
public static PacketContainer[][] constructSpawnPackets(final Player player, Disguise disguise, Entity disguisedEntity)
{
if (disguise.getEntity() == null)
disguise.setEntity(disguisedEntity);
2016-05-09 17:28:38 +02:00
ArrayList<PacketContainer> packets = new ArrayList<>();
// This sends the armor packets so that the player isn't naked.
2014-05-23 00:30:56 +02:00
// Please note it only sends the packets that wouldn't be sent normally
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isEquipmentPacketsEnabled())
{
for (EquipmentSlot slot : EquipmentSlot.values())
{
ItemStack itemstack = disguise.getWatcher().getItemStack(slot);
2016-05-09 17:28:38 +02:00
if (itemstack != null && itemstack.getTypeId() != 0)
{
2014-05-23 00:33:50 +02:00
ItemStack item = null;
2016-05-09 17:28:38 +02:00
if (disguisedEntity instanceof LivingEntity)
{
item = ReflectionManager.getEquipment(slot, disguisedEntity);
2014-05-23 00:33:50 +02:00
}
2016-05-09 17:28:38 +02:00
if (item == null || item.getType() == Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.ENTITY_EQUIPMENT);
2016-05-09 17:28:38 +02:00
2014-05-23 00:33:50 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
2014-05-23 00:33:50 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, ReflectionManager.createEnumItemSlot(slot));
2014-05-23 00:33:50 +02:00
mods.write(2, ReflectionManager.getNmsItem(itemstack));
2016-05-09 17:28:38 +02:00
2014-05-23 00:33:50 +02:00
packets.add(packet);
}
}
}
}
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isMiscDisguisesForLivingEnabled())
{
if (disguise.getWatcher() instanceof LivingWatcher)
{
PacketContainer packet = new PacketContainer(Server.UPDATE_ATTRIBUTES);
2016-05-09 17:28:38 +02:00
List<WrappedAttribute> attributes = new ArrayList<WrappedAttribute>();
Builder builder = WrappedAttribute.newBuilder().attributeKey("generic.maxHealth");
2016-05-09 17:28:38 +02:00
if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet())
{
builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth());
2016-05-09 17:28:38 +02:00
}
else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity() && disguisedEntity instanceof Damageable)
{
builder.baseValue(((Damageable) disguisedEntity).getMaxHealth());
2016-05-09 17:28:38 +02:00
}
else
{
builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth());
}
2016-05-09 17:28:38 +02:00
builder.packet(packet);
2016-05-09 17:28:38 +02:00
attributes.add(builder.build());
2016-05-09 17:28:38 +02:00
packet.getIntegers().write(0, disguisedEntity.getEntityId());
packet.getAttributeCollectionModifier().write(0, attributes);
2016-05-09 17:28:38 +02:00
packets.add(packet);
}
}
2016-05-09 17:28:38 +02:00
PacketContainer[] spawnPackets = new PacketContainer[2 + packets.size()];
PacketContainer[] delayedPackets = new PacketContainer[0];
2016-05-09 17:28:38 +02:00
for (int i = 0; i < packets.size(); i++)
{
spawnPackets[i + 2] = packets.get(i);
}
2016-05-09 17:28:38 +02:00
Location loc = disguisedEntity.getLocation().clone().add(0, getYModifier(disguisedEntity, disguise), 0);
2016-05-09 17:28:38 +02:00
byte yaw = (byte) (int) (loc.getYaw() * 256.0F / 360.0F);
byte pitch = (byte) (int) (loc.getPitch() * 256.0F / 360.0F);
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isMovementPacketsEnabled())
{
yaw = getYaw(disguise.getType(), disguisedEntity.getType(), yaw);
pitch = getPitch(disguise.getType(), DisguiseType.getType(disguisedEntity.getType()), pitch);
}
2016-05-09 17:28:38 +02:00
if (disguise.getType() == DisguiseType.EXPERIENCE_ORB)
{
spawnPackets[0] = new PacketContainer(Server.SPAWN_ENTITY_EXPERIENCE_ORB);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = spawnPackets[0].getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, Math.floor(loc.getX() * 32));
mods.write(2, Math.floor(loc.getY() * 32) + 2);
mods.write(3, Math.floor(loc.getZ() * 32));
mods.write(4, 1);
2016-05-09 17:28:38 +02:00
}
else if (disguise.getType() == DisguiseType.PAINTING)
{
spawnPackets[0] = new PacketContainer(Server.SPAWN_ENTITY_PAINTING);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = spawnPackets[0].getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, disguisedEntity.getUniqueId());
mods.write(2, ReflectionManager.getBlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
mods.write(3, ReflectionManager.getEnumDirection(((int) loc.getYaw()) % 4));
2016-05-09 17:28:38 +02:00
int id = ((MiscDisguise) disguise).getData();
2016-05-09 17:28:38 +02:00
mods.write(4, ReflectionManager.getEnumArt(Art.values()[id]));
// Make the teleport packet to make it visible..
spawnPackets[1] = new PacketContainer(Server.ENTITY_TELEPORT);
2016-05-09 17:28:38 +02:00
mods = spawnPackets[1].getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, Math.floor(loc.getX() * 32D));
mods.write(2, Math.floor(loc.getY() * 32D));
mods.write(3, Math.floor(loc.getZ() * 32D));
mods.write(4, yaw);
mods.write(5, pitch);
2016-05-09 17:28:38 +02:00
}
else if (disguise.getType().isPlayer())
{
PlayerDisguise playerDisguise = (PlayerDisguise) disguise;
2016-05-09 17:28:38 +02:00
String name = playerDisguise.getSkin() != null ? playerDisguise.getSkin() : playerDisguise.getName();
int entityId = disguisedEntity.getEntityId();
boolean removeName = false;
2016-05-09 17:28:38 +02:00
if (!DisguiseUtilities.hasGameProfile(name))
{
removeName = !DisguiseUtilities.getAddedByPlugins().contains(name);
}
2016-05-09 17:28:38 +02:00
WrappedGameProfile gameProfile = playerDisguise.getGameProfile();
2016-05-09 17:28:38 +02:00
if (removeName)
{
DisguiseUtilities.getAddedByPlugins().remove(name);
}
2016-05-09 17:28:38 +02:00
Object entityPlayer = ReflectionManager.createEntityPlayer(player.getWorld(), gameProfile);
spawnPackets[0] = ProtocolLibrary.getProtocolManager()
2016-05-09 17:28:38 +02:00
.createPacketConstructor(Server.NAMED_ENTITY_SPAWN, entityPlayer).createPacket(entityPlayer);
2016-05-09 17:28:38 +02:00
// Write spawn packet in order
spawnPackets[0].getIntegers().write(0, entityId); // Id
spawnPackets[0].getDoubles().write(0, loc.getX());
spawnPackets[0].getDoubles().write(1, loc.getY());
spawnPackets[0].getDoubles().write(2, loc.getZ());
2016-05-09 17:28:38 +02:00
spawnPackets[0].getDataWatcherModifier().write(0,
createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher())); // watcher,
// duh
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping())
{
2014-09-29 06:04:47 +02:00
PacketContainer[] newPackets = new PacketContainer[spawnPackets.length + 1];
2016-05-09 17:28:38 +02:00
2014-09-29 06:04:47 +02:00
System.arraycopy(spawnPackets, 1, newPackets, 2, spawnPackets.length - 1);
2016-05-09 17:28:38 +02:00
2014-09-29 06:04:47 +02:00
newPackets[0] = spawnPackets[0];
spawnPackets = newPackets;
2016-05-09 17:28:38 +02:00
PacketContainer[] bedPackets = DisguiseUtilities.getBedPackets(player,
loc.clone().subtract(0, PacketsManager.getYModifier(disguisedEntity, disguise), 0), player.getLocation(),
((PlayerDisguise) disguise));
2016-05-09 17:28:38 +02:00
System.arraycopy(bedPackets, 0, spawnPackets, 1, 2);
}
2016-05-09 17:28:38 +02:00
ArrayList<PacketContainer> newPackets = new ArrayList<PacketContainer>();
newPackets.add(null);
2016-05-09 17:28:38 +02:00
for (PacketContainer spawnPacket : spawnPackets)
{
if (spawnPacket != null)
{ // Get rid of empty packet '1' if it exists.
newPackets.add(spawnPacket);
}
}
2016-05-09 17:28:38 +02:00
// Send player info along with the disguise
spawnPackets = newPackets.toArray(new PacketContainer[newPackets.size()]);
spawnPackets[0] = new PacketContainer(Server.PLAYER_INFO);
2016-05-09 17:28:38 +02:00
// Add player to the list, necessary to spawn them
spawnPackets[0].getModifier().write(0, ReflectionManager.getEnumPlayerInfoAction(0));
2016-05-09 17:28:38 +02:00
List playerList = new ArrayList();
2016-05-09 17:28:38 +02:00
playerList.add(ReflectionManager.getPlayerInfoData(spawnPackets[0].getHandle(), playerDisguise.getGameProfile()));
spawnPackets[0].getModifier().write(1, playerList);
2016-05-09 17:28:38 +02:00
// Remove player from the list
PacketContainer delayedPacket = spawnPackets[0].shallowClone();
2016-05-09 17:28:38 +02:00
delayedPacket.getModifier().write(0, ReflectionManager.getEnumPlayerInfoAction(4));
2016-05-09 17:28:38 +02:00
delayedPackets = new PacketContainer[]
{
delayedPacket
};
}
else if (disguise.getType().isMob() || disguise.getType() == DisguiseType.ARMOR_STAND)
{
Vector vec = disguisedEntity.getVelocity();
2016-05-09 17:28:38 +02:00
spawnPackets[0] = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = spawnPackets[0].getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, UUID.randomUUID());
mods.write(2, disguise.getType().getTypeId());
2016-05-09 17:28:38 +02:00
// region Vector calculations
double d1 = 3.9D;
double d2 = vec.getX();
double d3 = vec.getY();
double d4 = vec.getZ();
if (d2 < -d1)
d2 = -d1;
if (d3 < -d1)
d3 = -d1;
if (d4 < -d1)
d4 = -d1;
if (d2 > d1)
d2 = d1;
if (d3 > d1)
d3 = d1;
if (d4 > d1)
d4 = d1;
2016-05-09 17:28:38 +02:00
// endregion
mods.write(3, loc.getX());
mods.write(4, loc.getY());
mods.write(5, loc.getZ());
mods.write(6, (int) (d2 * 8000.0D));
mods.write(7, (int) (d3 * 8000.0D));
mods.write(8, (int) (d4 * 8000.0D));
mods.write(9, yaw);
mods.write(10, pitch);
2016-05-09 17:28:38 +02:00
spawnPackets[0].getDataWatcherModifier().write(0,
createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher()));
2016-05-09 17:28:38 +02:00
}
else if (disguise.getType().isMisc())
{
int objectId = disguise.getType().getObjectId();
int data = ((MiscDisguise) disguise).getData();
2016-05-09 17:28:38 +02:00
if (disguise.getType() == DisguiseType.FALLING_BLOCK)
{
data = ReflectionManager.getCombinedId(((MiscDisguise) disguise).getId(), data);
2016-05-09 17:28:38 +02:00
}
else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == 0)
{
// If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners entity id
data = disguisedEntity.getEntityId();
2016-05-09 17:28:38 +02:00
}
else if (disguise.getType() == DisguiseType.ITEM_FRAME)
{
data = ((((int) loc.getYaw() % 360) + 720 + 45) / 90) % 4;
}
2016-05-09 17:28:38 +02:00
Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity);
2016-05-09 17:28:38 +02:00
spawnPackets[0] = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.SPAWN_ENTITY, nmsEntity, objectId, data)
.createPacket(nmsEntity, objectId, data);
spawnPackets[0].getModifier().write(8, pitch);
spawnPackets[0].getModifier().write(9, yaw);
2016-05-09 17:28:38 +02:00
if (disguise.getType() == DisguiseType.ITEM_FRAME)
{
if (data % 2 == 0)
{
spawnPackets[0].getModifier().write(4, (int) Math.floor((loc.getZ() + (data == 0 ? -1 : 1)) * 32D));
2016-05-09 17:28:38 +02:00
}
else
{
spawnPackets[0].getModifier().write(2, (int) Math.floor((loc.getX() + (data == 3 ? -1 : 1)) * 32D));
2014-06-20 23:37:53 +02:00
}
}
}
2016-05-09 17:28:38 +02:00
if (spawnPackets[1] == null || disguise.isPlayerDisguise())
{
int entry = spawnPackets[1] == null ? 1 : 0;
2016-05-09 17:28:38 +02:00
if (entry == 0)
{
entry = spawnPackets.length;
spawnPackets = Arrays.copyOf(spawnPackets, spawnPackets.length + 1);
}
// Make a packet to turn his head!
2016-05-09 17:28:38 +02:00
spawnPackets[entry] = new PacketContainer(Server.ENTITY_HEAD_ROTATION);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = spawnPackets[entry].getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, yaw);
}
2016-05-09 17:28:38 +02:00
return new PacketContainer[][]
{
spawnPackets, delayedPackets
};
}
/**
* Create a new datawatcher but with the 'correct' values
*/
2016-05-09 17:28:38 +02:00
private static WrappedDataWatcher createDataWatcher(WrappedDataWatcher watcher, FlagWatcher flagWatcher)
{
2013-11-18 12:49:04 +01:00
WrappedDataWatcher newWatcher = new WrappedDataWatcher();
2016-05-09 17:28:38 +02:00
try
{
List<WrappedWatchableObject> list = DisguiseConfig.isMetadataPacketsEnabled()
? flagWatcher.convert(watcher.getWatchableObjects()) : flagWatcher.getWatchableObjects();
for (WrappedWatchableObject watchableObject : list)
{
if (watchableObject == null)
continue;
if (watchableObject.getValue() == null)
continue;
if (Registry.get(watchableObject.getValue().getClass()) == null)
continue;
WrappedDataWatcherObject obj = new WrappedDataWatcherObject(watchableObject.getIndex(),
Registry.get(watchableObject.getValue().getClass()));
newWatcher.setObject(obj, watchableObject.getValue());
}
2016-05-09 17:28:38 +02:00
}
catch (Exception ex)
{
ex.printStackTrace();
}
2016-05-09 17:28:38 +02:00
return newWatcher;
}
2016-05-09 17:28:38 +02:00
public static byte getPitch(DisguiseType disguiseType, DisguiseType entityType, byte value)
{
switch (disguiseType)
{
case MINECART:
case MINECART_CHEST:
case MINECART_COMMAND:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
value = (byte) -value;
break;
default:
break;
}
2016-05-09 17:28:38 +02:00
switch (entityType)
{
case MINECART:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
value = (byte) -value;
break;
default:
break;
}
2016-05-09 17:28:38 +02:00
return value;
}
/**
* Add the yaw for the disguises
*/
2016-05-09 17:28:38 +02:00
public static byte getYaw(DisguiseType disguiseType, EntityType entityType, byte value)
{
switch (disguiseType)
{
case MINECART:
case MINECART_CHEST:
case MINECART_COMMAND:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
value += 64;
break;
case ENDER_DRAGON:
value -= 128;
break;
case WITHER_SKULL:
value -= 128;
break;
case ARROW:
case TIPPED_ARROW:
case SPECTRAL_ARROW:
value = (byte) -value;
break;
case PAINTING:
case ITEM_FRAME:
value = (byte) -(value + 128);
break;
default:
if (disguiseType.isMisc() && disguiseType != DisguiseType.ARMOR_STAND)
{
value -= 64;
2016-05-09 17:28:38 +02:00
}
break;
}
2016-05-09 17:28:38 +02:00
switch (entityType)
{
case MINECART:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
value -= 64;
break;
case ENDER_DRAGON:
case WITHER_SKULL:
value += 128;
break;
case ARROW:
value = (byte) -value;
break;
case PAINTING:
case ITEM_FRAME:
value = (byte) -(value - 128);
break;
default:
if (!entityType.isAlive())
{
value += 64;
}
break;
}
return value;
}
/**
* Get the Y level to add to the disguise for realism.
*/
2016-05-09 17:28:38 +02:00
public static double getYModifier(Entity entity, Disguise disguise)
{
double yMod = 0;
2016-05-09 17:28:38 +02:00
if ((disguise.getType() != DisguiseType.PLAYER || !((PlayerWatcher) disguise.getWatcher()).isSleeping())
2016-05-09 17:28:38 +02:00
&& entity.getType() == EntityType.DROPPED_ITEM)
{
yMod -= 0.13;
}
2016-05-09 17:28:38 +02:00
switch (disguise.getType())
{
case BAT:
if (entity instanceof LivingEntity)
return yMod + ((LivingEntity) entity).getEyeHeight();
case MINECART:
case MINECART_COMMAND:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
switch (entity.getType())
{
case MINECART:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
2016-05-09 17:28:38 +02:00
return yMod;
default:
2016-05-09 17:28:38 +02:00
return yMod + 0.4;
}
case TIPPED_ARROW:
case SPECTRAL_ARROW:
case BOAT:
case EGG:
case ENDER_PEARL:
case ENDER_SIGNAL:
case FIREWORK:
case PAINTING:
case SMALL_FIREBALL:
case SNOWBALL:
case SPLASH_POTION:
case THROWN_EXP_BOTTLE:
case WITHER_SKULL:
return yMod + 0.7;
case PLAYER:
if (DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping())
{
return yMod + 0.35;
}
break;
case DROPPED_ITEM:
return yMod + 0.13;
default:
break;
}
return yMod;
}
/**
* Creates the packet listeners
*/
2016-05-09 17:28:38 +02:00
public static void init(LibsDisguises plugin)
{
libsDisguises = plugin;
2016-05-09 17:28:38 +02:00
soundsListener = new PacketAdapter(libsDisguises, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT,
Server.ENTITY_STATUS)
{
@Override
2016-05-09 17:28:38 +02:00
public void onPacketSending(PacketEvent event)
{
if (event.isCancelled())
{
return;
}
2016-05-09 17:28:38 +02:00
if (event.isAsync())
{
2016-03-15 02:53:25 +01:00
return;
}
2016-05-09 17:28:38 +02:00
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return;
2016-05-09 17:28:38 +02:00
event.setPacket(event.getPacket().deepClone());
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = event.getPacket().getModifier();
2016-05-09 17:28:38 +02:00
Player observer = event.getPlayer();
2016-05-09 17:28:38 +02:00
if (event.getPacketType() == Server.NAMED_SOUND_EFFECT)
{
Object soundEffect = mods.read(0);
2016-05-09 17:28:38 +02:00
SoundType soundType = null;
2016-05-09 17:28:38 +02:00
Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(2)) / 8D,
((Integer) mods.read(3)) / 8D, ((Integer) mods.read(4)) / 8D);
2016-05-09 17:28:38 +02:00
Entity disguisedEntity = null;
DisguiseSound entitySound = null;
2016-05-09 17:28:38 +02:00
Disguise disguise = null;
2016-05-09 17:28:38 +02:00
Entity[] entities = soundLoc.getChunk().getEntities();
2016-05-09 17:28:38 +02:00
for (Entity entity : entities)
{
Disguise entityDisguise = DisguiseAPI.getDisguise(observer, entity);
2016-05-09 17:28:38 +02:00
if (entityDisguise != null)
{
Location loc = entity.getLocation();
2016-05-09 17:28:38 +02:00
loc = new Location(observer.getWorld(), ((int) (loc.getX() * 8)) / 8D, ((int) (loc.getY() * 8)) / 8D,
((int) (loc.getZ() * 8)) / 8D);
2016-05-09 17:28:38 +02:00
if (loc.equals(soundLoc))
{
entitySound = DisguiseSound.getType(entity.getType().name());
2016-05-09 17:28:38 +02:00
if (entitySound != null)
{
Object obj = null;
2016-05-09 17:28:38 +02:00
if (entity instanceof LivingEntity)
{
try
{
// Use reflection so that this works for either int or double methods
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
2016-05-09 17:28:38 +02:00
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0)
{
soundType = SoundType.DEATH;
2016-05-09 17:28:38 +02:00
}
else
{
obj = null;
}
2016-05-09 17:28:38 +02:00
}
catch (Exception e)
{
e.printStackTrace();
}
}
2016-05-09 17:28:38 +02:00
if (obj == null)
{
boolean hasInvun = false;
2016-05-09 17:28:38 +02:00
Object nmsEntity = ReflectionManager.getNmsEntity(entity);
2016-05-09 17:28:38 +02:00
try
{
if (entity instanceof LivingEntity)
{
hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks")
.getInt(nmsEntity) == ReflectionManager
.getNmsField("EntityLiving", "maxNoDamageTicks")
.getInt(nmsEntity);
}
else
{
Class clazz = ReflectionManager.getNmsClass("DamageSource");
2016-05-09 17:28:38 +02:00
hasInvun = (Boolean) ReflectionManager
.getNmsMethod("Entity", "isInvulnerable", clazz)
.invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC"));
}
2016-05-09 17:28:38 +02:00
}
catch (Exception ex)
{
ex.printStackTrace();
}
2016-05-09 17:28:38 +02:00
soundType = entitySound.getType(ReflectionManager.convertSoundEffectToString(soundEffect),
!hasInvun);
}
2016-05-09 17:28:38 +02:00
if (soundType != null)
{
disguise = entityDisguise;
disguisedEntity = entity;
break;
}
}
}
}
}
2016-05-09 17:28:38 +02:00
if (disguise != null)
{
if (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != event.getPlayer())
{
if (disguise.isSoundsReplaced())
{
String sound = null;
2016-05-09 17:28:38 +02:00
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
2016-05-09 17:28:38 +02:00
if (dSound != null)
sound = dSound.getSound(soundType);
2016-05-09 17:28:38 +02:00
if (sound == null)
{
event.setCancelled(true);
2016-05-09 17:28:38 +02:00
}
else
{
if (sound.equals("step.grass"))
{
try
{
int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(),
soundLoc.getBlockY() - 1, soundLoc.getBlockZ());
2016-05-09 17:28:38 +02:00
Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "a", int.class)
2016-05-09 17:28:38 +02:00
.invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId);
if (block != null)
{
Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block);
2016-05-09 17:28:38 +02:00
mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "getStepSound")
.invoke(step));
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
}
2016-05-09 17:28:38 +02:00
}
catch (Exception ex)
{
ex.printStackTrace();
}
// There is no else statement. Because seriously. This should never be null. Unless
// someone is
// sending fake sounds. In which case. Why cancel it.
2016-05-09 17:28:38 +02:00
}
else
{
mods.write(0, ReflectionManager.getCraftSoundEffect(sound));
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
2016-05-09 17:28:38 +02:00
// Time to change the pitch and volume
if (soundType == SoundType.HURT || soundType == SoundType.DEATH
2016-05-09 17:28:38 +02:00
|| soundType == SoundType.IDLE)
{
// If the volume is the default
2016-05-09 17:28:38 +02:00
if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume()))
{
mods.write(5, dSound.getDamageAndIdleSoundVolume());
}
2016-05-09 17:28:38 +02:00
// Here I assume its the default pitch as I can't calculate if its real.
if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity
2016-05-09 17:28:38 +02:00
&& ((MobDisguise) disguise).doesDisguiseAge())
{
boolean baby = false;
2016-05-09 17:28:38 +02:00
if (disguisedEntity instanceof Zombie)
{
baby = ((Zombie) disguisedEntity).isBaby();
2016-05-09 17:28:38 +02:00
}
else if (disguisedEntity instanceof Ageable)
{
baby = !((Ageable) disguisedEntity).isAdult();
}
2016-05-09 17:28:38 +02:00
if (((MobDisguise) disguise).isAdult() == baby)
{
float pitch = (Integer) mods.read(5);
2016-05-09 17:28:38 +02:00
if (baby)
{
// If the pitch is not the expected
if (pitch > 97 || pitch < 111)
return;
2016-05-09 17:28:38 +02:00
pitch = (DisguiseUtilities.random.nextFloat()
- DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F;
// Min = 1.5
// Cap = 97.5
// Max = 1.7
// Cap = 110.5
2016-05-09 17:28:38 +02:00
}
else
{
// If the pitch is not the expected
if (pitch >= 63 || pitch <= 76)
return;
2016-05-09 17:28:38 +02:00
pitch = (DisguiseUtilities.random.nextFloat()
- DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F;
// Min = 1
// Cap = 63
// Max = 1.2
// Cap = 75.6
}
2016-05-09 17:28:38 +02:00
pitch *= 63;
2016-05-09 17:28:38 +02:00
if (pitch < 0)
pitch = 0;
2016-05-09 17:28:38 +02:00
if (pitch > 255)
pitch = 255;
2016-05-09 17:28:38 +02:00
mods.write(6, (int) pitch);
}
}
}
}
}
}
}
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.ENTITY_STATUS)
{
if ((byte) mods.read(1) == 2)
{
// It made a damage animation
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0);
2016-05-09 17:28:38 +02:00
Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
2016-05-09 17:28:38 +02:00
if (disguise != null && !disguise.getType().isPlayer()
2016-05-09 17:28:38 +02:00
&& (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer()))
{
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name());
2016-05-09 17:28:38 +02:00
if (disSound == null)
return;
2016-05-09 17:28:38 +02:00
SoundType soundType = null;
Object obj = null;
2016-05-09 17:28:38 +02:00
if (entity instanceof LivingEntity)
{
try
{
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
2016-05-09 17:28:38 +02:00
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0)
{
soundType = SoundType.DEATH;
2016-05-09 17:28:38 +02:00
}
else
{
obj = null;
}
2016-05-09 17:28:38 +02:00
}
catch (Exception e)
{
e.printStackTrace();
}
}
2016-05-09 17:28:38 +02:00
if (obj == null)
{
soundType = SoundType.HURT;
}
2016-05-09 17:28:38 +02:00
if (disSound.getSound(soundType) == null
2016-05-09 17:28:38 +02:00
|| (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()))
{
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())
{
cancelSound = !cancelSound;
2016-05-09 17:28:38 +02:00
if (cancelSound)
return;
}
2016-05-09 17:28:38 +02:00
disSound = DisguiseSound.getType(disguise.getType().name());
2016-05-09 17:28:38 +02:00
if (disSound != null)
{
String sound = disSound.getSound(soundType);
2016-05-09 17:28:38 +02:00
if (sound != null)
{
Location loc = entity.getLocation();
2016-05-09 17:28:38 +02:00
PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT);
2016-05-09 17:28:38 +02:00
mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
Object craftSoundEffect = ReflectionManager.getCraftSoundEffect(sound);
2016-05-09 17:28:38 +02:00
mods.write(0, craftSoundEffect);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh
mods.write(2, (int) (loc.getX() * 8D));
mods.write(3, (int) (loc.getY() * 8D));
mods.write(4, (int) (loc.getZ() * 8D));
mods.write(5, disSound.getDamageAndIdleSoundVolume());
2016-05-09 17:28:38 +02:00
float pitch;
2016-05-09 17:28:38 +02:00
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult())
{
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat())
* 0.2F + 1.5F;
}
else
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat())
* 0.2F + 1.0F;
if (disguise.getType() == DisguiseType.BAT)
pitch *= 95F;
2016-05-09 17:28:38 +02:00
pitch *= 63;
2016-05-09 17:28:38 +02:00
if (pitch < 0)
pitch = 0;
2016-05-09 17:28:38 +02:00
if (pitch > 255)
pitch = 255;
2016-05-09 17:28:38 +02:00
mods.write(6, (int) pitch);
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
}
}
}
};
2016-05-09 17:28:38 +02:00
// Self disguise (/vsd) listener
viewDisguisesListener = new PacketAdapter(libsDisguises, ListenerPriority.HIGH, Server.NAMED_ENTITY_SPAWN,
Server.ATTACH_ENTITY, Server.REL_ENTITY_MOVE, Server.ENTITY_MOVE_LOOK, Server.ENTITY_LOOK, Server.ENTITY_TELEPORT,
Server.ENTITY_HEAD_ROTATION, Server.ENTITY_METADATA, Server.ENTITY_EQUIPMENT, Server.ANIMATION, Server.BED,
Server.ENTITY_EFFECT, Server.ENTITY_VELOCITY, Server.UPDATE_ATTRIBUTES, Server.ENTITY_STATUS)
{
@Override
2016-05-09 17:28:38 +02:00
public void onPacketSending(PacketEvent event)
{
if (event.isCancelled())
return;
2016-05-09 17:28:38 +02:00
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return;
2016-05-09 17:28:38 +02:00
final Player observer = event.getPlayer();
2016-05-09 17:28:38 +02:00
if (event.getPacket().getIntegers().read(0) == observer.getEntityId())
{
if (DisguiseAPI.isSelfDisguised(observer))
{
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
PacketContainer[][] transformed = transformPacket(event.getPacket(), observer, observer);
2016-05-09 17:28:38 +02:00
PacketContainer[] packets = transformed == null ? null : transformed[0];
2016-05-09 17:28:38 +02:00
final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1];
2016-05-09 17:28:38 +02:00
if (packets == null)
{
packets = new PacketContainer[]
{
event.getPacket()
};
}
2016-05-09 17:28:38 +02:00
for (PacketContainer packet : packets)
{
if (packet.getType() != Server.PLAYER_INFO)
{
if (packet.equals(event.getPacket()))
{
packet = packet.shallowClone();
2014-09-27 04:42:00 +02:00
}
2016-05-09 17:28:38 +02:00
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
}
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
2016-05-09 17:28:38 +02:00
if (delayedPackets != null && delayedPackets.length > 0)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
public void run()
{
try
{
for (PacketContainer packet : delayedPackets)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}, 2);
}
2016-05-09 17:28:38 +02:00
if (event.getPacketType() == Server.ENTITY_METADATA)
{
event.setPacket(event.getPacket().deepClone());
2016-05-09 17:28:38 +02:00
for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0))
{
if (watch.getIndex() == 0)
{
byte b = (byte) watch.getValue();
2016-05-09 17:28:38 +02:00
byte a = (byte) (b | 1 << 5);
2016-05-09 17:28:38 +02:00
if ((b & 1 << 3) != 0)
a = (byte) (a | 1 << 3);
2016-05-09 17:28:38 +02:00
watch.setValue(a);
}
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN)
{
event.setCancelled(true);
2016-05-09 17:28:38 +02:00
PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, observer.getEntityId());
2016-05-09 17:28:38 +02:00
List<WrappedWatchableObject> watchableList = new ArrayList<>();
Byte b = 1 << 5;
2016-05-09 17:28:38 +02:00
if (observer.isSprinting())
b = (byte) (b | 1 << 3);
2016-05-09 17:28:38 +02:00
WrappedWatchableObject watch = new WrappedWatchableObject(
ReflectionManager.createDataWatcherItem(0, b));
watchableList.add(watch);
packet.getWatchableCollectionModifier().write(0, watchableList);
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.ANIMATION)
{
if (event.getPacket().getIntegers().read(1) != 2)
{
event.setCancelled(true);
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE
|| event.getPacketType() == Server.ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK
|| event.getPacketType() == Server.ENTITY_TELEPORT
|| event.getPacketType() == Server.ENTITY_HEAD_ROTATION
|| event.getPacketType() == Server.ENTITY_EFFECT
2016-05-09 17:28:38 +02:00
|| event.getPacketType() == Server.ENTITY_EQUIPMENT)
{
event.setCancelled(true);
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.ENTITY_STATUS)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
2016-05-09 17:28:38 +02:00
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer()
2016-05-09 17:28:38 +02:00
&& event.getPacket().getBytes().read(0) == 2)
{
event.setCancelled(true);
}
}
}
}
}
};
2016-05-09 17:28:38 +02:00
inventoryListener = new PacketAdapter(libsDisguises, ListenerPriority.HIGHEST, Server.SET_SLOT, Server.WINDOW_ITEMS,
PacketType.Play.Client.HELD_ITEM_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT,
PacketType.Play.Client.WINDOW_CLICK)
{
@Override
2016-05-09 17:28:38 +02:00
public void onPacketReceiving(final PacketEvent event)
{
if (event.isCancelled())
return;
2016-05-09 17:28:38 +02:00
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return;
2016-05-09 17:28:38 +02:00
if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory)
2016-05-09 17:28:38 +02:00
&& event.getPlayer().getVehicle() == null)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
2016-05-09 17:28:38 +02:00
// If player is disguised, views self disguises and has a inventory modifier
2016-05-09 17:28:38 +02:00
if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{
// If they are in creative and clicked on a slot
2016-05-09 17:28:38 +02:00
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT)
{
int slot = event.getPacket().getIntegers().read(0);
2016-05-09 17:28:38 +02:00
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
int armorSlot = Math.abs((slot - 5) - 3);
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory()
.getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
2016-05-09 17:28:38 +02:00
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
2016-05-09 17:28:38 +02:00
if (slot + 36 == currentSlot)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
2016-05-09 17:28:38 +02:00
if (item != null && item.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
}
// If the player switched item, aka he moved from slot 1 to slot 2
2016-05-09 17:28:38 +02:00
else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT)
{
if (disguise.isHidingHeldItemFromSelf())
{
// From logging, it seems that both bukkit and nms uses the same thing for the slot switching.
// 0 1 2 3 - 8
// If the packet is coming, then I need to replace the item they are switching to
// As for the old item, I need to restore it.
org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand();
// If his old weapon isn't air
2016-05-09 17:28:38 +02:00
if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, 0);
mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36);
mods.write(2, ReflectionManager.getNmsItem(currentlyHeld));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory()
.getItem(event.getPacket().getIntegers().read(0));
2016-05-09 17:28:38 +02:00
// If his new weapon isn't air either!
2016-05-09 17:28:38 +02:00
if (newHeld != null && newHeld.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK)
{
int slot = event.getPacket().getIntegers().read(1);
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack clickedItem;
2016-05-09 17:28:38 +02:00
if (event.getPacket().getShorts().read(0) == 1)
{
// Its a shift click
clickedItem = event.getPacket().getItemModifier().read(0);
2016-05-09 17:28:38 +02:00
if (clickedItem != null && clickedItem.getType() != Material.AIR)
{
// Rather than predict the clients actions
// Lets just update the entire inventory..
2016-05-09 17:28:38 +02:00
Bukkit.getScheduler().runTask(libsDisguises, new Runnable()
{
public void run()
{
event.getPlayer().updateInventory();
}
});
}
2016-05-09 17:28:38 +02:00
return;
2016-05-09 17:28:38 +02:00
}
else
{
// If its not a player inventory click
// Shift clicking is exempted for the item in hand..
2016-05-09 17:28:38 +02:00
if (event.getPacket().getIntegers().read(0) != 0)
{
return;
}
2016-05-09 17:28:38 +02:00
clickedItem = event.getPlayer().getItemOnCursor();
}
2016-05-09 17:28:38 +02:00
if (clickedItem != null && clickedItem.getType() != Material.AIR)
{
// If the slot is a armor slot
2016-05-09 17:28:38 +02:00
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
// Else if its a hotbar slot
2016-05-09 17:28:38 +02:00
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
2016-05-09 17:28:38 +02:00
// Check if the player is on the same slot as the slot that its setting
2016-05-09 17:28:38 +02:00
if (slot == currentSlot + 36)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
2013-11-18 04:24:25 +01:00
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
2016-05-09 17:28:38 +02:00
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
false);
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
}
}
}
}
@Override
2016-05-09 17:28:38 +02:00
public void onPacketSending(PacketEvent event)
{
// If the inventory is the players inventory
if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory)
2016-05-09 17:28:38 +02:00
&& event.getPlayer().getVehicle() == null && event.getPacket().getIntegers().read(0) == 0)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
2016-05-09 17:28:38 +02:00
// If the player is disguised, views self disguises and is hiding a item.
if (disguise != null && disguise.isSelfDisguiseVisible()
2016-05-09 17:28:38 +02:00
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{
// If the server is setting the slot
// Need to set it to air if its in a place it shouldn't be.
// Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this
/**
* Done
*/
2016-05-09 17:28:38 +02:00
if (event.getPacketType() == Server.SET_SLOT)
{
// The raw slot
// nms code has the start of the hotbar being 36.
int slot = event.getPacket().getIntegers().read(1);
2016-05-09 17:28:38 +02:00
// If the slot is a armor slot
2016-05-09 17:28:38 +02:00
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
// Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3);
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory()
.getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
event.setPacket(event.getPacket().shallowClone());
2016-05-09 17:28:38 +02:00
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
}
}
// Else if its a hotbar slot
2016-05-09 17:28:38 +02:00
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
2016-05-09 17:28:38 +02:00
// Check if the player is on the same slot as the slot that its setting
2016-05-09 17:28:38 +02:00
if (slot == currentSlot + 36)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
2016-05-09 17:28:38 +02:00
if (item != null && item.getType() != Material.AIR)
{
event.setPacket(event.getPacket().shallowClone());
2016-05-09 17:28:38 +02:00
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
}
}
}
}
2016-05-09 17:28:38 +02:00
}
else if (event.getPacketType() == Server.WINDOW_ITEMS)
{
event.setPacket(event.getPacket().deepClone());
2016-05-09 17:28:38 +02:00
StructureModifier<ItemStack[]> mods = event.getPacket().getItemArrayModifier();
ItemStack[] items = mods.read(0);
2016-05-09 17:28:38 +02:00
for (int slot = 0; slot < items.length; slot++)
{
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
// Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3);
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory()
.getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
// Else if its a hotbar slot
2016-05-09 17:28:38 +02:00
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
2016-05-09 17:28:38 +02:00
// Check if the player is on the same slot as the slot that its setting
2016-05-09 17:28:38 +02:00
if (slot == currentSlot + 36)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
2016-05-09 17:28:38 +02:00
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
}
}
}
2016-05-09 17:28:38 +02:00
mods.write(0, items);
}
}
}
}
};
}
2016-05-09 17:28:38 +02:00
public static boolean isHearDisguisesEnabled()
{
return soundsListenerEnabled;
}
2016-05-09 17:28:38 +02:00
public static boolean isInventoryListenerEnabled()
{
return inventoryModifierEnabled;
}
2016-05-09 17:28:38 +02:00
public static boolean isViewDisguisesListenerEnabled()
{
return viewDisguisesListenerEnabled;
}
2016-05-09 17:28:38 +02:00
public static void setHearDisguisesListener(boolean enabled)
{
if (soundsListenerEnabled != enabled) {
soundsListenerEnabled = enabled;
if (soundsListenerEnabled){
ProtocolLibrary.getProtocolManager().addPacketListener(soundsListener);
} else {
ProtocolLibrary.getProtocolManager().removePacketListener(soundsListener);
}
}
}
2016-05-09 17:28:38 +02:00
public static void setInventoryListenerEnabled(boolean enabled)
{
if (inventoryModifierEnabled != enabled)
{
inventoryModifierEnabled = enabled;
2016-05-09 17:28:38 +02:00
if (inventoryModifierEnabled)
{
ProtocolLibrary.getProtocolManager().addPacketListener(inventoryListener);
2016-05-09 17:28:38 +02:00
}
else
{
ProtocolLibrary.getProtocolManager().removePacketListener(inventoryListener);
}
2016-05-09 17:28:38 +02:00
for (Player player : Bukkit.getOnlinePlayers())
{
Disguise disguise = DisguiseAPI.getDisguise(player, player);
2016-05-09 17:28:38 +02:00
if (disguise != null)
{
if (viewDisguisesListenerEnabled && disguise.isSelfDisguiseVisible()
2016-05-09 17:28:38 +02:00
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{
player.updateInventory();
}
}
}
}
}
2016-05-09 17:28:38 +02:00
public static void setupMainPacketsListener()
{
if (clientInteractEntityListener != null)
{
if (mainListener != null)
{
ProtocolLibrary.getProtocolManager().removePacketListener(mainListener);
}
2016-05-09 17:28:38 +02:00
List<PacketType> packetsToListen = new ArrayList<>();
// Add spawn packets
{
packetsToListen.add(Server.NAMED_ENTITY_SPAWN);
packetsToListen.add(Server.SPAWN_ENTITY_EXPERIENCE_ORB);
packetsToListen.add(Server.SPAWN_ENTITY);
packetsToListen.add(Server.SPAWN_ENTITY_LIVING);
packetsToListen.add(Server.SPAWN_ENTITY_PAINTING);
}
2016-05-09 17:28:38 +02:00
// Add packets that always need to be enabled to ensure safety
{
packetsToListen.add(Server.ENTITY_METADATA);
}
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isCollectPacketsEnabled())
{
packetsToListen.add(Server.COLLECT);
}
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isMiscDisguisesForLivingEnabled())
{
packetsToListen.add(Server.UPDATE_ATTRIBUTES);
}
2016-05-09 17:28:38 +02:00
// The bed packet.
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isBedPacketsEnabled())
{
packetsToListen.add(Server.BED);
}
2016-05-09 17:28:38 +02:00
// Add movement packets
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isMovementPacketsEnabled())
{
packetsToListen.add(Server.ENTITY_LOOK);
packetsToListen.add(Server.ENTITY_MOVE_LOOK);
packetsToListen.add(Server.ENTITY_HEAD_ROTATION);
packetsToListen.add(Server.ENTITY_TELEPORT);
packetsToListen.add(Server.REL_ENTITY_MOVE);
}
2016-05-09 17:28:38 +02:00
2014-06-15 03:09:14 +02:00
// Add equipment packet
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isEquipmentPacketsEnabled())
{
packetsToListen.add(Server.ENTITY_EQUIPMENT);
}
2016-05-09 17:28:38 +02:00
// Add the packet that ensures if they are sleeping or not
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isAnimationPacketsEnabled())
{
packetsToListen.add(Server.ANIMATION);
}
2016-05-09 17:28:38 +02:00
// Add the packet that makes sure that entities with armor do not send unpickupable armor on death
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isEntityStatusPacketsEnabled())
{
packetsToListen.add(Server.ENTITY_STATUS);
}
2016-05-09 17:28:38 +02:00
mainListener = new PacketAdapter(libsDisguises, ListenerPriority.HIGH, packetsToListen)
{
@Override
2016-05-09 17:28:38 +02:00
public void onPacketSending(PacketEvent event)
{
if (event.isCancelled())
2014-09-15 04:54:14 +02:00
return;
2016-05-09 17:28:38 +02:00
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return;
2016-05-09 17:28:38 +02:00
final Player observer = event.getPlayer();
2016-05-09 17:28:38 +02:00
// First get the entity, the one sending this packet
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
2016-05-09 17:28:38 +02:00
org.bukkit.entity.Entity entity = entityModifer.read((Server.COLLECT == event.getPacketType() ? 1 : 0));
2016-05-09 17:28:38 +02:00
// If the entity is the same as the sender. Don't disguise!
// Prevents problems and there is no advantage to be gained.
if (entity == observer)
return;
2016-05-09 17:28:38 +02:00
PacketContainer[][] packets = transformPacket(event.getPacket(), event.getPlayer(), entity);
2016-05-09 17:28:38 +02:00
if (packets != null)
{
event.setCancelled(true);
2016-05-09 17:28:38 +02:00
try
{
for (PacketContainer packet : packets[0])
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
2016-05-09 17:28:38 +02:00
final PacketContainer[] delayed = packets[1];
2016-05-09 17:28:38 +02:00
if (delayed.length > 0)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
public void run()
{
try
{
for (PacketContainer packet : delayed)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}, 2);
}
2016-05-09 17:28:38 +02:00
}
catch (InvocationTargetException ex)
{
ex.printStackTrace();
}
}
}
};
2016-05-09 17:28:38 +02:00
ProtocolLibrary.getProtocolManager().addPacketListener(mainListener);
}
}
2016-05-09 17:28:38 +02:00
public static void setViewDisguisesListener(boolean enabled)
{
if (viewDisguisesListenerEnabled != enabled)
{
viewDisguisesListenerEnabled = enabled;
2016-05-09 17:28:38 +02:00
if (viewDisguisesListenerEnabled)
{
ProtocolLibrary.getProtocolManager().addPacketListener(viewDisguisesListener);
2016-05-09 17:28:38 +02:00
}
else
{
ProtocolLibrary.getProtocolManager().removePacketListener(viewDisguisesListener);
}
2016-05-09 17:28:38 +02:00
for (Player player : Bukkit.getOnlinePlayers())
{
Disguise disguise = DisguiseAPI.getDisguise(player, player);
2016-05-09 17:28:38 +02:00
if (disguise != null)
{
if (disguise.isSelfDisguiseVisible())
{
if (enabled)
{
DisguiseUtilities.setupFakeDisguise(disguise);
2016-05-09 17:28:38 +02:00
}
else
{
DisguiseUtilities.removeSelfDisguise(player);
}
2016-05-09 17:28:38 +02:00
if (inventoryModifierEnabled && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{
player.updateInventory();
}
}
}
}
}
}
/**
* Transform the packet magically into the one I have always dreamed off. My true luv!!! This will return null if its not
* transformed
*/
2016-05-09 17:28:38 +02:00
public static PacketContainer[][] transformPacket(PacketContainer sentPacket, Player observer, Entity entity)
{
PacketContainer[] packets = null;
2016-05-09 17:28:38 +02:00
PacketContainer[] delayedPackets = new PacketContainer[0];
2016-05-09 17:28:38 +02:00
try
{
Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
2016-05-09 17:28:38 +02:00
// If disguised.
2016-05-09 17:28:38 +02:00
if (disguise != null)
{
packets = new PacketContainer[]
{
sentPacket
};
// This packet sends attributes
2016-05-09 17:28:38 +02:00
if (sentPacket.getType() == Server.UPDATE_ATTRIBUTES)
{
if (disguise.isMiscDisguise())
{
packets = new PacketContainer[0];
2016-05-09 17:28:38 +02:00
}
else
{
List<WrappedAttribute> attributes = new ArrayList<>();
2016-05-09 17:28:38 +02:00
for (WrappedAttribute attribute : sentPacket.getAttributeCollectionModifier().read(0))
{
if (attribute.getAttributeKey().equals("generic.maxHealth"))
{
packets[0] = new PacketContainer(Server.UPDATE_ATTRIBUTES);
2016-05-09 17:28:38 +02:00
Builder builder;
2016-05-09 17:28:38 +02:00
if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet())
{
builder = WrappedAttribute.newBuilder();
builder.attributeKey("generic.maxHealth");
builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth());
2016-05-09 17:28:38 +02:00
}
else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity())
{
builder = WrappedAttribute.newBuilder(attribute);
2016-05-09 17:28:38 +02:00
}
else
{
builder = WrappedAttribute.newBuilder();
builder.attributeKey("generic.maxHealth");
builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth());
}
2016-05-09 17:28:38 +02:00
builder.packet(packets[0]);
2016-05-09 17:28:38 +02:00
attributes.add(builder.build());
break;
}
}
2016-05-09 17:28:38 +02:00
if (!attributes.isEmpty())
{
packets[0].getIntegers().write(0, entity.getEntityId());
packets[0].getAttributeCollectionModifier().write(0, attributes);
2016-05-09 17:28:38 +02:00
}
else
{
packets = new PacketContainer[0];
}
}
}
// Else if the packet is sending entity metadata
2016-05-09 17:28:38 +02:00
else if (sentPacket.getType() == Server.ENTITY_METADATA)
{
if (DisguiseConfig.isMetadataPacketsEnabled() && !isStaticMetadataDisguiseType(disguise))
{
List<WrappedWatchableObject> watchableObjects = disguise.getWatcher()
.convert(packets[0].getWatchableCollectionModifier().read(0));
packets[0] = new PacketContainer(sentPacket.getType());
2016-05-09 17:28:38 +02:00
StructureModifier<Object> newMods = packets[0].getModifier();
2016-05-09 17:28:38 +02:00
newMods.write(0, entity.getEntityId());
2016-05-09 17:28:38 +02:00
packets[0].getWatchableCollectionModifier().write(0, watchableObjects);
2016-05-09 17:28:38 +02:00
}
else
{
packets = new PacketContainer[0];
}
}
// Else if the packet is spawning..
2016-05-09 17:28:38 +02:00
else if (sentPacket.getType() == Server.NAMED_ENTITY_SPAWN || sentPacket.getType() == Server.SPAWN_ENTITY_LIVING
|| sentPacket.getType() == Server.SPAWN_ENTITY_EXPERIENCE_ORB
2016-05-09 17:28:38 +02:00
|| sentPacket.getType() == Server.SPAWN_ENTITY || sentPacket.getType() == Server.SPAWN_ENTITY_PAINTING)
{
PacketContainer[][] spawnPackets = constructSpawnPackets(observer, disguise, entity);
2016-05-09 17:28:38 +02:00
packets = spawnPackets[0];
delayedPackets = spawnPackets[1];
}
// Else if the disguise is attempting to send players a forbidden packet
2016-05-09 17:28:38 +02:00
else if (sentPacket.getType() == Server.ANIMATION)
{
if (disguise.getType().isMisc() || (packets[0].getIntegers().read(1) == 2 && (!disguise.getType().isPlayer()
|| (DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping()))))
{
2013-10-18 02:58:05 +02:00
packets = new PacketContainer[0];
}
}
2016-05-09 17:28:38 +02:00
// Else if the disguise is collecting stuff
else if (sentPacket.getType() == Server.COLLECT)
{
if (disguise.getType().isMisc())
{
packets = new PacketContainer[0];
2016-05-09 17:28:38 +02:00
}
else if (DisguiseConfig.isBedPacketsEnabled() && disguise.getType().isPlayer()
&& ((PlayerWatcher) disguise.getWatcher()).isSleeping())
{
PacketContainer newPacket = new PacketContainer(Server.ANIMATION);
2016-05-09 17:28:38 +02:00
StructureModifier<Integer> mods = newPacket.getIntegers();
mods.write(0, disguise.getEntity().getEntityId());
mods.write(1, 3);
2016-05-09 17:28:38 +02:00
packets = new PacketContainer[]
{
newPacket, sentPacket
};
}
}
// Else if the disguise is moving.
2016-05-09 17:28:38 +02:00
else if (sentPacket.getType() == Server.ENTITY_MOVE_LOOK || sentPacket.getType() == Server.ENTITY_LOOK
|| sentPacket.getType() == Server.ENTITY_TELEPORT || sentPacket.getType() == Server.REL_ENTITY_MOVE)
{
if (disguise.getType() == DisguiseType.RABBIT && (sentPacket.getType() == Server.REL_ENTITY_MOVE
|| sentPacket.getType() == Server.ENTITY_MOVE_LOOK))
{
// Rabbit robbing...
2014-09-27 13:59:03 +02:00
if (entity.getMetadata("LibsRabbitHop").isEmpty()
|| System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() < 100
2016-05-09 17:28:38 +02:00
|| System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() > 500)
{
2014-09-27 13:59:03 +02:00
if (entity.getMetadata("LibsRabbitHop").isEmpty()
2016-05-09 17:28:38 +02:00
|| System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() > 500)
{
2014-09-27 13:59:03 +02:00
entity.removeMetadata("LibsRabbitHop", libsDisguises);
2016-05-09 17:28:38 +02:00
entity.setMetadata("LibsRabbitHop",
new FixedMetadataValue(libsDisguises, System.currentTimeMillis()));
2014-09-27 13:59:03 +02:00
}
2016-05-09 17:28:38 +02:00
2014-09-27 13:59:03 +02:00
packets = Arrays.copyOf(packets, packets.length + 1);
2016-05-09 17:28:38 +02:00
packets[1] = new PacketContainer(Server.ENTITY_STATUS);
2016-05-09 17:28:38 +02:00
2014-09-27 13:59:03 +02:00
packets[1].getIntegers().write(0, entity.getEntityId());
packets[1].getBytes().write(0, (byte) 1);
}
}
2016-05-09 17:28:38 +02:00
// Stop wither skulls from looking
if (sentPacket.getType() == Server.ENTITY_LOOK && disguise.getType() == DisguiseType.WITHER_SKULL)
{
packets = new PacketContainer[0];
2016-05-09 17:28:38 +02:00
}
else if (sentPacket.getType() != Server.REL_ENTITY_MOVE)
{
packets[0] = sentPacket.shallowClone();
2016-05-09 17:28:38 +02:00
2014-09-04 19:22:26 +02:00
StructureModifier<Byte> bytes = packets[0].getBytes();
2016-05-09 17:28:38 +02:00
byte yawValue = bytes.read(0);
byte pitchValue = bytes.read(1);
2016-05-09 17:28:38 +02:00
bytes.write(0, getYaw(disguise.getType(), entity.getType(), yawValue));
bytes.write(1, getPitch(disguise.getType(), DisguiseType.getType(entity.getType()), pitchValue));
2016-05-09 17:28:38 +02:00
if (sentPacket.getType() == Server.ENTITY_TELEPORT && disguise.getType() == DisguiseType.ITEM_FRAME)
{
StructureModifier<Double> doubles = packets[0].getDoubles();
2016-05-09 17:28:38 +02:00
2014-09-04 19:22:26 +02:00
Location loc = entity.getLocation();
2016-05-09 17:28:38 +02:00
double data = (((loc.getYaw() % 360) + 720 + 45) / 90) % 4;
2016-05-09 17:28:38 +02:00
if (data % 2 == 0)
{
if (data % 2 == 0)
{
doubles.write(3, loc.getZ());
2016-05-09 17:28:38 +02:00
}
else
{
doubles.write(1, loc.getZ());
2014-06-20 23:37:53 +02:00
}
}
2016-05-09 17:28:38 +02:00
2014-09-04 19:22:26 +02:00
double y = getYModifier(entity, disguise);
2016-05-09 17:28:38 +02:00
if (y != 0)
{
2014-09-04 19:22:26 +02:00
y *= 32;
doubles.write(2, doubles.read(2) + y);
2014-09-04 19:22:26 +02:00
}
}
}
}
2016-05-09 17:28:38 +02:00
// Else if the disguise is updating equipment
else if (sentPacket.getType() == Server.ENTITY_EQUIPMENT)
{
EquipmentSlot slot = ReflectionManager.createEquipmentSlot(packets[0].getModifier().read(1));
2016-05-09 17:28:38 +02:00
org.bukkit.inventory.ItemStack itemStack = disguise.getWatcher().getItemStack(slot);
2016-05-09 17:28:38 +02:00
if (itemStack != null)
{
packets[0] = packets[0].shallowClone();
2016-05-09 17:28:38 +02:00
packets[0].getModifier().write(2,
(itemStack.getTypeId() == 0 ? null : ReflectionManager.getNmsItem(itemStack)));
}
2016-05-09 17:28:38 +02:00
if (disguise.getWatcher().isRightClicking() && slot == EquipmentSlot.HAND)
{
ItemStack heldItem = packets[0].getItemModifier().read(0);
2016-05-09 17:28:38 +02:00
if (heldItem != null && heldItem.getType() != Material.AIR)
{
// Convert the datawatcher
List<WrappedWatchableObject> list = new ArrayList<>();
2016-05-09 17:28:38 +02:00
if (DisguiseConfig.isMetadataPacketsEnabled() && !isStaticMetadataDisguiseType(disguise))
{
WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager
.createDataWatcherItem(0, WrappedDataWatcher.getEntityWatcher(entity).getByte(0)));
list.add(watch);
2016-05-09 17:28:38 +02:00
list = disguise.getWatcher().convert(list);
2016-05-09 17:28:38 +02:00
}
else
{
for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects())
{
if (obj.getIndex() == 0)
{
list.add(obj);
break;
}
}
}
2016-05-09 17:28:38 +02:00
// Construct the packets to return
PacketContainer packetBlock = new PacketContainer(Server.ENTITY_METADATA);
2016-05-09 17:28:38 +02:00
packetBlock.getModifier().write(0, entity.getEntityId());
packetBlock.getWatchableCollectionModifier().write(0, list);
2016-05-09 17:28:38 +02:00
PacketContainer packetUnblock = packetBlock.deepClone();
// Make a packet to send the 'unblock'
2016-05-09 17:28:38 +02:00
for (WrappedWatchableObject watcher : packetUnblock.getWatchableCollectionModifier().read(0))
{
watcher.setValue((byte) ((byte) watcher.getValue() & ~(1 << 4)));
}
2016-05-09 17:28:38 +02:00
2014-01-31 10:56:05 +01:00
// Send the unblock before the itemstack change so that the 2nd metadata packet works. Why? Scheduler
// delay.
2016-05-09 17:28:38 +02:00
packets = new PacketContainer[]
{
packetUnblock, packets[0], packetBlock
};
// Silly mojang made the right clicking datawatcher value only valid for one use. So I have to reset
// it.
}
}
}
2016-05-09 17:28:38 +02:00
// If the entity is going into a bed, stop everything but players from doing this
else if (sentPacket.getType() == Server.BED)
{
if (!disguise.getType().isPlayer())
{
packets = new PacketContainer[0];
}
}
2016-05-09 17:28:38 +02:00
// If the entity is updating their Facebook status, stop them from showing death
else if (sentPacket.getType() == Server.ENTITY_STATUS)
{
if (packets[0].getBytes().read(0) == (byte) 3)
{
packets = new PacketContainer[0];
}
}
2016-05-09 17:28:38 +02:00
// If the entity is rotating his head
else if (sentPacket.getType() == Server.ENTITY_HEAD_ROTATION)
{
if (disguise.getType().isPlayer() && entity.getType() != EntityType.PLAYER)
{
Location loc = entity.getLocation();
2016-05-09 17:28:38 +02:00
byte pitch = getPitch(disguise.getType(), DisguiseType.getType(entity.getType()),
(byte) (int) (loc.getPitch() * 256.0F / 360.0F));
byte yaw = getYaw(disguise.getType(), entity.getType(), sentPacket.getBytes().read(0));
2016-05-09 17:28:38 +02:00
PacketContainer rotation = new PacketContainer(Server.ENTITY_HEAD_ROTATION);
2016-05-09 17:28:38 +02:00
StructureModifier<Object> mods = rotation.getModifier();
2016-05-09 17:28:38 +02:00
mods.write(0, entity.getEntityId());
mods.write(1, yaw);
2016-05-09 17:28:38 +02:00
PacketContainer look = new PacketContainer(Server.ENTITY_LOOK);
2016-05-09 17:28:38 +02:00
look.getIntegers().write(0, entity.getEntityId());
look.getBytes().write(0, yaw);
look.getBytes().write(1, pitch);
2016-05-09 17:28:38 +02:00
packets = new PacketContainer[]
{
look, rotation
};
}
}
2016-05-09 17:28:38 +02:00
// Whatever
else
{
packets = null;
}
2014-09-27 13:59:03 +02:00
}
2016-05-09 17:28:38 +02:00
}
catch (Exception e)
{
e.printStackTrace();
}
2016-05-09 17:28:38 +02:00
return packets == null ? null : new PacketContainer[][]
{
packets, delayedPackets
};
}
/**
* Returns true if this disguise type doesn't have changing metadata.
2016-05-09 17:28:38 +02:00
*
* @param disguise
* @return
*/
2016-05-09 17:28:38 +02:00
public static boolean isStaticMetadataDisguiseType(Disguise disguise)
{
return (disguise.getType() == DisguiseType.WOLF || disguise.getType() == DisguiseType.OCELOT
|| disguise.getType() == DisguiseType.ENDERMAN || disguise.getType() == DisguiseType.SHULKER
|| disguise.getType() == DisguiseType.SPLASH_POTION || disguise.getType() == DisguiseType.FIREWORK
|| disguise.getType() == DisguiseType.DROPPED_ITEM || disguise.getType() == DisguiseType.ENDER_CRYSTAL);
}
}