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

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?

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

Posted (edited)
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?

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I myself am quite new to the modding scene, I started modding in February but have been in development hell a lot and have struggled to focus on one mod though I have been developing the current mod I've been working on since September and have made a lot of progress; during this time I have found a lot of helpful resources and have learnt a lot of things.    1. You can, I have had no Java experience previously and since modding have managed to be quite knowledgeable in Java.   2. Modding does not require massive teams, I and many others work alone, though having a team is certainly helpful in the modding process to get things done in a quicker and more organised way.   3. I absolutely have to recommend ModdingByKaupenjoe, he makes loads of tutorials for Minecraft from 1.16.5 and above with support for Forge, Fabric, and as of his 1.21 tutorials, Neoforge. These tutorials are really well made, covering almost every modding topic, (such as items, blocks, mobs, worldgen, etc.) and are pretty easy to follow, and Kaupenjoe always leaves the link to his GitHub repositories where you can view the code of that tutorial at your own pace as well as linking textures in the description of his videos for you to use. These forums are also quite good if you need help, though I have found that it sometimes takes a little while for a response but it is always worth the wait; from my experience the people on these forums have always been kind and helpful. There are also user-submitted tutorials that may be helpful as well.  Using GitHub to search up a certain class that you are wanting to use in your mod is also quite helpful, the video I have linked goes into more detail. I would also recommend planning out your mod before you make it to have a clearer idea of how you want your mod to be, including sketches and annotations are also a good idea for this. It has helped me make progress a lot quicker when programming as I already know how I want things to look/act.   I hope all this information helps!
    • I don't know what I did differently, but the error code changed to "Invalid signature for profile public key" so I set "enforce-secure-profile" to false since only my friend will be allowed to join anyway and then everything worked properly. Here's the logs anyway, but I hope the problem doesn't come back. https://paste.ee/p/Ri44L
    • I tried to look for similar issues here on the forum already and couldn't find a fix. Just people who didn't read the rules properly.
  • Topics

×
×
  • Create New...

Important Information

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