Skip to content

Commit 2bd2d95

Browse files
Rename GenericItem to T (FabricMC#510)
Co-authored-by: Miroma <its.miroma@proton.me>
1 parent 9400e5d commit 2bd2d95

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

develop/items/first-item.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Mojang does this with their items as well! Check out the `Items` class for inspi
2424

2525
@[code transcludeWith=:::1](@/reference/latest/src/main/java/com/example/docs/item/ModItems.java)
2626

27-
Notice how we're using a `GenericItem`, which allows us to use the same method `register` for registering any type of item that extends `Item`. We're also using a [`Function`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/function/Function.html) interface for the factory, which allows us to specify how we want our item to be created given the item properties.
27+
Notice how we're using `T`, a [generic type](https://docs.oracle.com/javase/tutorial/java/generics/types.html) that extends `Item`. This allows us to use the same method `register` for registering any type of item that extends `Item`. We're also using a [`Function`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/function/Function.html) for the factory, which allows us to specify how we want our item to be created given the item properties.
2828

2929
## Registering an Item {#registering-an-item}
3030

reference/1.21.10/src/main/java/com/example/docs/item/ModItems.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ public InteractionResult use(Level world, Player user, InteractionHand hand) {
160160
public static final Item THROWING_KNIVES = register("throwing_knives", Item::new, new Item.Properties().stacksTo(3));
161161

162162
// :::1
163-
public static <GenericItem extends Item> GenericItem register(String name, Function<Item.Properties, GenericItem> itemFactory, Item.Properties settings) {
163+
public static <T extends Item> T register(String name, Function<Item.Properties, T> itemFactory, Item.Properties settings) {
164164
// Create the item key.
165165
ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, name));
166166

167167
// Create the item instance.
168-
GenericItem item = itemFactory.apply(settings.setId(itemKey));
168+
T item = itemFactory.apply(settings.setId(itemKey));
169169

170170
// Register the item.
171171
Registry.register(BuiltInRegistries.ITEM, itemKey, item);

reference/latest/src/main/java/com/example/docs/item/ModItems.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ public InteractionResult use(Level level, Player user, InteractionHand hand) {
198198
public static final Item THROWING_KNIVES = register("throwing_knives", Item::new, new Item.Properties().stacksTo(3));
199199

200200
// :::1
201-
public static <GenericItem extends Item> GenericItem register(String name, Function<Item.Properties, GenericItem> itemFactory, Item.Properties settings) {
201+
public static <T extends Item> T register(String name, Function<Item.Properties, T> itemFactory, Item.Properties settings) {
202202
// Create the item key.
203203
ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, name));
204204

205205
// Create the item instance.
206-
GenericItem item = itemFactory.apply(settings.setId(itemKey));
206+
T item = itemFactory.apply(settings.setId(itemKey));
207207

208208
// Register the item.
209209
Registry.register(BuiltInRegistries.ITEM, itemKey, item);

0 commit comments

Comments
 (0)