Spawn ArmorStand with packets

Discussion in 'Spigot Plugin Development' started by MaTaMoR_, Sep 21, 2016.

  1. Hi, i'm trying to spawn an ArmorStand with packets so i can create personal holograms, but when i send the packet nothing happens.

    Code:
    Code (Java):

    public class EntityNameable extends EntityArmorStand implements Nameable {

        private final HologramController controller;

        private boolean lockTick;
        private HologramLine parentPiece;

        public EntityNameable(HologramController controller, World world, HologramLine parentPiece) {
            super(world);
            super.a(new NullBoundingBox());

            this.controller = controller;

            setInvisible(true);
            setSmall(true);
            setArms(false);
            setGravity(true);
            setBasePlate(true);
            n(true);
            this.parentPiece = parentPiece;
            setLockTick(true);
        }

        @Override
        public void b(NBTTagCompound nbttagcompound) {
        }

        @Override
        public boolean c(NBTTagCompound nbttagcompound) {
            return false;
        }

        @Override
        public boolean d(NBTTagCompound nbttagcompound) {
            return false;
        }

        @Override
        public void e(NBTTagCompound nbttagcompound) {
        }


        @Override
        public boolean isInvulnerable(DamageSource source) {
            return true;
        }

        @Override
        public void setName(String text) {
            super.setCustomName(text);
            super.setCustomNameVisible(!text.isEmpty());
        }

        @Override
        public String getName() {
            return super.getCustomName();
        }

        @Override
        public void setCustomNameVisible(boolean visible) {
        }

        @Override
        public boolean a(EntityHuman human, Vec3D vec3d) {
            return true;
        }

        @Override
        public boolean d(int i, ItemStack item) {
            return false;
        }

        @Override
        public void setEquipment(int i, ItemStack item) {

        }

        @Override
        public void a(AxisAlignedBB boundingBox) {

        }

        @Override
        public void t_() {
            if (!lockTick) {
                super.s_();
            }
        }

        @Override
        public void makeSound(String sound, float f1, float f2) {

        }

        public void setLockTick(boolean lock) {
            lockTick = lock;
        }

        @Override
        public void die() {
            setLockTick(false);
            super.die();
        }

        @Override
        public CraftEntity getBukkitEntity() {
            if (super.bukkitEntity == null) this.bukkitEntity = new CraftNameable(this.world.getServer(), this);
            return this.bukkitEntity;
        }

        @Override
        public void teleport(Location location) {
            getBukkitEntity().teleport(location);
        }

        @Override
        public void setPosition(double x, double y, double z) {
            super.setPosition(x, y, z);

            PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this);
            for(EntityHuman entityHuman : this.world.players) {
                if(entityHuman instanceof EntityPlayer) {
                    EntityPlayer player = (EntityPlayer) entityHuman;

                    double distanceSquared = Math.pow(player.locX - this.locX, 2) + Math.pow(player.locZ - this.locZ, 2);
                    if (distanceSquared < 8192 && player.playerConnection != null) {
                        player.playerConnection.sendPacket(teleportPacket);
                    }
                }
            }
        }

        @Override
        public HologramLine getLine() {
            return parentPiece;
        }

    //HERE I SEND THE PACKET!!!!!!!!!!!!!!

        @Override
        public void send(Player player) {
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;

            PacketPlayOutSpawnEntityLiving spawnPacket = new PacketPlayOutSpawnEntityLiving(this);

            System.out.print("Send packet");
            connection.sendPacket(spawnPacket);
        }

        @Override
        public void remove(Player player) {
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;

            PacketPlayOutEntityDestroy removePacket = new PacketPlayOutEntityDestroy(getId());

            connection.sendPacket(removePacket);
        }

        @Override
        public boolean isRegistered() {
            return getWorld().a(getId()) == this;
        }

        @Override
        public boolean register() {
            Validate.isTrue(!isRegistered(), "The entity is already registered");
            boolean registered = controller.addEntityToWorld(getWorld(), this);

            System.out.print("Register: " + registered);
            return registered;
        }

        @Override
        public void unregister() {
            Validate.isTrue(isRegistered(), "The entity is not registered");
            super.die();
        }
    }
     
     
  2. <boop didnt quite read over everything>
     
  3. Im not doing that, there's not need to do what you said.
     
  4. Its okay, realised a few seconds after pressing send xD, ill fire up my pc and check it out :3
     
  5. If you have ~2 hours I can give you a working method. I am at school atm. I have methods to add an ArmoStand, edit an ArmorStand and to delete an ArmorStand.
     
  6. If you're at school what are you doing here ? Well, i do the same :p
     
    • Like Like x 1
  7. I had a test and I am done with it. Now I am allowed to do something for myself :p
     
  8. For spawning it:

    Code (Text):
    EntityArmorStand eas = new EntityArmorStand(someWorld);
            PacketPlayOutSpawnEntityLiving spawnPacket = new PacketPlayOutSpawnEntityLiving(eas);
    And then send it to the player(s) via EntityPlayer#playerConnection.
    For teleporting it it was
    Code (Text):
            PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(eas);
    I believe. You need to chance the EntityArmorStand location before sending the EntityTeleport Packet
     
  9. The location isn't the problem, i set it when i create the entity. If i directly register the entity into the world it works just fine.
     
  10. Please show the code you use for spawning/creating the entity and sending the packet. Have you also tried spawning a regular EntityArmorStand..?
     
  11. You can see where i send the packet in the code, also i just create a new instance of it and set it's position.
     
  12. Here is how to add an ArmorStand:

    Code (Text):
            WorldServer s = ((CraftWorld)p.getWorld()).getHandle();
            EntityArmorStand stand = new EntityArmorStand(s);

            stand.setLocation(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
            stand.setCustomName(name);
            stand.setCustomNameVisible(true);
            stand.setGravity(true);
            stand.setSmall(true);
            stand.setInvisible(true);


            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);

    If you wish on how I did it with my plugin secretboxes, here is the full methods I use:

    Code (Text):
    public static HashMap<Integer, String> entityIds = new HashMap<>();
       
        public static void addArmorStand(Player p, Location loc, HashMap<Integer, String> map, String name){



            WorldServer s = ((CraftWorld)p.getWorld()).getHandle();
            EntityArmorStand stand = new EntityArmorStand(s);

            stand.setLocation(loc.getX(), loc.getY() + 0.5, loc.getZ(), 0, 0);
            stand.setCustomName(name);
            stand.setCustomNameVisible(true);
            stand.setGravity(true);
            stand.setSmall(true);
            stand.setInvisible(true);


            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);

            map.put(stand.getId(), p.getUniqueId().toString());
        }
       
        public static void editArmorStand(Player player, HashMap<Integer, ?> map, String name){

            for(Integer i : map.keySet()){
                if(map.get(i).equals(player.getUniqueId().toString())){
                    DataWatcher daw = new DataWatcher(null);
                    daw.a(2, name);
                    PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(i, daw, true);
                    ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packet);
                }
            }

        }


        public static void removeArmorStand(Player player, HashMap<Integer, ?> map){


            Iterator<Integer> it = map.keySet().iterator();
            while(it.hasNext()){
                Integer item = it.next();
                if(map.get(item).equals(player.getUniqueId().toString())){
                    PacketPlayOutEntityDestroy pa = new PacketPlayOutEntityDestroy(item);
                    ((CraftPlayer)player).getHandle().playerConnection.sendPacket(pa);

                    it.remove();

                }
            }
        }
     
  13. Yep, im using the same packet, still not work, i don't know why.
     
    #13 MaTaMoR_, Sep 21, 2016
    Last edited: Sep 21, 2016
  14. While working on holograms for a server I was unable to spawn the custom armor stand entity via packets.

    I never looked into it, I just used the default EntityArmorStand.

    Just a note so you can potentially find a proper workaround.

    Edit: Decided to look into it. In order for you to be able to spawn a entity via PacketPlayerOutSpawnEntityLiving you must have.
    An entity id, a unique id, The entities class to be registered under EntityTypes.f (variable, used for the method EntityTypes.a), and position.

    Due to you making a custom ArmorStandClass and not registering it as a EntityType the entity type id is returning 0 when the client receives this packet it will ignore that completely.
     
    #14 Borlea, Sep 22, 2016
    Last edited: Sep 22, 2016
  15. How i can register the entity ?