Jump to content

[1.18.2] Transfer durability to a new item durability from the old one in crafting.


Recommended Posts

Posted

Hi all. I have an item in the mod with durability, from which another item is crafted. Can I somehow transfer durability to a new item?

 

here is the item class code:

 

package com.zombier.slimesing.items;

import net.minecraft.world.item.CreativeModeTab;

import net.minecraft.world.item.Item;

import net.minecraft.world.item.ItemStack;

 

public class NeedlepItem extends Item {

public NeedlepItem(){

     super(new Properties()

     .tab(CreativeModeTab.TAB_TOOLS)

    .stacksTo(1) .defaultDurability(128)

    .setNoRepair() );

     @Override public boolean hasContainerItem(ItemStack stack){ return true; }

     @Override public ItemStack getContainerItem(ItemStack itemStack) {

     ItemStack stack = itemStack.copy();

     stack.setDamageValue(itemStack.getDamageValue() + 1);

     return stack;

}

}

 

If I'm returning the item, don't mind it, I'll fix it. The main thing is to understand how to transfer strength to a new item. It must be damaged by the same number of units as the previous one. The idea is that I have a needle that has durability, and I have a needle with thread that should take on the same durability after crafting.

Posted

that sounds like a vanilla feature where you combine two items in crafting table. doesn't matter that it's not the same.

did you look at RepairItemRecipe class?

  • Like 1
Posted

You need to make your own subclass of CraftingRecipe (I assume this is the crafting table?) along with a Serializer for it.

Then override Recipe.assemble() to do what you want.

 

See for example FireworkRocketRecipe.

  • Like 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
32 minutes ago, warjort said:

You need to make your own subclass of CraftingRecipe (I assume this is the crafting table?) along with a Serializer for it.

Then override Recipe.assemble() to do what you want.

 

See for example FireworkRocketRecipe.

Could you provide a link to an example? I can't find it on the internet. Sorry if my questions are stupid, I'm just learning modding. "See for example FireworkRocketRecipe"

Posted
34 minutes ago, warjort said:

You need to make your own subclass of CraftingRecipe (I assume this is the crafting table?) along with a Serializer for it.

Then override Recipe.assemble() to do what you want.

 

See for example FireworkRocketRecipe.

I wrote a class, but I don't understand from which class it should be inherited.

 

Code:

public class CraftingRecipe extends RepairItemRecipe {

    public CraftingRecipe(ResourceLocation resourceLocation) {

    super(resourceLocation);

   }

}

Posted
4 hours ago, denysmy said:

I wrote a class, but I don't understand from which class it should be inherited.

then stop and breathe.

place a cursor on "RepairItemRecipe" identifier and press ctrl+b (or ctrl+click it). study that class until you understand it completely. do not proceed until you understand repair recipe class and firework recipe class.

  • Like 1
Posted (edited)
5 hours ago, MFMods said:

then stop and breathe.

place a cursor on "RepairItemRecipe" identifier and press ctrl+b (or ctrl+click it). study that class until you understand it completely. do not proceed until you understand repair recipe class and firework recipe class.

Thank you, i really started to understand it.

Edited by denysmy
Posted
5 hours ago, MFMods said:

then stop and breathe.

place a cursor on "RepairItemRecipe" identifier and press ctrl+b (or ctrl+click it). study that class until you understand it completely. do not proceed until you understand repair recipe class and firework recipe class.

Thanks for the advice. I have overridden the method. How can I initialize it in the code so that this craft will work? I understand that most likely it is somewhere in the main class, but how?

Posted
3 hours ago, MFMods said:

register the recipe serializer in similar manner to how you register blocks (DeferredRegister<RecipeSerializer<?>>, RegistryObject<RecipeSerializer<?>>).

Thanks a lot, you helped me a lot!

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.

Announcements



×
×
  • Create New...

Important Information

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