Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.vinetos.disguise.utils;
- import fr.vinetos.disguise.Disguise;
- import io.netty.channel.Channel;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.EntityType;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerQuitEvent;
- import org.bukkit.event.server.PluginDisableEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.scheduler.BukkitRunnable;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- /**
- * @author cocoraid https://www.mcdev.fr/threads/déguiser-un-joueur-en-mob-toute-les-versions.1061/
- * last Modif by Vinetos (06/05/2016 19:10)
- */
- public class DisguiseUtil implements Listener {
- private static Map<UUID, DisguiseUtil> disguise = new HashMap<>();
- private static Disguise instance = Disguise.getInstance();
- Class<?> packetEntityClass;
- private Reflection.MethodInvoker getHandle;
- private Reflection.FieldAccessor<?> playerConnection;
- private Reflection.MethodInvoker sendPacket;
- private Player p;
- private boolean disguised;
- private EntityType entityType;
- private boolean is1_9OrNewer = is1_9();
- @SuppressWarnings("deprecation")
- public DisguiseUtil(EntityType entityType, Player p) {
- this.p = p;
- this.entityType = entityType;
- disguised = false;
- getHandle = Reflection.getMethod("{obc}.entity.CraftPlayer",
- "getHandle");
- playerConnection = Reflection.getField("{nms}.EntityPlayer",
- "playerConnection", Object.class);
- sendPacket = Reflection.getMethod("{nms}.PlayerConnection",
- "sendPacket", Object.class);
- packetEntityClass = Reflection.getClass("{nms}.Entity"
- + entityType.getName());
- disguise.put(p.getUniqueId(), this);
- }
- public static void register() {
- new PacketEvent();
- instance.getServer().getPluginManager()
- .registerEvents(new Listener() {
- @EventHandler
- public void onPlayerQuitEvent(PlayerQuitEvent e) {
- Player p = e.getPlayer();
- DisguiseUtil d = DisguiseUtil.findByPlayer(p);
- if (d == null) return;
- if (d.isDisguised())
- d.setDisguised(false);
- d.remove();
- }
- @EventHandler
- public void onDisable(PluginDisableEvent e) {
- if (e.getPlugin().equals(instance)) {
- for (DisguiseUtil disguise : DisguiseUtil.disguise.values()) {
- disguise.removeDisguise();
- }
- }
- }
- }, instance);
- }
- public static Map<UUID, DisguiseUtil> getDisguised() {
- return disguise;
- }
- public static DisguiseUtil findByPlayer(Player p) {
- return findByUniqueId(p.getUniqueId());
- }
- public static DisguiseUtil findByUniqueId(UUID uuid) {
- return disguise.containsKey(uuid) ? disguise.get(uuid) : null;
- }
- public void disguisePlayer(Player cur) {
- Reflection.ConstructorInvoker packetEntityConstructor = Reflection.getConstructor(
- packetEntityClass, Reflection.getClass("{nms}.World"));
- Reflection.MethodInvoker getHandleWorld = Reflection.getMethod("{obc}.CraftWorld",
- "getHandle");
- Object entity = packetEntityConstructor.invoke(getHandleWorld.invoke(p
- .getWorld()));
- if (this.entityType == EntityType.BAT) {
- Reflection.MethodInvoker setAsleep = Reflection.getMethod(Reflection.getClass("{nms}.EntityBat"), "setAsleep", boolean.class);
- setAsleep.invoke(entity, false);
- }
- Reflection.MethodInvoker setPos = Reflection.getMethod(
- Reflection.getClass("{nms}.Entity"), "setLocation",
- double.class, double.class, double.class, float.class,
- float.class);
- setPos.invoke(entity, p.getLocation().getX(), p.getLocation().getY(), p
- .getLocation().getZ(), p.getLocation().getYaw(), p
- .getLocation().getPitch());
- Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
- Reflection.getClass("{nms}.PacketPlayOutSpawnEntityLiving"),
- Reflection.getClass("{nms}.EntityLiving"));
- Object packetSpawn = packetSpawnConstructor.invoke(entity);
- Reflection.getField(packetSpawn.getClass(), int.class, 0).set(
- packetSpawn, getplayerID());
- hideEntity(cur);
- sendPacket(packetSpawn, cur);
- setEquipment(cur);
- }
- public void disguisePlayer() {
- Reflection.ConstructorInvoker packetEntityConstructor = Reflection.getConstructor(
- packetEntityClass, Reflection.getClass("{nms}.World"));
- Reflection.MethodInvoker getHandleWorld = Reflection.getMethod("{obc}.CraftWorld",
- "getHandle");
- Object entity = packetEntityConstructor.invoke(getHandleWorld.invoke(p
- .getWorld()));
- if (this.entityType == EntityType.BAT) {
- Reflection.MethodInvoker setAsleep = Reflection.getMethod(Reflection.getClass("{nms}.EntityBat"), "setAsleep", boolean.class);
- setAsleep.invoke(entity, false);
- }
- Reflection.MethodInvoker setPos = Reflection.getMethod(
- Reflection.getClass("{nms}.Entity"), "setLocation",
- double.class, double.class, double.class, float.class,
- float.class);
- setPos.invoke(entity, p.getLocation().getX(), p.getLocation().getY(), p
- .getLocation().getZ(), p.getLocation().getYaw(), p
- .getLocation().getPitch());
- Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
- Reflection.getClass("{nms}.PacketPlayOutSpawnEntityLiving"),
- Reflection.getClass("{nms}.EntityLiving"));
- Object packetSpawn = packetSpawnConstructor.invoke(entity);
- Reflection.getField(packetSpawn.getClass(), int.class, 0).set(
- packetSpawn, getplayerID());
- hideEntity();
- sendPacket(packetSpawn);
- setEquipment();
- disguised = true;
- }
- private void setEquipment() {
- setEquipment(p);
- }
- private void setEquipment(Player p) {
- sendPacket(buildEquipmentPacket(0, p.getInventory().getItemInMainHand()));
- if(is1_9OrNewer) {
- /*On 1.9: 0 = Main Hand ; 1 = Off Hand ; 2 = Boots ; 3 = Leggings ; 4 = Chest ; 5 = Helmet */
- sendPacket(buildEquipmentPacket(2, p.getInventory().getBoots()));
- sendPacket(buildEquipmentPacket(3, p.getInventory().getLeggings()));
- sendPacket(buildEquipmentPacket(4, p.getInventory().getChestplate()));
- sendPacket(buildEquipmentPacket(5, p.getInventory().getHelmet()));
- } else {
- /*On 1.9: 0 = Main Hand ; 1 = Boots ; 2 = Leggings ; 3 = Chest ; 4 = Helmet */
- sendPacket(buildEquipmentPacket(1, p.getInventory().getBoots()));
- sendPacket(buildEquipmentPacket(2, p.getInventory().getLeggings()));
- sendPacket(buildEquipmentPacket(3, p.getInventory().getChestplate()));
- sendPacket(buildEquipmentPacket(4, p.getInventory().getHelmet()));
- }
- }
- private Object buildEquipmentPacket(int slot, ItemStack item) {
- Class<?> is = Reflection.getClass("{obc}.inventory.CraftItemStack");
- Reflection.MethodInvoker getHandleItemStack = Reflection.getMethod(is,
- "asNMSCopy", ItemStack.class);
- Reflection.ConstructorInvoker packetEquipConstructor = Reflection
- .getConstructor(Reflection
- .getClass("{nms}.PacketPlayOutEntityEquipment"));
- Object packet = packetEquipConstructor.invoke();
- Reflection.getField(packet.getClass(), int.class, 0).set(
- packet, getplayerID());
- if (is1_9OrNewer) {
- Class<?> enumItemSlot = Reflection.getMinecraftClass("EnumItemSlot");
- Reflection.getField(packet.getClass(), enumItemSlot, 0).set(
- packet, enumItemSlot.getEnumConstants()[slot]);
- }
- else
- Reflection.getField(packet.getClass(), int.class, 1).set(
- packet, slot);
- Reflection.getField(packet.getClass(),
- Reflection.getMinecraftClass("ItemStack"), 0).set(
- packet,
- getHandleItemStack.invoke(is, item));
- return packet;
- }
- public void removeDisguise() {
- hideEntity();
- Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
- Reflection.getClass("{nms}.PacketPlayOutNamedEntitySpawn"),
- Reflection.getClass("{nms}.EntityHuman"));
- Object packetSpawn = packetSpawnConstructor.invoke(this.getHandle
- .invoke(p));
- sendPacket(packetSpawn);
- setEquipment();
- remove();
- }
- private void sendPacket(Object packet) {
- for (Player cur : Bukkit.getOnlinePlayers()) {
- if (!cur.equals(p)) {
- sendPacket.invoke(
- playerConnection.get(this.getHandle.invoke(cur)),
- packet);
- }
- }
- }
- private void sendPacket(Object packet, Player cur) {
- if (!cur.equals(p)) {
- sendPacket.invoke(playerConnection.get(this.getHandle.invoke(cur)),
- packet);
- }
- }
- private int getplayerID() {
- Reflection.MethodInvoker getHandle = Reflection.getMethod(
- "{obc}.entity.CraftEntity", "getHandle");
- return Reflection.getField(Reflection.getMinecraftClass("Entity"),
- int.class, 2).get(getHandle.invoke(p));
- }
- private void hideEntity() {
- Reflection.ConstructorInvoker packetDestroyConstructor = Reflection
- .getConstructor(Reflection
- .getClass("{nms}.PacketPlayOutEntityDestroy"));
- Object packetDestroy = packetDestroyConstructor.invoke();
- int[] id;
- id = new int[1];
- id[0] = getplayerID();
- Reflection.getField(packetDestroy.getClass(), Object.class, 0).set(
- packetDestroy, id);
- sendPacket(packetDestroy);
- }
- private void hideEntity(Player cur) {
- Reflection.ConstructorInvoker packetDestroyConstructor = Reflection
- .getConstructor(Reflection
- .getClass("{nms}.PacketPlayOutEntityDestroy"));
- Object packetDestroy = packetDestroyConstructor.invoke();
- int[] id;
- id = new int[1];
- id[0] = getplayerID();
- Reflection.getField(packetDestroy.getClass(), Object.class, 0).set(
- packetDestroy, id);
- sendPacket(packetDestroy, cur);
- }
- public boolean isDisguised() {
- return disguised;
- }
- public void setDisguised(boolean disguised) {
- this.disguised = disguised;
- }
- private void remove() {
- getDisguised().remove(this);
- }
- private boolean is1_9() {
- String packageName = Bukkit.getServer().getClass().getPackage().getName();
- return packageName.substring(packageName.lastIndexOf('.') + 1).contains("1_9");
- }
- static class PacketEvent extends TinyProtocol.PacketListener {
- private Class<?> packet = Reflection
- .getClass("{nms}.PacketPlayOutNamedEntitySpawn");
- @Override
- public Object onPacketOutAsync(final Player receiver, Channel channel,
- Object packet) {
- if (!this.packet.isInstance(packet))
- return packet;
- UUID id = Reflection.getField(packet.getClass(), UUID.class, 0)
- .get(packet);
- final Player p = Bukkit.getPlayer(id);
- if (DisguiseUtil.disguise.containsKey(id)) {
- if (findByPlayer(p).disguised) {
- Bukkit.getScheduler().runTask(Disguise.getInstance(), new Runnable() {
- @Override
- public void run() {
- new DisguiseUtil(EntityType.PLAYER, receiver).disguisePlayer(receiver);
- }
- });
- }
- }
- return super.onPacketOutAsync(receiver, channel, packet);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment