#Registering item issues/error

50 messages · Page 1 of 1 (latest)

unique moon
#

I don't know how o solve this but i keep getting Caused by: java.lang.NullPointerException: Item id not set

here is my code:


import io.github.beez131github.grow_gc.Grow_Gc;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class ModItems {

    public static final Item ROOTED_GOLDEN_CARROT = registerItem("rooted_golden_carrot", new Item(new Item.Settings()
            .registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(Grow_Gc.MOD_ID, "rooted_golden_carrot")))));

    private static Item registerItem(String name, Item item) {
        return Registry.register(Registries.ITEM, Identifier.of(Grow_Gc.MOD_ID, name), item);
    }
    public static void registerModItems() {
        Grow_Gc.LOGGER.info("Registering Mod Items for " + Grow_Gc.MOD_ID);
    }

}
merry crystal
#

Life Pro Tip:

when you get stuck, look at how minecraft does stuff

unique moon
#

ok, i have been trying with that for a while but i just keep getting Cannot resolve method 'registryKey' in 'Settings' , this is the best try so far:

package io.github.beez131github.grow_gc.item;

import io.github.beez131github.grow_gc.GrowGc;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class ModItems {
    // Register the item using the correct identifier
    public static final Item ROOTED_GOLDEN_CARROT = registerItem(
        "rooted_golden_carrot",
        new Item(new Item.Settings())
    );

    private static Item registerItem(String name, Item item) {
        Identifier id = Identifier.of(GrowGc.MOD_ID, name);
        RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id);

        Item.Settings settings = new Item.Settings()
            .registryKey(key); 

    
        return Registry.register(
            Registries.ITEM, 
            key,             // The registry key
            new Item(settings) 
        );
    }


    // Method to register all mod items
    public static void registerModItems() {
        GrowGc.LOGGER.info("Registering Mod Items for " + GrowGc.MOD_ID);
    }
}
``` I have no clue what im not doing but i just don't know what to do from here.
merry crystal
#
    public static final Item ROOTED_GOLDEN_CARROT = registerItem(
        "rooted_golden_carrot",
---> no  new Item(new Item.Settings())
    );
#

take a look at vanilla's Items class for an idea on how to handle adding the registry key in the register method

unique moon
merry crystal
#

did you follow the official docs tutorial for setting up the environment?

#

what IDE are you using

unique moon
unique moon
merry crystal
#

if you hit shift 4 times in a row, and search for Items does it show up?

unique moon
#

no, im just trying a possible fix real quick

#

ok fixed it

unique moon
#

i looked in vanilla code and found no registryKey in Items and i even looked at a 1.21.3 example so something must have changed in 1.21.3 --> 1.21.4
checking fabric 1.21.4 blog

merry crystal
#

you looked at the Items class's register method?

#

where they specifically add the key?

#

are you using mojmap?

unique moon
unique moon
#

ok so

#

i looked at a axample for 1.21.4, same methods and everything and i still get the error

#

so, tbh i don't know what to do at this point, only thing i can think of is something else not inside ModItems is causing the issue now

merry crystal
#

whats the error

unique moon
#

"item id not set"

merry crystal
#

and are you sure you are not creating a new Item without a registryKey in the properties?

unique moon
#
package io.github.beez131github.grow_gc.item;

import io.github.beez131github.grow_gc.GrowGc;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.item.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class ModItems {
    // Rooted Golden Carrot Keys
    public static final RegistryKey<Item> ROOTED_GOLDEN_CARROT_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(GrowGc.MOD_ID, "rooted_golden_carrot"));

    // Rooted Golden Carrot Item
    public static final Item ROOTED_GOLDEN_CARROT = register(
        new Item(new Item.Settings()),
        ROOTED_GOLDEN_CARROT_KEY
    );

    // Register Method
    public static Item register(Item item, RegistryKey<Item> registryKey) {
        return Registry.register(Registries.ITEM, registryKey.getValue(), item);
    }

    public static void initialize() {
        GrowGc.LOGGER.info("Registered Mod Items for " + GrowGc.MOD_ID);

        ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> {
            entries.addItem(ROOTED_GOLDEN_CARROT);
        });}
}

Here is the code if u want to see it

#

exact error Caused by: java.lang.NullPointerException: Item id not set

merry crystal
#
    public static final Item ROOTED_GOLDEN_CARROT = register(
        new Item(new Item.Settings()),  <--- still no
        ROOTED_GOLDEN_CARROT_KEY  
    );
unique moon
#

so are u saying to remove it, by saying "still no ROOTED_GOLDEN_CARROT_KEY" it sounds like u saying its missing

merry crystal
#

its wrong

#

thats not how vanilla does it nor is that how the blog post i linked does it

#

you should not be doing new Item(new Item.Settings())

unique moon
#

ok, well here is what i have now, with the 3 examples i have of 1.21.4 mods this should be exactly the same as how they do it, but i get the error below. if this is "wrong" then its making 0 sense because its how 3 1.21.4 mods made in the last 1 day have done it.

package io.github.beez131github.grow_gc.item;

import io.github.beez131github.grow_gc.GrowGc;;
import net.minecraft.item.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class ModItems {

    // Rooted Golden Carrot Keys
    public static final Item ROOTED_GOLDEN_CARROT = registerItem("rooted_golden_carrot", new Item(new Item.Settings()
        .registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(GrowGc.MOD_ID, "rooted_golden_carrot")))
        .maxCount(1)
    ));

    public static Item registerItem(String name, Item item) {
        return Registry.register(Registries.ITEM, Identifier.of(GrowGc.MOD_ID, name), item);
    }

    public static void registerModItems() {
        GrowGc.LOGGER.info("Registering Mod Items for " + GrowGc.MOD_ID);
    }
}
#

stills gives the error here tho

        .registryKey(ROOTED_GOLDEN_CARROT_KEY) // Registering the item with the correct key
        ^
  symbol:   method registryKey(RegistryKey<Item>)
  location: class Settings```
merry crystal
#

the error you posted does not go with the code you posted

#

.registryKey(ROOTED_GOLDEN_CARROT_KEY) is nowhere in the code you just posted

#

now that you struggled your way through it, let me explain how vanilla is doing it and how it is recommended to be done:

private static Item register(
  String itemId, 
  Function<Item.Settings, Item> factory, 
  Item.Settings settings
) {

this is the signature of the regsiter method.
it takes 3 arguments:

  • just the path portion of the identifier in the item id (you will add your mod id later)
  • a method reference that will take an Item.Settings object and return some Item object
  • an ItemSettings object
#

a note here, in case you arent aware of the syntax
a method can be passed as a reference by using the :: syntax
MyClass::someMethod will pass a method named someMethod belonging to the MyClass class. depending on context this may be static or instanced.

the ::new method references constructors. so MyClass::new references the constructor of the MyClass class

#

with that bit of information, you can deduce that the Item constructor, which takes an Item.Settings object and returns an Item object, can be passed as the factory argument to our register method

#

so one could call this method:

register("my_item", Item::new, new Item.Settings().maxCount(1))
#

inside the method, you now have 3 things to work with. the first thing you have is the item name. With this you can create a RegistryKey

RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id(itemId));

#

you can then pass this registrykey into the properties
settings.registryKey(key)

#

now, we dont need that to be on its own line, we can inline that. You can also create the item by passing the settings into the factory method
factory.apply(settings.registryKey(key))

#

the apply method is the method defined by the Java Function interface

#

so now that you have a way to make a registry key, and a way to make an item with the properties with the registry key, all thats left is to register it.

you can actually use the registry key in place of the Identifier in the Registry.register call

#

Registry.register(Registries.ITEM, key, factory.apply(settings.registryKey(key)));

#

since the Registry.register method returns the object being registered, you can just return the result of that method call