Set items in the player crafting

Discussion in 'Spigot Plugin Development' started by MineStein, Mar 6, 2018.

  1. Hi! After some light research it appeared that setting items in the player's 2x2 player crafting was not possible; however, after looking at the server I have shown at the bottom, it is clearly possible through a plugin.

    Does anyone have any ideas on how this works?

    Server: then do /warp mk
    • Useful Useful x 1
  2. That seems to only get the raw slot clicked, not necessarily the slot you can place items in (Inventory::setItem).

    You can send a packet, specifically PacketPlayOutSetSlot,

    The packet works differentely from the traditional Inventory::setItem.

    Quoted from Spigot's source when using PacketPlayOutSetSlot:
    As it says, you'll want to work with slots 1-4 in the packet to manipulate the 2x2 grid craft slots.
  3. I believe I got it! The code for anyone looking at this thread in the future:

    Code (Text):
        public void onInventoryOpen(final InventoryOpenEvent event) {
            Player player = (Player) event.getPlayer();

            ItemStack itemStack = new ItemStack(Material.BLAZE_POWDER); {
                ItemMeta meta = itemStack.getItemMeta();



            for (int i = 1; i <= 4; i++) {
                PacketPlayOutSetSlot packet = new PacketPlayOutSetSlot(0, i, CraftItemStack.asNMSCopy(itemStack));

                ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);

    There's only one issue with this. At the moment the items only appear once you click on the slot and re-open the inventory. Any ideas?
    #4 MineStein, Mar 7, 2018
    Last edited: Mar 7, 2018
  4. Have you tried updating the inventory? There’s a method for it :p Include it outside the for loop that you send the packet in and you should be good to go.
  5. Calling player#updateInventory is both deprecated and does not fix the issue. Rather, it appears to make it so that the items do not appear at all. I've tried calling this method both in the loop and outside of it.
  6. I was just reading the protocol wiki. I would try PacketPlayOutWindowItems. If this does not work, then I do not believe there is any actual way to update the inventory view.

    However, if anyone can correct me feel free to do so. It’s been a while since I’ve touched plugin making in any shape or form so I don’t remember much.
    • Useful Useful x 1
  7. Hmm, I couldn't seem to get this working myself. The parameters are a bit confusing though. Do you have an example of how you would do it because mine clearly isn't working for me?

    This is definitely possible one way or another on a Spigot server.
  8. set the items with Player#getOpenInventory#getTopInventory
    • Agree Agree x 1
  9. This won't support setting the player crafting though, which is what I need for this plugin. As we've figured out player crafting items need to be faked out as player crafting appears to handled client side only.
  10. Important note to everyone who wants to use Packets to set items: The items set with that method will only exists clientside and cannot actually be used by them (if thats what you wanted) and disappear with the next inventory update. If they can be then... thats where hackers get their items from ;)
    On topic: PlayerInventory.getTopInventory().setContents(); does actually work, I just tested it.
    Watch out not to do this when the player has any inventory open that is not the normal crafting grid and not to override any items already present. Also those items in the slots are now actually *there* so unless you do something about it, the players can do whatever they want with them.
    I trust you know how to handle that :)
    What probably went wrong: All itemstack operations are copy by value, and editing an array that was returned by getContents wont do anything. It's tricky pitfall but it's also clear why it has to be that way.
    PS: there is a slot for the result (index 0) to but it does not seem to do anything when set.
    Indexing goes like reading left to right, top to bottom.
    Also if you do need to use craftbukkit classes and packets I have something for you to cope with version dependency:

Share This Page