Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.08 KB | None | 0 0
  1. package me.antihack.principal;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.UUID;
  7. import net.minecraft.server.v1_8_R1.DataWatcher;
  8. import net.minecraft.server.v1_8_R1.EnumGamemode;
  9. import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
  10. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityDestroy;
  11. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityEquipment;
  12. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityTeleport;
  13. import net.minecraft.server.v1_8_R1.PacketPlayOutNamedEntitySpawn;
  14. import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;
  15. import net.minecraft.server.v1_8_R1.PlayerInfoData;
  16. import org.bukkit.Bukkit;
  17. import org.bukkit.Location;
  18. import org.bukkit.Material;
  19. import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
  20. import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
  21. import org.bukkit.craftbukkit.v1_8_R1.util.CraftChatMessage;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.inventory.ItemStack;
  24. import com.mojang.authlib.GameProfile;
  25.  
  26. public class NPC {
  27.  
  28.     private DataWatcher watcher;
  29.     private Material chestplate;
  30.     private Material leggings;
  31.     private Location location;
  32.     private Material inHand;
  33.     private Material helmet;
  34.     private Material boots;
  35.     private String tablist;
  36.     private int entityID;
  37.     private String name;
  38.     private UUID uuid;
  39.  
  40.     public NPC(String name, String tablist, UUID uuid, int entityID, Location location, Material inHand) {
  41.         this.location = location;
  42.         this.tablist = tablist;
  43.         this.name = name;
  44.         this.uuid = uuid;
  45.         this.entityID = entityID;
  46.         this.inHand = inHand;
  47.         this.watcher = new DataWatcher(null);
  48.         watcher.a(6, (float) 20);
  49.     }
  50.  
  51.     public NPC(String name, Location location) {
  52.         this(name, name, UUID.randomUUID(), new Random().nextInt(10000), location, Material.AIR);
  53.     }
  54.  
  55.     public NPC(String name, Location location, Material inHand) {
  56.         this(name, name, UUID.randomUUID(), new Random().nextInt(10000), location, inHand);
  57.     }
  58.  
  59.     public NPC(String name, String tablist, Location location) {
  60.         this(name, tablist, UUID.randomUUID(), new Random().nextInt(10000), location, Material.AIR);
  61.     }
  62.  
  63.     public NPC(String name, String tablist, Location location, Material inHand) {
  64.         this(name, tablist, UUID.randomUUID(), new Random().nextInt(10000), location, inHand);
  65.     }
  66.  
  67.     @SuppressWarnings("deprecation")
  68.     public void spawn(Player online) {
  69.         try {
  70.             PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
  71.             this.addToTablist();
  72.  
  73.             this.setValue(packet, "a", this.entityID);
  74.             this.setValue(packet, "b", this.uuid);
  75.             this.setValue(packet, "c", this.toFixedPoint(this.location.getX()));
  76.             this.setValue(packet, "d", this.toFixedPoint(this.location.getY()));
  77.             this.setValue(packet, "e", this.toFixedPoint(this.location.getZ()));
  78.             this.setValue(packet, "f", this.toPackedByte(this.location.getYaw()));
  79.             this.setValue(packet, "g", this.toPackedByte(this.location.getPitch()));
  80.             this.setValue(packet, "h", this.inHand == null ? 0 : this.inHand.getId());
  81.             this.setValue(packet, "i", this.watcher);
  82.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  83.         } catch(Exception e) {
  84.             e.printStackTrace();
  85.         }
  86.     }
  87.  
  88.     @SuppressWarnings("deprecation")
  89.     public void despawn() {
  90.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[] { this.entityID });
  91.         this.removeFromTablist();
  92.         for(Player online : Bukkit.getOnlinePlayers()) {
  93.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  94.         }
  95.     }
  96.  
  97.     @SuppressWarnings("deprecation")
  98.     public void changePlayerlistName(String name) {
  99.         try {
  100.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  101.             PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(name)[0]);
  102.             @SuppressWarnings("unchecked")
  103.             List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  104.             players.add(data);
  105.  
  106.             this.setValue(packet, "a", EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);
  107.             this.setValue(packet, "b", players);
  108.             this.tablist = name;
  109.  
  110.             for(Player online : Bukkit.getOnlinePlayers()) {
  111.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  112.             }
  113.         } catch(Exception e) {
  114.             e.printStackTrace();
  115.         }
  116.     }
  117.  
  118.     @SuppressWarnings("deprecation")
  119.     private void addToTablist() {
  120.         try {
  121.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  122.             PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(this.tablist)[0]);
  123.             @SuppressWarnings("unchecked")
  124.             List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  125.             players.add(data);
  126.  
  127.             this.setValue(packet, "a", EnumPlayerInfoAction.ADD_PLAYER);
  128.             this.setValue(packet, "b", players);
  129.  
  130.             for(Player online : Bukkit.getOnlinePlayers()) {
  131.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  132.             }
  133.         } catch(Exception e) {
  134.             e.printStackTrace();
  135.         }
  136.     }
  137.  
  138.     @SuppressWarnings("deprecation")
  139.     private void removeFromTablist() {
  140.         try {
  141.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  142.             PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(this.tablist)[0]);
  143.             @SuppressWarnings("unchecked")
  144.             List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  145.             players.add(data);
  146.  
  147.             this.setValue(packet, "a", EnumPlayerInfoAction.REMOVE_PLAYER);
  148.             this.setValue(packet, "b", players);
  149.  
  150.             for(Player online : Bukkit.getOnlinePlayers()) {
  151.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  152.             }
  153.         } catch(Exception e) {
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.  
  158.     @SuppressWarnings("deprecation")
  159.     public void teleport(Location location) {
  160.         try {
  161.             PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  162.  
  163.             this.setValue(packet, "a", this.entityID);
  164.             this.setValue(packet, "b", this.toFixedPoint(location.getX()));
  165.             this.setValue(packet, "c", this.toFixedPoint(location.getY()));
  166.             this.setValue(packet, "d", this.toFixedPoint(location.getZ()));
  167.             this.setValue(packet, "e", this.toPackedByte(location.getYaw()));
  168.             this.setValue(packet, "f", this.toPackedByte(location.getPitch()));
  169.             this.setValue(packet, "g", this.location.getBlock().getType() == Material.AIR ? false : true);
  170.             this.location = location;
  171.  
  172.             for(Player online : Bukkit.getOnlinePlayers()) {
  173.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  174.             }
  175.         } catch(Exception e) {
  176.             e.printStackTrace();
  177.         }
  178.     }
  179.  
  180.     @SuppressWarnings("deprecation")
  181.     public void setItemInHand(Material material) {
  182.         try {
  183.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  184.  
  185.             this.setValue(packet, "a", this.entityID);
  186.             this.setValue(packet, "b", 0);
  187.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  188.             this.inHand = material;
  189.  
  190.             for(Player online : Bukkit.getOnlinePlayers()) {
  191.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  192.             }
  193.         } catch(Exception e) {
  194.             e.printStackTrace();
  195.         }
  196.     }
  197.  
  198.     public Material getItemInHand() {
  199.         return this.inHand;
  200.     }
  201.  
  202.     @SuppressWarnings("deprecation")
  203.     public void setHelmet(Material material) {
  204.         try {
  205.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  206.  
  207.             this.setValue(packet, "a", this.entityID);
  208.             this.setValue(packet, "b", 4);
  209.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  210.             this.helmet = material;
  211.  
  212.             for(Player online : Bukkit.getOnlinePlayers()) {
  213.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  214.             }
  215.         } catch(Exception e) {
  216.             e.printStackTrace();
  217.         }
  218.     }
  219.  
  220.     public Material getHelmet() {
  221.         return this.helmet;
  222.     }
  223.  
  224.     @SuppressWarnings("deprecation")
  225.     public void setChestplate(Material material) {
  226.         try {
  227.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  228.  
  229.             this.setValue(packet, "a", this.entityID);
  230.             this.setValue(packet, "b", 3);
  231.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  232.             this.chestplate = material;
  233.  
  234.             for(Player online : Bukkit.getOnlinePlayers()) {
  235.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  236.             }
  237.         } catch(Exception e) {
  238.             e.printStackTrace();
  239.         }
  240.     }
  241.  
  242.     public Material getChestplate() {
  243.         return this.chestplate;
  244.     }
  245.  
  246.     @SuppressWarnings("deprecation")
  247.     public void setLeggings(Material material) {
  248.         try {
  249.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  250.  
  251.             this.setValue(packet, "a", this.entityID);
  252.             this.setValue(packet, "b", 2);
  253.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  254.             this.leggings = material;
  255.  
  256.             for(Player online : Bukkit.getOnlinePlayers()) {
  257.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  258.             }
  259.         } catch(Exception e) {
  260.             e.printStackTrace();
  261.         }
  262.     }
  263.  
  264.     public Material getLeggings() {
  265.         return this.leggings;
  266.     }
  267.  
  268.     @SuppressWarnings("deprecation")
  269.     public void setBoots(Material material) {
  270.         try {
  271.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  272.  
  273.             this.setValue(packet, "a", this.entityID);
  274.             this.setValue(packet, "b", 1);
  275.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  276.             this.boots = material;
  277.  
  278.             for(Player online : Bukkit.getOnlinePlayers()) {
  279.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  280.             }
  281.         } catch(Exception e) {
  282.             e.printStackTrace();
  283.         }
  284.     }
  285.  
  286.     public Material getBoots() {
  287.         return this.boots;
  288.     }
  289.  
  290.     public int getEntityID() {
  291.         return this.entityID;
  292.     }
  293.  
  294.     public UUID getUUID() {
  295.         return this.uuid;
  296.     }
  297.  
  298.     public Location getLocation() {
  299.         return this.location;
  300.     }
  301.  
  302.     public String getName() {
  303.         return this.name;
  304.     }
  305.  
  306.     public String getPlayerlistName() {
  307.         return this.tablist;
  308.     }
  309.  
  310.     private void setValue(Object instance, String field, Object value) throws Exception {
  311.         Field f = instance.getClass().getDeclaredField(field);
  312.         f.setAccessible(true);
  313.         f.set(instance, value);
  314.     }
  315.  
  316.     private Object getValue(Object instance, String field) throws Exception {
  317.         Field f = instance.getClass().getDeclaredField(field);
  318.         f.setAccessible(true);
  319.         return f.get(instance);
  320.     }
  321.  
  322.     private int toFixedPoint(double d) {
  323.         return (int) (d * 32.0);
  324.     }
  325.  
  326.     private byte toPackedByte(float f) {
  327.         return (byte) ((int) (f * 256.0F / 360.0F));
  328.     }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement