Jump to content

[Tutorial] How to Create Items with Custom Defined Properties


Hipposgrumm

Recommended Posts

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).

Spoiler
package com.example.customitem.items.properties;

import com.example.customitem.Main;

public enum CustomProperty {
    COLORFUL("colorful", true),
    DOMINANT("dominant", false),
  	RECESSIVE("recessive", false)
  	INVISIBLE("invisible", true)
	
  	// Every attribute value has to be defined.
    private String name;
  	private boolean observable
						   // You can place any attributes you want here.
    private CustomProperty(String name, boolean observable) {
        this.name = name;
      	this.observable = observable;
    }

    private static ResourceLocation buildTrim(String id) {
        ResourceLocation armorLoc = new ResourceLocation(Main.MODID, "textures/trims/models/armor/"+id);
        return armorLoc;
    }

    public String getId() {
        return this.name;
    }

    public boolean isObservable() {
        return this.observable;
    }
    
  	// You can add this if you want, but its not required.
    @Override
  	public String toString() {
      	return "Value: "+this.name+", Observable: "+this.observable
    }
}

 

 

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

Spoiler
package com.example.customitem.items;

import com.example.customitem.items.properties.CustomProperty;
import net.minecraft.world.item.Item;

public class CustomItemType extends Item {
    public CustomProperty property;

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

    public CustomItemType(CustomProperty property, Item.Properties properties) {
        super(properties);
        this.property = property;
    }
}

 

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

Spoiler
package com.example.customitem;

import com.example.customitem.items.CustomItemType;
import com.example.customitem.items.properties.CustomProperty;
import com.mojang.logging.LogUtils;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.slf4j.Logger;

@Mod(Main.MODID)
public class Main {
    public static final String MODID = "customitem";
    private static final Logger LOGGER = LogUtils.getLogger();

    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);

    public static final RegistryObject<Item> RAINBOW = ITEMS.register("rainbow", () -> new CustomItemType(CustomProperty.RAINBOW, new Item.Properties().tab(CreativeModeTab.TAB_MISC)));
  	public static final RegistryObject<Item> DOMINANT = ITEMS.register("dominant", () -> new CustomItemType(CustomProperty.DOMINANT, new Item.Properties().tab(CreativeModeTab.TAB_MISC)));
  	public static final RegistryObject<Item> RECESSIVE = ITEMS.register("recessive", () -> new CustomItemType(CustomProperty.RECESSIVE, new Item.Properties().tab(CreativeModeTab.TAB_MISC)));
  	public static final RegistryObject<Item> INVISIBLE = ITEMS.register("invisible", () -> new CustomItemType(CustomProperty.INVISIBLE, new Item.Properties().tab(CreativeModeTab.TAB_MISC)));

    public Main() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        SMITHING_TEMPLATES.register(modEventBus);
        NEW_SMITHING_MENUS.register(modEventBus);
        TRIMMING_RECIPES.register(modEventBus);

        MinecraftForge.EVENT_BUS.register(this);
}

 

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?

Link to comment
Share on other sites

17 hours ago, 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).

 

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

17 hours ago, Hipposgrumm said:
  	RECESSIVE("recessive", false)
  	INVISIBLE("invisible", true)

Mixing tabs and spaces; also this will error.

17 hours ago, Hipposgrumm said:
private static ResourceLocation buildTrim(String id) {
        ResourceLocation armorLoc = new ResourceLocation(Main.MODID, "textures/trims/models/armor/"+id);
        return armorLoc;
    }

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.

17 hours ago, Hipposgrumm said:
public CustomProperty property;

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

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

17 hours ago, Hipposgrumm said:
        SMITHING_TEMPLATES.register(modEventBus);
        NEW_SMITHING_MENUS.register(modEventBus);
        TRIMMING_RECIPES.register(modEventBus);

        MinecraftForge.EVENT_BUS.register(this)

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.

17 hours ago, Hipposgrumm said:
((CustomItemType) itemstack.getItem()).getValue()

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.

Link to comment
Share on other sites

1 hour ago, 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.

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?

Link to comment
Share on other sites

56 minutes ago, Hipposgrumm said:

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

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.

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.



×
×
  • Create New...

Important Information

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