Disguises

Discussion in 'Plugin Development' started by MCCoding, Sep 27, 2014.

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

    MCCoding

    I'm trying to make a somewhat simple disguise object in my plugin that allows me to extend on and then create better disguises, for example I extend on it to create a sheep disguise and with that I can then make it sheared change colours without having a huge disguise class. From this I know I will need to have three key things which is DataWatchers for the colours in the example (I think) and the packets to send to the players when disguised and the movement packets, at this point I have no clue on how I could achieve this I have never done much with packets if someone can show me examples on how I can use them for the disguising or could tell me what packets I need to create and destroy the disguises it would be much appreciated thanks!
     
  2. Offline

    CraftCreeper6

    MCCoding
    Want to know how easy my disguises are?
    1) Spawn the animal you want
    2) Hide the player
    3) Every second teleport the animal to the player.
    Done <3
     
  3. Offline

    MCCoding

  4. Offline

    CraftCreeper6

    MCCoding
    Yes, yes it does. Just use DisguseAPI...
     
  5. Offline

    MCCoding

    CraftCreeper6
    I'd much rather do it myself, make is more flexible for me later on
     
  6. Offline

    ChipDev

    We won't spoon feed you a whole plugin.
     
  7. Offline

    MCCoding

    ChipDev
    I'm not asking for that I'm simply asking what packets I need to use and if there is any documentation on how I can use them.
     
  8. Offline

    ChipDev

    Thats what I want.
    http://wiki.vg/Protocol
    But more of a JavaDoc :p
     
    MCCoding likes this.
  9. Offline

    Da_Co0k1eZ

    In my plugin, I use:
    - PacketPlayOutEntityDestroy
    - PacketPlayOutSpawnEntityLiving
    - PacketPlayOutEntityLook
    - PacketPlayOutEntityEquipment
    - PacketPlayOutEntityHeadRotation
     
    MCCoding likes this.
  10. Offline

    MCCoding

    Thanks for that Da_Co0k1eZ and ChipDev, Da_Co0k1eZ how would I update or refresh the created entity with the packets? Do I have to destroy and recreate them every time the player moves or is there a way to update the spawn packets with the movement ones?
     
  11. Offline

    fireblast709

    MCCoding generally you create new packets every time you send them (that's what MC does, at least)
     
  12. Offline

    MCCoding

    fireblast709
    Do you know of any examples of it or at least where it is located in the source? Just so I have a reference so I know how I can structure all of this
     
  13. Offline

    Da_Co0k1eZ

    MCCoding
    No, you don't recreate the entity, just update the current one.
    Use PacketPlayOutEntityLiving to spawn it into the world initially, then setup a bukkit runnable to send the packets to update the mob to every player.
    Usually I store the entity for the mob in an object in order to keep track of it.
    (Btw, the packet to update all of the mob's movements is PacketPlayOutEntityLook)

    MCCoding Also, the protocol page ChipDev suggested is a good source for all of the packets and what they do.

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

    MCCoding

    Da_Co0k1eZ

    This is what I have done so far with the spawn packet, but the problem is that I need to use a NMS entity to fill in some of the fields in the PacketPlayOutSpawnEntityLiving packet, there fore I need to have a NMS entity to use for the fields. I'm wondering if there is a better way to do this so in my Disguise object all i can just use new Disguise(Player, DisguiseType).

    EDIT: Also would the entity be the player getting disguised or the entity of the disguise eg a Blaze which i'm trying to disguise as?
     
  15. Offline

    Da_Co0k1eZ

    Here, this is the code I use to create my disguises.
    It would be easier for me to spoon feed than for you to throw a dart randomly at a target and hope that it hits.
    Code:java
    1. private DisguiseType disguise;
    2. private UUID player;
    3. private EntityLiving entity;
    4.  
    5. public Disguise(DisguiseType d, UUID p) {
    6. disguise = d;
    7. player = p;
    8. Location location = Bukkit.getServer().getPlayer(p).getLocation();
    9. switch(disguise) {
    10. case ZOMBIE:
    11. entity = new EntityZombie(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    12. break;
    13. case WITHER_SKELETON:
    14. entity = new EntitySkeleton(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    15. ((EntitySkeleton)entity).setSkeletonType(1);
    16. break;
    17. case SKELETON:
    18. entity = new EntitySkeleton(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    19. break;
    20. case ZOMBIEPIG:
    21. entity = new EntityPigZombie(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    22. break;
    23. case BLAZE:
    24. entity = new EntityBlaze(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    25. break;
    26. case ENDERMAN:
    27. entity = new EntityEnderman(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    28. break;
    29. case CREEPER:
    30. entity = new EntityCreeper(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    31. break;
    32. case SPIDER:
    33. entity = new EntitySpider(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    34. break;
    35. case WITCH:
    36. entity = new EntityWitch(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    37. break;
    38. case WITHER_BOSS:
    39. entity = new EntityWither(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    40. break;
    41. case GHAST:
    42. entity = new EntityGhast(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    43. break;
    44. case GIANT:
    45. entity = new EntityGiantZombie(((CraftWorld)Bukkit.getServer().getPlayer(p).getWorld()).getHandle());
    46. break;
    47. }
    48. if(d != null) {
    49. entity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
    50. entity.d(Bukkit.getServer().getPlayer(p).getEntityId());
    51. disguiseToAll();
    52. }
    53. }
    54.  
    55. public Player getPlayer() {
    56. return Bukkit.getPlayer(player);
    57. }
    58.  
    59. @SuppressWarnings("deprecation")
    60. public void removeDisguise() {
    61. this.disguise = null;
    62. Packet packetDestroy = new PacketPlayOutEntityDestroy(new int[] { Bukkit.getPlayer(player).getEntityId() });
    63. PacketPlayOutNamedEntitySpawn packetSpawn = new PacketPlayOutNamedEntitySpawn(((CraftPlayer)getPlayer()).getHandle());
    64. for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    65. if(p.getUniqueId() != player) {
    66. EntityPlayer nmsPlayer = ((CraftPlayer)p).getHandle();
    67. nmsPlayer.playerConnection.sendPacket(packetDestroy);
    68. nmsPlayer.playerConnection.sendPacket(packetSpawn);
    69. }
    70. }
    71. }
    72.  
    73. public void changeDisguise(DisguiseType d) {
    74. removeDisguise();
    75. this.disguise = d;
    76. disguiseToAll();
    77. }
    78.  
    79. @SuppressWarnings("deprecation")
    80. public void disguiseToAll() {
    81. Packet packetDestroy = new PacketPlayOutEntityDestroy(new int[] { Bukkit.getPlayer(player).getEntityId() });
    82. Packet packetSpawn = new PacketPlayOutSpawnEntityLiving(entity);
    83. for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    84. if(p.getUniqueId() != player) {
    85. EntityPlayer nmsPlayer = ((CraftPlayer)p).getHandle();
    86. nmsPlayer.playerConnection.sendPacket(packetDestroy);
    87. nmsPlayer.playerConnection.sendPacket(packetSpawn);
    88. }
    89. }
    90. new BukkitRunnable() {
    91. @Override
    92. public void run() {
    93. if(disguise == null) {
    94. return;
    95. }
    96. updateDisguise();
    97. }
    98. }.runTaskTimer(Bukkit.getPluginManager().getPlugin("SurviveTheHorde"), 20L, 20L);
    99. }
    100.  
    101. @SuppressWarnings("deprecation")
    102. public void updateDisguise() {
    103. for(Player observer : Bukkit.getServer().getOnlinePlayers()) {
    104. if (!observer.getWorld().equals(Bukkit.getPlayer(player).getWorld())) {
    105. return;
    106. }
    107. if (observer.getUniqueId().equals(player)) {
    108. return;
    109. }
    110. int entityId = Bukkit.getPlayer(player).getEntityId();
    111. if(disguise.isBiped()) {
    112. Packet[] packets = new Packet[7];
    113. Location location = Bukkit.getPlayer(player).getLocation();
    114. packets[0] = new PacketPlayOutEntityLook(entityId, (byte)(int)(location.getYaw() * 256.0F / 360.0F), (byte)(int)(location.getPitch() * 256.0F / 360.0F));
    115. EntityEquipment equipment = Bukkit.getPlayer(player).getEquipment();
    116. packets[1] = new PacketPlayOutEntityEquipment(entityId, 0, CraftItemStack.asNMSCopy(equipment.getItemInHand()));
    117. packets[2] = new PacketPlayOutEntityEquipment(entityId, 1, CraftItemStack.asNMSCopy(equipment.getBoots()));
    118. packets[3] = new PacketPlayOutEntityEquipment(entityId, 2, CraftItemStack.asNMSCopy(equipment.getLeggings()));
    119. packets[4] = new PacketPlayOutEntityEquipment(entityId, 3, CraftItemStack.asNMSCopy(equipment.getChestplate()));
    120. packets[5] = new PacketPlayOutEntityEquipment(entityId, 4, CraftItemStack.asNMSCopy(equipment.getHelmet()));
    121. Entity entity = ((CraftPlayer)Bukkit.getPlayer(player)).getHandle();
    122. packets[6] = new PacketPlayOutEntityHeadRotation(entity, (byte)(int)(location.getYaw() * 256.0F / 360.0F));
    123. EntityPlayer nmsplayer = ((CraftPlayer)observer).getHandle();
    124. for(int i = 0; i < packets.length; i++) {
    125. nmsplayer.playerConnection.sendPacket(packets[I]);[/I]
    126. [I] }[/I]
    127. [I] return;[/I]
    128. [I] }[/I]
    129. [I] Packet[] packets = new Packet[1];[/I]
    130. [I] Location location = Bukkit.getPlayer(player).getLocation();[/I]
    131. [I] packets[0] = new PacketPlayOutEntityLook(entityId, (byte)(int)(location.getYaw() * 256.0F / 360.0F), (byte)(int)(location.getPitch() * 256.0F / 360.0F));[/I]
    132. [I] Entity entity = ((CraftPlayer)Bukkit.getPlayer(player)).getHandle();[/I]
    133. [I] packets[1] = new PacketPlayOutEntityHeadRotation(entity, (byte)(int)(location.getYaw() * 256.0F / 360.0F));[/I]
    134. [I] EntityPlayer nmsplayer = ((CraftPlayer)observer).getHandle();[/I]
    135. [I] for(int i = 0; i < packets.length; i++) {[/I]
    136. [I] nmsplayer.playerConnection.sendPacket(packets[I]);[/I][/I]
    137. [I] }[/I]
    138. [I] }[/I]
    139. [I] }[/I]
    140.  
    141. [I] public static enum DisguiseType {[/I]
    142. [I] ZOMBIE(Type.BIPED), WITHER_SKELETON(Type.BIPED), SKELETON(Type.BIPED), ZOMBIEPIG(Type.BIPED), BLAZE(Type.MOB), ENDERMAN(Type.MOB), CREEPER(Type.MOB), SPIDER(Type.MOB), WITCH(Type.MOB), WITHER_BOSS(Type.MOB), GHAST(Type.MOB), GIANT(Type.MOB);[/I]
    143.  
    144. [I] private Type type;[/I]
    145.  
    146. [I] DisguiseType(Type type) {[/I]
    147. [I] this.type = type;[/I]
    148. [I] }[/I]
    149.  
    150. [I] public Type getType() {[/I]
    151. [I] return type;[/I]
    152. [I] }[/I]
    153.  
    154. [I] public boolean isBiped() {[/I]
    155. [I] if(type == Type.BIPED) {[/I]
    156. [I] return true;[/I]
    157. [I] }[/I]
    158. [I] return false;[/I]
    159. [I] }[/I]
    160.  
    161. [I] public static enum Type {[/I]
    162. [I] BIPED, MOB;[/I]
    163. [I] }[/I]
    164. [I] }[/I]
     
  16. Offline

    MCCoding

    Da_Co0k1eZ
    I had figured what I was doing wrong, I was casting the entity to something that would not work with a CraftPlayer, but I have 2 weird bugs/issues you may have a answer to, firstly is when I PacketPlayOutEntityEquipment I have to re-equip armour and change items in hand to show it also when a player flys the mob starts dropping, is there any way to fix these bugs?
     
  17. Offline

    Da_Co0k1eZ

    MCCoding
    To fix the mob's equipment, use the setEquipment() method on the entity, and get the equipment from EntityPlayer.
    As for the mob dropping whenever you fly, I don't think that you can fix that without writing your own packets using protocol lib or something similar.
     
  18. Offline

    MCCoding

    Da_Co0k1eZ
    I have managed to fix it all but for some weird reason when you disguise as certain mobs, for instance a Iron Golem it kicks players saying "Internal Exception: net.minecraft.util.io.handler.codec.EncoderException: NullPointerException"
     
Thread Status:
Not open for further replies.

Share This Page