diff --git a/src/main/java/com/uncreativebunch/magicmod/MagicMod.java b/src/main/java/com/uncreativebunch/magicmod/MagicMod.java index d6bcee0..7eb74e2 100644 --- a/src/main/java/com/uncreativebunch/magicmod/MagicMod.java +++ b/src/main/java/com/uncreativebunch/magicmod/MagicMod.java @@ -1,6 +1,8 @@ package com.uncreativebunch.magicmod; +import com.uncreativebunch.magicmod.block.ModBlocks; import com.uncreativebunch.magicmod.item.ModItemGroups; +import com.uncreativebunch.magicmod.item.ModItems; import net.fabricmc.api.ModInitializer; import org.slf4j.Logger; @@ -24,6 +26,8 @@ public class MagicMod implements ModInitializer { LOGGER.info("Hello Fabric world!"); LOGGER.info("Starting Magic Mod..."); + ModBlocks.init(); + ModItems.init(); ModItemGroups.init(); LOGGER.info("Finished Magic Mod initialization!"); } diff --git a/src/main/java/com/uncreativebunch/magicmod/block/ModBlocks.java b/src/main/java/com/uncreativebunch/magicmod/block/ModBlocks.java new file mode 100644 index 0000000..d221ae9 --- /dev/null +++ b/src/main/java/com/uncreativebunch/magicmod/block/ModBlocks.java @@ -0,0 +1,84 @@ +package com.uncreativebunch.magicmod.block; + +import com.uncreativebunch.magicmod.MagicMod; +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.sound.BlockSoundGroup; +import net.minecraft.util.Identifier; + +import java.util.function.Function; + +public class ModBlocks { + //region Block Definitions + + public static final Block MAGICAL_CRAFTING_TABLE = register( + "magical_crafting_table", + Block::new, + AbstractBlock.Settings + .create() + .sounds(BlockSoundGroup.AMETHYST_BLOCK), + true + ); + + //endregion + + /** + * Used to statically initialize this class. Nothing needs to be in it for now. + */ + public static void init() { + MagicMod.LOGGER.info("Block initialization complete"); + } + + /** + * Registers a new {@link Block}. + * @param name The name of the {@link Block} to register. + * @param factory The block factory to use - typically Block::new + * @param settings The {@link AbstractBlock.Settings} to use for creation. + * @param shouldRegisterItem Determines if an item should be created with the block, for use in the inventory. + * @return The newly registered {@link Block}. + */ + public static Block register( + String name, + Function factory, + AbstractBlock.Settings settings, + boolean shouldRegisterItem) { + + RegistryKey blockKey = keyOfBlock(name); + Block block = factory.apply(settings.registryKey(blockKey)); + + // We don't always want to create items for blocks - sometimes the block is in an "active" state, + // which we don't want to create an item instance for, just the block. + if (shouldRegisterItem) { + RegistryKey itemKey = keyOfItem(name); + BlockItem blockItem = new BlockItem(block, new Item.Settings().registryKey(itemKey)); + Registry.register(Registries.ITEM, itemKey, blockItem); + } + + // Finally return the block we want to register. + return Registry.register(Registries.BLOCK, blockKey, block); + } + + /** + * Gets a {@link RegistryKey} for the specified block name. + * @param name The name of the {@link Block} to use. + * @return The {@link RegistryKey} for the specified {@link Block}. + */ + public static RegistryKey keyOfBlock(String name) { + return RegistryKey.of(RegistryKeys.BLOCK, Identifier.of(MagicMod.MOD_ID, name)); + } + + /** + * Gets a {@link RegistryKey} for the specified item name. + * @param name The name of the {@link Item} to use. + * @return The {@link RegistryKey} of the specified {@link Item}. + */ + public static RegistryKey keyOfItem(String name) { + return RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MagicMod.MOD_ID, name)); + } +} diff --git a/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java b/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java index 91d1885..d587622 100644 --- a/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java +++ b/src/main/java/com/uncreativebunch/magicmod/item/ModItemGroups.java @@ -1,6 +1,7 @@ package com.uncreativebunch.magicmod.item; import com.uncreativebunch.magicmod.MagicMod; +import com.uncreativebunch.magicmod.block.ModBlocks; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; import net.minecraft.item.ItemGroup; @@ -31,7 +32,7 @@ public class ModItemGroups { */ 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. + .displayName(Text.translatable("magic_mod.magic_group")) // Gets the name from the translation file in the mod resources. .build(); @@ -42,12 +43,11 @@ public class ModItemGroups { * 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); + itemGroup.add(ModBlocks.MAGICAL_CRAFTING_TABLE.asItem()); }); 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 index 441843a..34859f0 100644 --- a/src/main/java/com/uncreativebunch/magicmod/item/ModItems.java +++ b/src/main/java/com/uncreativebunch/magicmod/item/ModItems.java @@ -22,17 +22,18 @@ public class ModItems { .food(ModFoodComponents.TESTING_POWDER_COMPONENT, ModConsumableComponents.TESTING_POWDER_NIGHTVIS) ); + public static RegistryKey keyOf(String id) { + return RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MagicMod.MOD_ID, id)); + } 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)); + RegistryKey itemKey = keyOf(name); Item item = factory.apply(settings.registryKey(itemKey)); - Registry.register(Registries.ITEM, itemKey, item); - - return item; + return Registry.register(Registries.ITEM, itemKey, item); } /** diff --git a/src/main/resources/assets/magic_mod/blockstates/magical_crafting_table.json b/src/main/resources/assets/magic_mod/blockstates/magical_crafting_table.json new file mode 100644 index 0000000..3e5ac3f --- /dev/null +++ b/src/main/resources/assets/magic_mod/blockstates/magical_crafting_table.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "magic_mod:block/magical_crafting_table" + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/magic_mod/items/magical_crafting_table.json b/src/main/resources/assets/magic_mod/items/magical_crafting_table.json new file mode 100644 index 0000000..b50e749 --- /dev/null +++ b/src/main/resources/assets/magic_mod/items/magical_crafting_table.json @@ -0,0 +1,6 @@ +{ + "model": { + "type": "minecraft:model", + "model": "magic_mod:block/magical_crafting_table" + } +} \ 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 index bd9c592..54f7066 100644 --- a/src/main/resources/assets/magic_mod/lang/en_us.json +++ b/src/main/resources/assets/magic_mod/lang/en_us.json @@ -1,4 +1,5 @@ { + "block.magic_mod.magical_crafting_table": "Magic Crafter", "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/block/magical_crafting_table.json b/src/main/resources/assets/magic_mod/models/block/magical_crafting_table.json new file mode 100644 index 0000000..329901b --- /dev/null +++ b/src/main/resources/assets/magic_mod/models/block/magical_crafting_table.json @@ -0,0 +1,12 @@ +{ + "parent": "minecraft:block/cube", + "textures": { + "up": "minecraft:block/ancient_debris_top", + "down": "minecraft:block/bamboo_block", + "north": "minecraft:block/cobblestone", + "east": "minecraft:block/bedrock", + "south": "minecraft:block/birch_leaves", + "west": "minecraft:block/creaking_heart", + "particle": "minecraft:block/creaking_heart" + } +} \ No newline at end of file diff --git a/src/main/resources/data/magic-mod/recipe/testing_powder.json b/src/main/resources/data/magic-mod/recipe/testing_powder.json deleted file mode 100644 index 4f3e59c..0000000 --- a/src/main/resources/data/magic-mod/recipe/testing_powder.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "type": "minecraft:crafting_shaped", - "pattern": [ - " /", - " / ", - "/ " - ], - "key": { - "/": "minecraft:stick" - }, - "result": { - "id": "magic_mod:testing_powder", - "count": 1 - } -} \ No newline at end of file diff --git a/src/main/resources/data/magic_mod/loot_table/blocks/magical_crafting_table.json b/src/main/resources/data/magic_mod/loot_table/blocks/magical_crafting_table.json new file mode 100644 index 0000000..fcbe76b --- /dev/null +++ b/src/main/resources/data/magic_mod/loot_table/blocks/magical_crafting_table.json @@ -0,0 +1,19 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "magic_mod:testing_powder" + } + ], + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ] + } + ] +} \ No newline at end of file