This guide explains the basic steps to add a new item to Minecraft using MCPitanLib.
It references the implementation in ExampleMod.java as an example.
Define the MOD ID used for identifying your mod and get the MCPitanLib registry.
public class ExampleMod extends CommonModInitializer {
public static final String MOD_ID = "examplemod"; // Replace with your MOD ID
public static CompatRegistryV2 registry;
@Override
public void init() {
registry = super.registry; // Get the registry from CommonModInitializer
// ... other initialization processes ...
}
// Helper method to easily create an Identifier using the MOD ID
public static CompatIdentifier _id(String path) {
return CompatIdentifier.of(MOD_ID, path);
}
@Override
public String getId() {
return MOD_ID;
}
// ...
}
Define the ID for the item you want to add using CompatIdentifier.
This will be in the format MOD_ID:item_name.
public class ExampleMod extends CommonModInitializer {
// ... (code from above) ...
// Define the Item ID
public static CompatIdentifier EXAMPLE_ITEM_ID = _id("example_item"); // Becomes "examplemod:example_item"
// ...
}
Create an instance of the item to be added.
With MCPitanLib, you typically use the CompatItem class and CompatibleItemSettings.
// Defined within ExampleMod.java, either in the init method or as a static field for item registration
// Variable to hold the registration result of the item (RegistryResult type)
public static RegistryResult<Item> EXAMPLE_ITEM;
// Register the item within the init method
@Override
public void init() {
// ...
registry = super.registry;
// ...
// Register the item
EXAMPLE_ITEM = registry.registerItem(EXAMPLE_ITEM_ID, () ->
new CompatItem(CompatibleItemSettings.of(EXAMPLE_ITEM_ID) // Pass the settings
// .maxCount(16) // For example, if you want to set max stack size to 16
.addGroup(ItemGroups.INGREDIENTS) // Add to the "Ingredients" creative tab
)
);
// ...
}
Points:
If you want to create an item with more complex behavior, you can create your own class that inherits from CompatItem.
// Example: An item with special behavior on right-click
public class MyCustomItem extends CompatItem {
public MyCustomItem(CompatibleItemSettings settings) {
super(settings);
}
@Override
public public StackActionResult onRightClick(ItemUseEvent e) {
// Action on right-click
if (!e.isClient())
e.player.sendMessage(TextUtil.literal("Custom item was used!"));
return e.success();
}
}
// Registration in ExampleMod.java
// ...
public static RegistryResult<Item> MY_CUSTOM_ITEM;
public static CompatIdentifier MY_CUSTOM_ITEM_ID = _id("my_custom_item");
// ...
// Inside the init method
MY_CUSTOM_ITEM = registry.registerItem(MY_CUSTOM_ITEM_ID, () ->
new MyCustomItem(CompatibleItemSettings.of(MY_CUSTOM_ITEM_ID)
.addGroup(ItemGroups.TOOLS) // Add to the "Tools" tab
)
);
// ...
Using MCPitanLib, you can easily add multi versions compatible items with the steps above.
The key classes and methods are:
For more detailed settings or custom behaviors, please refer to other APIs in MCPitanLib and other parts of ExampleMod.java (e.g., ExampleGuiItem)