public static final Item DIAMOND = register("diamond");public static Item register(String id) { // id: diamond
return register(keyOf(id), Item::new, new Item.Settings()); // id: diamond
// Item::new 相当于
// Function<Item.Settings, Item> func = settings -> new Item(settings);
}public static Item register(RegistryKey<Item> key, Function<Item.Settings, Item> factory, Item.Settings settings) {
Item item = (Item)factory.apply(settings.registryKey(key));
if (item instanceof BlockItem blockItem) {
blockItem.appendBlocks(Item.BLOCK_ITEMS, item);
}
return Registry.register(Registries.ITEM, key, item);
}模仿写
public static Item register(String name, Function<Item.Settings, Item> itemFactory, Item.Settings settings) {
// itemFactory:一个函数,输入是 Item.Settings,输出是一个 Item 对象
// settings:物品的设置,控制物品的属性(如堆叠大小、耐久度等)。
// String ID
identifier = Identifier.of(HachimeMainClient.MOD_ID, name);// Identifier.of(namespace, path) 创建一个 Identifier,类似 "modid:suspicious_substance"
HachimeMainClient.LOGGER.info("identifier:" + identifier); // hachime:screen_trigger
// Create the item key.( also known as the *registry key* in the source codes)
RegistryKey<Item> itemKey = RegistryKey.of(RegistryKeys.ITEM, identifier);
// Create the item instance.
Item.Settings setting = settings.registryKey(itemKey);
Item item = itemFactory.apply(setting);
// Register the item.
Registry.register(Registries.ITEM, itemKey, item);
return item;
}
class Factory implements Function<Item.Settings, Item> {
@Override
public Item apply(Item.Settings settings) {
return new ScreenTrigger(settings);
}
}
static Factory factory = new Factory();
public static final Item SCREEN_TRIGGER = register(
"screen_trigger",
factory,
new Item.Settings()
);- The string ID, usually just called “ID”, is a human-readable Identifier that uniquely identifies the registered value in a registry. This should stay the same between two game versions, and is usually used for disk storage.
- The numeric ID or raw ID is an integer assigned automatically by the registry to each registered value. This is not guaranteed to stay the same between two game versions, and is usually used for networking purposes.
- The registered value, often just called “value” in the code, is the value added to the registry. The registry’s type parameter determines the type of the registered value.
- Each registered value can also be identified with a registry key. A registry key is a combination of the registry’s ID and the registered value’s ID. Using a registry key makes the type of the ID’s associated value clear, as the type parameter contains the type.