[Help!] Hologram without ProtocolLib

Discussion in 'Plugin Development' started by ArthurMaker, Feb 25, 2014.

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

    ArthurMaker

    I'm trying to make this without using ProtocoLib, but it doesn't work... :(
    What am I doing wrong?

    Code:java
    1. package Millenary.Craft.Hologram;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import net.minecraft.server.v1_7_R1.DataWatcher;
    6. import net.minecraft.server.v1_7_R1.Packet;
    7. import net.minecraft.server.v1_7_R1.PacketPlayOutAttachEntity;
    8. import net.minecraft.server.v1_7_R1.PacketPlayOutSpawnEntity;
    9. import net.minecraft.server.v1_7_R1.PacketPlayOutSpawnEntityLiving;
    10.  
    11. import org.bukkit.Location;
    12. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    13. import org.bukkit.entity.EntityType;
    14. import org.bukkit.entity.Player;
    15.  
    16. public class Hologram {
    17.  
    18. private static int entityId = Short.MAX_VALUE;
    19. private static final int WITHER_SKULL = 66;
    20.  
    21. @SuppressWarnings("deprecation")
    22. public static void spawnNametag(Player p, Location loc, double dy, String message) {
    23.  
    24. int hId = entityId++;
    25. int sId = entityId++;
    26.  
    27. PacketPlayOutSpawnEntityLiving horse = new PacketPlayOutSpawnEntityLiving();
    28. PacketPlayOutSpawnEntity skull = new PacketPlayOutSpawnEntity();
    29. PacketPlayOutAttachEntity attach = new PacketPlayOutAttachEntity();
    30.  
    31. // Initialize horse packet
    32. try {
    33. Field a = horse.getClass().getDeclaredField("a");
    34. a.setAccessible(true);
    35. a.set(horse, hId);
    36. a.setAccessible(false);
    37.  
    38. Field b = horse.getClass().getDeclaredField("b");
    39. b.setAccessible(true);
    40. b.set(horse, (byte) EntityType.HORSE.getTypeId());
    41. b.setAccessible(false);
    42. } catch(Exception x) {
    43. x.printStackTrace();
    44. }
    45.  
    46. setLocation(horse, dy, loc);
    47.  
    48. try{
    49. Field w = PacketPlayOutSpawnEntityLiving.class.getDeclaredField("l");
    50. w.setAccessible(true);
    51. w.set(horse, getWatcher(message));
    52. w.setAccessible(false);
    53. } catch(Exception x) {
    54. x.printStackTrace();
    55. }
    56.  
    57. // Initialize skull packet
    58. try {
    59. Field a = skull.getClass().getDeclaredField("a");
    60. a.setAccessible(true);
    61. a.set(skull, sId);
    62. a.setAccessible(false);
    63.  
    64. Field b = skull.getClass().getDeclaredField("b");
    65. b.setAccessible(true);
    66. b.set(skull, (byte) WITHER_SKULL);
    67. b.setAccessible(false);
    68. } catch(Exception x) {
    69. x.printStackTrace();
    70. }
    71.  
    72. setLocation(skull, dy, loc);
    73.  
    74. // The horse is riding on the skull
    75. try{
    76. setAttach(attach, hId, sId);
    77. } catch(Exception x) {
    78. x.printStackTrace();
    79. }
    80.  
    81. sendPacket(p, horse);
    82. sendPacket(p, skull);
    83. sendPacket(p, attach);
    84. }
    85.  
    86. private static void setLocation(PacketPlayOutSpawnEntity living, double dy, Location loc) {
    87. try{
    88. Field c = living.getClass().getDeclaredField("c");
    89. c.setAccessible(true); // allows us to access the field
    90. c.set(living, (int) Math.floor(loc.getX() * 32.0D));
    91. c.setAccessible(false);
    92. Field d = living.getClass().getDeclaredField("d");
    93. d.setAccessible(true); // allows us to access the field
    94. d.set(living, (int) Math.floor(loc.getY() * 32.0D));
    95. d.setAccessible(false);
    96. Field e = living.getClass().getDeclaredField("e");
    97. e.setAccessible(true); // allows us to access the field
    98. e.set(living, (int) Math.floor(loc.getZ() * 32.0D));
    99. e.setAccessible(false);
    100. } catch(Exception x) {
    101. x.printStackTrace();
    102. }
    103. }
    104.  
    105. private static void setLocation(PacketPlayOutSpawnEntityLiving living, double dy, Location loc) {
    106. try{
    107. Field c = living.getClass().getDeclaredField("c");
    108. c.setAccessible(true); // allows us to access the field
    109. c.set(living, (int) Math.floor(loc.getX() * 32.0D));
    110. c.setAccessible(false);
    111. Field d = living.getClass().getDeclaredField("d");
    112. d.setAccessible(true); // allows us to access the field
    113. d.set(living, (int) Math.floor(loc.getY() * 32.0D));
    114. d.setAccessible(false);
    115. Field e = living.getClass().getDeclaredField("e");
    116. e.setAccessible(true); // allows us to access the field
    117. e.set(living, (int) Math.floor(loc.getZ() * 32.0D));
    118. e.setAccessible(false);
    119. } catch(Exception x) {
    120. x.printStackTrace();
    121. }
    122. }
    123.  
    124. private static DataWatcher getWatcher(String text) {
    125. DataWatcher watcher = new DataWatcher(null);
    126.  
    127. watcher.a(10, (String) text); // Entity name
    128. watcher.a(11, (Byte) (byte) 1); // Show name, 1 = show, 0 = don't show
    129. watcher.a(12, -1700000); //Wither health, 300 = full
    130. // health
    131.  
    132. return watcher;
    133. }
    134.  
    135. private static void setAttach(PacketPlayOutAttachEntity a, int h, int s) {
    136. try {
    137. Field fa = a.getClass().getDeclaredField("a");
    138. fa.setAccessible(true); // allows us to access the field
    139. fa.set(a, (int) h);
    140. fa.setAccessible(false);
    141.  
    142. Field fb = a.getClass().getDeclaredField("b");
    143. fb.setAccessible(true); // allows us to access the field
    144. fb.set(a, (int) s);
    145. fb.setAccessible(false);
    146. } catch(Exception e){
    147. e.printStackTrace();
    148. }
    149. }
    150.  
    151. private static void sendPacket(Player player, Packet packet){
    152. try{
    153. ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packet);
    154. } catch(Exception x) {
    155. x.printStackTrace();
    156. }
    157. }
    158. }
     
Thread Status:
Not open for further replies.

Share This Page