Jump to content

Recommended Posts

Posted

First I am making a new topic on this because I feel like forge has changed a lot since 1.13 and a lot isn't the same as all the tutorials out there.

 

So back in the days I always wanted to make (minecraft) mods, now some years later I am finally a professional programmer so I thought let's take another shot at it in my free hours.

 

I've watched some tutorials with forge 1.13 and up and I noticed that a lot of these guys keep static references everywhere which in my opinion is very bad practice. For example if you would develop for Android then a static reference will almost always fall out of context at some point and cause crashes. I watched these tutorials and started making a starter mod with some basic item(s). 

 

This is how I initialize the mod which is nothing special but then I have an `ItemList` class where I would keep all my items which I init from the EventBusSubscriber.

 

Then I get an instance of every item within it's class so I can keep item properties like group in it's own class to keep my code organized. I hate it when I would have to put half of the settings within the class and the others in ItemList.

 

So for the more experienced modders out there. What do you think about below approach and how could I improve it. Also I would like to know more about how you guys did it. I feel like the documentation is really outdated and incomplete. 

 

Code below:
 

@Mod("xiocraft")
public class XioCraftMod
{
    public static XioCraftMod instance;
    public static final String ID = "xiocraft";
    private static final Logger LOGGER = LogManager.getLogger();

    public XioCraftMod()
    {
        instance = this;

        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientRegistries);

        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {

    }

    private void clientRegistries(final FMLClientSetupEvent event)
    {

    }

    @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    public static class RegistryEvents {
        @SubscribeEvent
        public static void registerBlocks(final RegistryEvent.Register<Block> event)
        {

        }

        @SubscribeEvent
        public static void registerItems(final RegistryEvent.Register<Item> event)
        {
            ItemList.register(event);
        }
    }
}

 

public class ItemList {

    public static Item dough;
    public static Item lahmacun;

    public static void register(final RegistryEvent.Register<Item> event) {
        event.getRegistry().registerAll(
                dough = DoughItem.getInstance(),
                lahmacun = LahmacunItem.getInstance()
        );
    }

}

 

public class DoughItem extends Item {

    public static final String ID = "dough";

    public DoughItem(Properties properties) {
        super(properties);
    }

    public static Item getInstance() {
        return new DoughItem(
            new Item.Properties()
                .group(ItemGroup.FOOD)
        )
        .setRegistryName(new ResourceLocation(XioCraftMod.ID, DoughItem.ID));
    }
}

 

  • Finiox changed the title to What is bad/good practice when it comes to initializing
Posted (edited)
5 hours ago, diesieben07 said:
  • Static references to your items are okay here, since the items will live for the duration of the game anyways. You can use @ObjectHolder if you want, but it's not a requirement.
  • Code style: A static getInstance method is usually associated with the singleton pattern. May I suggest newInstance?

In the end, all that matters is that you instantiate and initialize your items and blocks during a forge controlled event (such as RegistryEvent.Register<Item>).

Ah thanks. I didn't see the `getInstance` whoops 

Edited by Finiox

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.