Solved Client side Items

Discussion in 'Plugin Development' started by LRK, Nov 16, 2017.

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

    LRK

    Hello guys,

    I'm currently working on a cookie clicker plugin - sound pretty simple :)
    but I'm stuck with this Issue:

    When a player clicks on the cookie block it spawns an item on the players client using
    Packets:

    Code:
        private void spawnClientItem(Player p, ItemStack item, Location spawnLoc, Vector velocity) {
            WorldServer s = ((CraftWorld)p.getWorld()).getHandle();
            EntityItem entityItem = new EntityItem(s);
            entityItem.setItemStack(CraftItemStack.asNMSCopy(item));
            entityItem.setLocation(spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ(), 0, 0);
    
            PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(entityItem, 2, 100);
           
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
    My problem is, that the spawned item is always a stone block.
    I tried a few things modifying values of the packet with Reflections, but I wasn't able to get anything to work.
    Also I want the item to have a specific velocity.

    Does somebody know what I'm doing wrong or can help me?
    Any Help would be nice!

    I did more research and found a perfect thread.
    With this thread I was able to fix the problem on my own.

    Here's my Solution:
    Code:
            EntityItem item = new EntityItem(((CraftWorld) loc.getWorld()).getHandle());
            item.setLocation(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
            item.setItemStack(CraftItemStack.asNMSCopy(stack));
    
            PacketPlayOutSpawnEntity spawnItem = new PacketPlayOutSpawnEntity(item, 2, 100);
            PacketPlayOutEntityMetadata data = new PacketPlayOutEntityMetadata(item.getId(), item.getDataWatcher(), true);
             // Create velocity paket - velocity is a Vector
            PacketPlayOutEntityVelocity velo = new PacketPlayOutEntityVelocity(item.getId(), velocity.getX(), velocity.getY(), velocity.getZ());
    
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(spawnItem);
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(velo);
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(data);
    
            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                @Override
                public void run() {
                    // Remove Item after 40 ticks (2 seconds)
                    PacketPlayOutEntityDestroy destroyItem = new PacketPlayOutEntityDestroy(item.getId());
    
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(destroyItem);
                }
            }, 40L);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 16, 2017
Thread Status:
Not open for further replies.

Share This Page