dev/MCPitanLib/Item Addition の変更点
#author("2025-06-16T13:32:36+09:00","default:pitan","pitan")
* How to Add Items with MCPitanLib [#x39c2589]
#author("2025-06-16T13:34:35+09:00","default:pitan","pitan")
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.
**Prerequisites [#v110afc8]
- MCPitanLib is already integrated into your project.
- Your mod's main class (e.g., ''ExampleMod'') inherits from ''CommonModInitializer''.
**Steps [#ucbcb02b]
*** 1. Prepare MOD ID and Registry [#l02c5ab2]
Define the MOD ID used for identifying your mod and get the MCPitanLib registry.
#gcode(java){{
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;
}
// ...
}
}}
*** 2. Define Item ID [#a8fc76fb]
Define the ID for the item you want to add using ''CompatIdentifier''.
This will be in the format ''MOD_ID:item_name''.
#gcode(java){{
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"
// ...
}
}}
*** 3. Prepare Item Instance and Settings [#rbbefa46]
Create an instance of the item to be added.
With MCPitanLib, you typically use the ''CompatItem'' class and ''CompatibleItemSettings''.
- '''''CompatibleItemSettings.of()''''': Configures basic item settings (stack size, creative tab, etc.).
- '''''CompatItem''''': The most basic item class, which takes ''CompatibleItemSettings'' as an argument.
#gcode(java){{
// 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:'''
- ''CompatibleItemSettings.of(CompatIdentifier id)'': Passing the item ID as an argument generates an appropriate settings instance for the respective mod loader.
- ''.addGroup(ItemGroups.INGREDIENTS)'': Adds the created item to a specific tab in the creative mode inventory (in this case, the "Ingredients" tab). The ''ItemGroups'' class defines other tabs as well.
- ''() -> new CompatItem(...)'': Passes a lambda expression (Supplier) that provides the item instance. This allows for deferred initialization.
*** 4. (Optional) Create a Custom Item Class [#x60ffdd1]
If you want to create an item with more complex behavior, you can create your own class that inherits from ''CompatItem''.
#gcode(java){{
// Example: An item with special behavior on right-click
public class MyCustomItem extends CompatItem {
public MyCustomItem(CompatibleItemSettings settings) {
super(settings);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
// Action on right-click
if (!world.isClient()) {
user.sendMessage(Text.literal("Custom item was used!"), false);
}
return TypedActionResult.success(user.getStackInHand(hand));
}
}
// 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
)
);
// ...
}}
*** Summary [#nef17c9c]
Using MCPitanLib, you can easily add cross-platform compatible items with the steps above.
The key classes and methods are:
- ''CompatRegistryV2#registerItem(CompatIdentifier id, Supplier<Item> itemSupplier)''
- ''CompatIdentifier#of(String modId, String path)''
- ''CompatItem''
- ''CompatibleItemSettings#of(CompatIdentifier id)''
For more detailed settings or custom behaviors, please refer to other APIs in MCPitanLib and other parts of ''ExampleMod.java'' (e.g., ''ExampleGuiItem'')