feat(testing_powder): create new consumable

This commit is contained in:
alphafetus committed 2025-04-10 22:57:57 -04:00
1 parent e6df7adb20
commit 901749b3a5
10 files changed
+190 -3

No files matched your search

@@ -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!");
}
}
@@ -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();
}
@@ -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();
}
@@ -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<ItemGroup> 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");
}
}
@@ -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<Item.Settings, Item> 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<Item> 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");
}
}
@@ -0,0 +1,6 @@
{
"model": {
"type": "minecraft:model",
"model": "magic_mod:item/testing_powder"
}
}
@@ -0,0 +1,4 @@
{
"item.magic_mod.testing_powder": "Testing Powder (edible)",
"magic_mod.magic_group": "Testing Mod Items"
}
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "magic_mod:item/testing_powder"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 329 B

@@ -0,0 +1,15 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
" /",
" / ",
"/ "
],
"key": {
"/": "minecraft:stick"
},
"result": {
"id": "magic_mod:testing_powder",
"count": 1
}
}