Jump to content

Recommended Posts

Posted

Sometimes, you want to create an item that can be called upon and also have a value attached. This tutorial will help you create a custom item property.

These items can also be detected with the instanceof expression. It is better to use instanceof for these items instead of tags because if some madlad datapacker decides to add some random item to your list of custom items, it can really screw up your mod and crash the game from an invalid method statement (if it for some reason doesn't bump into a ClassCastException first) because that custom (possibly vanilla) item won't have that specific property.

 

How to Do It:

You want to do this with a modded item you register in this mod. Trying to modify an item added by another mod or vanilla minecraft will require events or mixins.

First of all, create an Enum for your custom property (you don't have to do this, it just makes it more professional cool and easier to define your values with methods).

  Reveal hidden contents

 

You must also create a class for your custom item type. 

  Reveal hidden contents

Once you have that, you can register it like a normal item, just with (your custom item type) instead of Item.

  Reveal hidden contents

And you're done! You can call upon any value attached to the item by using:

((CustomItemType) itemstack.getItem()).getValue()

 

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Posted
  On 2/19/2023 at 9:40 PM, Hipposgrumm said:

First of all, create an Enum for your custom property (you don't have to do this, it just makes it more professional cool and easier to define your values with methods).

  Reveal hidden contents
Expand  

In general, I would prefer an interface since enums are not extensible without hacky asm (like what Forge does to vanilla enums).

  On 2/19/2023 at 9:40 PM, Hipposgrumm said:
  	RECESSIVE("recessive", false)
  	INVISIBLE("invisible", true)
Expand  

Mixing tabs and spaces; also this will error.

  On 2/19/2023 at 9:40 PM, Hipposgrumm said:
private static ResourceLocation buildTrim(String id) {
        ResourceLocation armorLoc = new ResourceLocation(Main.MODID, "textures/trims/models/armor/"+id);
        return armorLoc;
    }
Expand  

This is bad practice for two reasons. First, you are constructing the resourcelocation over and over. I would just store this as a field on construction. Second, this is introducing client side asset locations on the server which, in my opinion, is just bad practice since the server should need to know nothing about the client.

  On 2/19/2023 at 9:40 PM, Hipposgrumm said:
public CustomProperty property;

  	// For getting the property.
    public CustomProperty getProperty() {
        return this.property;
    }
Expand  

Public field and getter? Also, should be final and probably an interface for better extensibility.

  On 2/19/2023 at 9:40 PM, Hipposgrumm said:
        SMITHING_TEMPLATES.register(modEventBus);
        NEW_SMITHING_MENUS.register(modEventBus);
        TRIMMING_RECIPES.register(modEventBus);

        MinecraftForge.EVENT_BUS.register(this)
Expand  

I have no idea what any of this has to do with registering the item. Additionally, the item register isn't event added to the mod event bus.

  On 2/19/2023 at 9:40 PM, Hipposgrumm said:
((CustomItemType) itemstack.getItem()).getValue()
Expand  

Probably should perform instanceof check first yes. Though, ideally, you shouldn't have to perform a check at all as there would be a base method would consume this such that these costly checks are unneeded.

Posted (edited)
  On 2/20/2023 at 3:32 PM, ChampionAsh5357 said:

This is bad practice for two reasons. First, you are constructing the resourcelocation over and over. I would just store this as a field on construction. Second, this is introducing client side asset locations on the server which, in my opinion, is just bad practice since the server should need to know nothing about the client.

Expand  

Sorry, that part is left over from where I got my code from.

 

FINE! I DON'T KNOW HOW TO MOD! DELETE THIS PLEASE @ChampionAsh5357!

Edited by Hipposgrumm

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Posted
  On 2/20/2023 at 5:21 PM, Hipposgrumm said:

FINE! I DON'T KNOW HOW TO MOD! DELETE THIS PLEASE @ChampionAsh5357!

Expand  

Why? It points out mistakes and provides constructive criticism. I like having this information so I can fix things and get other perspectives (Lex, Curle, and sci are typically the people who do this with me). Personally, I would like if more people did it on the docs though.

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.