PacketPlayOutEntitySpawn does not render

Discussion in 'Plugin Development' started by harry_potter4567, Oct 18, 2015.

Thread Status:
Not open for further replies.
  1. So, im trying to write a disguise plugin. Most of this works great, but once a player trys to disguise, it needs to move and wait a few seconds for the entity it disguised to to render. Entities like withers or blazes do spawn the particles, they normally spawn at the correct position but the entity itself is not rendered.
    I allready tried not to use reflection and had the same problem.
    My code:

    Code:
    public class PlayerDisguise {
    
        private UUID playerUUID;
        private DisguiseType type;
        private String name;
        private ArrayList<UUID> show = null;
        private ItemStack hand, helmet, chest, leggings, boots;
    
        private static ArrayList<PlayerDisguise> all = new ArrayList<PlayerDisguise>();
    
        public PlayerDisguise(Player p, DisguiseType type, Player... show) {
            this(p, type, null, null, null, null, null, null, show);
        }
    
        public PlayerDisguise(Player p, DisguiseType type, boolean showName, Player... show) {
            this(p, type, showName ? p.getDisplayName() : null, null, null, null, null, null, show);
        }
    
        public PlayerDisguise(Player p, DisguiseType type, String name, Player... show) {
            this(p, type, name, null, null, null, null, null, show);
        }
    
        public PlayerDisguise(Player p, DisguiseType type, String name, boolean showEquipment, Player... show) {
            this(p, type, name, showEquipment ? p.getInventory().getItemInHand() : null, showEquipment ? p.getInventory().getHelmet() : null, showEquipment ? p.getInventory().getChestplate() : null, showEquipment ? p.getInventory().getLeggings() : null, showEquipment ? p.getInventory().getBoots() : null, show);
        }
    
        public PlayerDisguise(Player p, DisguiseType type, String name, ItemStack hand, ItemStack helmet, ItemStack chest, ItemStack leggings, ItemStack boots, Player... show) {
            this.playerUUID = p.getUniqueId();
            this.type = type;
            this.name = name;
            this.hand = hand;
            this.helmet = helmet;
            this.chest = chest;
            this.leggings = leggings;
            this.boots = boots;
       
       
            if(show != null) {
                this.show = new ArrayList<UUID>();
           
                for(Player all : show) {
                    this.show.add(all.getUniqueId());
                }
            }
       
            for(PlayerDisguise all : new ArrayList<PlayerDisguise>(all)) {
                if(all.getPlayerUUID().equals(playerUUID)) {
                    PlayerDisguise.all.remove(all);
                }
            }
       
            all.add(this);
            send(this.show);
        }
    
        private void send(ArrayList<UUID> uuids) {
            Player disguised = getPlayer();
            /*PacketPlayOutEntityDestroy destroy = new PacketPlayOutEntityDestroy(new int[] {disguised.getEntityId()});
       
            EntityBlaze entity = new EntityBlaze(((CraftWorld) disguised.getWorld()).getHandle());
            entity.setPosition(disguised.getLocation().getX(), disguised.getLocation().getY(), disguised.getLocation().getZ());
            entity.d(disguised.getEntityId());
            PacketPlayOutSpawnEntityLiving spawn = new PacketPlayOutSpawnEntityLiving(entity);*/
            try {
           
                Object destroy = ReflectionUtils.getNMSClass("PacketPlayOutEntityDestroy").getConstructor(int[].class).newInstance(new int[] {disguised.getEntityId()} );
           
                Object entity = ReflectionUtils.getNMSClass(type.getClassName()).getConstructor(ReflectionUtils.getNMSClass("World")).newInstance(ReflectionUtils.injectMethod(disguised.getWorld(), "getHandle"));
                entity.getClass().getMethod("setPosition", double.class, double.class, double.class).invoke(entity, disguised.getLocation().getX(), disguised.getLocation().getY(), disguised.getLocation().getZ());
                entity.getClass().getMethod("d", int.class).invoke(entity, disguised.getEntityId());
                type.handleSpecific(entity);
           
           
                if(name != null) {
                    entity.getClass().getMethod("setCustomName", String.class).invoke(entity, name);
                    entity.getClass().getMethod("setCustomNameVisible", boolean.class).invoke(entity, true);
                }
           
                Object spawn = ReflectionUtils.getNMSClass("PacketPlayOutSpawnEntityLiving").getConstructor(ReflectionUtils.getNMSClass("EntityLiving")).newInstance(entity);
           
                for(Player all : Bukkit.getOnlinePlayers()) {
                    if((uuids != null && !uuids.isEmpty() && !uuids.contains(all.getUniqueId())) || all.equals(disguised)) continue;
               
                    ReflectionUtils.sendPacket(all, destroy);
                    ReflectionUtils.sendPacket(all, spawn);
                }
           
                //TODO: PREVENT ERROR WHEN NOTHING IN HAND
                if(hand != null || hand.getType() != null || hand.getType() != Material.AIR) sendArmor(0, hand, uuids);
                if(helmet != null) sendArmor(4, helmet, uuids);
                if(chest != null) sendArmor(3, chest, uuids);
                if(leggings != null) sendArmor(2, leggings, uuids);
                if(boots != null) sendArmor(1, boots, uuids);
           
            } catch (InstantiationException | IllegalAccessException
                    | IllegalArgumentException | InvocationTargetException
                    | NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
        }
    
        private void sendArmor(int slotID, ItemStack is, ArrayList<UUID> uuids) {
            Player disguised = getPlayer();
       
            try {
                Object nmsIS = ReflectionUtils.getCraftbukkitClass("inventory.CraftItemStack").getMethod("asNMSCopy", ItemStack.class).invoke(null, is);
                Object packet = ReflectionUtils.getNMSClass("PacketPlayOutEntityEquipment").getConstructor(int.class, int.class, nmsIS.getClass()).newInstance(disguised.getEntityId(), slotID, nmsIS);
                for(Player all : Bukkit.getOnlinePlayers()) {
                    if((uuids != null && !uuids.isEmpty() && !uuids.contains(all.getUniqueId())) || all.equals(disguised)) continue;
                    ReflectionUtils.sendPacket(all, packet);
                }
            } catch (InstantiationException | IllegalAccessException
                    | IllegalArgumentException | InvocationTargetException
                    | NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
        }
    
        public Player getPlayer() {
            return Bukkit.getPlayer(playerUUID);
        }
    
        public static PlayerDisguise getDisguise(Player p) {
       
            for(PlayerDisguise all : all) {
                if(all.getPlayerUUID().equals(p.getUniqueId())) {
                    return all;
                }
            }
       
            return null;
        }
    
    
        public UUID getPlayerUUID() {
            return playerUUID;
        }
    
        public DisguiseType getType() {
            return type;
        }
    
        public void setType(DisguiseType type) {
            this.type = type;
            send(show);
        }
    
        public String getDisplayName() {
            return name;
        }
    
        public void setDisplayName(String name) {
            this.name = name;
            send(show);
        }
    
    
    
        public ItemStack getItemInHand() {
            return hand;
        }
    
        public void setItemInHand(ItemStack hand) {
            this.hand = hand;
            sendArmor(0, hand, show);
        }
    
        public ItemStack getHelmet() {
            return helmet;
        }
    
        public void setHelmet(ItemStack helmet) {
            sendArmor(4, helmet, show);
            this.helmet = helmet;
        }
    
        public ItemStack getChestplate() {
            return chest;
        }
    
        public void setChestplate(ItemStack chest) {
            sendArmor(3, chest, show);
            this.chest = chest;
        }
    
        public ItemStack getLeggings() {
            return leggings;
        }
    
        public void setLeggings(ItemStack leggings) {
            sendArmor(2, leggings, show);
            this.leggings = leggings;
        }
    
        public ItemStack getBoots() {
            return boots;
        }
    
        public void setBoots(ItemStack boots) {
            sendArmor(1, boots, show);
            this.boots = boots;
        }
    
        public static ArrayList<PlayerDisguise> getAll() {
            return all;
        }
    
    
    
    
    
    
        public enum DisguiseType {
            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 String className;
       
            DisguiseType(String className) {
                this.className = className;
            }
       
            public String getClassName() {
                return className;
            }
       
            public Object handleSpecific(Object entity) {
           
                try {
               
               
                    switch(this) {
                    case BAT:
                        entity.getClass().getMethod("setAsleep", boolean.class).invoke(entity, false);
                        break;
                    case WITHER_SKELETON:
                        entity.getClass().getMethod("setSkeletonType", int.class).invoke(entity, 1);
                        break;
                    default:
                        break;
               
                    }
               
               
               
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    e.printStackTrace();
                }
           
                return null;
            }
       
        }
    
    }
    


    Thanks in advance for any help
     
    Last edited: Oct 20, 2015
  2. Anyone, please ? - bump
     
Thread Status:
Not open for further replies.

Share This Page