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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello all! I'm currently trying to make the Biomes o' Plenty mod (1.12.2 version) compatible with my shaderpack (BSL v8.5). Even after I've already inserted a "shaders" folder with a correctly-formatted blocks.PROPERTIES file within the assets folder of the .jar, it still does not wave the textures from the mod. I definitely thought this was going to work since I noticed the 1.19.2 version of the mod laid it out exactly like that and it worked perfectly when my PC was able to run 1.19.2 Forge... ^^;;; Could there be some missing link I'm not getting at here?
    • After much experimentation I finally realised why this doesn't work and how to fix it. The critical component to understand here, is that ItemCraftedEvent is called before the crafting ingredients are purged from the crafting slots; this all happens in ResultSlot.java's onTake method: We can see here the first line fires checkTakeAchievements, this is what fires the ItemCraftedEvent (after a few method calls). Then we can see the purge behaviour. I worked around this by creating a CarverItem class, which uses hasCraftingRemainingItem and getCraftingRemainingItem to hook into ResultSlot.onTake: Now, I reconstruct my ItemCraftedEvent handler, and using some smart tags (which I omit for brevity), I handle two cases: 1. When crafting a carved item, I damage the tool used in the recipe 2. When crafting a carving tool from a repair recipe, I manually purge the input tools as with the new CarverItem class, as otherwise items would be duplicated   This has now greatly improved implementation, beyond my expectations, for the standard crafting table and the user's inventory crafting grid. A user can now click or shift-click 'carve' items, with no duplication or issues that I have spotted so far.  
    • Hi, I'm having issues launching my Forge 1.20.1 installation (47.2.0). I keep getting error code 1, and all the logs abruptly pause with the game attempting to open GL 4.6. Latest Crash: https://pastebin.com/zsP5Wipk Debug (No idea what the real name is) Log: https://pastebin.com/CzvqhNq0   Thanks for the help!
    • In the most up to date version of Allthemods 6 (v1.9.2) the stoned bee is not able to be interacted with apart from putting it into an empty bee jar. I can't breed the bee or put it into my aviary breeder. When I try to put it into the breeder it says import unsuccessful. The stoned bee is one of the most important bees for advancing in the mod so it is rather unfortunate that it is not working as intended.
    • Everytime I try to run the Forge 1.20.1 - 47.1.0 Installer, only appears an a black window and then closes. I don't appear the installing menu anymore.
  • Topics

×
×
  • Create New...

Important Information

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