Spawn Fake Entities With Packets

Discussion in 'Plugin Development' started by Freack100, Dec 24, 2014.

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

    Freack100

    Hello Bukkit forums,
    today I wanted to ask if someone could tell me how to "spawn" entities with packets. As "simple" as that.
    BTW, I used the search function and didn't found anything, if there is a thread about this, and i didn't find it, I'm sorry.

    Here is my current code:
    Code (open)

    Code:
        public void sendToPlayer(Player p){
            PacketPlayOutSpawnEntityLiving create = new PacketPlayOutSpawnEntityLiving();
    
            setField(create,create.getClass(),"a",this.entityId);
            setField(create,create.getClass(),"b",(byte) type.getTypeId());
            setField(create,create.getClass(),"c",toFixedPoint(_location.getX()));
            setField(create,create.getClass(),"d",toFixedPoint(_location.getY()));
            setField(create,create.getClass(),"e",toFixedPoint(_location.getZ()));
            setField(create,create.getClass(),"f",(byte) 0);
            setField(create,create.getClass(),"g",(byte) 0);
            setField(create,create.getClass(),"h",(byte) 0);
            setField(create,create.getClass(),"i",(short) 0);
            setField(create,create.getClass(),"j",(short) 0);
            setField(create,create.getClass(),"k",(short) 0);
    
    
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(create);
        }


    So, now I get a DecoderException from Netty every time I try to spawn an entity.
    I hope some Bukkit coding god could help me :)

    Merry Christmas,
    Freack100
     
  2. Offline

    Skionz

    @Freack100 Can't you just use your packets arguments?
     
  3. Offline

    Freack100

    @Skionz
    Oh, umm, never saw that the Packet takes arguments :confused:
    Thanks!
     
  4. Offline

    Skionz

  5. Offline

    Freack100

    @Skionz
    Now there is a new problem... I know what kind of Class it takes (EntityLiving) but I don't know how to:
    1. Tell the FakeEntity class which entity class it should use
    2. Create a new instance of the entity to send it with the packet

    Here is my current code:
    Show Spoiler

    Code:
        public void sendToPlayer(Player p) {
            PacketPlayOutSpawnEntityLiving create = null;
            try {
                EntityLiving entity = (EntityLiving) entityClass.getConstructors()[0].newInstance((net.minecraft.server.v1_7_R4.World)p.getLocation().getWorld());
                entity.setLocation(_location.getX(),_location.getY(),_location.getZ(),_location.getYaw(),_location.getPitch());
                entity.setHealth(20);
                entity.spawnIn((World) p.getWorld());
                create = new PacketPlayOutSpawnEntityLiving(entity);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            if (create != null) {
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(create);
            }
        }


    #EDIT:
    entityClass is :
    Code:
    private Class<? extends Entity> entityClass; 
    and it is initialized in the constructor.
     
  6. Offline

    Freack100

  7. Offline

    Freack100

  8. Offline

    Aquaatic

    I don't know if you still need it, but...
    Code:
    //for spawning an armorstand, but other entities will work too
    public void spawn(Location loc, Player p) {
            WorldServer s = ((CraftWorld)loc.getWorld()).getHandle();
            EntityArmorStand stand = new EntityArmorStand(s);
           
            stand.setLocation(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
            stand.setCustomName("I'm a Armorstand!");
            stand.setCustomNameVisible(true);
            stand.setGravity(true);
            //and so on
           
            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
        }
    works fine for me :)
     
    Orange Tabby likes this.
Thread Status:
Not open for further replies.

Share This Page