Library [CLASS] Mob Disguise! [1.8 support!]

Discussion in 'Resources' started by mine-care, Mar 15, 2015.

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

    mine-care

    @DarkBladee12 thanks a ton! :O i didnt notice that! Btw great utility :D
     
  2. Offline

    DarkBladee12

    @mine-care
    Thanks, it's always nice to see that my class if of great use for someone ;) Btw the link still doesn't work, because you copied the shortened version from the forum. Opening the link and copying it from the address line should work.
     
  3. Offline

    mine-care

    @DarkBladee12 Aww i am such an idiot
    No comment on this please ^
    XD
    It works now!
     
  4. Offline

    Malpika

    Hi! I have this problem :( Could you help me?

    Thanks for your work!

    1.jpg
     
  5. Offline

    mug561

    @Malpika change it from bukkit.getOnlinePlayers, to one player. That's pretty much what its saying to do. if you want to do it to all online players, just iterate through the collection with a for loop.
     
  6. Offline

    DarkBladee12

    @Malpika @mug561
    You could also use Bukkit.getOnlinePlayers().toArray(new Player[0]) since there's already a method which sends the disguise to multiple players. However this method requires an array instead of a collection.
     
  7. Offline

    meguy26

    @mine-care
    So I'm trying to understand how this works, and I get most of it, but what I don't understand is this:
    Code:
    Object world = ReflectionUtils.invokeMethod(disguised.getWorld(),
                    "getHandle", null);
    According to the decompiler, the method "getHandle" in CraftWorld returns a WorldServer, but the EntityLiving constructor only accepts a World, not a WorldServer, how is it that this still works? Am I reading it wrong? Am I using the wrong version?!

    EDIT:
    Lol, I understand now, WorldServer extends world! I feel a little dumb now.
     
    Last edited: Aug 28, 2015
  8. Offline

    Malpika

    ¬°Gracias!
     
    mine-care likes this.
  9. Offline

    mine-care

    @meguy26 :- )
    Dont feel dumb, reflection isnt the easyest thing to follow (for me at least)
    Im happy to help if you need any more guidance :D
     
  10. Offline

    meguy26

    @mine-care
    Thanks, but I do understand it now, I just find it odd that "WorldServer" extends "World"
     
    mine-care likes this.
  11. Offline

    caderape

    @mine-care Good work dude !
    i tried it in 1.8, but the mob skin wont appear. I stay invisible and just have the effect.
    Any ideas ?
     
  12. Offline

    mine-care

    @caderape that's really strange, I had used it on 1.8 for a little while without any such problem...
    Hmm
     
  13. Offline

    caderape

    @mine-care It might come from me. this is the second way i try and both i don't see the skin.
     
  14. Offline

    mine-care

    @caderape What is your code? can i see it?
     
  15. Offline

    caderape

    @mine-care This is the same as you.
    I dun have any erros. It work fine, almost fine. I can see the effect like for blaze ,or the sound with enderdragon. But i'm just invisible.

    Code:
    import java.util.Collection;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import fr.caderape.test.ReflectionUtils.PackageType;
    
    public class Disguise {
        private static final String bukkitversion = Bukkit.getServer().getClass()
                .getPackage().getName().substring(23);
        private String customName;
        private EntityDisguise type;
        private Player disguised;
        private ItemStack hand, helm, chst, leg, boot;
    
        public Disguise(Player p, EntityDisguise type) {
            this(p, type, null);
        }
    
        public Disguise(Player p, EntityDisguise type, String name) {
            this(p, type, name, null, null, null, null, null);
        }
    
        public Disguise(Player p, EntityDisguise type, String name,
                ItemStack inhand, ItemStack helmet, ItemStack chestplate,
                ItemStack leggings, ItemStack boots) {
            this.customName = name;
            this.type = type;
            this.disguised = p;
            this.hand = inhand;
            this.helm = helmet;
            this.chst = chestplate;
            this.leg = leggings;
            this.boot = boots;
        }
        public void sendDisguise(Player to) throws Exception {
            if (to.equals(disguised))
                throw new IllegalArgumentException(
                        "Target Player cannot be the same as the disguised player");
            Object packetplayoutentitydestroy = ReflectionUtils.instantiateObject(
                    "PacketPlayOutEntityDestroy", PackageType.MINECRAFT_SERVER,
                    new int[] { disguised.getEntityId() });
            Object world = ReflectionUtils.invokeMethod(disguised.getWorld(), "getHandle");
            Class<?> entity = Class.forName(type.getClassName());
            Object ent = ReflectionUtils.instantiateObject(entity, world);
            ReflectionUtils.invokeMethod(ent, "setPosition", disguised
                    .getLocation().getX(), disguised.getLocation().getY(),
                    disguised.getLocation().getZ());
            ReflectionUtils.getMethod(entity, "d", int.class).invoke(ent,
                    disguised.getEntityId());
            if (customName != null) {
                ReflectionUtils.getMethod(entity, "setCustomName", String.class)
                        .invoke(ent, customName);
                ReflectionUtils.getMethod(entity, "setCustomNameVisible",
                        boolean.class).invoke(ent, true);
            }
            handleSpecialTypes(type, ent);
            Object packetplayoutspawnentityliving = ReflectionUtils
                    .instantiateObject("PacketPlayOutSpawnEntityLiving",
                            PackageType.MINECRAFT_SERVER, ent);
            sendPacket(to, packetplayoutentitydestroy);
            sendPacket(to, packetplayoutspawnentityliving);
            if (hand != null)
                sendArmorContentPackets(to, disguised.getEntityId(), 0, hand);
            if (helm != null)
                sendArmorContentPackets(to, disguised.getEntityId(), 1, helm);
            if (chst != null)
                sendArmorContentPackets(to, disguised.getEntityId(), 2, chst);
            if (leg != null)
                sendArmorContentPackets(to, disguised.getEntityId(), 3, leg);
            if (boot != null)
                sendArmorContentPackets(to, disguised.getEntityId(), 4, boot);
        }
        private void sendDisguise(Player... players) {
            for (Player P : players) {
                if (P.equals(disguised))
                    continue;
                try {
                    sendDisguise(P);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
       
       
        public void sendDisguise(Collection<? extends Player> players) {
            for (Player P : players) {
                if (P.equals(disguised))
                    continue;
                try {
                    sendDisguise(P);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void updateDisguise(Player forwho) throws Exception {
            sendDisguise(forwho);
        }
    
        public void updateDisguise(Player... players) {
            sendDisguise(players);
        }
    
        public void changePlayerDisguise(EntityDisguise type, Player sendto)
                throws Exception {
            this.type = type;
            sendDisguise(sendto);
        }
    
        public void changePlayerDisguise(EntityDisguise type, Player... sendto)
                throws Exception {
            this.type = type;
            sendDisguise(sendto);
        }
    
        private void sendPacket(Player p, Object pack) throws Exception {
            Class<?> packet = Class.forName("net.minecraft.server." + bukkitversion
                    + ".Packet");
            Class<?> craftPlayer = Class.forName("org.bukkit.craftbukkit."
                    + bukkitversion + ".entity.CraftPlayer");
            Object handle = craftPlayer.getMethod("getHandle").invoke(p);
            Object con = handle.getClass().getField("playerConnection").get(handle);
            con.getClass().getMethod("sendPacket", packet).invoke(con, pack);
        }
    
        private void sendArmorContentPackets(Player to, int entityID, int slot,
                ItemStack item) throws Exception {
            PackageType type;
            if (bukkitversion.startsWith("v1_7_"))
                type = PackageType.CRAFTBUKKIT;
            else
                type = PackageType.CRAFTBUKKIT_INVENTORY;
            Object craftitmstk = ReflectionUtils.getMethod("CraftItemStack", type,
                    "asNMSCopy", item.getClass()).invoke(null, item);
            Object metadarapacket = ReflectionUtils.instantiateObject(
                    "PacketPlayOutEntityEquipment", PackageType.MINECRAFT_SERVER,
                    entityID, slot, craftitmstk);
            sendPacket(to, metadarapacket);
        }
    
        private Object handleSpecialTypes(EntityDisguise type, Object entity)
                throws Exception {
            switch (type) {
            case WITHER_SKELETON:
                ReflectionUtils.invokeMethod(entity, "setSkeletonType", 1);
                break;
            default:
                break;
            }
            return entity;
        }
     
        public ItemStack getItemInHand() {
            return hand;
        }
        public void setItemInHand(ItemStack hand) {
            this.hand = hand;
        }
        public ItemStack getHelmet() {
            return helm;
        }
        public void setHelmet(ItemStack helm) {
            this.helm = helm;
        }
        public ItemStack getChestplate() {
            return chst;
        }
        public void setChestplate(ItemStack chst) {
            this.chst = chst;
        }
        public ItemStack getLeggings() {
            return leg;
        }
        public void setLeggings(ItemStack leg) {
            this.leg = leg;
        }
        public ItemStack getBoots() {
            return boot;
        }
        public void setBoots(ItemStack boot) {
            this.boot = boot;
        }
        public String getCustomName() {
            return customName;
        }
        public void setCustomName(String customName) {
            this.customName = customName;
        }
        public EntityDisguise getType() {
            return type;
        }
        public void setType(EntityDisguise type) {
            this.type = type;
        }
        public Player getDisguised() {
            return disguised;
        }
    }
    
    
    enum EntityDisguise {
        ZOMBIE("EntityZombie"), WITHER_SKELETON("EntitySkeleton"), SKELETON(
                "EntitySkeleton"), ZOMBIEPIG("EntityPigZombie"), BLAZE(
                "EntityBlaze"), ENDERMAN("EntityEnderman"), CREEPER("EntityCreeper"), SPIDER(
                "EntitySpider"), WITCH("EntityWitch"), WITHER_BOSS("EntityWither"), GHAST(
                "EntityGhast"), GIANT("EntityGiantZombie"), SLIME("EntitySlime"), CAVE_SPIDER(
                "EntityCaveSpider"), SILVERFISH("EntitySilverfish"), MAGMA_CUBE(
                "EntityMagmaCube"), BAT("EntityBat"), PIG("EntityPig"), SHEEP(
                "EntitySheep"), COW("EntityCow"), CHICKEN("EntityChicken"), SQUID(
                "EntitySquid"), WOLF("EntityWolf"), OCELOT("EntityOcelot"), HORSE(
                "EntityHorse"), VILLAGER("EntityVillager"), IRON_GOLEM(
                "EntityIronGolem"), SNOWMAN("EntitySnowman"), ENDER_DRAGON(
                "EntityEnderDragon"), MOOSHROOM("EntityMushroomCow");
     
       
        private final String cls;
        EntityDisguise(String cls) {
            this.cls = cls;
        }
    
        public String getClassName() {
            return "net.minecraft.server."
                    + Bukkit.getServer().getClass().getPackage().getName()
                            .substring(23) + "." + cls;
        }
    }
     
  16. Offline

    mine-care

    @caderape i mean the line where you send the disguise to the player, not my class :p
     
  17. Offline

    caderape

    @mine-care ha :D

    Code:
    Disguise disguise = new Disguise(player, EntityDisguise.BLAZE);
            disguise.sendDisguise(Bukkit.getOnlinePlayers());
     
  18. Offline

    mine-care

  19. Offline

    caderape

    @mine-care Yep it is. that must come from my spigot version. or i dun know what
     
  20. Offline

    meguy26

    @caderape
    What version of spigot are you using? It seemed to work fine on 1.8.3 for me
     
    mine-care likes this.
  21. Offline

    SerCoProGamer

Thread Status:
Not open for further replies.

Share This Page