Jump to content

[FIXED] [1.14.4] How to make item stay in the crafting grid?


Xander402

Recommended Posts

I know what a containerItem is, but can this field be somehow utilized in order to make the item stay in the crafting grid? It's not about returning some empty bottle from recipe that requires a bottled substance. It's about a knife staying untouched after crafting something with it.

I tried two things:

package ...

import ...

public class UtilityItem extends Item {

    public UtilityItem(Properties p_i48487_1_) {
        super(p_i48487_1_);
    }

    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        return new ItemStack(this);
    }
}

with regular initialization, and

    // In item init
    public static Item knife = new Item(new Item.Properties().group(SushiMod.TOOLS_GROUP)).setRegistryName(rloc("knife"));
    static {
        knife = new Item(new Item.Properties()
                .containerItem(knife)
                .group(SushiMod.TOOLS_GROUP)).setRegistryName(rloc("knife"));
    }

...which I actually didn't think through. The first had no effect and the second made the knife turn into a purple-black-checked block.minecraft.air item after crafting.

Edited by Xander402

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

Some mods apply damage to a tool item when it's used to craft something. That suggests me that I'd need to use something different than containerItem. But what?

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

The 2nd makes zero sense:

  • Don't create items in a static initializer.
  • You assign a value to the knife field, only to immediately overwrite it... with an almost identical value.
  • It's unknown if you register this Item anywhere, probably you didn't, which is why it did not work. All Items must be created and registered in the appropriate registry events.

Yes, I know it's stupid. ? I was aware of this when writing that post.

Edited by Xander402

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

Oh, well,

Quote

That suggests me that I'd need to use something different than containerItem. 

I was wrong.

11 minutes ago, diesieben07 said:
7 hours ago, Xander402 said:

getContainerItem

Overriding this has no effect if you do not also override hasContainerItem.

That makes sense. The item stays in crafting table after overriding hasContainerItem. Thanks!

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

2 hours ago, Xander402 said:

Some mods apply damage to a tool item when it's used to craft something.

For somebody who would like to know how to do it, here's my solution:
 

import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;

public class KnifeItem extends SwordItem {

    public KnifeItem(IItemTier p_i48460_1_, int p_i48460_2_, float p_i48460_3_, Properties p_i48460_4_) {
        super(p_i48460_1_, p_i48460_2_, p_i48460_3_, p_i48460_4_);
    }

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

    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        ItemStack ret = new ItemStack(this);
        ret.setDamage(itemStack.getDamage() + 1);
        return ret;
    }
}

 

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

3 hours ago, Xander402 said:

ret.setDamage(itemStack.getDamage() + 1);

You should use the tryDamageItem() method(s) instead so that your knife can take (and be affected by) the Unbreaking enchantment.

  • Thanks 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yeah, you're right. I totally didn't think about Unbreaking, because this knife is intended to be just a kitchen tool. Although I'm not gonna take away a possibility to use this item as a full-fledged weapon.
Just it is
attemptDamageItem(), not tryDamageItem().

Edited by Xander402

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

Link to comment
Share on other sites

5 hours ago, Xander402 said:

attemptDamageItem

That's the one. There's a dozen dozen dozen methods that have had their names changed and I can't remember exactly the right thing.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yes, I know that. ? I don't blame you. I've been playing around with modding, adding some basic items and blocks ever since 1.9, and I had to deal with all the changes (not only in naming), too.

Quote

Just it is attemptDamageItem(), not tryDamageItem().

That's just for people who would potentially come to this post not willing to do research and expecting everything to be expounded exactly as it is.

Edited by Xander402

A few bytes about me:

public class Xander402 extends Modder implements IForumMember {
    int javaExperience, moddingExperience;
    LearningWay preferredLearningWay;
    public Xander402() {
        this.javaExperience = BIG;
        this.moddingExperience = NOT_SO_BIG;
        this.preferredLearningWay = LearningWay.through("exampes");
      	super(/*displayName*/"Xander402", /*moddingSince*/"1.9", preferredLearningWay);
    }
    @Override
    public Goal getReasonOfJoining(Forum forum) { return new Goal(() -> { while (true) moddingExperience++; }); }
}

 

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.