Skip to content

Common Items and Blocks

Zsombor Prisznyák edited this page Feb 6, 2019 · 10 revisions

Common Items and Blocks are our solutions to having 15 different versions of Copper with different textures. But if you are reading this you are probably more interested in Cotton Resources. If possible ALWAYS use Cotton Resources instead of Common Items and Blocks. So stop reading this now and click THIS LINK.



...Are you still reading this? Shit. Apparently, you are either too stubborn to use Cotton Resources (Go and check it out now, it's awesome!) or want to add a common item that isn't supported by the awesome Cotton Resources.

How do Common Items and Blocks work?

Well, all Cotton's Common Items and Blocks really do is that it checks if a block or item has already been registered for a given name. If yes it will return the block/item to you. If not it will use the default block or Item you have provided. Sounds complicated? It isn't. Let me show you an example:

Adding a Common Item

Let's say you want to use an adamantium_ingot in your mod and also make sure that it is compatible with other mods that may add it. After making sure that Cotton Resources doesn't support adamantium ingots (go check now) you decide to implement it yourself. All you have to do is to follow these 2 simple steps:

1. Register your Item using CommonItems.register()

For this Example, we will create our Item in the main mod class. But you can, of course, do this in any other function that is called during your mod's init.

public static Item adamantiumIngot;

@Override
public void onInitialize() {
    adamantiumIngot = CommonItems.register("adamantium_ingot", new Item((new Item.Settings()).itemGroup(Cotton.commonGroup)));
    //...
}

Tip: If you want to make it easier for users to find your item consider adding it to Cotton's Common tab!

2. Provide resources for it

Just like for any other item you will have to create an item model JSON file, a translation key, and a texture. The only difference to what you would normally do is that you will have to use the namespace "cotton" instead of your modid. And that's it! When another mod tries to add an item called "admantium_ingot" it will end up using the same item you are.

Adding a Common Block

Let's say that after adding adamantium ingot you decide that you also want to add adamantium_ore to your mod. After (again) making sure that Cotton Resources doesn't support adamantium ore you can follow these 2 steps below to add it yourself:

1. Register your Block using CommonBlock.register()

This time there are two methods you can call. The first one is pretty much identical to the registerItem mentioned above:

CommonBlocks.register("adamantium_ore", new Block(Block.Settings.copy(Blocks.STONE)));

This is the default and what you will want to use in almost all cases. This function also takes care of registering the BlockItem for you. However, in some cases, you might want to use a custom BlockItem. In these cases you can use the following function:

Block adamantium_ore = new Block(Block.Settings.copy(Blocks.STONE));
BlockItem blockItem = new BlockItem(adamantium_ore, new Item.Settings().itemGroup(Cotton.commonGroup));
CommonBlocks.register("adamantium_ore", adamantium_ore, blockItem);

Tip: If you provide your own BlockItem and want to make it easier for users to find it consider adding it to Cotton's Common tab!

2. Provide resources for it

The same thing as above, you will now need to provide resources for your new block. So add a blockstate.json, block model, item model, translation key, and a texture file to the "cotton" namespace.

Using Common Items in Recipes and Loot Tables

Use them the same way as if you were to use your own items, but use the cotton namespace instead of your own. The files should be in your own namespace tough.