diff --git a/src/main/java/com/uncreativebunch/magicmod/MagicMod.java b/src/main/java/com/uncreativebunch/magicmod/MagicMod.java index e17cc78..d6bcee0 100644 --- a/src/main/java/com/uncreativebunch/magicmod/MagicMod.java +++ b/src/main/java/com/uncreativebunch/magicmod/MagicMod.java @@ -1,5 +1,6 @@ package com.uncreativebunch.magicmod; +import com.uncreativebunch.magicmod.item.ModItemGroups; import net.fabricmc.api.ModInitializer; import org.slf4j.Logger; @@ -8,9 +9,11 @@ import org.slf4j.LoggerFactory; public class MagicMod implements ModInitializer { public static final String MOD_ID = "magic_mod"; - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. + /** + * This logger is used to write text to the console and the log file. + * It is considered best practice to use your mod id as the logger's name. + * That way, it's clear which mod wrote info, warnings, and errors. + */ public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); @Override @@ -20,5 +23,8 @@ public class MagicMod implements ModInitializer { // Proceed with mild caution. LOGGER.info("Hello Fabric world!"); + LOGGER.info("Starting Magic Mod..."); + ModItemGroups.init(); + LOGGER.info("Finished Magic Mod initialization!"); } } \ No newline at end of file diff --git a/src/main/java/com/uncreativebunch/magicmod/component/ModConsumableComponents.java b/src/main/java/com/uncreativebunch/magicmod/component/ModConsumableComponents.java new file mode 100644 index 0000000..cdb6697 --- /dev/null +++ b/src/main/java/com/uncreativebunch/magicmod/component/ModConsumableComponents.java @@ -0,0 +1,27 @@ +package com.uncreativebunch.magicmod.component; + +import net.minecraft.component.type.ConsumableComponent; +import net.minecraft.component.type.ConsumableComponents; +import net.minecraft.entity.effect.StatusEffectInstance; +import net.minecraft.entity.effect.StatusEffects; +import net.minecraft.item.consume.ApplyEffectsConsumeEffect; + +/** + * Contains the {@link ConsumableComponent} objects that are a part of this mod. + * These are what gives the player side effects as a result of eating or drinking. + */ +public class ModConsumableComponents { + /** If a consumable item has this component, it is guaranteed to give the player night vision for 60 seconds. */ + public static ConsumableComponent TESTING_POWDER_NIGHTVIS = + ConsumableComponents + .food() // This is for a food item, not a drink. + .consumeEffect(new ApplyEffectsConsumeEffect( + new StatusEffectInstance( + StatusEffects.NIGHT_VISION, // the effect to apply. + 60 * 20, // There are 20 ticks per second - we want 60 seconds of night vis. + 1 // The intensity - could be strength 2, haste 3, etc. + ), + 1 // The probability that the consumer gets the effect. + )) + .build(); +} diff --git a/src/main/java/com/uncreativebunch/magicmod/component/ModFoodComponents.java b/src/main/java/com/uncreativebunch/magicmod/component/ModFoodComponents.java new file mode 100644 index 0000000..e1cde66 --- /dev/null +++ b/src/main/java/com/uncreativebunch/magicmod/component/ModFoodComponents.java @@ -0,0 +1,22 @@ +package com.uncreativebunch.magicmod.component; + +import net.minecraft.component.type.FoodComponent; + +/** + * Contains all the {@link FoodComponent}s that are used within this mod. + * + * Food items don't need to have their own specific component - these components can + * be reused. + */ +public class ModFoodComponents { + /** + * Food component currently only used with the TESTING_POWDER item. + */ + public static FoodComponent TESTING_POWDER_COMPONENT = + new FoodComponent + .Builder() + .alwaysEdible() // Players can eat this regardless of hunger levels + .saturationModifier(0.6f) // Sets how "long" hunger lasts before it starts decreasing again. + .nutrition(2) // Sets the number of hunger points given. + .build(); +} diff --git a/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java b/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java new file mode 100644 index 0000000..91d1885 --- /dev/null +++ b/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java @@ -0,0 +1,55 @@ +package com.uncreativebunch.magicmod.item; + +import com.uncreativebunch.magicmod.MagicMod; +import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; +import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.minecraft.item.ItemGroup; +import net.minecraft.item.ItemStack; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +/** + * Contains all the {@link ItemGroup} objects in this mod. + * These are what group the items added to the mod within the inventory interface. + */ +public class ModItemGroups { + /** + * The registry key for the MAGIC_GROUP. + */ + public static final RegistryKey MAGIC_GROUP_KEY = + RegistryKey.of( + Registries.ITEM_GROUP.getKey(), // We want an ItemGroup key, not a key for an Item, Block, etc. + Identifier.of(MagicMod.MOD_ID, "magic") // This just says we're making it for our mod and gives + // a specific id to our group. + ); + + /** + * The item group that contains all magical items. + */ + public static final ItemGroup MAGIC_GROUP = FabricItemGroup.builder() + .icon(() -> new ItemStack(ModItems.TESTING_POWDER)) // The item to show as the group's icon. Can be anything. + .displayName(Text.translatable("magic-mod.magic_group")) // Gets the name from the translation file in the mod resources. + .build(); + + + /** + * Handles first-time initialization. + * Must be called in order for all ItemGroups to be usable in game. + * This will also initialize the {@link ModItems} class in order to register all + * custom items. + */ + public static void init() { + ModItems.init(); + + Registry.register(Registries.ITEM_GROUP, MAGIC_GROUP_KEY, MAGIC_GROUP); + ItemGroupEvents.modifyEntriesEvent(MAGIC_GROUP_KEY) + .register(itemGroup -> { + itemGroup.add(ModItems.TESTING_POWDER); + }); + + MagicMod.LOGGER.info("Item group initialization complete"); + } +} diff --git a/src/main/java/com/uncreativebunch/magicmod/item/ModItems.java b/src/main/java/com/uncreativebunch/magicmod/item/ModItems.java new file mode 100644 index 0000000..441843a --- /dev/null +++ b/src/main/java/com/uncreativebunch/magicmod/item/ModItems.java @@ -0,0 +1,46 @@ +package com.uncreativebunch.magicmod.item; + +import com.uncreativebunch.magicmod.MagicMod; +import com.uncreativebunch.magicmod.component.ModConsumableComponents; +import com.uncreativebunch.magicmod.component.ModFoodComponents; +import net.minecraft.item.Item; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.Registries; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Function; + +public class ModItems { + + public static final Item TESTING_POWDER = register( + "testing_powder", + Item::new, + new Item.Settings() + .food(ModFoodComponents.TESTING_POWDER_COMPONENT, ModConsumableComponents.TESTING_POWDER_NIGHTVIS) + ); + + + public static Item register(String name, @NotNull Function factory, Item.Settings settings) { + // Create the item's registry key (get it from the ITEM registry) + // Create the item with the specified settings and give it the correct registry key + // Register the item with the registry (use the key we found and associate it with the new item) + + RegistryKey itemKey = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MagicMod.MOD_ID, name)); + Item item = factory.apply(settings.registryKey(itemKey)); + Registry.register(Registries.ITEM, itemKey, item); + + return item; + } + + /** + * Handles first-time initialization. + * Must be called in order for all the item instances to be registered + * and used within the game. + */ + public static void init() { + MagicMod.LOGGER.info("Modded item initialization complete"); + } +} diff --git a/src/main/resources/assets/magic_mod/items/testing_powder.json b/src/main/resources/assets/magic_mod/items/testing_powder.json new file mode 100644 index 0000000..c61066b --- /dev/null +++ b/src/main/resources/assets/magic_mod/items/testing_powder.json @@ -0,0 +1,6 @@ +{ + "model": { + "type": "minecraft:model", + "model": "magic_mod:item/testing_powder" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/magic_mod/lang/en_us.json b/src/main/resources/assets/magic_mod/lang/en_us.json new file mode 100644 index 0000000..bd9c592 --- /dev/null +++ b/src/main/resources/assets/magic_mod/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "item.magic_mod.testing_powder": "Testing Powder (edible)", + "magic_mod.magic_group": "Testing Mod Items" +} \ No newline at end of file diff --git a/src/main/resources/assets/magic_mod/models/item/testing_powder.json b/src/main/resources/assets/magic_mod/models/item/testing_powder.json new file mode 100644 index 0000000..b31b423 --- /dev/null +++ b/src/main/resources/assets/magic_mod/models/item/testing_powder.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/generated", + "textures": { + "layer0": "magic_mod:item/testing_powder" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/magic_mod/textures/item/testing_powder.png b/src/main/resources/assets/magic_mod/textures/item/testing_powder.png new file mode 100644 index 0000000..9575ebb Binary files /dev/null and b/src/main/resources/assets/magic_mod/textures/item/testing_powder.png differ diff --git a/src/main/resources/data/magic-mod/recipe/testing_powder.json b/src/main/resources/data/magic-mod/recipe/testing_powder.json new file mode 100644 index 0000000..4f3e59c --- /dev/null +++ b/src/main/resources/data/magic-mod/recipe/testing_powder.json @@ -0,0 +1,15 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + " /", + " / ", + "/ " + ], + "key": { + "/": "minecraft:stick" + }, + "result": { + "id": "magic_mod:testing_powder", + "count": 1 + } +} \ No newline at end of file