2013-11-22 21:10:20 +01:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
import me.libraryaddict.disguise.DisguiseAPI;
|
|
|
|
import me.libraryaddict.disguise.LibsDisguises;
|
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;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
2013-11-22 21:10:20 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType;
|
2013-08-13 05:19:50 +02:00
|
|
|
|
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;
|
2013-11-18 20:25:48 +01:00
|
|
|
import org.bukkit.entity.Ageable;
|
2013-08-13 05:19:50 +02:00
|
|
|
import org.bukkit.entity.Arrow;
|
|
|
|
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;
|
2013-11-18 20:25:48 +01:00
|
|
|
import org.bukkit.entity.Zombie;
|
2013-11-18 04:24:25 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-08-13 05:19:50 +02:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
2013-12-07 15:30:30 +01:00
|
|
|
import com.comphenix.protocol.PacketType;
|
2013-08-13 05:19:50 +02:00
|
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
|
|
|
import com.comphenix.protocol.ProtocolManager;
|
|
|
|
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;
|
2013-11-18 12:49:04 +01:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
public class PacketsManager {
|
2013-10-18 03:11:35 +02:00
|
|
|
private static boolean cancelSound;
|
2013-10-04 10:21:24 +02:00
|
|
|
private static PacketListener inventoryListenerClient;
|
|
|
|
private static PacketListener inventoryListenerServer;
|
|
|
|
private static boolean inventoryModifierEnabled;
|
2013-10-18 03:11:35 +02:00
|
|
|
private static LibsDisguises libsDisguises;
|
2013-08-13 05:19:50 +02:00
|
|
|
private static PacketListener soundsListener;
|
|
|
|
private static boolean soundsListenerEnabled;
|
|
|
|
private static PacketListener viewDisguisesListener;
|
|
|
|
private static boolean viewDisguisesListenerEnabled;
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static void addPacketListeners(JavaPlugin libsDisguises) {
|
2013-08-13 05:19:50 +02:00
|
|
|
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
|
2013-12-07 15:30:30 +01:00
|
|
|
manager.addPacketListener(new PacketAdapter(libsDisguises, ListenerPriority.HIGH,
|
|
|
|
PacketType.Play.Server.NAMED_ENTITY_SPAWN, PacketType.Play.Server.ENTITY_METADATA,
|
|
|
|
PacketType.Play.Server.ANIMATION, PacketType.Play.Server.ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_LOOK,
|
|
|
|
PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB,
|
|
|
|
PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.SPAWN_ENTITY_LIVING,
|
|
|
|
PacketType.Play.Server.SPAWN_ENTITY_PAINTING, PacketType.Play.Server.COLLECT,
|
|
|
|
PacketType.Play.Server.UPDATE_ATTRIBUTES, PacketType.Play.Server.ENTITY_EQUIPMENT, PacketType.Play.Server.BED,
|
|
|
|
PacketType.Play.Server.ENTITY_STATUS) {
|
2013-08-13 05:19:50 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
|
|
|
final Player observer = event.getPlayer();
|
|
|
|
// First get the entity, the one sending this packet
|
|
|
|
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
|
2013-12-07 15:30:30 +01:00
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read((PacketType.Play.Server.COLLECT == event.getPacketType() ? 1
|
|
|
|
: 0));
|
2013-08-13 05:19:50 +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;
|
|
|
|
PacketContainer[] packets = transformPacket(event.getPacket(), event.getPlayer());
|
|
|
|
if (packets.length == 0)
|
|
|
|
event.setCancelled(true);
|
|
|
|
else {
|
|
|
|
event.setPacket(packets[0]);
|
|
|
|
final PacketContainer[] delayedPackets = new PacketContainer[packets.length - 1];
|
2013-11-23 03:53:17 +01:00
|
|
|
for (int i = 1; i < packets.length; i++) {
|
2013-08-13 05:19:50 +02:00
|
|
|
delayedPackets[i - 1] = packets[i];
|
2013-11-23 03:53:17 +01:00
|
|
|
}
|
|
|
|
if (delayedPackets.length > 0) {
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
for (PacketContainer packet : delayedPackets) {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
|
|
|
|
}
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
2013-11-23 03:53:17 +01:00
|
|
|
});
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Now 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.
|
2013-10-04 10:21:24 +02:00
|
|
|
// Because it kicks you for hacking.
|
2013-12-07 15:30:30 +01:00
|
|
|
manager.addPacketListener(new PacketAdapter(libsDisguises, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY) {
|
2013-08-13 05:19:50 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketReceiving(PacketEvent event) {
|
|
|
|
try {
|
|
|
|
Player observer = event.getPlayer();
|
|
|
|
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
|
2013-12-06 08:19:08 +01:00
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read(ReflectionManager.isAfter17() ? 0 : 1);
|
2013-12-01 13:36:55 +01:00
|
|
|
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow) {
|
2013-08-13 05:19:50 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-13 11:44:54 +02:00
|
|
|
/**
|
|
|
|
* Construct the packets I need to spawn in the disguise
|
|
|
|
*/
|
|
|
|
public static PacketContainer[] constructSpawnPackets(Disguise disguise, Entity disguisedEntity) {
|
2013-08-13 05:19:50 +02:00
|
|
|
if (disguise.getEntity() == null)
|
|
|
|
disguise.setEntity(disguisedEntity);
|
2013-11-19 11:36:25 +01:00
|
|
|
Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity);
|
2013-08-13 05:19:50 +02:00
|
|
|
ArrayList<PacketContainer> packets = new ArrayList<PacketContainer>();
|
2013-11-30 11:09:29 +01:00
|
|
|
// This sends the armor packets so that the player isn't naked.
|
|
|
|
for (int nmsSlot = 0; nmsSlot < 5; nmsSlot++) {
|
|
|
|
int armorSlot = nmsSlot - 1;
|
|
|
|
if (armorSlot < 0)
|
|
|
|
armorSlot = 4;
|
|
|
|
org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(armorSlot);
|
2013-08-13 05:19:50 +02:00
|
|
|
if (itemstack != null && itemstack.getTypeId() != 0) {
|
|
|
|
ItemStack item = null;
|
2013-11-30 11:09:29 +01:00
|
|
|
if (disguisedEntity instanceof LivingEntity) {
|
|
|
|
if (nmsSlot == 0) {
|
2013-11-18 12:49:04 +01:00
|
|
|
item = ((LivingEntity) disguisedEntity).getEquipment().getItemInHand();
|
2013-11-30 11:09:29 +01:00
|
|
|
} else {
|
|
|
|
item = ((LivingEntity) disguisedEntity).getEquipment().getArmorContents()[armorSlot];
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 12:49:04 +01:00
|
|
|
if (item == null || item.getType() == Material.AIR) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT);
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = packet.getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
2013-11-30 11:09:29 +01:00
|
|
|
mods.write(1, nmsSlot);
|
2013-11-18 04:24:25 +01:00
|
|
|
mods.write(2, ReflectionManager.getNmsItem(itemstack));
|
2013-08-13 05:19:50 +02:00
|
|
|
packets.add(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PacketContainer[] spawnPackets = new PacketContainer[2 + packets.size()];
|
|
|
|
for (int i = 0; i < packets.size(); i++) {
|
|
|
|
spawnPackets[i + 2] = packets.get(i);
|
|
|
|
}
|
2013-08-19 19:01:30 +02:00
|
|
|
Location loc = disguisedEntity.getLocation().clone().add(0, getYModifier(disguisedEntity, disguise.getType()), 0);
|
2013-11-27 04:26:31 +01:00
|
|
|
byte yaw = getYaw(disguise.getType(), disguise.getEntity().getType(), (byte) (int) (loc.getYaw() * 256.0F / 360.0F));
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) {
|
|
|
|
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[0] = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB);
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = spawnPackets[0].getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
|
|
|
mods.write(1, (int) Math.floor(loc.getX() * 32));
|
|
|
|
mods.write(2, (int) Math.floor(loc.getY() * 32) + 2);
|
|
|
|
mods.write(3, (int) Math.floor(loc.getZ() * 32));
|
|
|
|
mods.write(4, 1);
|
|
|
|
|
|
|
|
} else if (disguise.getType() == DisguiseType.PAINTING) {
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[0] = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_PAINTING);
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = spawnPackets[0].getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
|
|
|
mods.write(1, loc.getBlockX());
|
|
|
|
mods.write(2, loc.getBlockY());
|
|
|
|
mods.write(3, loc.getBlockZ());
|
|
|
|
mods.write(4, ((int) loc.getYaw()) % 4);
|
2013-11-07 16:37:36 +01:00
|
|
|
int id = ((MiscDisguise) disguise).getData();
|
2013-11-23 20:43:36 +01:00
|
|
|
mods.write(5, ReflectionManager.getEnumArt(Art.values()[id]));
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
// Make the teleport packet to make it visible..
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[1] = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
|
2013-08-13 05:19:50 +02:00
|
|
|
mods = spawnPackets[1].getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
2013-11-06 10:11:39 +01:00
|
|
|
mods.write(1, (int) Math.floor(loc.getX() * 32D));
|
2013-08-13 05:19:50 +02:00
|
|
|
mods.write(2, (int) Math.floor(loc.getY() * 32D));
|
2013-11-06 10:11:39 +01:00
|
|
|
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
|
2013-08-13 05:19:50 +02:00
|
|
|
mods.write(4, yaw);
|
|
|
|
mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
|
|
|
|
|
|
|
|
} else if (disguise.getType().isPlayer()) {
|
|
|
|
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[0] = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
|
2013-11-19 11:36:25 +01:00
|
|
|
StructureModifier<String> stringMods = spawnPackets[0].getStrings();
|
2013-12-05 08:38:41 +01:00
|
|
|
if (stringMods.size() > 0) {
|
|
|
|
for (int i = 0; i < stringMods.size(); i++) {
|
|
|
|
stringMods.write(i, ((PlayerDisguise) disguise).getName());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spawnPackets[0].getModifier().write(1, ReflectionManager.getGameProfile(((PlayerDisguise) disguise).getName()));
|
2013-11-19 11:36:25 +01:00
|
|
|
}
|
|
|
|
StructureModifier<Integer> intMods = spawnPackets[0].getIntegers();
|
|
|
|
intMods.write(0, disguisedEntity.getEntityId());
|
|
|
|
intMods.write(1, (int) Math.floor(loc.getX() * 32));
|
|
|
|
intMods.write(2, (int) Math.floor(loc.getY() * 32));
|
|
|
|
intMods.write(3, (int) Math.floor(loc.getZ() * 32));
|
2013-08-13 05:19:50 +02:00
|
|
|
ItemStack item = null;
|
|
|
|
if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) {
|
2013-11-18 04:24:25 +01:00
|
|
|
item = ((Player) disguisedEntity).getItemInHand();
|
2013-08-13 05:19:50 +02:00
|
|
|
} else if (disguisedEntity instanceof LivingEntity) {
|
2013-11-18 04:24:25 +01:00
|
|
|
item = ((LivingEntity) disguisedEntity).getEquipment().getItemInHand();
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
2013-11-19 11:36:25 +01:00
|
|
|
intMods.write(4, (item == null || item.getType() == Material.AIR ? 0 : item.getTypeId()));
|
|
|
|
StructureModifier<Byte> byteMods = spawnPackets[0].getBytes();
|
|
|
|
byteMods.write(0, yaw);
|
|
|
|
byteMods.write(1, (byte) (int) (loc.getPitch() * 256F / 360F));
|
2013-11-18 12:49:04 +01:00
|
|
|
spawnPackets[0].getDataWatcherModifier().write(0,
|
|
|
|
createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher()));
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
} else if (disguise.getType().isMob()) {
|
|
|
|
|
2013-11-22 21:10:20 +01:00
|
|
|
DisguiseValues values = DisguiseValues.getDisguiseValues(disguise.getType());
|
2013-08-13 05:19:50 +02:00
|
|
|
Vector vec = disguisedEntity.getVelocity();
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[0] = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = spawnPackets[0].getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
|
|
|
mods.write(1, (int) disguise.getType().getEntityType().getTypeId());
|
|
|
|
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;
|
2013-11-18 04:24:25 +01:00
|
|
|
mods.write(2, values.getEntitySize(loc.getX()));
|
2013-08-13 05:19:50 +02:00
|
|
|
mods.write(3, (int) Math.floor(loc.getY() * 32D));
|
2013-11-18 04:24:25 +01:00
|
|
|
mods.write(4, values.getEntitySize(loc.getZ()));
|
2013-08-13 05:19:50 +02:00
|
|
|
mods.write(5, (int) (d2 * 8000.0D));
|
|
|
|
mods.write(6, (int) (d3 * 8000.0D));
|
|
|
|
mods.write(7, (int) (d4 * 8000.0D));
|
|
|
|
mods.write(8, yaw);
|
|
|
|
mods.write(9, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
|
2013-11-18 12:49:04 +01:00
|
|
|
spawnPackets[0].getDataWatcherModifier().write(0,
|
|
|
|
createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher()));
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
} else if (disguise.getType().isMisc()) {
|
|
|
|
|
|
|
|
int id = disguise.getType().getEntityId();
|
|
|
|
int data = 0;
|
2013-11-07 16:37:36 +01:00
|
|
|
if (((MiscDisguise) disguise).getId() >= 0) {
|
2013-11-23 20:43:36 +01:00
|
|
|
if (((MiscDisguise) disguise).getData() >= 0) {
|
2013-08-13 05:19:50 +02:00
|
|
|
data = (((MiscDisguise) disguise).getId() | ((MiscDisguise) disguise).getData() << 16);
|
2013-11-23 20:43:36 +01:00
|
|
|
} else {
|
2013-08-13 05:19:50 +02:00
|
|
|
data = ((MiscDisguise) disguise).getId();
|
2013-11-23 20:43:36 +01:00
|
|
|
}
|
2013-11-07 16:37:36 +01:00
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
// This won't actually work.
|
|
|
|
// But if someone constructing the disguise uses it properly. It will work.
|
|
|
|
if (disguise.getType() == DisguiseType.FISHING_HOOK)
|
|
|
|
data = disguise.getEntity().getEntityId();
|
2013-11-25 04:52:09 +01:00
|
|
|
/* else if (disguise.getType() == DisguiseType.ITEM_FRAME) {
|
|
|
|
data = (int) loc.getYaw();
|
|
|
|
if (data < 0)
|
|
|
|
data = -data;
|
|
|
|
}*/
|
2013-11-19 11:36:25 +01:00
|
|
|
spawnPackets[0] = ProtocolLibrary.getProtocolManager()
|
2013-12-07 15:30:30 +01:00
|
|
|
.createPacketConstructor(PacketType.Play.Server.SPAWN_ENTITY, nmsEntity, id, data)
|
|
|
|
.createPacket(nmsEntity, id, data);
|
2013-11-19 11:36:25 +01:00
|
|
|
spawnPackets[0].getModifier().write(2, (int) Math.floor(loc.getY() * 32D));
|
|
|
|
spawnPackets[0].getModifier().write(8, yaw);
|
2013-08-13 05:19:50 +02:00
|
|
|
|
2013-11-23 18:59:56 +01:00
|
|
|
// Make the teleport packet to make it visible..
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[1] = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
|
2013-11-23 18:59:56 +01:00
|
|
|
StructureModifier<Object> mods = spawnPackets[1].getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
|
|
|
mods.write(1, (int) Math.floor(loc.getX() * 32D));
|
|
|
|
mods.write(2, (int) Math.floor(loc.getY() * 32D));
|
|
|
|
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
|
|
|
|
mods.write(4, yaw);
|
|
|
|
mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
|
2013-11-23 03:25:25 +01:00
|
|
|
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
if (spawnPackets[1] == null) {
|
|
|
|
// Make a packet to turn his head!
|
2013-12-07 15:30:30 +01:00
|
|
|
spawnPackets[1] = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = spawnPackets[1].getModifier();
|
|
|
|
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
|
|
|
}
|
|
|
|
return spawnPackets;
|
|
|
|
}
|
|
|
|
|
2013-08-13 11:44:54 +02:00
|
|
|
/**
|
|
|
|
* Create a new datawatcher but with the 'correct' values
|
|
|
|
*/
|
2013-11-18 12:49:04 +01:00
|
|
|
private static WrappedDataWatcher createDataWatcher(WrappedDataWatcher watcher, FlagWatcher flagWatcher) {
|
|
|
|
WrappedDataWatcher newWatcher = new WrappedDataWatcher();
|
2013-08-13 05:19:50 +02:00
|
|
|
try {
|
|
|
|
// Calling c() gets the watchable objects exactly as they are.
|
2013-11-18 12:49:04 +01:00
|
|
|
List<WrappedWatchableObject> list = watcher.getWatchableObjects();
|
|
|
|
for (WrappedWatchableObject watchableObject : flagWatcher.convert(list)) {
|
|
|
|
newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue());
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return newWatcher;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-23 18:59:56 +01:00
|
|
|
public static byte getPitch(DisguiseType disguiseType, DisguiseType entityType, byte value) {
|
|
|
|
switch (disguiseType) {
|
|
|
|
case MINECART:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
|
|
|
value = (byte) -value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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) {
|
2013-11-23 18:59:56 +01:00
|
|
|
case MINECART:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
|
|
|
value += 64;
|
|
|
|
break;
|
2013-08-13 05:19:50 +02:00
|
|
|
case ENDER_DRAGON:
|
2013-08-19 19:01:30 +02:00
|
|
|
case WITHER_SKULL:
|
2013-08-13 05:19:50 +02:00
|
|
|
value -= 128;
|
|
|
|
break;
|
2013-11-25 04:52:09 +01:00
|
|
|
// case ITEM_FRAME:
|
2013-08-13 05:19:50 +02:00
|
|
|
case ARROW:
|
|
|
|
value = (byte) -value;
|
|
|
|
break;
|
|
|
|
case PAINTING:
|
|
|
|
value = (byte) -(value + 128);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (disguiseType.isMisc()) {
|
|
|
|
value -= 64;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (entityType) {
|
2013-11-23 18:59:56 +01:00
|
|
|
case MINECART:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
|
|
|
value -= 64;
|
|
|
|
break;
|
2013-08-13 05:19:50 +02:00
|
|
|
case ENDER_DRAGON:
|
2013-08-19 19:01:30 +02:00
|
|
|
case WITHER_SKULL:
|
2013-08-13 05:19:50 +02:00
|
|
|
value += 128;
|
|
|
|
break;
|
2013-11-25 04:52:09 +01:00
|
|
|
// case ITEM_FRAME:
|
2013-08-13 05:19:50 +02:00
|
|
|
case ARROW:
|
|
|
|
value = (byte) -value;
|
|
|
|
break;
|
|
|
|
case PAINTING:
|
|
|
|
value = (byte) -(value - 128);
|
|
|
|
break;
|
|
|
|
default:
|
2013-11-27 04:26:31 +01:00
|
|
|
if (!entityType.isAlive()) {
|
2013-08-13 05:19:50 +02:00
|
|
|
value += 64;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-10-04 10:21:24 +02:00
|
|
|
/**
|
|
|
|
* Get the Y level to add to the disguise for realism.
|
|
|
|
*/
|
2013-08-19 19:01:30 +02:00
|
|
|
private static double getYModifier(Entity entity, DisguiseType disguiseType) {
|
|
|
|
switch (disguiseType) {
|
|
|
|
case BAT:
|
|
|
|
if (entity instanceof LivingEntity)
|
|
|
|
return ((LivingEntity) entity).getEyeHeight();
|
|
|
|
case MINECART:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
2013-11-23 18:38:46 +01:00
|
|
|
switch (entity.getType()) {
|
|
|
|
case MINECART:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 0.4;
|
|
|
|
}
|
2013-11-19 11:36:25 +01:00
|
|
|
case ARROW:
|
|
|
|
case BOAT:
|
|
|
|
case EGG:
|
|
|
|
case ENDER_PEARL:
|
|
|
|
case ENDER_SIGNAL:
|
|
|
|
case FIREWORK:
|
2013-08-19 19:01:30 +02:00
|
|
|
case PAINTING:
|
|
|
|
case SMALL_FIREBALL:
|
|
|
|
case SNOWBALL:
|
|
|
|
case SPLASH_POTION:
|
|
|
|
case THROWN_EXP_BOTTLE:
|
|
|
|
case WITHER_SKULL:
|
|
|
|
return 0.7;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2013-12-07 15:30:30 +01:00
|
|
|
soundsListener = new PacketAdapter(libsDisguises, ListenerPriority.NORMAL, PacketType.Play.Server.NAMED_SOUND_EFFECT,
|
|
|
|
PacketType.Play.Server.ENTITY_STATUS) {
|
2013-08-13 05:19:50 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
2013-10-18 02:34:53 +02:00
|
|
|
if (event.isCancelled())
|
|
|
|
return;
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> mods = event.getPacket().getModifier();
|
|
|
|
Player observer = event.getPlayer();
|
2013-12-07 15:30:30 +01:00
|
|
|
if (event.getPacketType() == PacketType.Play.Server.NAMED_SOUND_EFFECT) {
|
2013-08-13 05:19:50 +02:00
|
|
|
String soundName = (String) mods.read(0);
|
|
|
|
SoundType soundType = null;
|
|
|
|
Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(1)) / 8D,
|
|
|
|
((Integer) mods.read(2)) / 8D, ((Integer) mods.read(3)) / 8D);
|
|
|
|
Entity disguisedEntity = null;
|
|
|
|
DisguiseSound entitySound = null;
|
2013-12-01 14:36:42 +01:00
|
|
|
Disguise disguise = null;
|
2013-08-13 05:19:50 +02:00
|
|
|
for (Entity entity : soundLoc.getChunk().getEntities()) {
|
2013-12-01 14:36:42 +01:00
|
|
|
Disguise entityDisguise = DisguiseAPI.getDisguise(observer, entity);
|
|
|
|
if (entityDisguise != null) {
|
2013-08-13 05:19:50 +02:00
|
|
|
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) {
|
2013-11-22 16:20:03 +01:00
|
|
|
Object obj = null;
|
|
|
|
if (entity instanceof LivingEntity) {
|
|
|
|
try {
|
|
|
|
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-11-22 17:02:53 +01:00
|
|
|
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) {
|
2013-11-22 16:20:03 +01:00
|
|
|
soundType = SoundType.DEATH;
|
|
|
|
} else {
|
|
|
|
obj = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
2013-08-13 05:19:50 +02:00
|
|
|
boolean hasInvun = false;
|
2013-11-18 20:25:48 +01:00
|
|
|
Object nmsEntity = ReflectionManager.getNmsEntity(entity);
|
|
|
|
try {
|
2013-11-19 11:38:34 +01:00
|
|
|
Class entityClass = ReflectionManager.getNmsClass("Entity");
|
2013-11-18 20:25:48 +01:00
|
|
|
if (entity instanceof LivingEntity) {
|
2013-11-19 11:38:34 +01:00
|
|
|
hasInvun = entityClass.getField("noDamageTicks").getInt(nmsEntity) == ReflectionManager
|
|
|
|
.getNmsClass("EntityLiving").getField("maxNoDamageTicks")
|
|
|
|
.getInt(nmsEntity);
|
2013-11-18 20:25:48 +01:00
|
|
|
} else {
|
2013-11-19 11:38:34 +01:00
|
|
|
hasInvun = (Boolean) entityClass.getMethod("isInvulnerable").invoke(nmsEntity);
|
2013-11-18 20:25:48 +01:00
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
soundType = entitySound.getType(soundName, !hasInvun);
|
|
|
|
}
|
|
|
|
if (soundType != null) {
|
2013-12-01 14:36:42 +01:00
|
|
|
disguise = entityDisguise;
|
2013-08-13 05:19:50 +02:00
|
|
|
disguisedEntity = entity;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disguise != null) {
|
2013-11-22 21:19:30 +01:00
|
|
|
if (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != event.getPlayer()) {
|
2013-11-22 20:52:15 +01:00
|
|
|
if (disguise.isSoundsReplaced()) {
|
2013-08-13 05:19:50 +02:00
|
|
|
String sound = null;
|
|
|
|
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
|
|
|
|
if (dSound != null && soundType != null)
|
|
|
|
sound = dSound.getSound(soundType);
|
|
|
|
if (sound == null) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
} else {
|
|
|
|
if (sound.equals("step.grass")) {
|
2013-11-18 20:25:48 +01:00
|
|
|
try {
|
|
|
|
int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(),
|
|
|
|
soundLoc.getBlockY() - 1, soundLoc.getBlockZ());
|
|
|
|
Class blockClass = ReflectionManager.getNmsClass("Block");
|
2013-12-06 08:13:45 +01:00
|
|
|
Object block;
|
|
|
|
if (ReflectionManager.isAfter17()) {
|
|
|
|
block = blockClass.getMethod("REGISTRY", int.class).invoke(null, typeId);
|
|
|
|
} else {
|
|
|
|
block = ((Object[]) blockClass.getField("byId").get(null))[typeId];
|
|
|
|
}
|
2013-11-18 20:25:48 +01:00
|
|
|
if (block != null) {
|
|
|
|
Object step = blockClass.getField("stepSound").get(block);
|
|
|
|
mods.write(0, step.getClass().getMethod("getStepSound").invoke(step));
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
// 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, 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 (soundType != SoundType.IDLE
|
|
|
|
&& ((Float) mods.read(4)).equals(entitySound.getDamageSoundVolume())) {
|
|
|
|
mods.write(4, dSound.getDamageSoundVolume());
|
|
|
|
}
|
|
|
|
// 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()) {
|
2013-11-18 20:25:48 +01:00
|
|
|
boolean baby = false;
|
|
|
|
if (disguisedEntity instanceof Zombie) {
|
|
|
|
baby = ((Zombie) disguisedEntity).isBaby();
|
|
|
|
} else if (disguisedEntity instanceof Ageable) {
|
|
|
|
baby = !((Ageable) disguisedEntity).isAdult();
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
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;
|
|
|
|
pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F;
|
|
|
|
// 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;
|
|
|
|
pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F;
|
|
|
|
// Min = 1
|
|
|
|
// Cap = 63
|
|
|
|
// Max = 1.2
|
|
|
|
// Cap = 75.6
|
|
|
|
}
|
|
|
|
pitch *= 63;
|
|
|
|
if (pitch < 0)
|
|
|
|
pitch = 0;
|
|
|
|
if (pitch > 255)
|
|
|
|
pitch = 255;
|
|
|
|
mods.write(5, (int) pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 15:30:30 +01:00
|
|
|
} else if (event.getPacketType() == PacketType.Play.Server.ENTITY_STATUS) {
|
2013-11-18 04:24:25 +01:00
|
|
|
if ((Byte) mods.read(1) == 1) {
|
2013-08-13 05:19:50 +02:00
|
|
|
// It made a damage animation
|
|
|
|
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0);
|
2013-12-01 14:36:42 +01:00
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
|
2013-11-22 21:19:30 +01:00
|
|
|
if (disguise != null && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) {
|
2013-08-13 05:19:50 +02:00
|
|
|
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name());
|
|
|
|
if (disSound == null)
|
|
|
|
return;
|
|
|
|
SoundType soundType = null;
|
2013-11-22 16:20:03 +01:00
|
|
|
Object obj = null;
|
|
|
|
if (entity instanceof LivingEntity) {
|
|
|
|
try {
|
|
|
|
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-11-22 17:02:53 +01:00
|
|
|
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) {
|
2013-11-22 16:20:03 +01:00
|
|
|
soundType = SoundType.DEATH;
|
|
|
|
} else {
|
|
|
|
obj = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
2013-08-13 05:19:50 +02:00
|
|
|
soundType = SoundType.HURT;
|
|
|
|
}
|
|
|
|
if (disSound.getSound(soundType) == null
|
2013-11-22 21:19:30 +01:00
|
|
|
|| (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) {
|
|
|
|
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) {
|
2013-10-18 02:34:53 +02:00
|
|
|
cancelSound = !cancelSound;
|
|
|
|
if (cancelSound)
|
|
|
|
return;
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
disSound = DisguiseSound.getType(disguise.getType().name());
|
|
|
|
if (disSound != null) {
|
|
|
|
String sound = disSound.getSound(soundType);
|
|
|
|
if (sound != null) {
|
|
|
|
Location loc = entity.getLocation();
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT);
|
2013-08-13 05:19:50 +02:00
|
|
|
mods = packet.getModifier();
|
|
|
|
mods.write(0, sound);
|
|
|
|
mods.write(1, (int) (loc.getX() * 8D));
|
|
|
|
mods.write(2, (int) (loc.getY() * 8D));
|
|
|
|
mods.write(3, (int) (loc.getZ() * 8D));
|
|
|
|
mods.write(4, disSound.getDamageSoundVolume());
|
|
|
|
float pitch;
|
|
|
|
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) {
|
|
|
|
pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F;
|
|
|
|
} else
|
|
|
|
pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F;
|
|
|
|
if (disguise.getType() == DisguiseType.BAT)
|
|
|
|
pitch *= 95F;
|
|
|
|
pitch *= 63;
|
|
|
|
if (pitch < 0)
|
|
|
|
pitch = 0;
|
|
|
|
if (pitch > 255)
|
|
|
|
pitch = 255;
|
|
|
|
mods.write(5, (int) pitch);
|
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-12-07 15:30:30 +01:00
|
|
|
viewDisguisesListener = new PacketAdapter(libsDisguises, ListenerPriority.HIGH,
|
|
|
|
PacketType.Play.Server.NAMED_ENTITY_SPAWN, PacketType.Play.Server.ATTACH_ENTITY,
|
|
|
|
PacketType.Play.Server.REL_ENTITY_MOVE, PacketType.Play.Server.ENTITY_MOVE_LOOK,
|
|
|
|
PacketType.Play.Server.ENTITY_LOOK, PacketType.Play.Server.ENTITY_TELEPORT,
|
|
|
|
PacketType.Play.Server.ENTITY_HEAD_ROTATION, PacketType.Play.Server.ENTITY_METADATA,
|
|
|
|
PacketType.Play.Server.ENTITY_EQUIPMENT, PacketType.Play.Server.ANIMATION, PacketType.Play.Server.BED,
|
|
|
|
PacketType.Play.Server.ENTITY_EFFECT, PacketType.Play.Server.ENTITY_VELOCITY,
|
|
|
|
PacketType.Play.Server.UPDATE_ATTRIBUTES) {
|
2013-08-13 05:19:50 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
2013-11-25 03:45:23 +01:00
|
|
|
final Player observer = event.getPlayer();
|
2013-11-25 04:26:24 +01:00
|
|
|
if (event.getPacket().getIntegers().read(0) == observer.getEntityId()) {
|
2013-11-25 03:45:23 +01:00
|
|
|
int fakeId = DisguiseAPI.getFakeDisguise(observer.getEntityId());
|
2013-08-13 05:19:50 +02:00
|
|
|
if (fakeId > 0) {
|
2013-10-18 02:34:53 +02:00
|
|
|
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
|
2013-11-25 03:45:23 +01:00
|
|
|
PacketContainer[] packets = transformPacket(event.getPacket(), observer);
|
2013-11-23 18:31:41 +01:00
|
|
|
final PacketContainer[] delayedPackets = new PacketContainer[packets.length > 0 ? packets.length - 1 : 0];
|
2013-11-23 03:53:17 +01:00
|
|
|
for (int i = 0; i < packets.length; i++) {
|
|
|
|
PacketContainer packet = packets[i];
|
|
|
|
if (packet.equals(event.getPacket()))
|
|
|
|
packet = packet.deepClone();
|
|
|
|
packet.getModifier().write(0, fakeId);
|
|
|
|
if (i == 0) {
|
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-25 03:45:23 +01:00
|
|
|
delayedPackets[i - 1] = packet;
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
2013-11-23 03:53:17 +01:00
|
|
|
}
|
|
|
|
if (delayedPackets.length > 0) {
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
for (PacketContainer packet : delayedPackets) {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
|
|
|
|
}
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
|
2013-12-07 15:30:30 +01:00
|
|
|
if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
|
2013-08-13 05:19:50 +02:00
|
|
|
event.setPacket(event.getPacket().deepClone());
|
2013-11-18 12:49:04 +01:00
|
|
|
Iterator<WrappedWatchableObject> itel = event.getPacket().getWatchableCollectionModifier().read(0)
|
|
|
|
.iterator();
|
2013-08-13 05:19:50 +02:00
|
|
|
while (itel.hasNext()) {
|
2013-11-18 12:49:04 +01:00
|
|
|
WrappedWatchableObject watch = itel.next();
|
2013-11-18 20:48:26 +01:00
|
|
|
if (watch.getIndex() == 0) {
|
2013-11-18 12:49:04 +01:00
|
|
|
byte b = (Byte) watch.getValue();
|
2013-08-13 05:19:50 +02:00
|
|
|
byte a = (byte) (b | 1 << 5);
|
|
|
|
if ((b & 1 << 3) != 0)
|
|
|
|
a = (byte) (a | 1 << 3);
|
2013-11-18 12:49:04 +01:00
|
|
|
watch.setValue(a);
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 15:33:56 +01:00
|
|
|
} else if (event.getPacketType() == PacketType.Play.Server.NAMED_ENTITY_SPAWN) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
|
2013-11-25 03:45:23 +01:00
|
|
|
StructureModifier<Object> mods = packet.getModifier();
|
|
|
|
mods.write(0, observer.getEntityId());
|
|
|
|
List<WrappedWatchableObject> watchableList = new ArrayList<WrappedWatchableObject>();
|
|
|
|
byte b = (byte) (0 | 1 << 5);
|
|
|
|
if (observer.isSprinting())
|
|
|
|
b = (byte) (b | 1 << 3);
|
|
|
|
watchableList.add(new WrappedWatchableObject(0, b));
|
|
|
|
packet.getWatchableCollectionModifier().write(0, watchableList);
|
|
|
|
event.setPacket(packet);
|
2013-12-07 15:33:56 +01:00
|
|
|
} else if (event.getPacketType() == PacketType.Play.Server.ATTACH_ENTITY
|
2013-12-07 15:30:30 +01:00
|
|
|
|| event.getPacketType() == PacketType.Play.Server.REL_ENTITY_MOVE
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_MOVE_LOOK
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_LOOK
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_TELEPORT
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_HEAD_ROTATION
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_EFFECT
|
|
|
|
|| event.getPacketType() == PacketType.Play.Server.ENTITY_EQUIPMENT) {
|
2013-11-25 03:45:23 +01:00
|
|
|
event.setCancelled(true);
|
2013-12-07 15:30:30 +01:00
|
|
|
}
|
2013-09-29 00:15:12 +02:00
|
|
|
|
2013-12-07 15:30:30 +01:00
|
|
|
/* case PacketType.Play.Server.ENTITY_STATUS:
|
2013-11-25 03:45:23 +01:00
|
|
|
if (DisguiseAPI.getDisguise(entity).canHearSelfDisguise()
|
|
|
|
&& (Byte) event.getPacket().getModifier().read(1) == 1) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
break;*/
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-10-04 10:21:24 +02:00
|
|
|
// TODO Potentionally combine both listeners.
|
2013-12-07 15:30:30 +01:00
|
|
|
inventoryListenerServer = new PacketAdapter(libsDisguises, ListenerPriority.HIGHEST, PacketType.Play.Server.SET_SLOT,
|
|
|
|
PacketType.Play.Server.WINDOW_ITEMS) {
|
2013-10-04 10:21:24 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
|
|
|
// If the inventory is the players inventory
|
2013-12-02 11:21:10 +01:00
|
|
|
if (event.getPlayer().isOnline() && event.getPlayer().getVehicle() == null
|
|
|
|
&& event.getPacket().getIntegers().read(0) == 0) {
|
2013-12-01 14:36:42 +01:00
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
|
2013-10-04 10:21:24 +02:00
|
|
|
// If the player is disguised, views self disguises and is hiding a item.
|
2013-11-22 20:52:15 +01:00
|
|
|
if (disguise != null && disguise.isSelfDisguiseVisible()
|
2013-10-04 10:21:24 +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
|
2013-10-18 01:53:56 +02:00
|
|
|
/**
|
|
|
|
* Done
|
|
|
|
*/
|
2013-12-07 15:30:30 +01:00
|
|
|
if (event.getPacketType() == PacketType.Play.Server.SET_SLOT) {
|
2013-10-04 10:21:24 +02: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()
|
2013-11-18 04:24:25 +01:00
|
|
|
.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
|
2013-10-04 10:21:24 +02: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());
|
2013-11-18 12:49:04 +01:00
|
|
|
event.getPacket()
|
|
|
|
.getModifier()
|
2013-11-18 04:24:25 +01:00
|
|
|
.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
|
2013-10-04 10:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 15:33:56 +01:00
|
|
|
} else if (event.getPacketType() == PacketType.Play.Server.WINDOW_ITEMS) {
|
2013-10-07 18:56:47 +02:00
|
|
|
event.setPacket(event.getPacket().deepClone());
|
2013-11-18 12:49:04 +01:00
|
|
|
StructureModifier<ItemStack[]> mods = event.getPacket().getItemArrayModifier();
|
|
|
|
ItemStack[] items = mods.read(0);
|
2013-10-07 18:56:47 +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);
|
|
|
|
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
|
|
|
|
if (item != null && item.getType() != Material.AIR) {
|
2013-11-18 12:49:04 +01:00
|
|
|
items[slot] = new org.bukkit.inventory.ItemStack(0);
|
2013-10-07 18:56:47 +02: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) {
|
2013-11-18 12:49:04 +01:00
|
|
|
items[slot] = new org.bukkit.inventory.ItemStack(0);
|
2013-10-07 18:56:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 12:49:04 +01:00
|
|
|
mods.write(0, items);
|
2013-10-04 10:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-12-07 15:30:30 +01:00
|
|
|
inventoryListenerClient = new PacketAdapter(libsDisguises, ListenerPriority.HIGHEST,
|
|
|
|
PacketType.Play.Client.HELD_ITEM_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT,
|
|
|
|
PacketType.Play.Client.WINDOW_CLICK) {
|
2013-10-04 10:21:24 +02:00
|
|
|
@Override
|
2013-10-18 01:53:56 +02:00
|
|
|
public void onPacketReceiving(final PacketEvent event) {
|
2013-11-05 18:09:44 +01:00
|
|
|
if (event.getPlayer().getVehicle() == null) {
|
2013-12-01 14:36:42 +01:00
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
|
2013-11-05 18:09:44 +01:00
|
|
|
// If player is disguised, views self disguises and has a inventory modifier
|
2013-11-22 20:52:15 +01:00
|
|
|
if (disguise != null && disguise.isSelfDisguiseVisible()
|
2013-11-05 18:09:44 +01:00
|
|
|
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
|
|
|
|
// If they are in creative and clicked on a slot
|
2013-12-07 15:30:30 +01:00
|
|
|
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) {
|
2013-11-05 18:09:44 +01:00
|
|
|
int slot = event.getPacket().getIntegers().read(0);
|
|
|
|
if (slot >= 5 && slot <= 8) {
|
|
|
|
if (disguise.isHidingArmorFromSelf()) {
|
2013-11-26 23:40:38 +01:00
|
|
|
int armorSlot = Math.abs((slot - 5) - 3);
|
2013-11-05 18:09:44 +01:00
|
|
|
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
|
2013-10-04 10:21:24 +02:00
|
|
|
if (item != null && item.getType() != Material.AIR) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-10-04 10:21:24 +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)));
|
2013-10-04 10:21:24 +02:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
|
|
|
|
false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:09: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) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-11-05 18:09:44 +01: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)));
|
2013-11-05 18:09:44 +01:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
|
|
|
|
false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-04 10:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:09:44 +01:00
|
|
|
// If the player switched item, aka he moved from slot 1 to slot 2
|
2013-12-07 15:30:30 +01:00
|
|
|
else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT) {
|
2013-11-05 18:09:44 +01:00
|
|
|
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) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-11-05 18:09:44 +01:00
|
|
|
StructureModifier<Object> mods = packet.getModifier();
|
|
|
|
mods.write(0, 0);
|
|
|
|
mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36);
|
2013-11-18 04:24:25 +01:00
|
|
|
mods.write(2, ReflectionManager.getNmsItem(currentlyHeld));
|
2013-11-05 18:09:44 +01:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
2013-10-04 10:21:24 +02:00
|
|
|
}
|
2013-11-05 18:09:44 +01:00
|
|
|
}
|
|
|
|
org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory()
|
|
|
|
.getItem(event.getPacket().getIntegers().read(0));
|
|
|
|
// If his new weapon isn't air either!
|
|
|
|
if (newHeld != null && newHeld.getType() != Material.AIR) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-10-04 10:21:24 +02:00
|
|
|
StructureModifier<Object> mods = packet.getModifier();
|
|
|
|
mods.write(0, 0);
|
2013-11-05 18:09:44 +01:00
|
|
|
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
|
2013-11-18 04:24:25 +01:00
|
|
|
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
|
2013-10-04 10:21:24 +02:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:09:44 +01:00
|
|
|
}
|
2013-12-07 15:33:56 +01:00
|
|
|
} else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) {
|
2013-11-05 18:09:44 +01:00
|
|
|
int slot = event.getPacket().getIntegers().read(1);
|
|
|
|
org.bukkit.inventory.ItemStack clickedItem;
|
|
|
|
if (event.getPacket().getIntegers().read(3) == 1) {
|
|
|
|
// 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().scheduleSyncDelayedTask(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()) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-10-04 10:21:24 +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)));
|
2013-10-04 10:21:24 +02:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
|
|
|
|
false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:09: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) {
|
2013-12-07 15:30:30 +01:00
|
|
|
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SET_SLOT);
|
2013-11-05 18:09:44 +01: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)));
|
2013-11-05 18:09:44 +01:00
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
|
|
|
|
false);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-04 10:21:24 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:09: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) {
|
|
|
|
if (soundsListenerEnabled != enabled) {
|
|
|
|
soundsListenerEnabled = enabled;
|
|
|
|
if (soundsListenerEnabled) {
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(soundsListener);
|
|
|
|
} else {
|
|
|
|
ProtocolLibrary.getProtocolManager().removePacketListener(soundsListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 10:21:24 +02:00
|
|
|
public static void setInventoryListenerEnabled(boolean enabled) {
|
|
|
|
if (inventoryModifierEnabled != enabled) {
|
|
|
|
inventoryModifierEnabled = enabled;
|
|
|
|
if (inventoryModifierEnabled) {
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(inventoryListenerClient);
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(inventoryListenerServer);
|
|
|
|
} else {
|
|
|
|
ProtocolLibrary.getProtocolManager().removePacketListener(inventoryListenerClient);
|
|
|
|
ProtocolLibrary.getProtocolManager().removePacketListener(inventoryListenerServer);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Transform the packet magically into the one I have always dreamed off. My true luv!!!
|
|
|
|
*/
|
2013-08-13 05:19:50 +02:00
|
|
|
private static PacketContainer[] transformPacket(PacketContainer sentPacket, Player observer) {
|
|
|
|
PacketContainer[] packets = new PacketContainer[] { sentPacket };
|
|
|
|
try {
|
|
|
|
// First get the entity, the one sending this packet
|
|
|
|
StructureModifier<Entity> entityModifer = sentPacket.getEntityModifier(observer.getWorld());
|
2013-12-07 15:30:30 +01:00
|
|
|
org.bukkit.entity.Entity entity = entityModifer
|
|
|
|
.read((PacketType.Play.Server.COLLECT == sentPacket.getType() ? 1 : 0));
|
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) {
|
2013-12-07 15:30:30 +01:00
|
|
|
// If packet is PacketType.Play.Server.UPDATE_ATTRIBUTES
|
2013-08-13 05:19:50 +02:00
|
|
|
// This packet sends attributes
|
2013-12-07 15:33:56 +01:00
|
|
|
if (sentPacket.getType() == PacketType.Play.Server.UPDATE_ATTRIBUTES) {
|
2013-11-18 12:49:04 +01:00
|
|
|
packets = new PacketContainer[0];
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Else if the packet is sending entity metadata
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_METADATA) {
|
2013-11-18 12:49:04 +01:00
|
|
|
List<WrappedWatchableObject> watchableObjects = disguise.getWatcher().convert(
|
|
|
|
packets[0].getWatchableCollectionModifier().read(0));
|
2013-12-07 15:30:30 +01:00
|
|
|
packets[0] = new PacketContainer(sentPacket.getType());
|
2013-08-13 05:19:50 +02:00
|
|
|
StructureModifier<Object> newMods = packets[0].getModifier();
|
|
|
|
newMods.write(0, entity.getEntityId());
|
2013-11-18 12:49:04 +01:00
|
|
|
packets[0].getWatchableCollectionModifier().write(0, watchableObjects);
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Else if the packet is spawning..
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.NAMED_ENTITY_SPAWN
|
2013-12-07 15:30:30 +01:00
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING
|
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB
|
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY
|
2013-12-07 15:33:56 +01:00
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_PAINTING) {
|
2013-08-13 11:44:54 +02:00
|
|
|
packets = constructSpawnPackets(disguise, entity);
|
2013-12-07 15:33:56 +01:00
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
|
|
|
|
// Else if the disguise is attempting to send players a forbidden packet
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.ANIMATION) {
|
2013-10-18 02:58:05 +02:00
|
|
|
if (disguise.getType().isMisc() || (packets[0].getIntegers().read(1) == 3 && !disguise.getType().isPlayer())) {
|
|
|
|
packets = new PacketContainer[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.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];
|
2013-12-01 14:36:42 +01:00
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
2013-12-07 15:33:56 +01:00
|
|
|
|
2013-08-13 05:19:50 +02:00
|
|
|
// Else if the disguise is moving.
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_MOVE_LOOK
|
2013-12-07 15:30:30 +01:00
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK
|
2013-12-07 15:33:56 +01:00
|
|
|
|| sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
|
2013-12-07 15:30:30 +01:00
|
|
|
if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK
|
|
|
|
&& disguise.getType() == DisguiseType.WITHER_SKULL) {
|
2013-08-19 19:01:30 +02:00
|
|
|
packets = new PacketContainer[0];
|
|
|
|
} else {
|
|
|
|
packets[0] = sentPacket.shallowClone();
|
|
|
|
StructureModifier<Object> mods = packets[0].getModifier();
|
2013-11-23 18:59:56 +01:00
|
|
|
byte yawValue = (Byte) mods.read(4);
|
2013-11-27 04:26:31 +01:00
|
|
|
mods.write(4, getYaw(disguise.getType(), entity.getType(), yawValue));
|
2013-11-23 18:59:56 +01:00
|
|
|
byte pitchValue = (Byte) mods.read(5);
|
|
|
|
mods.write(5, getPitch(disguise.getType(), DisguiseType.getType(entity.getType()), pitchValue));
|
2013-12-07 15:30:30 +01:00
|
|
|
if (sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
|
2013-08-19 19:01:30 +02:00
|
|
|
double y = getYModifier(entity, disguise.getType());
|
|
|
|
if (y != 0) {
|
|
|
|
y *= 32;
|
|
|
|
mods.write(2, (Integer) mods.read(2) + (int) Math.floor(y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_EQUIPMENT) {
|
2013-08-13 05:19:50 +02:00
|
|
|
int slot = (Integer) packets[0].getModifier().read(1) - 1;
|
|
|
|
if (slot < 0)
|
|
|
|
slot = 4;
|
|
|
|
org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot);
|
|
|
|
if (itemstack != null) {
|
|
|
|
packets[0] = packets[0].shallowClone();
|
|
|
|
packets[0].getModifier().write(2,
|
2013-11-18 04:24:25 +01:00
|
|
|
(itemstack.getTypeId() == 0 ? null : ReflectionManager.getNmsItem(itemstack)));
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.BED) {
|
2013-09-29 00:15:12 +02:00
|
|
|
if (!disguise.getType().isPlayer()) {
|
|
|
|
packets = new PacketContainer[0];
|
|
|
|
}
|
|
|
|
}
|
2013-11-30 10:22:33 +01:00
|
|
|
|
2013-12-07 15:33:56 +01:00
|
|
|
else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_STATUS) {
|
2013-11-30 10:22:33 +01:00
|
|
|
if (packets[0].getBytes().read(0) == (byte) 3) {
|
|
|
|
packets = new PacketContainer[0];
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 05:19:50 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return packets;
|
|
|
|
}
|
|
|
|
}
|