Jump to content

Damaging an Item in recipes


MostafaSabry55

Recommended Posts

Hello, I'm back with another recipe and data gen problem, so basically I have a hammer item, and 2 ingots, they turn into plates (of their type), I've done that all ready and it works fine, now my problem is that the hammer doesn't seem to get damaged (because I got no idea how), here's my code :

HammerItem: (just incase I've done something wrong here!)

public class HammerItem extends Item {

    private static final Random RAND = new Random();

	public HammerItem() {
		super(new Item.Properties()
				.group(MorePlates.ITEMGROUP)
                .maxStackSize(1)
                .setNoRepair());
	}

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

    @Override
    public int getMaxDamage(ItemStack stack) {
        return Config.GENERAL.DURABILITY_HAMMER.get();
    }

    @Override
    public int getDamage(ItemStack stack) {
        return !stack.hasTag() ? getMaxDamage(stack) : stack.getOrCreateTag().getInt("Damage");
    }

    @Nonnull
    @Override
    public ItemStack getContainerItem(@Nonnull ItemStack stack) {
    	ItemStack container = stack.copy();
		if(container.attemptDamageItem(1, RAND, null)) {
			return ItemStack.EMPTY;
		} else {
			return container;
		}
    }
    
    @Override
	public boolean isEnchantable(@Nonnull ItemStack stack) {
		return true;
	}

    @Override
    public int getItemEnchantability() {
        return 14;
    }

    @Override
    public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantment) {
        return enchantment == Enchantments.UNBREAKING || enchantment == Enchantments.MENDING;
    }
}

 

And here's my recipe bit

Item PLATE = ForgeRegistries.ITEMS.getValue(new ResourceLocation(MorePlates.MODID, material.toString() + "_plate"));
Ingredient INGOT = Ingredient.fromTag(ItemTags.createOptional(material.getTag()));

ConditionalRecipe.builder()                  
.addCondition(        	
and(
    new BooleanCondition(() -> Config.GENERAL.PLATE_RECIPES.get(), BooleanCondition.Type.ENABLE_PLATE.get()),
    itemExists(MorePlates.MODID, material.toString() + "_plate")
   )
)
.addRecipe(
     ShapedRecipeBuilder.shapedRecipe(PLATE)
                        .patternLine("H")
                        .patternLine("I")
                        .patternLine("I")
                        .key('H', ForgeRegistries.ITEMS.getValue(new ResourceLocation(MorePlates.MODID, "hammer")))
                        .key('I', INGOT)
                        .setGroup("")
        	            .addCriterion("has_item", hasItem(ForgeRegistries.ITEMS.getValue(new ResourceLocation(MorePlates.MODID, "hammer"))))
                        ::build
)
.build(consumer, new ResourceLocation(MorePlates.MODID, material.toString() + "_plate"));

Thanks in regards!

Link to comment
Share on other sites

So from my understanding is that attempDamageItem is deprecated, and it should be moreover like this?

ItemStack container = stack.copy();
container.damageItem() //didnt check the pars yet
return container;

Also thanks for the Consumer bit

 

By the way, I am pretty sure this is also a problem.. (from my original post in the ShapedRecipeBuilder text)

.key('H', ForgeRegistries.ITEMS.getValue(new ResourceLocation(MorePlates.MODID, "hammer")))

I should be damaging the Item here, but how do I exactly make it do that? It cannot accept ItemStacks

Edited by MostafaSabry55
Link to comment
Share on other sites

45 minutes ago, MostafaSabry55 said:

So from my understanding is that attempDamageItem is deprecated, and it should be moreover like this?

Not deprecated iirc, but it's used within damageItem which already handles everything

45 minutes ago, MostafaSabry55 said:

By the way, I am pretty sure this is also a problem.. (from my original post in the ShapedRecipeBuilder text)

Wouldn't matter, item damage should be handled on the container item itself just like you have done. So, where you are doing it is where it should be done if I'm not mistaken.

  • Thanks 1
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.

Announcements



×
×
  • Create New...

Important Information

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