Jump to content

[Solved] Adding item to the game after registering


RekTek249

Recommended Posts

Hi, so I just started out forge modding, with no prior knowledge of it. I have a good knowledge of java and have coded some plugins before, but this is my first time when it comes to modding. So, I'm trying to follow the official forge documentation. I kind of tried to register the item into the game, however it doesn't appear in the creative tab I set it to or using /give. Here is the code I have thus far: (Proxies are displayed in the same class, but they're not.

Proxies:

Spoiler

//Proxies
public class ClientProxy extends CommonProxy {

    public void preInit() {
        super.preInit();
        RegisterItems.registerItemModels();
        RegisterItems.registerBlockModels();
    }

    public void init() {
        super.init();
    }

    public void postInit(){
        super.postInit();
    }

}
public class ServerProxy extends CommonProxy {

    public void preInit() {
        super.preInit();

    }

    public void init() {
        super.init();

    }

    public void postInit(){
        super.postInit();

    }


}
public abstract class CommonProxy {

    public void preInit() {
        RegisterItems.registerBlocks();
        RegisterItems.registerItems();

    }

    public void init() {


    }

    public void postInit(){

    }


}

 

Main

Spoiler

@Mod(modid = Main.MODID, name = Main.NAME, version = Main.VERSION)

public class Main {

    public static final String MODID = "pmod";
    public static final String NAME = "Practice Mod";
    public static final String VERSION = "1.0";


    @SidedProxy(clientSide = "rektek249.practicemod.client.ClientProxy", serverSide = "rektek249.practicemod.server.ServerProxy")
    public static CommonProxy proxy;


    @EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        proxy.preInit();
    }

    @EventHandler
    public void init(FMLInitializationEvent event) {
        proxy.init();

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event){
        proxy.postInit();

    }


}

 

RegisterItems.java

Spoiler

public class RegisterItems {

    static List<Item> items = new ArrayList<>();
    static List<Block> blocks = new ArrayList<>();


    public static void registerItems(){
            Item rbsteak = (SimpleItem)(new SimpleItem("Raid Boss Steak", "rbsteak", CreativeTabs.FOOD, 16));
            items.add(rbsteak);
            System.out.println("Items Registered");
    }
    public static void registerBlocks(){

    }
    public static void registerItemModels(){

        ModelResourceLocation resLoc;
        for(Item i : items){
            resLoc = new ModelResourceLocation("pmod:rbsteak", "inventory");
            ModelLoader.setCustomModelResourceLocation(i, 0, resLoc);
            System.out.println("One Model Registered");
        }
    }
    public static void registerBlockModels(){

    }

}

 

SimpleItem.java

Spoiler

public class SimpleItem extends Item {

    public SimpleItem(String unlocalizedName, String registryName, CreativeTabs tab){

        this.setUnlocalizedName(unlocalizedName);
        this.setRegistryName(registryName);
        this.setCreativeTab(tab);


    }
    public SimpleItem(String unlocalizedName, String registryName){

        this.setUnlocalizedName(unlocalizedName);
        this.setRegistryName(registryName);

    }
    public SimpleItem(String unlocalizedName, String registryName, CreativeTabs tab, int stackSize){

        this.setUnlocalizedName(unlocalizedName);
        this.setRegistryName(registryName);
        this.setCreativeTab(tab);
        this.setMaxStackSize(stackSize);


    }
    public SimpleItem(String unlocalizedName, String registryName, int stackSize){

        this.setUnlocalizedName(unlocalizedName);
        this.setRegistryName(registryName);
        this.setMaxStackSize(stackSize);


    }



}

 

So, am I missing something out there ? I tried to follow the documentation's recommendation as to proxies, server/client side stuff. Apparently I should have used an Interface for CommonProxy but I thought an abstract class would be a better choice. If you find any other issues in the code, don't hesitate to let me know.

Edited by RekTek249
Link to comment
Share on other sites

You're not actually registering any items or blocks:

http://mcforge.readthedocs.io/en/latest/concepts/registries/#registering-things

You will want to follow the same nomenclature for registering item models as well, using the ModelRegistryEvent inside your client proxy. e.g. like this (albeit my code takes in the data in a different way, storing it in an custom typed array, but still calling ModelLoader.setCustomModelResourceLocation from this method).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 hour ago, Draco18s said:

You're not actually registering any items or blocks:

http://mcforge.readthedocs.io/en/latest/concepts/registries/#registering-things

You will want to follow the same nomenclature for registering item models as well, using the ModelRegistryEvent inside your client proxy. e.g. like this (albeit my code takes in the data in a different way, storing it in an custom typed array, but still calling ModelLoader.setCustomModelResourceLocation from this method).

Alright, so I'm still not quite sure about how theses SubscribeEvent annotations... Your source code is quite long and seems to contain elements I do not need to use. I tried adding the following to my code: 

CommonProxy.java

Spoiler

@SubscribeEvent
public void registerItems(RegistryEvent.Register<Item> event) {
    event.getRegistry().register(RegisterItems.rbsteak);
}

ClientProxy.java

Spoiler

@SubscribeEvent
public void registerModels(ModelRegistryEvent event) {
    ModelResourceLocation resLoc = new ModelResourceLocation("pmod:rbsteak", "inventory");
    ModelLoader.setCustomModelResourceLocation(rbsteak, 0, resLoc);

}

 

However, it would seem theses two are not even fired up. I'm sure there's something I should know but I don't know here...

Link to comment
Share on other sites

5 minutes ago, RekTek249 said:

Alright, so I'm still not quite sure about how theses SubscribeEvent annotations... Your source code is quite long and seems to contain elements I do not need to use. I tried adding the following to my code: 

CommonProxy.java

  Reveal hidden contents


@SubscribeEvent
public void registerItems(RegistryEvent.Register<Item> event) {
    event.getRegistry().register(RegisterItems.rbsteak);
}

ClientProxy.java

  Reveal hidden contents


@SubscribeEvent
public void registerModels(ModelRegistryEvent event) {
    ModelResourceLocation resLoc = new ModelResourceLocation("pmod:rbsteak", "inventory");
    ModelLoader.setCustomModelResourceLocation(rbsteak, 0, resLoc);

}

 

However, it would seem theses two are not even fired up. I'm sure there's something I should know but I don't know here...

Actually, I managed to add it to the game using the following code in my previous function: 

Spoiler

public static void registerItems(){
        Item rbsteak = (SimpleItem)(new SimpleItem("Raid Boss Steak", "rbsteak", CreativeTabs.FOOD, 16));
        items.add(rbsteak);
        ForgeRegistries.ITEMS.register(rbsteak);
//Would use a foreach loop here if it wasn't one single item

}

Is it correct to use this ? It seems to be working, but there might be a reason for you to use the @SubscribeEvent instead.

Link to comment
Share on other sites

Do not use ForgeRegistries.ITEMS.register(rbsteak); directly. Use the registry events.

The problem is that you likely did not register the class with the event bus correctly / at all.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

5 minutes ago, Draco18s said:

Do not use ForgeRegistries.ITEMS.register(rbsteak); directly. Use the registry events.

The problem is that you likely did not register the class with the event bus correctly / at all.

For example, my common proxy:

Spoiler

@Mod.EventBusSubscriber
public abstract class CommonProxy {

    public void preInit() {
    }

    public void init() {


    }

    public void postInit(){

    }

    @SubscribeEvent
    public void registerItems(RegistryEvent.Register<Item> event) {
        event.getRegistry().register(RegisterItems.rbsteak);
    }




}

Would there be a problem in there ? It still won't work with your method. 

Link to comment
Share on other sites

registerItems needs to be static if you want to use the @Mod.EventBusSubscriber annotation.

 

 

Quote

 

Note

This does not register an instance of the class; it registers the class itself (i.e. the event handling methods must be static).

 

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 minutes ago, Draco18s said:

registerItems needs to be static if you want to use the @Mod.EventBusSubscriber annotation.

 

 

 

Yep, that did it... I rarely get to use annotations, other than @Deprecated, @Nullable and such vanilla ones. Thanks for your help.

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.