[Lib] Spawning Basic NPCs

Discussion in 'Resources' started by BurnBlader, Oct 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    SoThatsIt

    you have to use it for crouching, invisibility, sprinting, etc...

    or, do you mean set the entity to null? because i would guess that that would either throw an IllegalArgumentException or a NullPointer
     
  2. Offline

    DrJava

    CaptainBern
    SoThatsIt
    Code:
    private class LazyDataWatcher {
    private Object datawatcher;
     
    public LazyDataWatcher(){
    try {
    datawatcher = BarAPI.BarReflectionUtils.getNMSClass("DataWatcher");
    } catch (Exception e){
     
    }
    }
     
    public void write(int i, Object object){
    try{
    Method method = datawatcher.getClass().getMethod("a", int.class, Object.class);
     
    method.invoke(datawatcher, i, object);
    } catch(Exception e){
    e.printStackTrace();
    }
    }
     
    public Object getDataWatcherObject(){
    return datawatcher;
    }
    }
     
  3. Offline

    SoThatsIt

    turns out you can just set the entity to null.

    Does anyone know how to create a fake GameProfile to set the name of the npc? they changed the use of the String name to the use of a GameProfile Object

    edit: nvm figured it out, turns out the constructor is 2 strings and in the craftbukkit login manager class i found an example of how to create one which i changed to use reflection.

    Code:
    UUID uuid = UUID.nameUUIDFromBytes( ( "OfflinePlayer:" + name ).getBytes(Charsets.UTF_8));
         
    Object profile = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile").getConstructor(String.class , String.class).newInstance(uuid.toString().replaceAll("-" , "") , name);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    DrJava likes this.
  4. Offline

    BurnBlader

    Haven't got around to updating it yet, I'll try it soon.
     
  5. Offline

    CaptainBern Retired Staff

    SoThatsIt The authlib hasn't been versioned yet so you can just import GameProfile. Also, FYI, I'm working on an NPC-Lib (maybe BurnBlader remembers?) and it's now getting in a state that it's almost finished for release. It will be similar to this oly it will include better management, more features, cross-plugin compatibility etc...

    (Also the ID in the gameprofile isn't usable yet. Mojang will start giving each account an ID so people can change their player name. )
     
  6. Offline

    Ultimate_n00b



    =o what a surprise!
     
    DrJava likes this.
  7. Offline

    SoThatsIt

    as i have stated in one of my previous posts i wasnt sure if you meant setting the datawatcher to null or the entity you pass to the data watcher
     
  8. Offline

    Ultimate_n00b

    I had replied directly to where you asked the question. As well, how would setting the DataWatcher to null be a good idea?
     
  9. Offline

    SoThatsIt

    ok, i have gotten it to work quite well and have made something to make a human follow me around at a distance although i am stuck on turning the head of the human as it does not seem to change its yaw from PacketPlayOutRelEntityMoveLook and when i tried using PacketPlayOutEntityHeadRotation it did not work.

    here is my code:

    Code:
    Object look = PacketPlayOutEntityHeadRotation.getConstructor().newInstance();
               
                Field idField = PacketPlayOutEntityHeadRotation.getDeclaredField("a");
                Field yawField = PacketPlayOutEntityHeadRotation.getDeclaredField("b");
               
                idField.setAccessible(true);
                yawField.setAccessible(true);
               
                idField.set(look , id);
                yawField.set(look , yaw);
               
                ReflectionUtils.sendPacket(Arrays.asList(Bukkit.getOnlinePlayers()) , look);
    and to work out the yaw:

    Code:
    private double[] getPitchYaw(Location from, Location to)
        {
            double dX = from.getX() - to.getX();
            double dY = from.getY() - to.getY();
            double dZ = from.getZ() - to.getZ();
           
            double yaw = Math.atan2(dZ , dX);
           
            double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX) , dY) + Math.PI;
           
            return new double[]
            { getCompressedAngle(pitch) , getCompressedAngle(yaw) };
        }
    note, the sending to all players is simply because it is easier as i do not have a version which sends it to all players within a radius on hand.
     
  10. Offline

    DrJava

    Supply me with base code? <3 I don't need it to move; I just want it to stand there and do nothing.
     
  11. Offline

    SoThatsIt

    im not releasing my base code until i get it to all work
     
  12. Offline

    DrJava

    Ahh, okay.
    Sorry about asking to be spoon fed, I don't really work with packets
     
  13. Offline

    DrJava

  14. Offline

    Wouto1997

    Updated to 1.7 :)
    Code:java
    1. import java.lang.reflect.Field;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.Random;
    5.  
    6. import net.minecraft.server.v1_7_R1.DataWatcher;
    7. import net.minecraft.server.v1_7_R1.EntityHuman;
    8. import net.minecraft.server.v1_7_R1.PacketPlayOutRelEntityMove;
    9. import net.minecraft.server.v1_7_R1.PacketPlayOutAnimation;
    10. import net.minecraft.server.v1_7_R1.PacketPlayOutEntity;
    11. import net.minecraft.server.v1_7_R1.PacketPlayOutNamedEntitySpawn;
    12. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityDestroy;
    13. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityTeleport;
    14. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityHeadRotation;
    15. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityMetadata;
    16. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityEquipment;
    17. import net.minecraft.server.v1_7_R1.PacketPlayOutRelEntityMoveLook;
    18. import net.minecraft.util.com.mojang.authlib.GameProfile;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.Color;
    22. import org.bukkit.Location;
    23. import org.bukkit.World;
    24. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    25. import org.bukkit.craftbukkit.v1_7_R1.inventory.CraftItemStack;
    26. import org.bukkit.entity.Player;
    27. import org.bukkit.inventory.ItemStack;
    28.  
    29. public class Human {
    30.  
    31. String name;
    32. World world;
    33. public int id;
    34. Location l;
    35. int itemInHand;
    36.  
    37. private List<Integer> ids = new ArrayList<Integer>();
    38.  
    39. private void setPrivateField(Class type, Object object, String name, Object value) {
    40. try {
    41. Field f = type.getDeclaredField(name);
    42. f.setAccessible(true);
    43. f.set(object, value);
    44. f.setAccessible(false);
    45. } catch (Exception ex) {
    46. ex.printStackTrace();
    47. }
    48. }
    49.  
    50. public void setPitch(float pitch) {
    51. this.walk(0.0d, 0.0d, 0.0d, l.getYaw(), pitch);
    52. }
    53.  
    54. public void setYaw(float yaw) {
    55. this.walk(0.0d, 0.0d, 0.0d, yaw, l.getPitch());
    56. }
    57.  
    58. public Human(World w, String name, int id, Location l, int itemInHand) {
    59. this.name = name;
    60. this.world = w;
    61. this.id = id;
    62. this.l = l;
    63. this.itemInHand = itemInHand;
    64. DataWatcher d = new DataWatcher(null);
    65. d.a(0, (Object) (byte) 0);
    66. d.a(1, (Object) (short) 0);
    67. d.a(8, (Object) (byte) 0);
    68. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    69. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    70. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile("", name));
    71. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) l.getX() * 32));
    72. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) l.getY() * 32));
    73. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) l.getZ() * 32));
    74. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    75. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    76. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    77. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    78.  
    79. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    80. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    81. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) l.getX() * 32));
    82. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) l.getY() * 32));
    83. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) l.getZ() * 32));
    84. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    85. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    86.  
    87. for (Player p : Bukkit.getOnlinePlayers()) {
    88. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    89. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(tp);
    90. }
    91. ids.add(id);
    92. }
    93.  
    94. public Human(EntityHuman h) {
    95. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn(h);
    96. int id = new Random().nextInt(5000 - 1000) + 1000;
    97. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    98. this.id = id;
    99. PacketPlayOutEntityEquipment armor1 = new PacketPlayOutEntityEquipment(id, 1, h.inventory.getArmorContents()[0]);
    100. PacketPlayOutEntityEquipment armor2 = new PacketPlayOutEntityEquipment(id, 2, h.inventory.getArmorContents()[1]);
    101. PacketPlayOutEntityEquipment armor3 = new PacketPlayOutEntityEquipment(id, 3, h.inventory.getArmorContents()[2]);
    102. PacketPlayOutEntityEquipment armor4 = new PacketPlayOutEntityEquipment(id, 4, h.inventory.getArmorContents()[3]);
    103. PacketPlayOutEntityEquipment sword = new PacketPlayOutEntityEquipment(id, 0, h.inventory.getItem(0));
    104. for (Player p : Bukkit.getOnlinePlayers()) {
    105. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    106. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor1);
    107. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor2);
    108. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor3);
    109. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor4);
    110. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(sword);
    111. }
    112. }
    113.  
    114. public void teleport(Location loc) {
    115. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    116. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    117. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (loc.getX() * 32)));
    118. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (loc.getY() * 32)));
    119. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (loc.getZ() * 32)));
    120. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(loc.getYaw()));
    121. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(loc.getPitch()));
    122. this.l = loc;
    123. for (Player p : Bukkit.getOnlinePlayers()) {
    124. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(tp);
    125. }
    126. }
    127.  
    128. private byte getCompressedAngle(float value) {
    129. return (byte) ((value * 256.0F) / 360.0F);
    130. }
    131.  
    132. private byte getCompressedAngle2(float value) {
    133. return (byte) ((value * 256.0F) / 360.0F);
    134. }
    135.  
    136. public void remove() {
    137. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
    138. for (Player p : Bukkit.getOnlinePlayers()) {
    139. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    140. }
    141. }
    142.  
    143. @SuppressWarnings("deprecation")
    144. public void updateItems(ItemStack hand, ItemStack boots, ItemStack legs, ItemStack chest, ItemStack helmet) {
    145.  
    146. PacketPlayOutEntityEquipment[] ps = new PacketPlayOutEntityEquipment[]{
    147. new PacketPlayOutEntityEquipment(id, 1, CraftItemStack.asNMSCopy(boots)),
    148. new PacketPlayOutEntityEquipment(id, 2, CraftItemStack.asNMSCopy(legs)),
    149. new PacketPlayOutEntityEquipment(id, 3, CraftItemStack.asNMSCopy(chest)),
    150. new PacketPlayOutEntityEquipment(id, 4, CraftItemStack.asNMSCopy(helmet)),
    151. new PacketPlayOutEntityEquipment(id, 0, CraftItemStack.asNMSCopy(hand))
    152. };
    153. for (PacketPlayOutEntityEquipment pack : ps) {
    154. for (Player p : Bukkit.getOnlinePlayers()) {
    155. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(pack);
    156. }
    157. }
    158. }
    159.  
    160. @Deprecated
    161. public void setName(String s) {
    162. DataWatcher d = new DataWatcher(null);
    163. d.a(0, (Object) (byte) 0);
    164. d.a(1, (Object) (short) 0);
    165. d.a(8, (Object) (byte) 0);
    166. d.a(10, (Object) (String) s);
    167. //d.a(11, (Object) (byte) 0);
    168. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    169. for (Player p : Bukkit.getOnlinePlayers()) {
    170. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    171. }
    172. }
    173.  
    174. public void hideForPlayer(Player p) {
    175. DataWatcher d = new DataWatcher(null);
    176. d.a(0, (Object) (byte) 32);
    177. d.a(1, (Object) (short) 0);
    178. d.a(8, (Object) (byte) 0);
    179. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    180. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    181. }
    182.  
    183. public void showForPlayer(Player p) {
    184. DataWatcher d = new DataWatcher(null);
    185. d.a(0, (Object) (byte) 0);
    186. d.a(1, (Object) (short) 0);
    187. d.a(8, (Object) (byte) 0);
    188. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    189. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    190. }
    191.  
    192. public void addPotionColor(Color r) {
    193. int color = r.asBGR();
    194. final DataWatcher dw = new DataWatcher(null);
    195. dw.a(7, Integer.valueOf(color));
    196. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    197. for (Player p : Bukkit.getOnlinePlayers()) {
    198. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    199. }
    200. }
    201.  
    202. public void addPotionColor(int color) {
    203. final DataWatcher dw = new DataWatcher(null);
    204. dw.a(7, Integer.valueOf(color));
    205. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    206. for (Player p : Bukkit.getOnlinePlayers()) {
    207. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    208. }
    209. }
    210.  
    211. public void walk(double a, double b, double c) {
    212. walk(a, b, c, l.getYaw(), l.getPitch());
    213. }
    214.  
    215. public void walk(double a, double b, double c, float yaw, float pitch) {
    216. byte x = (byte) a;
    217. byte y = (byte) b;
    218. byte z = (byte) c;
    219. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook();
    220. setPrivateField(PacketPlayOutEntity.class, packet, "a", id);
    221. setPrivateField(PacketPlayOutEntity.class, packet, "b", x);
    222. setPrivateField(PacketPlayOutEntity.class, packet, "c", y);
    223. setPrivateField(PacketPlayOutEntity.class, packet, "d", z);
    224. setPrivateField(PacketPlayOutEntity.class, packet, "e", getCompressedAngle(yaw));
    225. setPrivateField(PacketPlayOutEntity.class, packet, "f", getCompressedAngle2(pitch));
    226.  
    227. PacketPlayOutEntityHeadRotation p2 = new PacketPlayOutEntityHeadRotation();
    228. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "a", id);
    229. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "b", getCompressedAngle(yaw));
    230.  
    231. for (Player p : Bukkit.getOnlinePlayers()) {
    232. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    233. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(p2);
    234. }
    235. l.setPitch(pitch);
    236. l.setYaw(yaw);
    237. l.add(a, b, c);
    238. }
    239.  
    240. public void sendtoplayer(Player who) {
    241. DataWatcher d = new DataWatcher(null);
    242. d.a(0, (Object) (byte) 0);
    243. d.a(1, (Object) (short) 0);
    244. d.a(8, (Object) (byte) 0);
    245. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    246. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    247. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile("", name));
    248. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) (l.getX() * 32)));
    249. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) (l.getY() * 32)));
    250. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) (l.getZ() * 32)));
    251. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    252. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    253. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    254. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    255.  
    256. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    257. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    258. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (l.getX() * 32)));
    259. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (l.getY() * 32)));
    260. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (l.getZ() * 32)));
    261. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    262. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    263.  
    264. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(spawn);
    265. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(tp);
    266. }
    267.  
    268. public void setInvisible() {
    269. DataWatcher d = new DataWatcher(null);
    270. d.a(0, (Object) (byte) 32);
    271. d.a(1, (Object) (short) 0);
    272. d.a(8, (Object) (byte) 0);
    273. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    274. for (Player p : Bukkit.getOnlinePlayers()) {
    275. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    276. }
    277. }
    278.  
    279. public void setCrouched() {
    280. DataWatcher d = new DataWatcher(null);
    281. d.a(0, (Object) (byte) 2);
    282. d.a(1, (Object) (short) 0);
    283. d.a(8, (Object) (byte) 0);
    284. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    285. for (Player p : Bukkit.getOnlinePlayers()) {
    286. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    287. }
    288. }
    289.  
    290. public void reset() {
    291. DataWatcher d = new DataWatcher(null);
    292. d.a(0, (Object) (byte) 0);
    293. d.a(1, (Object) (short) 0);
    294. d.a(8, (Object) (byte) 0);
    295. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    296. for (Player p : Bukkit.getOnlinePlayers()) {
    297. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    298. }
    299. }
    300.  
    301. public void sprint() {
    302. DataWatcher d = new DataWatcher(null);
    303. d.a(0, (Object) (byte) 8);
    304. d.a(1, (Object) (short) 0);
    305. d.a(8, (Object) (byte) 0);
    306. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    307. for (Player p : Bukkit.getOnlinePlayers()) {
    308. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    309. }
    310. }
    311.  
    312. @Deprecated
    313. public void block() {
    314. DataWatcher d = new DataWatcher(null);
    315. d.a(0, (Object) (byte) 16);
    316. d.a(1, (Object) (short) 0);
    317. d.a(6, (Object) (byte) 0);
    318. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    319. for (Player p : Bukkit.getOnlinePlayers()) {
    320. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    321. }
    322. }
    323.  
    324. public void damage() {
    325. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    326. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    327. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 2);
    328. for (Player p : Bukkit.getOnlinePlayers()) {
    329. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    330. }
    331. }
    332.  
    333. public void swingArm() {
    334. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    335. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    336. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 0);
    337. for (Player p : Bukkit.getOnlinePlayers()) {
    338. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    339. }
    340. }
    341.  
    342. @Deprecated
    343. public void eatInHand() {
    344. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    345. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    346. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 5);
    347. for (Player p : Bukkit.getOnlinePlayers()) {
    348. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    349. }
    350. }
    351.  
    352. public void sleep() {
    353. PacketPlayOutRelEntityMove packet17 = new PacketPlayOutRelEntityMove();
    354. setPrivateField(PacketPlayOutRelEntityMove.class, packet17, "a", id);
    355. setPrivateField(PacketPlayOutRelEntityMove.class, packet17, "b", (int) getX());
    356. setPrivateField(PacketPlayOutRelEntityMove.class, packet17, "c", (int) getY());
    357. setPrivateField(PacketPlayOutRelEntityMove.class, packet17, "d", (int) getZ());
    358. setPrivateField(PacketPlayOutRelEntityMove.class, packet17, "e", 0);
    359. for (Player p : Bukkit.getOnlinePlayers()) {
    360. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet17);
    361. }
    362. }
    363.  
    364. public double getX() {
    365. return l.getX();
    366. }
    367.  
    368. public double getY() {
    369. return l.getY();
    370. }
    371.  
    372. public double getZ() {
    373. return l.getZ();
    374. }
    375.  
    376. public Location getLocation() {
    377. return l;
    378. }
    379.  
    380. }
     
    Wruczek likes this.
  15. Offline

    Jannik8500

    - Updated to 1.7.4 - 1.7.6
    - Fixed .sleep();

    Code:java
    1. import java.lang.reflect.Field;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.Random;
    5.  
    6. import net.minecraft.server.v1_7_R2.DataWatcher;
    7. import net.minecraft.server.v1_7_R2.EntityHuman;
    8. import net.minecraft.server.v1_7_R2.PacketPlayOutAnimation;
    9. import net.minecraft.server.v1_7_R2.PacketPlayOutBed;
    10. import net.minecraft.server.v1_7_R2.PacketPlayOutEntity;
    11. import net.minecraft.server.v1_7_R2.PacketPlayOutEntityDestroy;
    12. import net.minecraft.server.v1_7_R2.PacketPlayOutEntityEquipment;
    13. import net.minecraft.server.v1_7_R2.PacketPlayOutEntityHeadRotation;
    14. import net.minecraft.server.v1_7_R2.PacketPlayOutEntityMetadata;
    15. import net.minecraft.server.v1_7_R2.PacketPlayOutEntityTeleport;
    16. import net.minecraft.server.v1_7_R2.PacketPlayOutNamedEntitySpawn;
    17. import net.minecraft.server.v1_7_R2.PacketPlayOutRelEntityMoveLook;
    18. import net.minecraft.util.com.mojang.authlib.GameProfile;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.Color;
    22. import org.bukkit.Location;
    23. import org.bukkit.World;
    24. import org.bukkit.craftbukkit.v1_7_R2.entity.CraftPlayer;
    25. import org.bukkit.craftbukkit.v1_7_R2.inventory.CraftItemStack;
    26. import org.bukkit.entity.Player;
    27. import org.bukkit.inventory.ItemStack;
    28.  
    29. public class Human {
    30.  
    31. String name;
    32. World world;
    33. public int id;
    34. Location l;
    35. int itemInHand;
    36.  
    37. private List<Integer> ids = new ArrayList<Integer>();
    38.  
    39. private void setPrivateField(Class type, Object object, String name, Object value) {
    40. try {
    41. Field f = type.getDeclaredField(name);
    42. f.setAccessible(true);
    43. f.set(object, value);
    44. f.setAccessible(false);
    45. } catch (Exception ex) {
    46. ex.printStackTrace();
    47. }
    48. }
    49.  
    50. public void setPitch(float pitch) {
    51. this.walk(0.0d, 0.0d, 0.0d, l.getYaw(), pitch);
    52. }
    53.  
    54. public void setYaw(float yaw) {
    55. this.walk(0.0d, 0.0d, 0.0d, yaw, l.getPitch());
    56. }
    57.  
    58. public Human(World w, String name, int id, Location l, int itemInHand) {
    59. this.name = name;
    60. this.world = w;
    61. this.id = id;
    62. this.l = l;
    63. this.itemInHand = itemInHand;
    64. DataWatcher d = new DataWatcher(null);
    65. d.a(0, (Object) (byte) 0);
    66. d.a(1, (Object) (short) 0);
    67. d.a(8, (Object) (byte) 0);
    68. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    69. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    70. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile("", name));
    71. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) l.getX() * 32));
    72. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) l.getY() * 32));
    73. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) l.getZ() * 32));
    74. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    75. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    76. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    77. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    78.  
    79. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    80. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    81. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) l.getX() * 32));
    82. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) l.getY() * 32));
    83. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) l.getZ() * 32));
    84. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    85. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    86.  
    87. for (Player p : Bukkit.getOnlinePlayers()) {
    88. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    89. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(tp);
    90. }
    91. ids.add(id);
    92. }
    93.  
    94. public Human(EntityHuman h) {
    95. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn(h);
    96. int id = new Random().nextInt(5000 - 1000) + 1000;
    97. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    98. this.id = id;
    99. PacketPlayOutEntityEquipment armor1 = new PacketPlayOutEntityEquipment(id, 1, h.inventory.getArmorContents()[0]);
    100. PacketPlayOutEntityEquipment armor2 = new PacketPlayOutEntityEquipment(id, 2, h.inventory.getArmorContents()[1]);
    101. PacketPlayOutEntityEquipment armor3 = new PacketPlayOutEntityEquipment(id, 3, h.inventory.getArmorContents()[2]);
    102. PacketPlayOutEntityEquipment armor4 = new PacketPlayOutEntityEquipment(id, 4, h.inventory.getArmorContents()[3]);
    103. PacketPlayOutEntityEquipment sword = new PacketPlayOutEntityEquipment(id, 0, h.inventory.getItem(0));
    104. for (Player p : Bukkit.getOnlinePlayers()) {
    105. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    106. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor1);
    107. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor2);
    108. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor3);
    109. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor4);
    110. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(sword);
    111. }
    112. }
    113.  
    114. public void teleport(Location loc) {
    115. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    116. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    117. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (loc.getX() * 32)));
    118. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (loc.getY() * 32)));
    119. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (loc.getZ() * 32)));
    120. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(loc.getYaw()));
    121. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(loc.getPitch()));
    122. this.l = loc;
    123. for (Player p : Bukkit.getOnlinePlayers()) {
    124. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(tp);
    125. }
    126. }
    127.  
    128. private byte getCompressedAngle(float value) {
    129. return (byte) ((value * 256.0F) / 360.0F);
    130. }
    131.  
    132. private byte getCompressedAngle2(float value) {
    133. return (byte) ((value * 256.0F) / 360.0F);
    134. }
    135.  
    136. public void remove() {
    137. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
    138. for (Player p : Bukkit.getOnlinePlayers()) {
    139. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    140. }
    141. }
    142.  
    143. @SuppressWarnings("deprecation")
    144. public void updateItems(ItemStack hand, ItemStack boots, ItemStack legs, ItemStack chest, ItemStack helmet) {
    145.  
    146. PacketPlayOutEntityEquipment[] ps = new PacketPlayOutEntityEquipment[]{
    147. new PacketPlayOutEntityEquipment(id, 1, CraftItemStack.asNMSCopy(boots)),
    148. new PacketPlayOutEntityEquipment(id, 2, CraftItemStack.asNMSCopy(legs)),
    149. new PacketPlayOutEntityEquipment(id, 3, CraftItemStack.asNMSCopy(chest)),
    150. new PacketPlayOutEntityEquipment(id, 4, CraftItemStack.asNMSCopy(helmet)),
    151. new PacketPlayOutEntityEquipment(id, 0, CraftItemStack.asNMSCopy(hand))
    152. };
    153. for (PacketPlayOutEntityEquipment pack : ps) {
    154. for (Player p : Bukkit.getOnlinePlayers()) {
    155. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(pack);
    156. }
    157. }
    158. }
    159.  
    160. @Deprecated
    161. public void setName(String s) {
    162. DataWatcher d = new DataWatcher(null);
    163. d.a(0, (Object) (byte) 0);
    164. d.a(1, (Object) (short) 0);
    165. d.a(8, (Object) (byte) 0);
    166. d.a(10, (Object) (String) s);
    167. //d.a(11, (Object) (byte) 0);
    168. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    169. for (Player p : Bukkit.getOnlinePlayers()) {
    170. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    171. }
    172. }
    173.  
    174. public void hideForPlayer(Player p) {
    175. DataWatcher d = new DataWatcher(null);
    176. d.a(0, (Object) (byte) 32);
    177. d.a(1, (Object) (short) 0);
    178. d.a(8, (Object) (byte) 0);
    179. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    180. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    181. }
    182.  
    183. public void showForPlayer(Player p) {
    184. DataWatcher d = new DataWatcher(null);
    185. d.a(0, (Object) (byte) 0);
    186. d.a(1, (Object) (short) 0);
    187. d.a(8, (Object) (byte) 0);
    188. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    189. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    190. }
    191.  
    192. public void addPotionColor(Color r) {
    193. int color = r.asBGR();
    194. final DataWatcher dw = new DataWatcher(null);
    195. dw.a(7, Integer.valueOf(color));
    196. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    197. for (Player p : Bukkit.getOnlinePlayers()) {
    198. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    199. }
    200. }
    201.  
    202. public void addPotionColor(int color) {
    203. final DataWatcher dw = new DataWatcher(null);
    204. dw.a(7, Integer.valueOf(color));
    205. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    206. for (Player p : Bukkit.getOnlinePlayers()) {
    207. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    208. }
    209. }
    210.  
    211. public void walk(double a, double b, double c) {
    212. walk(a, b, c, l.getYaw(), l.getPitch());
    213. }
    214.  
    215. public void walk(double a, double b, double c, float yaw, float pitch) {
    216. byte x = (byte) a;
    217. byte y = (byte) b;
    218. byte z = (byte) c;
    219. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook();
    220. setPrivateField(PacketPlayOutEntity.class, packet, "a", id);
    221. setPrivateField(PacketPlayOutEntity.class, packet, "b", x);
    222. setPrivateField(PacketPlayOutEntity.class, packet, "c", y);
    223. setPrivateField(PacketPlayOutEntity.class, packet, "d", z);
    224. setPrivateField(PacketPlayOutEntity.class, packet, "e", getCompressedAngle(yaw));
    225. setPrivateField(PacketPlayOutEntity.class, packet, "f", getCompressedAngle2(pitch));
    226.  
    227. PacketPlayOutEntityHeadRotation p2 = new PacketPlayOutEntityHeadRotation();
    228. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "a", id);
    229. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "b", getCompressedAngle(yaw));
    230.  
    231. for (Player p : Bukkit.getOnlinePlayers()) {
    232. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    233. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(p2);
    234. }
    235. l.setPitch(pitch);
    236. l.setYaw(yaw);
    237. l.add(a, b, c);
    238. }
    239.  
    240. public void sendtoplayer(Player who) {
    241. DataWatcher d = new DataWatcher(null);
    242. d.a(0, (Object) (byte) 0);
    243. d.a(1, (Object) (short) 0);
    244. d.a(8, (Object) (byte) 0);
    245. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    246. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    247. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile("", name));
    248. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) (l.getX() * 32)));
    249. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) (l.getY() * 32)));
    250. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) (l.getZ() * 32)));
    251. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    252. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    253. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    254. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    255.  
    256. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    257. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    258. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (l.getX() * 32)));
    259. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (l.getY() * 32)));
    260. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (l.getZ() * 32)));
    261. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    262. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    263.  
    264. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(spawn);
    265. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(tp);
    266. }
    267.  
    268. public void setInvisible() {
    269. DataWatcher d = new DataWatcher(null);
    270. d.a(0, (Object) (byte) 32);
    271. d.a(1, (Object) (short) 0);
    272. d.a(8, (Object) (byte) 0);
    273. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    274. for (Player p : Bukkit.getOnlinePlayers()) {
    275. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    276. }
    277. }
    278.  
    279. public void setCrouched() {
    280. DataWatcher d = new DataWatcher(null);
    281. d.a(0, (Object) (byte) 2);
    282. d.a(1, (Object) (short) 0);
    283. d.a(8, (Object) (byte) 0);
    284. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    285. for (Player p : Bukkit.getOnlinePlayers()) {
    286. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    287. }
    288. }
    289.  
    290. public void reset() {
    291. DataWatcher d = new DataWatcher(null);
    292. d.a(0, (Object) (byte) 0);
    293. d.a(1, (Object) (short) 0);
    294. d.a(8, (Object) (byte) 0);
    295. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    296. for (Player p : Bukkit.getOnlinePlayers()) {
    297. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    298. }
    299. }
    300.  
    301. public void sprint() {
    302. DataWatcher d = new DataWatcher(null);
    303. d.a(0, (Object) (byte) 8);
    304. d.a(1, (Object) (short) 0);
    305. d.a(8, (Object) (byte) 0);
    306. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    307. for (Player p : Bukkit.getOnlinePlayers()) {
    308. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    309. }
    310. }
    311.  
    312. @Deprecated
    313. public void block() {
    314. DataWatcher d = new DataWatcher(null);
    315. d.a(0, (Object) (byte) 16);
    316. d.a(1, (Object) (short) 0);
    317. d.a(6, (Object) (byte) 0);
    318. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    319. for (Player p : Bukkit.getOnlinePlayers()) {
    320. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    321. }
    322. }
    323.  
    324. public void damage() {
    325. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    326. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    327. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 2);
    328. for (Player p : Bukkit.getOnlinePlayers()) {
    329. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    330. }
    331. }
    332.  
    333. public void swingArm() {
    334. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    335. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    336. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 0);
    337. for (Player p : Bukkit.getOnlinePlayers()) {
    338. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    339. }
    340. }
    341.  
    342. @Deprecated
    343. public void eatInHand() {
    344. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    345. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    346. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 5);
    347. for (Player p : Bukkit.getOnlinePlayers()) {
    348. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    349. }
    350. }
    351.  
    352. public void sleep(Location loc) {
    353.  
    354. PacketPlayOutBed packet17 = new PacketPlayOutBed();
    355. setPrivateField(PacketPlayOutBed.class, packet17, "a", id);
    356. setPrivateField(PacketPlayOutBed.class, packet17, "b", (int) loc.getX());
    357. setPrivateField(PacketPlayOutBed.class, packet17, "c", (int) loc.getY());
    358. setPrivateField(PacketPlayOutBed.class, packet17, "d", (int) loc.getZ());
    359. // setPrivateField(PacketPlayOutBed.class, packet17, "e", 0);
    360.  
    361. for (Player p : Bukkit.getOnlinePlayers()){
    362. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet17);
    363. }
    364.  
    365.  
    366. }
    367.  
    368. public double getX() {
    369. return l.getX();
    370. }
    371.  
    372. public double getY() {
    373. return l.getY();
    374. }
    375.  
    376. public double getZ() {
    377. return l.getZ();
    378. }
    379.  
    380. public Location getLocation() {
    381. return l;
    382. }
    383.  
    384. }
     
  16. Offline

    Bammerbom

    elementalgodz11 this are npc's, only the hand and armour are visible. What would you else want with inventories?
     
  17. Offline

    Jannik8500

    - Updated to 1.7.7 - 1.7.9
    - Now work with UUID's

    Code:java
    1. import java.lang.reflect.Field;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.Random;
    5.  
    6. import net.minecraft.server.v1_7_R3.DataWatcher;
    7. import net.minecraft.server.v1_7_R3.EntityHuman;
    8. import net.minecraft.server.v1_7_R3.PacketPlayOutAnimation;
    9. import net.minecraft.server.v1_7_R3.PacketPlayOutBed;
    10. import net.minecraft.server.v1_7_R3.PacketPlayOutEntity;
    11. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityDestroy;
    12. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityEquipment;
    13. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityHeadRotation;
    14. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityMetadata;
    15. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityTeleport;
    16. import net.minecraft.server.v1_7_R3.PacketPlayOutNamedEntitySpawn;
    17. import net.minecraft.server.v1_7_R3.PacketPlayOutRelEntityMoveLook;
    18. import net.minecraft.util.com.mojang.authlib.GameProfile;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.Color;
    22. import org.bukkit.Location;
    23. import org.bukkit.World;
    24. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    25. import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemStack;
    26. import org.bukkit.entity.Player;
    27. import org.bukkit.inventory.ItemStack;
    28.  
    29. public class Human {
    30.  
    31. String name;
    32. World world;
    33. public int id;
    34. Location l;
    35. int itemInHand;
    36.  
    37. private List<Integer> ids = new ArrayList<Integer>();
    38.  
    39. private void setPrivateField(Class type, Object object, String name, Object value) {
    40. try {
    41. Field f = type.getDeclaredField(name);
    42. f.setAccessible(true);
    43. f.set(object, value);
    44. f.setAccessible(false);
    45. } catch (Exception ex) {
    46. ex.printStackTrace();
    47. }
    48. }
    49.  
    50. public void setPitch(float pitch) {
    51. this.walk(0.0d, 0.0d, 0.0d, l.getYaw(), pitch);
    52. }
    53.  
    54. public void setYaw(float yaw) {
    55. this.walk(0.0d, 0.0d, 0.0d, yaw, l.getPitch());
    56. }
    57.  
    58. public Human(World w, String name, int id, Location l, int itemInHand) {
    59. this.name = name;
    60. this.world = w;
    61. this.id = id;
    62. this.l = l;
    63. this.itemInHand = itemInHand;
    64. DataWatcher d = new DataWatcher(null);
    65. d.a(0, (Object) (byte) 0);
    66. d.a(1, (Object) (short) 0);
    67. d.a(8, (Object) (byte) 0);
    68. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    69. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    70. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile(Bukkit.getOfflinePlayer(name).getUniqueId(), name));
    71. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) l.getX() * 32));
    72. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) l.getY() * 32));
    73. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) l.getZ() * 32));
    74. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    75. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    76. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    77. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    78.  
    79. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    80. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    81. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) l.getX() * 32));
    82. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) l.getY() * 32));
    83. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) l.getZ() * 32));
    84. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    85. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    86.  
    87. for (Player p : Bukkit.getOnlinePlayers()) {
    88. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    89. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(tp);
    90. }
    91. ids.add(id);
    92. }
    93.  
    94. public Human(EntityHuman h) {
    95. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn(h);
    96. int id = new Random().nextInt(5000 - 1000) + 1000;
    97. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    98. this.id = id;
    99. PacketPlayOutEntityEquipment armor1 = new PacketPlayOutEntityEquipment(id, 1, h.inventory.getArmorContents()[0]);
    100. PacketPlayOutEntityEquipment armor2 = new PacketPlayOutEntityEquipment(id, 2, h.inventory.getArmorContents()[1]);
    101. PacketPlayOutEntityEquipment armor3 = new PacketPlayOutEntityEquipment(id, 3, h.inventory.getArmorContents()[2]);
    102. PacketPlayOutEntityEquipment armor4 = new PacketPlayOutEntityEquipment(id, 4, h.inventory.getArmorContents()[3]);
    103. PacketPlayOutEntityEquipment sword = new PacketPlayOutEntityEquipment(id, 0, h.inventory.getItem(0));
    104. for (Player p : Bukkit.getOnlinePlayers()) {
    105. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    106. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor1);
    107. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor2);
    108. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor3);
    109. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(armor4);
    110. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(sword);
    111. }
    112. }
    113.  
    114. public void teleport(Location loc) {
    115. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    116. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    117. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (loc.getX() * 32)));
    118. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (loc.getY() * 32)));
    119. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (loc.getZ() * 32)));
    120. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(loc.getYaw()));
    121. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(loc.getPitch()));
    122. this.l = loc;
    123. for (Player p : Bukkit.getOnlinePlayers()) {
    124. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(tp);
    125. }
    126. }
    127.  
    128. private byte getCompressedAngle(float value) {
    129. return (byte) ((value * 256.0F) / 360.0F);
    130. }
    131.  
    132. private byte getCompressedAngle2(float value) {
    133. return (byte) ((value * 256.0F) / 360.0F);
    134. }
    135.  
    136. public void remove() {
    137. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
    138. for (Player p : Bukkit.getOnlinePlayers()) {
    139. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    140. }
    141. }
    142.  
    143. @SuppressWarnings("deprecation")
    144. public void updateItems(ItemStack hand, ItemStack boots, ItemStack legs, ItemStack chest, ItemStack helmet) {
    145.  
    146. PacketPlayOutEntityEquipment[] ps = new PacketPlayOutEntityEquipment[]{
    147. new PacketPlayOutEntityEquipment(id, 1, CraftItemStack.asNMSCopy(boots)),
    148. new PacketPlayOutEntityEquipment(id, 2, CraftItemStack.asNMSCopy(legs)),
    149. new PacketPlayOutEntityEquipment(id, 3, CraftItemStack.asNMSCopy(chest)),
    150. new PacketPlayOutEntityEquipment(id, 4, CraftItemStack.asNMSCopy(helmet)),
    151. new PacketPlayOutEntityEquipment(id, 0, CraftItemStack.asNMSCopy(hand))
    152. };
    153. for (PacketPlayOutEntityEquipment pack : ps) {
    154. for (Player p : Bukkit.getOnlinePlayers()) {
    155. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(pack);
    156. }
    157. }
    158. }
    159.  
    160. @Deprecated
    161. public void setName(String s) {
    162. DataWatcher d = new DataWatcher(null);
    163. d.a(0, (Object) (byte) 0);
    164. d.a(1, (Object) (short) 0);
    165. d.a(8, (Object) (byte) 0);
    166. d.a(10, (Object) (String) s);
    167. //d.a(11, (Object) (byte) 0);
    168. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    169. for (Player p : Bukkit.getOnlinePlayers()) {
    170. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    171. }
    172. }
    173.  
    174. public void hideForPlayer(Player p) {
    175. DataWatcher d = new DataWatcher(null);
    176. d.a(0, (Object) (byte) 32);
    177. d.a(1, (Object) (short) 0);
    178. d.a(8, (Object) (byte) 0);
    179. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    180. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    181. }
    182.  
    183. public void showForPlayer(Player p) {
    184. DataWatcher d = new DataWatcher(null);
    185. d.a(0, (Object) (byte) 0);
    186. d.a(1, (Object) (short) 0);
    187. d.a(8, (Object) (byte) 0);
    188. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    189. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    190. }
    191.  
    192. public void addPotionColor(Color r) {
    193. int color = r.asBGR();
    194. final DataWatcher dw = new DataWatcher(null);
    195. dw.a(7, Integer.valueOf(color));
    196. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    197. for (Player p : Bukkit.getOnlinePlayers()) {
    198. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    199. }
    200. }
    201.  
    202. public void addPotionColor(int color) {
    203. final DataWatcher dw = new DataWatcher(null);
    204. dw.a(7, Integer.valueOf(color));
    205. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, dw, true);
    206. for (Player p : Bukkit.getOnlinePlayers()) {
    207. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    208. }
    209. }
    210.  
    211. public void walk(double a, double b, double c) {
    212. walk(a, b, c, l.getYaw(), l.getPitch());
    213. }
    214.  
    215. public void walk(double a, double b, double c, float yaw, float pitch) {
    216. byte x = (byte) a;
    217. byte y = (byte) b;
    218. byte z = (byte) c;
    219. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook();
    220. setPrivateField(PacketPlayOutEntity.class, packet, "a", id);
    221. setPrivateField(PacketPlayOutEntity.class, packet, "b", x);
    222. setPrivateField(PacketPlayOutEntity.class, packet, "c", y);
    223. setPrivateField(PacketPlayOutEntity.class, packet, "d", z);
    224. setPrivateField(PacketPlayOutEntity.class, packet, "e", getCompressedAngle(yaw));
    225. setPrivateField(PacketPlayOutEntity.class, packet, "f", getCompressedAngle2(pitch));
    226.  
    227. PacketPlayOutEntityHeadRotation p2 = new PacketPlayOutEntityHeadRotation();
    228. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "a", id);
    229. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "b", getCompressedAngle(yaw));
    230.  
    231. for (Player p : Bukkit.getOnlinePlayers()) {
    232. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    233. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(p2);
    234. }
    235. l.setPitch(pitch);
    236. l.setYaw(yaw);
    237. l.add(a, b, c);
    238. }
    239.  
    240. public void sendtoplayer(Player who) {
    241. DataWatcher d = new DataWatcher(null);
    242. d.a(0, (Object) (byte) 0);
    243. d.a(1, (Object) (short) 0);
    244. d.a(8, (Object) (byte) 0);
    245. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    246. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    247. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b", new GameProfile(Bukkit.getOfflinePlayer(name).getUniqueId(), name));
    248. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c", ((int) (l.getX() * 32)));
    249. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d", ((int) (l.getY() * 32)));
    250. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e", ((int) (l.getZ() * 32)));
    251. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f", getCompressedAngle(l.getYaw()));
    252. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g", getCompressedAngle(l.getPitch()));
    253. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h", itemInHand);
    254. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    255.  
    256. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    257. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    258. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b", ((int) (l.getX() * 32)));
    259. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c", ((int) (l.getY() * 32)));
    260. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d", ((int) (l.getZ() * 32)));
    261. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e", getCompressedAngle(l.getYaw()));
    262. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f", getCompressedAngle(l.getPitch()));
    263.  
    264. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(spawn);
    265. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(tp);
    266. }
    267.  
    268. public void setInvisible() {
    269. DataWatcher d = new DataWatcher(null);
    270. d.a(0, (Object) (byte) 32);
    271. d.a(1, (Object) (short) 0);
    272. d.a(8, (Object) (byte) 0);
    273. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    274. for (Player p : Bukkit.getOnlinePlayers()) {
    275. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    276. }
    277. }
    278.  
    279. public void setCrouched() {
    280. DataWatcher d = new DataWatcher(null);
    281. d.a(0, (Object) (byte) 2);
    282. d.a(1, (Object) (short) 0);
    283. d.a(8, (Object) (byte) 0);
    284. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    285. for (Player p : Bukkit.getOnlinePlayers()) {
    286. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    287. }
    288. }
    289.  
    290. public void reset() {
    291. DataWatcher d = new DataWatcher(null);
    292. d.a(0, (Object) (byte) 0);
    293. d.a(1, (Object) (short) 0);
    294. d.a(8, (Object) (byte) 0);
    295. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    296. for (Player p : Bukkit.getOnlinePlayers()) {
    297. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    298. }
    299. }
    300.  
    301. public void sprint() {
    302. DataWatcher d = new DataWatcher(null);
    303. d.a(0, (Object) (byte) 8);
    304. d.a(1, (Object) (short) 0);
    305. d.a(8, (Object) (byte) 0);
    306. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    307. for (Player p : Bukkit.getOnlinePlayers()) {
    308. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    309. }
    310. }
    311.  
    312. @Deprecated
    313. public void block() {
    314. DataWatcher d = new DataWatcher(null);
    315. d.a(0, (Object) (byte) 16);
    316. d.a(1, (Object) (short) 0);
    317. d.a(6, (Object) (byte) 0);
    318. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(id, d, true);
    319. for (Player p : Bukkit.getOnlinePlayers()) {
    320. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    321. }
    322. }
    323.  
    324. public void damage() {
    325. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    326. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    327. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 2);
    328. for (Player p : Bukkit.getOnlinePlayers()) {
    329. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    330. }
    331. }
    332.  
    333. public void swingArm() {
    334. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    335. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    336. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 0);
    337. for (Player p : Bukkit.getOnlinePlayers()) {
    338. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    339. }
    340. }
    341.  
    342. @Deprecated
    343. public void eatInHand() {
    344. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    345. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    346. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 5);
    347. for (Player p : Bukkit.getOnlinePlayers()) {
    348. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    349. }
    350. }
    351.  
    352. public void sleep(Location loc) {
    353.  
    354. PacketPlayOutBed packet17 = new PacketPlayOutBed();
    355. setPrivateField(PacketPlayOutBed.class, packet17, "a", id);
    356. setPrivateField(PacketPlayOutBed.class, packet17, "b", (int) loc.getX());
    357. setPrivateField(PacketPlayOutBed.class, packet17, "c", (int) loc.getY());
    358. setPrivateField(PacketPlayOutBed.class, packet17, "d", (int) loc.getZ());
    359. // setPrivateField(PacketPlayOutBed.class, packet17, "e", 0);
    360.  
    361. for (Player p : Bukkit.getOnlinePlayers()){
    362. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet17);
    363. }
    364.  
    365.  
    366. }
    367.  
    368. public double getX() {
    369. return l.getX();
    370. }
    371.  
    372. public double getY() {
    373. return l.getY();
    374. }
    375.  
    376. public double getZ() {
    377. return l.getZ();
    378. }
    379.  
    380. public Location getLocation() {
    381. return l;
    382. }
    383.  
    384. }
     
  18. Offline

    elementalgodz11

    How can I get an NPC by an ID?
     
  19. elementalgodz11 put all your npcs in hashmap with key and value as their id
     
  20. Offline

    Lactem

    Okay so I updated this to work actually work with the GameProfile. I also added stuff for saving to a file and reading from a file because otherwise all the npcs get deleted when the server restarts. You'll also want to use Human#sendToPlayer(player) when someone logs in.
    Code:java
    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.lang.reflect.Field;
    4. import java.util.ArrayList;
    5. import java.util.UUID;
    6.  
    7. import net.minecraft.server.v1_7_R3.DataWatcher;
    8. import net.minecraft.server.v1_7_R3.PacketPlayOutAnimation;
    9. import net.minecraft.server.v1_7_R3.PacketPlayOutBed;
    10. import net.minecraft.server.v1_7_R3.PacketPlayOutEntity;
    11. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityDestroy;
    12. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityEquipment;
    13. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityHeadRotation;
    14. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityMetadata;
    15. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityTeleport;
    16. import net.minecraft.server.v1_7_R3.PacketPlayOutNamedEntitySpawn;
    17. import net.minecraft.server.v1_7_R3.PacketPlayOutRelEntityMoveLook;
    18. import net.minecraft.util.com.mojang.authlib.GameProfile;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.Color;
    22. import org.bukkit.Location;
    23. import org.bukkit.World;
    24. import org.bukkit.configuration.ConfigurationSection;
    25. import org.bukkit.configuration.file.FileConfiguration;
    26. import org.bukkit.configuration.file.YamlConfiguration;
    27. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    28. import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemStack;
    29. import org.bukkit.entity.Player;
    30. import org.bukkit.inventory.ItemStack;
    31. import org.bukkit.plugin.java.JavaPlugin;
    32.  
    33. public class Human {
    34.  
    35. String name;
    36. World world;
    37. public int id;
    38. public static JavaPlugin plugin;
    39. Location l;
    40. int itemInHand;
    41. private UUID uuid;
    42.  
    43. public static ArrayList<Location> locations = new ArrayList<Location>();
    44. public static ArrayList<Human> humans = new ArrayList<Human>();
    45.  
    46. @SuppressWarnings("rawtypes")
    47. private void setPrivateField(Class type, Object object, String name,
    48. Object value) {
    49. try {
    50. Field f = type.getDeclaredField(name);
    51. f.setAccessible(true);
    52. f.set(object, value);
    53. f.setAccessible(false);
    54. } catch (Exception ex) {
    55. ex.printStackTrace();
    56. }
    57. }
    58.  
    59. public void setPitch(float pitch) {
    60. this.walk(0.0d, 0.0d, 0.0d, l.getYaw(), pitch);
    61. }
    62.  
    63. public void setYaw(float yaw) {
    64. this.walk(0.0d, 0.0d, 0.0d, yaw, l.getPitch());
    65. }
    66.  
    67. public Human(World w, String name, int id, Location l, int itemInHand) {
    68. this.name = name;
    69. this.world = w;
    70. this.id = id;
    71. this.l = l;
    72. this.itemInHand = itemInHand;
    73. this.uuid = UUID.randomUUID();
    74. DataWatcher d = new DataWatcher(null);
    75. d.a(0, (Object) (byte) 0);
    76. d.a(1, (Object) (short) 0);
    77. d.a(8, (Object) (byte) 0);
    78. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    79. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    80. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b",
    81. new GameProfile(uuid, name));
    82. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c",
    83. ((int) l.getX() * 32));
    84. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d",
    85. ((int) l.getY() * 32));
    86. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e",
    87. ((int) l.getZ() * 32));
    88. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f",
    89. getCompressedAngle(l.getYaw()));
    90. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g",
    91. getCompressedAngle(l.getPitch()));
    92. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h",
    93. itemInHand);
    94. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    95.  
    96. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    97. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    98. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b",
    99. ((int) l.getX() * 32));
    100. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c",
    101. ((int) l.getY() * 32));
    102. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d",
    103. ((int) l.getZ() * 32));
    104. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e",
    105. getCompressedAngle(l.getYaw()));
    106. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f",
    107. getCompressedAngle(l.getPitch()));
    108.  
    109. for (Player p : Bukkit.getOnlinePlayers()) {
    110. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawn);
    111. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(tp);
    112. }
    113. locations.add(l);
    114. humans.add(this);
    115. }
    116.  
    117. public void teleport(Location loc) {
    118. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    119. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    120. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b",
    121. ((int) (loc.getX() * 32)));
    122. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c",
    123. ((int) (loc.getY() * 32)));
    124. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d",
    125. ((int) (loc.getZ() * 32)));
    126. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e",
    127. getCompressedAngle(loc.getYaw()));
    128. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f",
    129. getCompressedAngle(loc.getPitch()));
    130. locations.remove(l);
    131. humans.remove(this);
    132. this.l = loc;
    133. locations.add(l);
    134. humans.add(this);
    135. for (Player p : Bukkit.getOnlinePlayers()) {
    136. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(tp);
    137. }
    138.  
    139. }
    140.  
    141. private byte getCompressedAngle(float value) {
    142. return (byte) ((value * 256.0F) / 360.0F);
    143. }
    144.  
    145. private byte getCompressedAngle2(float value) {
    146. return (byte) ((value * 256.0F) / 360.0F);
    147. }
    148.  
    149. public void remove() {
    150. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
    151. for (Player p : Bukkit.getOnlinePlayers()) {
    152. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    153. }
    154. }
    155.  
    156. public void updateItems(ItemStack hand, ItemStack boots, ItemStack legs,
    157. ItemStack chest, ItemStack helmet) {
    158.  
    159. PacketPlayOutEntityEquipment[] ps = new PacketPlayOutEntityEquipment[] {
    160. new PacketPlayOutEntityEquipment(id, 1,
    161. CraftItemStack.asNMSCopy(boots)),
    162. new PacketPlayOutEntityEquipment(id, 2,
    163. CraftItemStack.asNMSCopy(legs)),
    164. new PacketPlayOutEntityEquipment(id, 3,
    165. CraftItemStack.asNMSCopy(chest)),
    166. new PacketPlayOutEntityEquipment(id, 4,
    167. CraftItemStack.asNMSCopy(helmet)),
    168. new PacketPlayOutEntityEquipment(id, 0,
    169. CraftItemStack.asNMSCopy(hand)) };
    170. for (PacketPlayOutEntityEquipment pack : ps) {
    171. for (Player p : Bukkit.getOnlinePlayers()) {
    172. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(pack);
    173. }
    174. }
    175. }
    176.  
    177. @Deprecated
    178. public void setName(String s) {
    179. DataWatcher d = new DataWatcher(null);
    180. d.a(0, (Object) (byte) 0);
    181. d.a(1, (Object) (short) 0);
    182. d.a(8, (Object) (byte) 0);
    183. d.a(10, (Object) (String) s);
    184. // d.a(11, (Object) (byte) 0);
    185. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    186. id, d, true);
    187. for (Player p : Bukkit.getOnlinePlayers()) {
    188. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    189. }
    190. }
    191.  
    192. public void hideForPlayer(Player p) {
    193. DataWatcher d = new DataWatcher(null);
    194. d.a(0, (Object) (byte) 32);
    195. d.a(1, (Object) (short) 0);
    196. d.a(8, (Object) (byte) 0);
    197. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    198. id, d, true);
    199. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    200. }
    201.  
    202. public void showForPlayer(Player p) {
    203. DataWatcher d = new DataWatcher(null);
    204. d.a(0, (Object) (byte) 0);
    205. d.a(1, (Object) (short) 0);
    206. d.a(8, (Object) (byte) 0);
    207. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    208. id, d, true);
    209. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    210. }
    211.  
    212. public void addPotionColor(Color r) {
    213. int color = r.asBGR();
    214. final DataWatcher dw = new DataWatcher(null);
    215. dw.a(7, Integer.valueOf(color));
    216. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    217. id, dw, true);
    218. for (Player p : Bukkit.getOnlinePlayers()) {
    219. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    220. }
    221. }
    222.  
    223. public void addPotionColor(int color) {
    224. final DataWatcher dw = new DataWatcher(null);
    225. dw.a(7, Integer.valueOf(color));
    226. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    227. id, dw, true);
    228. for (Player p : Bukkit.getOnlinePlayers()) {
    229. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    230. }
    231. }
    232.  
    233. public void walk(double a, double b, double c) {
    234. walk(a, b, c, l.getYaw(), l.getPitch());
    235. }
    236.  
    237. public void walk(double a, double b, double c, float yaw, float pitch) {
    238. byte x = (byte) a;
    239. byte y = (byte) b;
    240. byte z = (byte) c;
    241. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook();
    242. setPrivateField(PacketPlayOutEntity.class, packet, "a", id);
    243. setPrivateField(PacketPlayOutEntity.class, packet, "b", x);
    244. setPrivateField(PacketPlayOutEntity.class, packet, "c", y);
    245. setPrivateField(PacketPlayOutEntity.class, packet, "d", z);
    246. setPrivateField(PacketPlayOutEntity.class, packet, "e",
    247. getCompressedAngle(yaw));
    248. setPrivateField(PacketPlayOutEntity.class, packet, "f",
    249. getCompressedAngle2(pitch));
    250.  
    251. PacketPlayOutEntityHeadRotation p2 = new PacketPlayOutEntityHeadRotation();
    252. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "a", id);
    253. setPrivateField(PacketPlayOutEntityHeadRotation.class, p2, "b",
    254. getCompressedAngle(yaw));
    255.  
    256. for (Player p : Bukkit.getOnlinePlayers()) {
    257. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    258. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(p2);
    259. }
    260. locations.remove(l);
    261. humans.remove(this);
    262. l.setPitch(pitch);
    263. l.setYaw(yaw);
    264. l.add(a, b, c);
    265. locations.add(l);
    266. humans.add(this);
    267. }
    268.  
    269. public void sendToPlayer(Player who) {
    270. DataWatcher d = new DataWatcher(null);
    271. d.a(0, (Object) (byte) 0);
    272. d.a(1, (Object) (short) 0);
    273. d.a(8, (Object) (byte) 0);
    274. PacketPlayOutNamedEntitySpawn spawn = new PacketPlayOutNamedEntitySpawn();
    275. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "a", id);
    276. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "b",
    277. new GameProfile(uuid, name));
    278. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "c",
    279. ((int) (l.getX() * 32)));
    280. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "d",
    281. ((int) (l.getY() * 32)));
    282. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "e",
    283. ((int) (l.getZ() * 32)));
    284. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "f",
    285. getCompressedAngle(l.getYaw()));
    286. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "g",
    287. getCompressedAngle(l.getPitch()));
    288. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "h",
    289. itemInHand);
    290. setPrivateField(PacketPlayOutNamedEntitySpawn.class, spawn, "i", d);
    291.  
    292. PacketPlayOutEntityTeleport tp = new PacketPlayOutEntityTeleport();
    293. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "a", id);
    294. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "b",
    295. ((int) (l.getX() * 32)));
    296. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "c",
    297. ((int) (l.getY() * 32)));
    298. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "d",
    299. ((int) (l.getZ() * 32)));
    300. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "e",
    301. getCompressedAngle(l.getYaw()));
    302. setPrivateField(PacketPlayOutEntityTeleport.class, tp, "f",
    303. getCompressedAngle(l.getPitch()));
    304.  
    305. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(spawn);
    306. ((CraftPlayer) who).getHandle().playerConnection.sendPacket(tp);
    307. locations.remove(l);
    308. humans.remove(this);
    309. this.l = who.getLocation();
    310. locations.add(l);
    311. humans.add(this);
    312. }
    313.  
    314. public void setInvisible() {
    315. DataWatcher d = new DataWatcher(null);
    316. d.a(0, (Object) (byte) 32);
    317. d.a(1, (Object) (short) 0);
    318. d.a(8, (Object) (byte) 0);
    319. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    320. id, d, true);
    321. for (Player p : Bukkit.getOnlinePlayers()) {
    322. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    323. }
    324. }
    325.  
    326. public void setCrouched() {
    327. DataWatcher d = new DataWatcher(null);
    328. d.a(0, (Object) (byte) 2);
    329. d.a(1, (Object) (short) 0);
    330. d.a(8, (Object) (byte) 0);
    331. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    332. id, d, true);
    333. for (Player p : Bukkit.getOnlinePlayers()) {
    334. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    335. }
    336. }
    337.  
    338. public void reset() {
    339. DataWatcher d = new DataWatcher(null);
    340. d.a(0, (Object) (byte) 0);
    341. d.a(1, (Object) (short) 0);
    342. d.a(8, (Object) (byte) 0);
    343. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    344. id, d, true);
    345. for (Player p : Bukkit.getOnlinePlayers()) {
    346. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    347. }
    348. }
    349.  
    350. public void sprint() {
    351. DataWatcher d = new DataWatcher(null);
    352. d.a(0, (Object) (byte) 8);
    353. d.a(1, (Object) (short) 0);
    354. d.a(8, (Object) (byte) 0);
    355. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    356. id, d, true);
    357. for (Player p : Bukkit.getOnlinePlayers()) {
    358. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    359. }
    360. }
    361.  
    362. @Deprecated
    363. public void block() {
    364. DataWatcher d = new DataWatcher(null);
    365. d.a(0, (Object) (byte) 16);
    366. d.a(1, (Object) (short) 0);
    367. d.a(6, (Object) (byte) 0);
    368. PacketPlayOutEntityMetadata packet40 = new PacketPlayOutEntityMetadata(
    369. id, d, true);
    370. for (Player p : Bukkit.getOnlinePlayers()) {
    371. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet40);
    372. }
    373. }
    374.  
    375. public void damage() {
    376. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    377. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    378. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 2);
    379. for (Player p : Bukkit.getOnlinePlayers()) {
    380. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    381. }
    382. }
    383.  
    384. public void swingArm() {
    385. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    386. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    387. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 0);
    388. for (Player p : Bukkit.getOnlinePlayers()) {
    389. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    390. }
    391. }
    392.  
    393. @Deprecated
    394. public void eatInHand() {
    395. PacketPlayOutAnimation packet18 = new PacketPlayOutAnimation();
    396. setPrivateField(PacketPlayOutAnimation.class, packet18, "a", id);
    397. setPrivateField(PacketPlayOutAnimation.class, packet18, "b", 5);
    398. for (Player p : Bukkit.getOnlinePlayers()) {
    399. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet18);
    400. }
    401. }
    402.  
    403. public void sleep(Location loc) {
    404. PacketPlayOutBed packet17 = new PacketPlayOutBed();
    405. setPrivateField(PacketPlayOutBed.class, packet17, "a", id);
    406. setPrivateField(PacketPlayOutBed.class, packet17, "b", (int) loc.getX());
    407. setPrivateField(PacketPlayOutBed.class, packet17, "c", (int) loc.getY());
    408. setPrivateField(PacketPlayOutBed.class, packet17, "d", (int) loc.getZ());
    409. // setPrivateField(PacketPlayOutBed.class, packet17, "e", 0);
    410.  
    411. for (Player p : Bukkit.getOnlinePlayers()) {
    412. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet17);
    413. }
    414. locations.remove(l);
    415. this.l = loc;
    416. locations.add(l);
    417. }
    418.  
    419. public double getX() {
    420. return l.getX();
    421. }
    422.  
    423. public double getY() {
    424. return l.getY();
    425. }
    426.  
    427. public double getZ() {
    428. return l.getZ();
    429. }
    430.  
    431. public Location getLocation() {
    432. return l;
    433. }
    434.  
    435. public static void serialize() {
    436. File file = new File(plugin.getDataFolder(), "npcs.yml");
    437. FileConfiguration npcs = YamlConfiguration.loadConfiguration(file);
    438. int current = 1;
    439. for (Human human : humans) {
    440. npcs.set("npcs.npc " + current + ".name", human.name);
    441. npcs.set("npcs.npc " + current + ".world", human.world.getName());
    442. npcs.set("npcs.npc " + current + ".id", human.id);
    443. npcs.set("npcs.npc " + current + ".location",
    444. locationToString(human.l));
    445. npcs.set("npcs.npc " + current + ".item", human.itemInHand);
    446. current++;
    447. }
    448. try {
    449. npcs.save(file);
    450. } catch (IOException e) {
    451. System.out.println("ERROR: " + e.getMessage());
    452. }
    453. }
    454.  
    455. public static ArrayList<Human> deserialize() {
    456. ArrayList<Human> humans = new ArrayList<Human>();
    457. File file = new File(plugin.getDataFolder() + "/npcs.yml");
    458. FileConfiguration npcs = YamlConfiguration.loadConfiguration(file);
    459. int current = 1;
    460. while (npcs.isConfigurationSection("npcs.npc " + current)) {
    461. ConfigurationSection sec = npcs.getConfigurationSection("npcs.npc "
    462. + current);
    463. Human human = new Human(Bukkit.getWorld(sec.getString("world")),
    464. sec.getString("name"), sec.getInt("id"),
    465. locationFromString(sec.getString("location")),
    466. sec.getInt("item"));
    467. humans.add(human);
    468. locations.add(human.l);
    469. current++;
    470. }
    471. try {
    472. npcs.set("npcs", null);
    473. npcs.save(file);
    474. } catch (IOException e) {
    475. System.out.println("ERROR: " + e.getMessage());
    476. }
    477. return humans;
    478. }
    479.  
    480. public static Location locationFromString(String string) {
    481. String[] location = string.split(",");
    482. return new Location(Bukkit.getWorld(location[0]),
    483. Double.parseDouble(location[1]),
    484. Double.parseDouble(location[2]),
    485. Double.parseDouble(location[3]));
    486. }
    487.  
    488. public static String locationToString(Location loc) {
    489. return loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY()
    490. + "," + loc.getZ();
    491. }
    492. }
    Now my questions is how to detect when a player clicks on them. Neither EntityDamageByEntityEvent nor PlayerInteractEntityEvent is fired when you left or right click the entities (because they're just packets). By the way, I used the ArrayList of locations to check if there's an entity at a location when a player clicks on that location.

    SoThatsIt Wouto1997 Jannik8500

    Well I came up with a fix, but there has to be a better way.
    Code:java
    1. private boolean isHumanInSight(Player player) {
    2. Location eyeLoc = null;
    3. List<Block> locs = player.getLineOfSight(null, 4);
    4. for (int i = 0; i < Human.locations.size(); i++) {
    5. Location loc = Human.locations.get(i);
    6. for (Block block : locs) {
    7. eyeLoc = block.getLocation();
    8. if (loc.getBlockX() == eyeLoc.getBlockX()
    9. && loc.getBlockY() == eyeLoc.getBlockY()
    10. && loc.getBlockZ() == eyeLoc.getBlockZ())
    11. return true;
    12. }
    13. }
    14. return false;
    15. }
    You just put this method in your listener class and call it when the player right/left clicks air.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  21. Offline

    BurnBlader

    I'm going to work on getting this to work with 1.7 over the next week or so. Thanks to everyone who previously updated it to 1.7.
     
    GuliPlays likes this.
  22. Offline

    mike546378

    I am looking for a way to do 2 things with this:
    - Add a skin to the NPCs (which I don't imagine would be too hard but I am not the best with packets)
    - Make the NPC lay down. I have managed to accomplish this with normal players using ProtocolLib (http://pastebin.com/GjFT8iE9) but am not sure how I would accomplish this with a NPC. Thanks for any help
     
  23. Offline

    megasaad44

    Can you make a PlayerNpcClickEvent so we can check if the player clicked the npc? Would be very useful.
     
  24. Offline

    OppositeGamer

    AMAZING!!!!
     
  25. Offline

    Lactem

    The way I made a while back is a little buggy (I'm referring to my latest message in this thread). You can either use that or make your own this way.
     
  26. Offline

    megasaad44

    Lactem Yeah I've lost interest in this. Sorry.
     
Thread Status:
Not open for further replies.

Share This Page