Jump to content

[1.15.2] ItemCraftedEvent problem


SirCaptainCaleb

Recommended Posts

When an Itemstack is shift clicked from the crafting table result slot, a copy of that itemstack is created and placed in the player's inventory, as opposed to the Itemstack stored in the ItemCraftedEvent. This causes changes to the itemstack to be ignored when the player shift clicks, but the changes are applied if the player simply left click. In the following example, any PickAxeItem crafted by the player will be maximally damaged when the player left clicks the item to pick it up. But if the player shift clicks the item, it is moved to the inventory without damaging it. I have been scratching my head for a while on how to get around this. Any ideas?

 

    @SubscribeEvent
    public void onPickaxeCraft(ItemCraftedEvent craftedEvent){
        ItemStack craftedItemStack = craftedEvent.getCrafting().getStack();
        if(craftedItemStack.isEmpty() || !(craftedItemStack.getItem() instanceof PickaxeItem)) return;
        craftedItemStack.setDamage(craftedItemStack.getMaxDamage());
    }

 

Edited by SirCaptainCaleb
Link to comment
Share on other sites

  • 1 year later...

I believe the problem still actual. The behavior is inconsistent across those two use cases. It should either be mutable or immutable. I would enhance the quick craft case with either move slot.onQuickCraft() into moveItemStackTo or add simulate mode to moveItemStackTo and first check if the move possible then call that slot.onQuickCraft() and do actual moveItemStackTo.

Link to comment
Share on other sites

  • 9 months later...

Hi all

I am making a mod that creates a map. In my Recipe class in the ItemStack assemble (CraftingContainer) method I can compute all the data, but I don't think the Level.getFreeMapId method should also be called. I do this in onItemCrafted (ItemCraftedEvent e): I add the map id in the compound tag, effectively modifying the result of the recipe. And I don't know how to intercept right shift-click on the result item. When quick crafting the resulting map does not have a map id. In this case the onItemCrafted (ItemCraftedEvent e) method does not work properly.

I'm using the 1.19 41.1.0 version atm.

The way around the problem I made is the following: I search in the player's inventory for a slot with an item equal to the one I just crafted and replace it with the one returned by the event.    

@SubscribeEvent
    public void onItemCrafted(ItemCraftedEvent e) {

        Player player = e.getEntity();
        ItemStack crafting = e.getCrafting();
	CompoundTag tag = crafting.getTag();
	if (tag == null || !tag.contains("my_tag")) {
		return;
	}

        Inventory inventory = player.getInventory();
        int slot = inventory.findSlotMatchingItem(crafting);
        if (slot != -1) {
            inventory.setItem(slot, crafting);
        }
		
	tag.remove("my_tag");

	...

    }

 

Edited by Zacomat
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.