Vinetos

DisguiseUtil

Jun 5th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.66 KB | None | 0 0
  1. package fr.vinetos.disguise.utils;
  2.  
  3. import fr.vinetos.disguise.Disguise;
  4. import io.netty.channel.Channel;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.entity.EntityType;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerQuitEvent;
  11. import org.bukkit.event.server.PluginDisableEvent;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.scheduler.BukkitRunnable;
  14.  
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.UUID;
  18.  
  19. /**
  20.  * @author cocoraid  https://www.mcdev.fr/threads/déguiser-un-joueur-en-mob-toute-les-versions.1061/
  21.  * last Modif by Vinetos (06/05/2016 19:10)
  22.  */
  23. public class DisguiseUtil implements Listener {
  24.  
  25.     private static Map<UUID, DisguiseUtil> disguise = new HashMap<>();
  26.     private static Disguise instance = Disguise.getInstance();
  27.     Class<?> packetEntityClass;
  28.     private Reflection.MethodInvoker getHandle;
  29.     private Reflection.FieldAccessor<?> playerConnection;
  30.     private Reflection.MethodInvoker sendPacket;
  31.     private Player p;
  32.     private boolean disguised;
  33.     private EntityType entityType;
  34.     private boolean is1_9OrNewer = is1_9();
  35.  
  36.  
  37.     @SuppressWarnings("deprecation")
  38.     public DisguiseUtil(EntityType entityType, Player p) {
  39.         this.p = p;
  40.         this.entityType = entityType;
  41.         disguised = false;
  42.         getHandle = Reflection.getMethod("{obc}.entity.CraftPlayer",
  43.                 "getHandle");
  44.         playerConnection = Reflection.getField("{nms}.EntityPlayer",
  45.                 "playerConnection", Object.class);
  46.         sendPacket = Reflection.getMethod("{nms}.PlayerConnection",
  47.                 "sendPacket", Object.class);
  48.         packetEntityClass = Reflection.getClass("{nms}.Entity"
  49.                 + entityType.getName());
  50.  
  51.         disguise.put(p.getUniqueId(), this);
  52.     }
  53.  
  54.     public static void register() {
  55.         new PacketEvent();
  56.         instance.getServer().getPluginManager()
  57.                 .registerEvents(new Listener() {
  58.                     @EventHandler
  59.                     public void onPlayerQuitEvent(PlayerQuitEvent e) {
  60.                         Player p = e.getPlayer();
  61.                         DisguiseUtil d = DisguiseUtil.findByPlayer(p);
  62.                         if (d == null) return;
  63.                         if (d.isDisguised())
  64.                             d.setDisguised(false);
  65.                         d.remove();
  66.  
  67.                     }
  68.  
  69.                     @EventHandler
  70.                     public void onDisable(PluginDisableEvent e) {
  71.                         if (e.getPlugin().equals(instance)) {
  72.                             for (DisguiseUtil disguise : DisguiseUtil.disguise.values()) {
  73.                                 disguise.removeDisguise();
  74.                             }
  75.                         }
  76.                     }
  77.                 }, instance);
  78.     }
  79.  
  80.     public static Map<UUID, DisguiseUtil> getDisguised() {
  81.         return disguise;
  82.     }
  83.  
  84.     public static DisguiseUtil findByPlayer(Player p) {
  85.         return findByUniqueId(p.getUniqueId());
  86.     }
  87.  
  88.     public static DisguiseUtil findByUniqueId(UUID uuid) {
  89.         return disguise.containsKey(uuid) ? disguise.get(uuid) : null;
  90.     }
  91.  
  92.     public void disguisePlayer(Player cur) {
  93.         Reflection.ConstructorInvoker packetEntityConstructor = Reflection.getConstructor(
  94.                 packetEntityClass, Reflection.getClass("{nms}.World"));
  95.         Reflection.MethodInvoker getHandleWorld = Reflection.getMethod("{obc}.CraftWorld",
  96.                 "getHandle");
  97.         Object entity = packetEntityConstructor.invoke(getHandleWorld.invoke(p
  98.                 .getWorld()));
  99.         if (this.entityType == EntityType.BAT) {
  100.             Reflection.MethodInvoker setAsleep = Reflection.getMethod(Reflection.getClass("{nms}.EntityBat"), "setAsleep", boolean.class);
  101.             setAsleep.invoke(entity, false);
  102.         }
  103.         Reflection.MethodInvoker setPos = Reflection.getMethod(
  104.                 Reflection.getClass("{nms}.Entity"), "setLocation",
  105.                 double.class, double.class, double.class, float.class,
  106.                 float.class);
  107.         setPos.invoke(entity, p.getLocation().getX(), p.getLocation().getY(), p
  108.                 .getLocation().getZ(), p.getLocation().getYaw(), p
  109.                 .getLocation().getPitch());
  110.         Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
  111.                 Reflection.getClass("{nms}.PacketPlayOutSpawnEntityLiving"),
  112.                 Reflection.getClass("{nms}.EntityLiving"));
  113.         Object packetSpawn = packetSpawnConstructor.invoke(entity);
  114.         Reflection.getField(packetSpawn.getClass(), int.class, 0).set(
  115.                 packetSpawn, getplayerID());
  116.         hideEntity(cur);
  117.         sendPacket(packetSpawn, cur);
  118.         setEquipment(cur);
  119.     }
  120.  
  121.     public void disguisePlayer() {
  122.         Reflection.ConstructorInvoker packetEntityConstructor = Reflection.getConstructor(
  123.                 packetEntityClass, Reflection.getClass("{nms}.World"));
  124.         Reflection.MethodInvoker getHandleWorld = Reflection.getMethod("{obc}.CraftWorld",
  125.                 "getHandle");
  126.         Object entity = packetEntityConstructor.invoke(getHandleWorld.invoke(p
  127.                 .getWorld()));
  128.         if (this.entityType == EntityType.BAT) {
  129.             Reflection.MethodInvoker setAsleep = Reflection.getMethod(Reflection.getClass("{nms}.EntityBat"), "setAsleep", boolean.class);
  130.             setAsleep.invoke(entity, false);
  131.         }
  132.         Reflection.MethodInvoker setPos = Reflection.getMethod(
  133.                 Reflection.getClass("{nms}.Entity"), "setLocation",
  134.                 double.class, double.class, double.class, float.class,
  135.                 float.class);
  136.         setPos.invoke(entity, p.getLocation().getX(), p.getLocation().getY(), p
  137.                 .getLocation().getZ(), p.getLocation().getYaw(), p
  138.                 .getLocation().getPitch());
  139.         Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
  140.                 Reflection.getClass("{nms}.PacketPlayOutSpawnEntityLiving"),
  141.                 Reflection.getClass("{nms}.EntityLiving"));
  142.         Object packetSpawn = packetSpawnConstructor.invoke(entity);
  143.         Reflection.getField(packetSpawn.getClass(), int.class, 0).set(
  144.                 packetSpawn, getplayerID());
  145.         hideEntity();
  146.         sendPacket(packetSpawn);
  147.         setEquipment();
  148.         disguised = true;
  149.     }
  150.  
  151.     private void setEquipment() {
  152.         setEquipment(p);
  153.  
  154.     }
  155.  
  156.     private void setEquipment(Player p) {
  157.         sendPacket(buildEquipmentPacket(0, p.getInventory().getItemInMainHand()));
  158.         if(is1_9OrNewer) {
  159.             /*On 1.9: 0 = Main Hand ; 1 = Off Hand ; 2 = Boots ; 3 = Leggings ; 4 = Chest ; 5 = Helmet */
  160.             sendPacket(buildEquipmentPacket(2, p.getInventory().getBoots()));
  161.             sendPacket(buildEquipmentPacket(3, p.getInventory().getLeggings()));
  162.             sendPacket(buildEquipmentPacket(4, p.getInventory().getChestplate()));
  163.             sendPacket(buildEquipmentPacket(5, p.getInventory().getHelmet()));
  164.         } else {
  165.             /*On 1.9: 0 = Main Hand ; 1 = Boots ; 2 = Leggings ; 3 = Chest ; 4 = Helmet */
  166.             sendPacket(buildEquipmentPacket(1, p.getInventory().getBoots()));
  167.             sendPacket(buildEquipmentPacket(2, p.getInventory().getLeggings()));
  168.             sendPacket(buildEquipmentPacket(3, p.getInventory().getChestplate()));
  169.             sendPacket(buildEquipmentPacket(4, p.getInventory().getHelmet()));
  170.         }
  171.  
  172.     }
  173.  
  174.     private Object buildEquipmentPacket(int slot, ItemStack item) {
  175.  
  176.         Class<?> is = Reflection.getClass("{obc}.inventory.CraftItemStack");
  177.         Reflection.MethodInvoker getHandleItemStack = Reflection.getMethod(is,
  178.                 "asNMSCopy", ItemStack.class);
  179.         Reflection.ConstructorInvoker packetEquipConstructor = Reflection
  180.                 .getConstructor(Reflection
  181.                         .getClass("{nms}.PacketPlayOutEntityEquipment"));
  182.  
  183.         Object packet = packetEquipConstructor.invoke();
  184.  
  185.         Reflection.getField(packet.getClass(), int.class, 0).set(
  186.                 packet, getplayerID());
  187.  
  188.         if (is1_9OrNewer) {
  189.             Class<?> enumItemSlot = Reflection.getMinecraftClass("EnumItemSlot");
  190.             Reflection.getField(packet.getClass(), enumItemSlot, 0).set(
  191.                     packet, enumItemSlot.getEnumConstants()[slot]);
  192.         }
  193.         else
  194.             Reflection.getField(packet.getClass(), int.class, 1).set(
  195.                     packet, slot);
  196.  
  197.         Reflection.getField(packet.getClass(),
  198.                 Reflection.getMinecraftClass("ItemStack"), 0).set(
  199.                 packet,
  200.                 getHandleItemStack.invoke(is, item));
  201.  
  202.         return packet;
  203.     }
  204.  
  205.     public void removeDisguise() {
  206.         hideEntity();
  207.         Reflection.ConstructorInvoker packetSpawnConstructor = Reflection.getConstructor(
  208.                 Reflection.getClass("{nms}.PacketPlayOutNamedEntitySpawn"),
  209.                 Reflection.getClass("{nms}.EntityHuman"));
  210.         Object packetSpawn = packetSpawnConstructor.invoke(this.getHandle
  211.                 .invoke(p));
  212.         sendPacket(packetSpawn);
  213.         setEquipment();
  214.         remove();
  215.     }
  216.  
  217.     private void sendPacket(Object packet) {
  218.         for (Player cur : Bukkit.getOnlinePlayers()) {
  219.             if (!cur.equals(p)) {
  220.                 sendPacket.invoke(
  221.                         playerConnection.get(this.getHandle.invoke(cur)),
  222.                         packet);
  223.             }
  224.         }
  225.     }
  226.  
  227.     private void sendPacket(Object packet, Player cur) {
  228.         if (!cur.equals(p)) {
  229.             sendPacket.invoke(playerConnection.get(this.getHandle.invoke(cur)),
  230.                     packet);
  231.         }
  232.     }
  233.  
  234.     private int getplayerID() {
  235.         Reflection.MethodInvoker getHandle = Reflection.getMethod(
  236.                 "{obc}.entity.CraftEntity", "getHandle");
  237.         return Reflection.getField(Reflection.getMinecraftClass("Entity"),
  238.                 int.class, 2).get(getHandle.invoke(p));
  239.     }
  240.  
  241.     private void hideEntity() {
  242.         Reflection.ConstructorInvoker packetDestroyConstructor = Reflection
  243.                 .getConstructor(Reflection
  244.                         .getClass("{nms}.PacketPlayOutEntityDestroy"));
  245.         Object packetDestroy = packetDestroyConstructor.invoke();
  246.         int[] id;
  247.         id = new int[1];
  248.         id[0] = getplayerID();
  249.         Reflection.getField(packetDestroy.getClass(), Object.class, 0).set(
  250.                 packetDestroy, id);
  251.         sendPacket(packetDestroy);
  252.     }
  253.  
  254.     private void hideEntity(Player cur) {
  255.         Reflection.ConstructorInvoker packetDestroyConstructor = Reflection
  256.                 .getConstructor(Reflection
  257.                         .getClass("{nms}.PacketPlayOutEntityDestroy"));
  258.         Object packetDestroy = packetDestroyConstructor.invoke();
  259.         int[] id;
  260.         id = new int[1];
  261.         id[0] = getplayerID();
  262.         Reflection.getField(packetDestroy.getClass(), Object.class, 0).set(
  263.                 packetDestroy, id);
  264.         sendPacket(packetDestroy, cur);
  265.     }
  266.  
  267.     public boolean isDisguised() {
  268.         return disguised;
  269.     }
  270.  
  271.     public void setDisguised(boolean disguised) {
  272.         this.disguised = disguised;
  273.     }
  274.  
  275.     private void remove() {
  276.         getDisguised().remove(this);
  277.     }
  278.  
  279.     private boolean is1_9() {
  280.         String packageName = Bukkit.getServer().getClass().getPackage().getName();
  281.         return packageName.substring(packageName.lastIndexOf('.') + 1).contains("1_9");
  282.     }
  283.  
  284.     static class PacketEvent extends TinyProtocol.PacketListener {
  285.         private Class<?> packet = Reflection
  286.                 .getClass("{nms}.PacketPlayOutNamedEntitySpawn");
  287.  
  288.         @Override
  289.         public Object onPacketOutAsync(final Player receiver, Channel channel,
  290.                                        Object packet) {
  291.             if (!this.packet.isInstance(packet))
  292.                 return packet;
  293.             UUID id = Reflection.getField(packet.getClass(), UUID.class, 0)
  294.                     .get(packet);
  295.             final Player p = Bukkit.getPlayer(id);
  296.             if (DisguiseUtil.disguise.containsKey(id)) {
  297.                 if (findByPlayer(p).disguised) {
  298.                     Bukkit.getScheduler().runTask(Disguise.getInstance(), new Runnable() {
  299.                         @Override
  300.                         public void run() {
  301.                             new DisguiseUtil(EntityType.PLAYER, receiver).disguisePlayer(receiver);
  302.                         }
  303.                     });
  304.                 }
  305.             }
  306.             return super.onPacketOutAsync(receiver, channel, packet);
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment