[NMS] Disguise help!

Discussion in 'Plugin Development' started by xXBeLkAXx, Aug 13, 2014.

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

    xXBeLkAXx

    Guys, hello! I'm making a disguise plugin! I have this class for the disguise:
    Code:java
    1. package com.fantazzyline.belka.pb;
    2. import java.lang.reflect.Field;
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import java.util.Random;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.entity.EntityType;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    11. import net.minecraft.server.v1_7_R1.DataWatcher;
    12. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityDestroy;
    13. import net.minecraft.server.v1_7_R1.PacketPlayOutRelEntityMoveLook;
    14. import net.minecraft.server.v1_7_R1.PacketPlayOutSpawnEntityLiving;
    15.  
    16. public class DisguiseFactory {
    17.  
    18. public static Map<String, Integer> ids = new HashMap<String, Integer>();
    19.  
    20. @SuppressWarnings("deprecation")
    21. public static void disguisePlayer(Player initiator, EntityType e) {
    22. PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving();
    23. Location loc = initiator.getLocation();
    24. int id = new Random().nextInt(5000 - 1000) + 1000;
    25. ids.put(initiator.getName(), id);
    26. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "a",
    27. (int) id);
    28. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "b",
    29. (byte) e.getTypeId());
    30. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "c",
    31. (int) Math.floor(loc.getBlockX() * 32.0D));
    32. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "d",
    33. (int) Math.floor(loc.getBlockY() * 32.0D));
    34. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "e",
    35. (int) Math.floor(loc.getBlockZ() * 32.0D));
    36. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "f",
    37. (byte) 0);
    38. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "g",
    39. (byte) 0);
    40. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "h",
    41. (byte) 0);
    42. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "i",
    43. (byte) 0);
    44. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "j",
    45. (byte) 0);
    46. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "k",
    47. (byte) 0);
    48. DataWatcher watcher = new DataWatcher(null);
    49. watcher.a(0, (Byte) (byte) 0);
    50. watcher.a(5, "Spooky");
    51. watcher.a(6, (Float) (float) 1);
    52. setPrivateField(PacketPlayOutSpawnEntityLiving.class, mobPacket, "l",
    53. watcher);
    54. for (final Player player : Bukkit.getServer().getOnlinePlayers()) {
    55. if (player != initiator) {
    56. ((CraftPlayer) player).getHandle().playerConnection
    57. .sendPacket(mobPacket);
    58. ((CraftPlayer) player).getHandle().playerConnection
    59. .sendPacket(new PacketPlayOutEntityDestroy(
    60. ((CraftPlayer) initiator).getHandle().getId()));
    61. }
    62. }
    63. }
    64. public static void walk(Player pl) {
    65. if (ids.containsKey(pl.getName())) {
    66. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook(ids.get(pl.getName()),
    67. (byte) pl.getLocation().getX(),
    68. (byte) pl.getLocation().getY(),
    69. (byte) pl.getLocation().getZ(),
    70. (byte) pl.getLocation().getYaw(),
    71. (byte) pl.getLocation().getPitch());
    72. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    73. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
    74. }
    75. }
    76. }
    77. public static void deleteDisguise(Player p) {
    78. if (ids.containsKey(p.getName())) {
    79. PacketPlayOutEntityDestroy mobPacket = new PacketPlayOutEntityDestroy(
    80. ids.get(p.getName()));
    81. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    82. ((CraftPlayer) player).getHandle().playerConnection
    83. .sendPacket(mobPacket);
    84. }
    85. ids.remove(p.getName());
    86. }
    87. }
    88. private static void setPrivateField(Class<?> type, Object object, String name,
    89. Object value) {
    90. try {
    91. Field f = type.getDeclaredField(name);
    92. f.setAccessible(true);
    93. f.set(object, value);
    94. f.setAccessible(false);
    95. } catch (Exception ex) {
    96. ex.printStackTrace();
    97. }
    98. }
    99. }

    And this class works fine! But entity moves wrong. I use walk(p) at the PlayerMoveEvent, and when I just rotate my had, my created entity "flying" far away. Help me, please, what's wrong?
     
  2. Offline

    xTigerRebornx

  3. Offline

    xXBeLkAXx

    xTigerRebornx
    What are u talking about? getX() and getBlockX()?
     
  4. Offline

    rbrick

    xXBeLkAXx
    xTigerRebornx means the x, y, z do not correspond to the location of the entity but the change from the original/previous position.
     
  5. Offline

    xXBeLkAXx

    rbrick
    So, what I should do?
     
  6. Offline

    xXBeLkAXx

    Bump! Please, help!
     
  7. Offline

    xTigerRebornx

    xXBeLkAXx Calculate the change in their x, y, and z and use those.
     
  8. Offline

    xXBeLkAXx

    xTigerRebornx
    Don't know how to do it. My walk packet -
    Code:java
    1. public static void walk(Player pl) {
    2. if (ids.containsKey(pl.getName())) {
    3. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook(ids.get(pl.getName()),
    4. (byte) pl.getLocation().getBlockX(),
    5. (byte) pl.getLocation().getY(),
    6. (byte) pl.getLocation().getZ(),
    7. (byte) pl.getLocation().getYaw(),
    8. (byte) pl.getLocation().getPitch());
    9. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    10. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
    11. }
    12. }
    13. }
     
  9. Offline

    xTigerRebornx

    xXBeLkAXx Its basic math, difference = subtraction. Subtract previous location values from new ones.
     
  10. Offline

    xXBeLkAXx

    xTigerRebornx
    I tried. But nothing changed :( Could you please type a code?
     
  11. Offline

    xTigerRebornx

    xXBeLkAXx I refuse to write your code if you can't do simple subtraction.
     
  12. Offline

    ChipDev

    Why u no Delta?
     
  13. Offline

    xXBeLkAXx

    xTigerRebornx
    My code:
    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. DisguiseFactory.walk(e.getPlayer());
    4. }
     
  14. Offline

    TheDummy

    xXBeLkAXx
    Code:java
    1.  
    2. public static void walk( String playerName, Location old, Location newLoc )
    3. {
    4. if( ids.containsKey( playerName ) )
    5. {
    6. PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutRelEntityMoveLook(
    7. ids.get( playerName ),
    8. (byte) ((newLoc.getBlockX() - oldLoc.getBlockX()) * 32), // Fixed Point Format
    9. (byte) ((newLoc.getBlockY() - oldLoc.getBlockY()) * 32), // Fixed Point Format
    10. (byte) ((newLoc.getBlockZ() - oldLoc.getBlockZ()) * 32), // Fixed Point Format
    11. (byte) ((newLoc.getYaw() + 180) / 360), // Site says "fraction of 360", might need tweaking
    12. (byte) ((newLoc.getPitch() + 180) / 360); // Site says "fraction of 360", might need tweaking
    13. for( Player p : Bukkit.getServer().getOnlinePlayers() )
    14. {
    15. ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
    16. }
    17. }
    18. }


    @Edit: Changed to fixed point and "fraction of 360."
    If yaw and pitch go from -180 to 180, then adding 180 changes to a scale of 0 to 360. Then you divide to get "fraction of 360."
     
  15. Offline

    xTigerRebornx

  16. Offline

    TheDummy

  17. Offline

    xXBeLkAXx

Thread Status:
Not open for further replies.

Share This Page