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