Util [Reflection] Disguise into mobs!

Discussion in 'Resources' started by xXBeLkAXx, Dec 28, 2014.

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

    xXBeLkAXx

    Hey, guys! I was playing with reflection, and made two utils, two disguises. I haven't wrote code in NMS, so I just downloaded code in NMS, and made it in reflection.

    Thanks:
    @DPOH-VAR - For helping me with reflection in some moments
    @Da_Co0k1eZ - For all code, what was made in NMS
    @ArthurMaker and his MillenaryAPI - For helping to find some fields and methods for me

    1. Change the skin, and disguise to other licensed player - http://bukkit.org/threads/class-change-players-skin-disguise.318544/

    2. This util, for disguising into mobs

    The class, what I used for reflection - https://forums.bukkit.org/threads/lib-reflection-utils.246850/

    It can be used in all 1.7.X - 1.8.X versions.

    Code:
    public class Disguise {
    
            private DisguiseType disguise;
            private String player;
            private RefClass entity;
            private Class<?> entityObject;
            private Object thisObject;
     
            @SuppressWarnings("deprecation")
            public Disguise(DisguiseType d, String p) {
            disguise = d;
            player = p;
            Location location = Bukkit.getServer().getPlayer(p).getLocation();
            switch(disguise) {
            case ZOMBIE:
            entity = getEntity("EntityZombie", p);
            break;
            case WITHER_SKELETON:
            entity = getEntity("EntitySkeleton", p);
     
            RefMethod methodSkeleton = entity.findMethodByName("setSkeletonType");
     
            methodSkeleton.of(thisObject).call(1);
            break;
            case SKELETON:
                entity = getEntity("EntitySkeleton", p);
            break;
            case ZOMBIEPIG:
                entity = getEntity("EntityPigZombie", p);
            break;
            case BLAZE:
                entity = getEntity("EntityBlaze", p);
            break;
            case ENDERMAN:
                entity = getEntity("EntityEnderman", p);
            break;
            case CREEPER:
                entity = getEntity("EntityCreeper", p);
            break;
            case SPIDER:
                entity = getEntity("EntitySpider", p);
            break;
            case WITCH:
                entity = getEntity("EntityWitch", p);
            break;
            case WITHER_BOSS:
                entity = getEntity("EntityWither", p);
            break;
            case GHAST:
                entity = getEntity("EntityGhast", p);
            break;
            case GIANT:
                entity = getEntity("EntityGiant", p);
            break;
            }
            if(d != null) {
     
            RefMethod m = entity.getMethod("setPosition", double.class, double.class, double.class);
            RefMethod mm = entity.getMethod("d", int.class);
            RefMethod mmm = entity.getMethod("setCustomName", String.class);
            RefMethod mmmm = entity.getMethod("setCustomNameVisible", boolean.class);
    
            m.of(thisObject).call(location.getX(), location.getY(), location.getZ());
            mm.of(thisObject).call(Bukkit.getServer().getPlayer(p).getEntityId());
            mmm.of(thisObject).call(ChatColor.YELLOW + Bukkit.getServer().getPlayer(p).getName());
            mmmm.of(thisObject).call(true);
     
            RefField rf = entity.getField("locX");
     
            rf.of(thisObject).set(location.getX());
     
            RefField rf1 = entity.getField("locY");
     
            rf1.of(thisObject).set(location.getY());
     
            RefField rf2 = entity.getField("locZ");
     
            rf2.of(thisObject).set(location.getZ());
     
     
            RefField rf3 = entity.getField("yaw");
     
            rf3.of(thisObject).set(location.getYaw());
     
            RefField rf4 = entity.getField("pitch");
     
            rf4.of(thisObject).set(location.getPitch());
     
            }
            }
      
            @SuppressWarnings("deprecation")
            public Player getPlayer() {
            return Bukkit.getPlayer(player);
            }
      
            @SuppressWarnings("deprecation")
            public void removeDisguise() {
            this.disguise = null;
    
            RefClass p29 = ReflectionUtil.getRefClass("{nms}.PacketPlayOutEntityDestroy");
     
            RefClass p20 = ReflectionUtil.getRefClass("{nms}.PacketPlayOutNamedEntitySpawn");
     
            RefConstructor pp20 = p20.getConstructor(ReflectionUtil.getRefClass("{nms}.EntityHuman"));
     
            RefConstructor pp29 = p29.getConstructor(int[].class);
     
            int[] entityId;
     
            entityId = new int[1];
     
            entityId[0] = Bukkit.getPlayer(player).getEntityId();
     
            Object packetEntityDestroy = pp29.create(entityId);
     
            Object packetNamedEntitySpawn = pp20.create((ReflectionUtil.getRefClass("{cb}.entity.CraftPlayer")).getMethod("getHandle").of(getPlayer()).call());
     
            RefClass classCraftPlayer = ReflectionUtil.getRefClass("{cb}.entity.CraftPlayer");
            RefMethod methodGetHandle = classCraftPlayer.getMethod("getHandle");
            RefClass classEntityPlayer = ReflectionUtil.getRefClass("{nms}.EntityPlayer");
            RefField fieldPlayerConnection = classEntityPlayer.getField("playerConnection");
            RefClass classPlayerConnection = ReflectionUtil.getRefClass("{nms}.PlayerConnection");
            RefMethod methodSendPacket = classPlayerConnection.findMethodByName("sendPacket");
     
            for (Player player : Bukkit.getOnlinePlayers()) {
         
                if(player != getPlayer()) {
                Object handle = methodGetHandle.of(player).call();
                Object connection = fieldPlayerConnection.of(handle).get();
         
                methodSendPacket.of(connection).call(packetEntityDestroy);
                methodSendPacket.of(connection).call(packetNamedEntitySpawn);
                }
            }
     
            }
      
            public void changeDisguise(DisguiseType d) {
            removeDisguise();
            this.disguise = d;
            Disguise dis = new Disguise(d, player);
            dis.disguiseToAll();
            }
      
            @SuppressWarnings("deprecation")
            public void disguiseToAll() {
         
                RefClass p29 = ReflectionUtil.getRefClass("{nms}.PacketPlayOutEntityDestroy");
         
                RefClass p20 = ReflectionUtil.getRefClass("{nms}.PacketPlayOutSpawnEntityLiving");
         
                RefConstructor pp20 = p20.getConstructor(ReflectionUtil.getRefClass("{nms}.EntityLiving"));
         
                RefConstructor pp29 = p29.getConstructor(int[].class);
         
                int[] entityId;
         
                entityId = new int[1];
         
                entityId[0] = Bukkit.getPlayer(player).getEntityId();
         
                Object packetEntityDestroy = pp29.create(entityId);
         
                Object packetNamedEntitySpawn = pp20.create(thisObject);
         
                RefClass classCraftPlayer = ReflectionUtil.getRefClass("{cb}.entity.CraftPlayer");
                RefMethod methodGetHandle = classCraftPlayer.getMethod("getHandle");
                RefClass classEntityPlayer = ReflectionUtil.getRefClass("{nms}.EntityPlayer");
                RefField fieldPlayerConnection = classEntityPlayer.getField("playerConnection");
                RefClass classPlayerConnection = ReflectionUtil.getRefClass("{nms}.PlayerConnection");
                RefMethod methodSendPacket = classPlayerConnection.findMethodByName("sendPacket");
         
                for (Player all : Bukkit.getOnlinePlayers()) {
                    if(all != Bukkit.getPlayer(player)) {
                    Object handle = methodGetHandle.of(all).call();
                    Object connection = fieldPlayerConnection.of(handle).get();
             
                    methodSendPacket.of(connection).call(packetEntityDestroy);
                    methodSendPacket.of(connection).call(packetNamedEntitySpawn);
                    }
                }
            }
      
            public static enum DisguiseType {
             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);
      
             private Type type;
      
             DisguiseType(Type type) {
             this.type = type;
             }
      
             public Type getType() {
             return type;
             }
      
             public boolean isBiped() {
             if(type == Type.BIPED) {
             return true;
             }
             return false;
             }
      
             public static enum Type {
             BIPED, MOB;
             }
             }
      
            @SuppressWarnings("deprecation")
            private RefClass getEntity(String entity, String p) {
                 RefClass ent = ReflectionUtil.getRefClass("{nms}." + entity);
          
                 RefConstructor entConstructor = ent.getConstructor(ReflectionUtil.getRefClass("{nms}.World"));
          
                 RefClass classCraftWorld = ReflectionUtil.getRefClass("{cb}.CraftWorld");
                  RefMethod methodGetHandle = classCraftWorld.getMethod("getHandle");
          
                  Object handle = methodGetHandle.of(Bukkit.getServer().getPlayer(p).getWorld()).call();
          
                  Object fin = entConstructor.create(handle);
           
                  this.thisObject = fin;
                  this.entityObject = fin.getClass();
           
                  return ReflectionUtil.getRefClass(entityObject);
             }
    
    }
    Thanks for watching this thread and good luck!

    Also, sorry for my bad english :D
     
    Last edited: Jan 3, 2015
  2. Offline

    ChipDev

    Cool!
     
  3. Offline

    xXBeLkAXx

    ChipDev likes this.
  4. Offline

    coco5843

    You're amazing !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    But there is a problem:
    [​IMG]
     
    Last edited: Dec 29, 2014
  5. Offline

    DemKazoo

    Omfg this is amazing! :D
     
    GrandmaJam likes this.
  6. Offline

    xXBeLkAXx

    @coco5843
    Oh, I can't see, what is it. It was a blaze? What happened?

    Also, please, send your code.

    @DemKazoo
    Thank you so much ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 22, 2015
  7. Offline

    coco5843


    new UtilDisguise(DisguiseType.BLAZE , p.getName()).disguiseToAll();
    I found why , when a player use 1.8 version , he cant' see the disguise
    sometimes disguise finally appear
    I can give you a method to get the player version , with reflection
     
    Last edited: Dec 29, 2014
  8. Offline

    JustBlender

    This can't be done. You can see the disguise only by another player.
     
  9. Offline

    coco5843

    not when a player use 1.8 minecraft
     
  10. Offline

    DemKazoo

    How would I use this to undisguise the player? :confused:
     
  11. Offline

    coco5843

    new UtilDisguise(DisguiseType.CREEPER , p.getName()).removeDisguise(); for example
     
  12. Offline

    xXBeLkAXx

    @coco5843
    I don't understand you good, but are u talking about to see disguises from the disguised player?
     
  13. Offline

    coco5843

    Noo when a player play with 1.8 version , he can't see an other disguise from an other player
     
  14. Offline

    xXBeLkAXx

    @coco5843
    Oh yes, I just tested it in 1.8 version... I'll think how to fix this.
     
    coco5843 likes this.
  15. Offline

    lewysryan

    Does this work for 1.8 yet? I really really want something like this for my minigames. @xXBeLkAXx
     
  16. Offline

    xXBeLkAXx

    @lewysryan
    It doesn't work nice for 1.8, how @coco5843 said. Now, it works nice in 1.7.2-1.7.10 versions
    --------------------------------
    CODE UPDATE! Fixed wither skeleton error, removed raw java reflection.
     
    Last edited: Jan 3, 2015
  17. Offline

    lewysryan

    @xXBeLkAXx any plans for it to get updated to work for 1.8? I can't find any other util for 1.8 disguises
     
  18. Offline

    xXBeLkAXx

    @lewysryan
    Yes, at now, only one plan - wait sponge, to get all work 1.8 versions
     
  19. Offline

    lewysryan

    But I'm using craftbukkit 1.8 r.01. :(
     
  20. Offline

    Funergy

  21. Offline

    RingOfStorms

    What the heck, sponge? Use spigot 1.8 which is now the recommended unofficial build of bukkit/craftbukkit.

    P.S. WHo the heck uses sponge, didn't even know that failure of a project was stilling under development?
     
    lewysryan and Funergy like this.
  22. Offline

    Funergy

  23. Offline

    RingOfStorms

    On the spigot website you download the BuiltTools.jar, along with installing other depedencies and run that jar. You will then have bukkit.jar, craftbukkit.jar, and spigot.jar for the most recent build.

    You can join the spigot irc or simply google spigot builttools to get a link to it.
     
    xTrollxDudex likes this.
  24. Offline

    xTrollxDudex

    Apparently their servers are in Romania where you have like no IP rights :p
     
  25. Offline

    Funnygatt

    How would we go about adding more entities, like pigs, cows etc? Would we just add them to the ENUM?
     
  26. Offline

    xXBeLkAXx

  27. Offline

    AdamQpzm

    @xXBeLkAXx No offence, but this is a bad resource. It's encouraging the bypassing of CraftBukkit's security measure. It was put in for a reason, and that's not something that should be ignored. Any resource that encourages risking the server owner's security for the developer's ease via reflection is bad.
     
  28. Offline

    NonameSL

    This seems nice, but I have 2 things to say:
    1. It's better to not use other libraries in utils because then it just makes things more complicated. Just use regular reflection or just actually call the methods in the actual classes.
    2. When you use the switch on the disguise in the constructor, you do this:
    Code:
     switch(disguise) {
            case ZOMBIE:
            entity = getEntity("EntityZombie", p);
            break;
            case WITHER_SKELETON:
            entity = getEntity("EntitySkeleton", p);
            RefMethod methodSkeleton = entity.findMethodByName("setSkeletonType");
            methodSkeleton.of(thisObject).call(1);
            break;
            case SKELETON:
                entity = getEntity("EntitySkeleton", p);
            break;
            case ZOMBIEPIG:
                entity = getEntity("EntityPigZombie", p);
            break;
            case BLAZE:
                entity = getEntity("EntityBlaze", p);
            break;
            case ENDERMAN:
                entity = getEntity("EntityEnderman", p);
            break;
            case CREEPER:
                entity = getEntity("EntityCreeper", p);
            break;
            case SPIDER:
                entity = getEntity("EntitySpider", p);
            break;
            case WITCH:
                entity = getEntity("EntityWitch", p);
            break;
            case WITHER_BOSS:
                entity = getEntity("EntityWither", p);
            break;
            case GHAST:
                entity = getEntity("EntityGhast", p);
            break;
            case GIANT:
                entity = getEntity("EntityGiant", p);
            break;
            }
    But why don't you just do this:
    Code:
        String str = disguise.toString();
        //Upper case for first letter then everything else in lower case, except ZombiePig which has an uppercase P
        if(disguise.equals(DisguiseType.WITHER_SKELETON)){
            //If it is a wither skeleton, do the stuff you did
            str="Skeleton";
            RefMethod methodSkeleton = entity.findMethodByName("setSkeletonType");
            methodSkeleton.of(thisObject).call(1);
        }else if(!disguise.equals(DisguiseType.PIGZOMBIE)str = str.substring(0, 1)+str.substring(1).toLowerCase().replaceAll("_(boss)?"/*Replace WITHER_BOSS with Wither*/, "");
        else str = "ZombiePig";
        entity = getEntity("Entity"+str+, p);
        //Checking for the wither skeleton
    
    
    Or with a switch:
    Code:
    switch(disguise){
        case WITHER_SKELETON:
            entity = getEntity("EntitySkeleton", p);
            RefMethod methodSkeleton = entity.findMethodByName("setSkeletonType");
            methodSkeleton.of(thisObject).call(1);
            break;
        case PIGZOMBIE:
            entity = getEntity("EntityPigZombie", p);
            break;
        default:
            entity = getEntity("Entity"+str.substring(0, 1)+str.substring(1).toLowerCase().replaceAll("_(boss)?"/*Replace WITHER_BOSS with Wither*/, ""), p);
    }
    }
    The switch is really un-neccesary, and takes up more lines of code. Just seems like extra work...
     
    Last edited: Jan 16, 2015
  29. Offline

    MagmaticBacca

    Much Amaze
     
  30. Offline

    ChintziSecilMC

    Sorry to open an old thread but how come there arent more Entity selections such as rabbits, iron golems, squids etc... is it possible to add these any know how i could go about this?

    @xXBeLkAXx
     
Thread Status:
Not open for further replies.

Share This Page