Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16] Making a recipe damage an item on use


Recommended Posts

So, I want to add shapeless recipes where you use Shears + (some item) to craft another item, but the shears stay, albeit with -1 durability. After a bit of a search, it seems like you can use getContainerItem() and similar methods to achieve this, but that's out of scope here with a vanilla item.

 

Then, how am I supposed to approach this? Maybe through a custom recipe type where one of the ingredients stay after crafting, but how do I even do that?

Edited by A Soulspark
Link to post
Share on other sites

Make a custom recipe, you can extend a base recipe class (in this case ShapelessRecipe). In there override getRemainingItems and make a custom remaining item for the shears.

Then make a custom IRecipeSerializer for it and register it using the normal registry stuff. Then you can use that new recipe serializer in your recipe JSON.

Link to post
Share on other sites

Ok, that seems to make sense! Though I'm having a bit of trouble with the damaging part, since the getRemainingItems() function only gives you an Inventory. I believe the best way to damage the item is through attemptDamageItem(), but idk how to get a java.util.Random instance in this context.

 

Here's the code for reference:

@Override
public NonNullList<ItemStack> getRemainingItems(CraftingInventory inv) {
  // get the default remaining items for a shapeless recipe 
  NonNullList<ItemStack> remainingItems = super.getRemainingItems(inv);

  // loop through each slot in the crafting inventory
  for (int i = 0; i < remainingItems.size(); i++) {
    ItemStack stack = inv.getStackInSlot(i);
    // if the item in the slot are shears
    if (stack.getItem() instanceof ShearsItem) {
      // try to damage the item, but this is *ugly*
      // how do I get a Random instance here ▬-▬
      stack.attemptDamageItem(1, [insert java.util.Random instance], null); // (int amount, Random random, ServerPlayerEntity entity)
      remainingItems.set(i, stack);
    }
  }

  return remainingItems;
}

 

Link to post
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.

Guest
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 Privacy Policy.