Jump to content

ItemGroup throws error


ItamarCohen

Recommended Posts

Hi I'm creating a Minecraft mode, andI'm trying to add silver ingot to the game as a test.

Unfortunately it doesn't work.

package com.lightning.lightningmod.setup;

import net.minecraft.world.item.Item;
import net.minecraftforge.registries.DeferredRegister;

public class ModItems {
    public static final DeferredRegister<Item> SILVER_INGOT = Registration.ITEMS.register("silver_ingot", () ->
            new Item(new Item.Properties().tab(ItemGroup.MATERIALS)));
}

It just doesn't recognize it as a class, with no option to import, any solutions?

Link to comment
Share on other sites

58 minutes ago, diesieben07 said:

Refer to the documentation on how to register things: https://mcforge.readthedocs.io/en/latest/concepts/registries/.

Note that you should not put a DeferredRegister and its entries in separate classes like you've done here.

I'm just following a tutorial...

Edited by ItamarCohen
Link to comment
Share on other sites

Main:

package com.lightning.lightningmod;

import com.lightning.lightningmod.setup.Registration;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fmlserverevents.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.stream.Collectors;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("lightningmod")
public class Main
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MODID = "lightningmod";
    public Main() {
        Registration.register();
        // Register the setup method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        // Register the enqueueIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
        // Register the processIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        // some preinit code
        LOGGER.info("HELLO FROM PREINIT");
        LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
    }

    private void enqueueIMC(final InterModEnqueueEvent event)
    {
        // some example code to dispatch IMC to another mod
        InterModComms.sendTo("lightningmod", "helloworld", () -> { LOGGER.info("Hello world from the MDK and Itamar"); return "Hello world";});
    }

    private void processIMC(final InterModProcessEvent event)
    {
        // some example code to receive and process InterModComms from other mods
        LOGGER.info("Got IMC {}", event.getIMCStream().
                map(m->m.messageSupplier().get()).
                collect(Collectors.toList()));
    }
    // You can use SubscribeEvent and let the Event Bus discover methods to call
    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
    }

    // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
    // Event bus for receiving Registry Events)
    @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    public static class RegistryEvents {
        @SubscribeEvent
        public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
            // register a new block here
            LOGGER.info("HELLO from Register Block");
        }
    }
}

 

Registration:

package com.lightning.lightningmod.setup;

import com.lightning.lightningmod.Main;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class Registration {
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Main.MODID);
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, Main.MODID);


    public static void register() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        BLOCKS.register(modEventBus);
        ITEMS.register(modEventBus);
    }
}

ModItems:

package com.lightning.lightningmod.setup;

import net.minecraft.world.item.Item;
import net.minecraft.world.item.*;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.registries.DeferredRegister;


public class ModItems {
    public static final DeferredRegister<Item> SILVER_INGOT = Registration.ITEMS.register("silver_ingot", () -> new Item(new Item.Properties().tab(ItemGroup.MATERIALS)));
}

The error is in the ModItems Class.

Cannot resolve symbol 'ItemGroup'

It's not giving an option to import it.

Link to comment
Share on other sites

Just now, ItamarCohen said:

1.17.1

 

I changed the registration file to:

package com.lightning.lightningmod.setup;

import com.lightning.lightningmod.Main;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class Registration {
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Main.MODID);
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, Main.MODID);
    public static final DeferredRegister<Item> SILVER_INGOT = Registration.ITEMS.register("silver_ingot", () -> new Item(new Item.Properties().tab(ItemGroup.MATERIALS)));


    public static void register() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        BLOCKS.register(modEventBus);
        ITEMS.register(modEventBus);
    }
}

 

Link to comment
Share on other sites

Code should look something like this:

public class ModItems {

	public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExampleMod.MODID);
	
	public static void initItems() {
		ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
	}
	
	public static final RegistryObject<Item> COPPER_INGOT = ITEMS.register("copper_ingot", ()->
		new Item(new Item.Properties().tab(CreativeModeTab.TAB_MATERIALS))
	);
}

Not an expert myself, but the main things here are:

1.- A deferred register must be in the same class as it's RegistryObjects (it's entries)

2.- The method register(name, supplier) of a DeferredRegister (the one you call when you need to register something) returns a RegistryObject, not a DeferredRegister. In your code you incorrectly assign it to a DeferredRegister.

3.- About the ItemGroup problem, at some point ItemGroup must've changed to CreativeModeTab (like in my example which i've tested and it works). I don't know if that's the case for the version of forge you're using. In any case, just check what argument the method 'tab' wants, and give it that. Like in the image.

image.png.97726cfd09e1100ba8c1732575df7809.png

 

idk if i messed up on something, hope this helps.

 

Link to comment
Share on other sites

15 hours ago, Tryhard said:

Code should look something like this:


public class ModItems {

	public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExampleMod.MODID);
	
	public static void initItems() {
		ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
	}
	
	public static final RegistryObject<Item> COPPER_INGOT = ITEMS.register("copper_ingot", ()->
		new Item(new Item.Properties().tab(CreativeModeTab.TAB_MATERIALS))
	);
}

Not an expert myself, but the main things here are:

1.- A deferred register must be in the same class as it's RegistryObjects (it's entries)

2.- The method register(name, supplier) of a DeferredRegister (the one you call when you need to register something) returns a RegistryObject, not a DeferredRegister. In your code you incorrectly assign it to a DeferredRegister.

3.- About the ItemGroup problem, at some point ItemGroup must've changed to CreativeModeTab (like in my example which i've tested and it works). I don't know if that's the case for the version of forge you're using. In any case, just check what argument the method 'tab' wants, and give it that. Like in the image.

image.png.97726cfd09e1100ba8c1732575df7809.png

 

idk if i messed up on something, hope this helps.

 

But, how do I get the item now? and add texture to it?

Link to comment
Share on other sites

15 hours ago, Tryhard said:

Code should look something like this:


public class ModItems {

	public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExampleMod.MODID);
	
	public static void initItems() {
		ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
	}
	
	public static final RegistryObject<Item> COPPER_INGOT = ITEMS.register("copper_ingot", ()->
		new Item(new Item.Properties().tab(CreativeModeTab.TAB_MATERIALS))
	);
}

Not an expert myself, but the main things here are:

1.- A deferred register must be in the same class as it's RegistryObjects (it's entries)

2.- The method register(name, supplier) of a DeferredRegister (the one you call when you need to register something) returns a RegistryObject, not a DeferredRegister. In your code you incorrectly assign it to a DeferredRegister.

3.- About the ItemGroup problem, at some point ItemGroup must've changed to CreativeModeTab (like in my example which i've tested and it works). I don't know if that's the case for the version of forge you're using. In any case, just check what argument the method 'tab' wants, and give it that. Like in the image.

image.png.97726cfd09e1100ba8c1732575df7809.png

 

idk if i messed up on something, hope this helps.

 

Do I need to call the initiates function?

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Of course? If you never call a function it doesn't do anything...?

To "get to the item" you just access the RegistryObject you got when registering it.

To add a texture, you just need to create a model for it in the correct folder in assets. Refer to vanilla blocks for more info.

OK

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.

Announcements



×
×
  • Create New...

Important Information

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