[UTIL|CLASS|TUTORIAL] Easy method to Disguise Players without APIs

Discussion in 'Resources' started by DevRosemberg, Nov 3, 2013.

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

    DevRosemberg

    Hello everyone, today i have created a simple method to Disguise Players as Mobs without any api, the code is this:

    Code:java
    1. public static void playerAsMob(Player toHide, EntityType mob, Player... canSee) {
    2. EntityLiving toSpawn = null;
    3. if (mob.equals(EntityType.BAT)){
    4. toSpawn = new EntityBat(((CraftPlayer) toHide).getHandle().world);
    5. }
    6. if (mob.equals(EntityType.BLAZE)){
    7. toSpawn = new EntityBlaze(((CraftPlayer) toHide).getHandle().world);
    8. }
    9. if (mob.equals(EntityType.CAVE_SPIDER)){
    10. toSpawn = new EntityCaveSpider(((CraftPlayer) toHide).getHandle().world);
    11. }
    12. if (mob.equals(EntityType.CHICKEN)){
    13. toSpawn = new EntityChicken(((CraftPlayer) toHide).getHandle().world);
    14. }
    15. if (mob.equals(EntityType.COW)){
    16. toSpawn = new EntityCow(((CraftPlayer) toHide).getHandle().world);
    17. }
    18. if (mob.equals(EntityType.CREEPER)){
    19. toSpawn = new EntityCreeper(((CraftPlayer) toHide).getHandle().world);
    20. }
    21. if (mob.equals(EntityType.ENDER_DRAGON)){
    22. toSpawn = new EntityEnderDragon(((CraftPlayer) toHide).getHandle().world);
    23. }
    24. if (mob.equals(EntityType.ENDERMAN)){
    25. toSpawn = new EntityEnderman(((CraftPlayer) toHide).getHandle().world);
    26. }
    27. if (mob.equals(EntityType.GHAST)){
    28. toSpawn = new EntityGhast(((CraftPlayer) toHide).getHandle().world);
    29. }
    30. if (mob.equals(EntityType.GIANT)){
    31. toSpawn = new EntityGiantZombie(((CraftPlayer) toHide).getHandle().world);
    32. }
    33. if (mob.equals(EntityType.HORSE)){
    34. toSpawn = new EntityHorse(((CraftPlayer) toHide).getHandle().world);
    35. }
    36. if (mob.equals(EntityType.IRON_GOLEM)){
    37. toSpawn = new EntityIronGolem(((CraftPlayer) toHide).getHandle().world);
    38. }
    39. if (mob.equals(EntityType.MAGMA_CUBE)){
    40. toSpawn = new EntityMagmaCube(((CraftPlayer) toHide).getHandle().world);
    41. }
    42. if (mob.equals(EntityType.MUSHROOM_COW)){
    43. toSpawn = new EntityMushroomCow(((CraftPlayer) toHide).getHandle().world);
    44. }
    45. if (mob.equals(EntityType.OCELOT)){
    46. toSpawn = new EntityOcelot(((CraftPlayer) toHide).getHandle().world);
    47. }
    48. if (mob.equals(EntityType.PIG)){
    49. toSpawn = new EntityPig(((CraftPlayer) toHide).getHandle().world);
    50. }
    51. if (mob.equals(EntityType.PIG_ZOMBIE)){
    52. toSpawn = new EntityPigZombie(((CraftPlayer) toHide).getHandle().world);
    53. }
    54. if (mob.equals(EntityType.SHEEP)){
    55. toSpawn = new EntitySheep(((CraftPlayer) toHide).getHandle().world);
    56. }
    57. if (mob.equals(EntityType.SILVERFISH)){
    58. toSpawn = new EntitySilverfish(((CraftPlayer) toHide).getHandle().world);
    59. }
    60. if (mob.equals(EntityType.SKELETON)){
    61. toSpawn = new EntitySkeleton(((CraftPlayer) toHide).getHandle().world);
    62. }
    63. if (mob.equals(EntityType.SLIME)){
    64. toSpawn = new EntitySlime(((CraftPlayer) toHide).getHandle().world);
    65. }
    66. if (mob.equals(EntityType.SNOWMAN)){
    67. toSpawn = new EntitySnowman(((CraftPlayer) toHide).getHandle().world);
    68. }
    69. if (mob.equals(EntityType.SPIDER)){
    70. toSpawn = new EntitySpider(((CraftPlayer) toHide).getHandle().world);
    71. }
    72. if (mob.equals(EntityType.SQUID)){
    73. toSpawn = new EntitySquid(((CraftPlayer) toHide).getHandle().world);
    74. }
    75. if (mob.equals(EntityType.VILLAGER)){
    76. toSpawn = new EntityVillager(((CraftPlayer) toHide).getHandle().world);
    77. }
    78. if (mob.equals(EntityType.WITCH)){
    79. toSpawn = new EntityWitch(((CraftPlayer) toHide).getHandle().world);
    80. }
    81. if (mob.equals(EntityType.WITHER)){
    82. toSpawn = new EntityWither(((CraftPlayer) toHide).getHandle().world);
    83. }
    84. if (mob.equals(EntityType.WOLF)){
    85. toSpawn = new EntityWolf(((CraftPlayer) toHide).getHandle().world);
    86. }
    87. if (mob.equals(EntityType.ZOMBIE)){
    88. toSpawn = new EntityZombie(((CraftPlayer) toHide).getHandle().world);
    89. }
    90. if (toSpawn == null) {
    91. throw new IllegalArgumentException("Mob must be living entity other than a player. Provided: " + mob.toString());
    92. }
    93. toSpawn.locX = toHide.getLocation().getX();
    94. toSpawn.locY = toHide.getLocation().getY();
    95. toSpawn.locZ = toHide.getLocation().getZ();
    96. toSpawn.id = ((CraftPlayer) toHide).getHandle().id;
    97. for (Player a : Bukkit.getOnlinePlayers()) {
    98. if (a.equals(toHide)) {
    99. continue;
    100. }
    101. for (Player b : canSee) {
    102. if (a.equals(b)) {
    103. continue;
    104. }
    105. }
    106. ((CraftPlayer)a).getHandle().playerConnection.sendPacket(new Packet29DestroyEntity(((CraftPlayer)toHide).getHandle().id));
    107. ((CraftPlayer)a).getHandle().playerConnection.sendPacket(new Packet24MobSpawn(toSpawn));
    108. }
    109. }
    110.  
    111. public static void playerBackToPlayer(Player toShow, Player... cantSee) {
    112. for (Player a : Bukkit.getOnlinePlayers()) {
    113. if (a.equals(toShow)) {
    114. continue;
    115. }
    116. for (Player b : cantSee) {
    117. if (a.equals(b)) {
    118. continue;
    119. }
    120. }
    121. ((CraftPlayer)a).getHandle().playerConnection.sendPacket(new Packet29DestroyEntity(((CraftPlayer) toShow).getHandle().id));
    122. ((CraftPlayer)a).getHandle().playerConnection.sendPacket(new Packet20NamedEntitySpawn(((CraftPlayer)toShow).getHandle()));
    123. }
    124. }


    Many Devs will think this wont work, it wont make the Entity Move around when the player moves etc, well, it actually does.

    Regards, DevRo_
     
    inventorman101 likes this.
  2. Offline

    RingOfStorms

    This is only a small part of what API's do for you. This is what they do in the base of it, but this will not be persistent. This doesn't take in account for players teleporting to the disguised player, loging in near a disguised player, walking towards a disguised player, etc.. All of those will load the player as normal. API's do all the annoying lifting for you and keep everything synced up.

    Edit: Also wanted to mention that this will not prevent crashes from occurring based on the client getting packets that simply don't match what the disguise is.

    So using this may be good for a quick quick thing, but I wouldn't count on it for any long periods of time w/o crashes.
     
    Ultimate_n00b, JPG2000 and jimuskin like this.
  3. Offline

    JPG2000

    DevRosemberg Even if it doesn't work as planned, its the first utill i've seen on this topic. Most people just use the disguise API.
     
  4. Offline

    DevRosemberg

    JPG2000 I know but many people who get hired and have to include this but dont want to use APIs will drop the job for that reason.
     
  5. Offline

    macguy8

    DevRosemberg
    Someone who gets hired for a custom game shouldn't have to look on the Bukkit forums for a util for something as small as sending packets. If the case happens the employer won't allow any APIs to be used, then that developer is probably in the wrong place.
     
  6. Offline

    Panjab

    Just create a scheduler or a runnable which disguises (?) the player every x second(s)? ;)
     
  7. Panjab That's a bad way of doing it. Just use protocolLib and listenen for outgoing packet x.
     
  8. Offline

    Goblom

    DevRosemberg Why arent you using a switch-case for this ? I would be much more effective in this type of situation.
    Code:java
    1. public static void playerAsMob(Player toHide, EntityType mob, Player... canSee) {
    2. EntityLiving toSpawn = null;
    3. switch(mob) {
    4. case BAT:
    5. toSpawn = new EntityBat(((CraftPlayer) toHide).getHandle().world);
    6. break;
    7. case BLAZE:
    8. toSpawn = new EntityBlaze(((CraftPlayer) toHide).getHandle().world);
    9. break;
    10. }
    11. }
     
    mazentheamazin and Skyost like this.
  9. Offline

    DevRosemberg

    macguy8 Well, in that case the developer wouldnt be that good to be hired for a project like that.
    Goblom We are just talking about miliseconds here, you can change it if you want to though.
     
  10. Offline

    Conarnar

    This isn't update proof. Though it's actually really easy to use reflection to fix that problem. :D
    Also here:
    Code:java
    1. throw new IllegalArgumentException("Mob must be living entity other than a player. Provided: " + mob.toString());

    You don't need toString() when appending a string. It automatically does that.
     
  11. Offline

    DevRosemberg

    Conarnar Or simply change some 3's to 4's :p
     
  12. Offline

    libraryaddict

    Well I can guarantee one thing with using this.
    Sooner or later.
    Someone is going to crash.
     
    nubebuster likes this.
  13. Offline

    SoThatsIt

    they move because when the player moves it sends packets to the users with the id, not the entity, so if you make the entity have the same id as the player when you move, it will move.
     
  14. SoThatsIt I don't know what you exactly mean since it's very unclear but when you use this way, you define that entity id x = mob y, so when the player with entity id x moves, it sends a packet to the server, and the server sends the move packet to the clients, since the clients think entity id x = mob y, they will see mob y moving around. This way of doing it is really not recommended. The best way to disguise people is done by libraryaddict in his Lib's disguises. It's compact and works perfect.
     
  15. Offline

    nubebuster

    @devro_ 1.7.2 new one?
     
  16. Offline

    DevRosemberg

  17. Offline

    Garris0n

    Y u no else if?
     
    DevRosemberg likes this.
  18. Offline

    DevRosemberg

    Garris0n Gona use a switch this time i think
     
  19. Offline

    Garris0n

    Ew, switch. They only accept primitive "numbers", though, don't they?
     
  20. Offline

    RawCode

    this method exists for years...
     
  21. Offline

    ArthurMaker

    I'm waiting for the update! :D
     
  22. Offline

    Georgeto

    Why should somenone use this?
     
  23. Offline

    DevRosemberg

    Georgeto Simpler than other Libraries.
     
  24. Offline

    Skyost

    DevRosemberg
    @Globom is right. You should use a switch statement because your code is hurting my eyes. And, you should not use .equals(...), if you want to compare enums, use == instead.
     
    Goblom likes this.
  25. Offline

    DevRosemberg

    Skyost
     
  26. Offline

    BRADLAAAAA

    How would you do this for a block or a falling sand entity and texture it as say a grass block?
     
  27. Offline

    Hypsiz

    So much if, why not switch?
     
  28. Offline

    ThaBozZLuki

    DevRosemberg can u please update this code for 1.7.9?
    I tried to but i failed at updating the Packets!
    Would be awesome.
    And would be awesome if u can add me on Skype, : daluky.daluky
     
  29. Offline

    Phasesaber

    Switches accept anything. I usually use it for Materials.
     
  30. Offline

    Garris0n

    DevRosemberg and Skyost like this.
Thread Status:
Not open for further replies.

Share This Page