Merge pull request #1 from The-Uncreative-Bunch/feature/first-crafting-block

Create new example block
This commit was merged in pull request #1.
This commit is contained in:
Lucas Vas authored and GitHub committed 2025-05-01 20:52:14 -04:00
commit d34fb7d033
14 files changed
+228 -7

No files matched your search

@@ -1,6 +1,8 @@
package com.uncreativebunch.magicmod; package com.uncreativebunch.magicmod;
import com.uncreativebunch.magicmod.block.ModBlocks;
import com.uncreativebunch.magicmod.item.ModItemGroups; import com.uncreativebunch.magicmod.item.ModItemGroups;
import com.uncreativebunch.magicmod.item.ModItems;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -24,6 +26,8 @@ public class MagicMod implements ModInitializer {
LOGGER.info("Hello Fabric world!"); LOGGER.info("Hello Fabric world!");
LOGGER.info("Starting Magic Mod..."); LOGGER.info("Starting Magic Mod...");
ModBlocks.init();
ModItems.init();
ModItemGroups.init(); ModItemGroups.init();
LOGGER.info("Finished Magic Mod initialization!"); LOGGER.info("Finished Magic Mod initialization!");
} }
@@ -0,0 +1,86 @@
package com.uncreativebunch.magicmod.block;
import com.uncreativebunch.magicmod.MagicMod;
import com.uncreativebunch.magicmod.block.crafting.MagicalCrafterBlock;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.SlabBlock;
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",
MagicalCrafterBlock::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<AbstractBlock.Settings,Block> factory,
AbstractBlock.Settings settings,
boolean shouldRegisterItem) {
RegistryKey<Block> 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<Item> itemKey = keyOfItem(name);
BlockItem blockItem = new BlockItem(block, new Item.Settings().registryKey(itemKey));
Registry.register(Registries.ITEM, itemKey, blockItem);
}
// 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<Block> 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<Item> keyOfItem(String name) {
return RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MagicMod.MOD_ID, name));
}
}
@@ -0,0 +1,47 @@
package com.uncreativebunch.magicmod.block.crafting;
import com.uncreativebunch.magicmod.MagicMod;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.SlabBlock;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class MagicalCrafterBlock extends SlabBlock {
/**
* Creates a new MagicalCrafterBlock.
* @param settings The settings of the block.
*/
public MagicalCrafterBlock(AbstractBlock.Settings settings) { super(settings); }
/**
* Occurs whenever this block is used. This is intended to open a crafting window in the future.
* @param state The current {@link BlockState} of this block.
* @param world The {@link World} that this block exists in.
* @param pos The {@link BlockPos} that this block is at.
* @param player The {@link PlayerEntity} that used this object.
* @param hit The {@link BlockHitResult} that contains information about this interaction.
* @return Whether the action passed, was canceled, or succeeded.
*/
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (world.isClient) return ActionResult.PASS;
MagicMod.LOGGER.info("Block was used at location {}", pos.toShortString());
MagicMod.LOGGER.info("Electrocuting player...");
BlockPos playerPos = player.getBlockPos();
LightningEntity lightningEntity = new LightningEntity(EntityType.LIGHTNING_BOLT, world);
lightningEntity.setPosition(playerPos.toCenterPos());
world.spawnEntity(lightningEntity);
MagicMod.LOGGER.info("Player electrocuted.");
return ActionResult.SUCCESS;
}
}
@@ -1,6 +1,7 @@
package com.uncreativebunch.magicmod.item; package com.uncreativebunch.magicmod.item;
import com.uncreativebunch.magicmod.MagicMod; 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.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
@@ -31,7 +32,7 @@ public class ModItemGroups {
*/ */
public static final ItemGroup MAGIC_GROUP = FabricItemGroup.builder() 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. .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(); .build();
@@ -42,12 +43,11 @@ public class ModItemGroups {
* custom items. * custom items.
*/ */
public static void init() { public static void init() {
ModItems.init();
Registry.register(Registries.ITEM_GROUP, MAGIC_GROUP_KEY, MAGIC_GROUP); Registry.register(Registries.ITEM_GROUP, MAGIC_GROUP_KEY, MAGIC_GROUP);
ItemGroupEvents.modifyEntriesEvent(MAGIC_GROUP_KEY) ItemGroupEvents.modifyEntriesEvent(MAGIC_GROUP_KEY)
.register(itemGroup -> { .register(itemGroup -> {
itemGroup.add(ModItems.TESTING_POWDER); itemGroup.add(ModItems.TESTING_POWDER);
itemGroup.add(ModBlocks.MAGICAL_CRAFTING_TABLE.asItem());
}); });
MagicMod.LOGGER.info("Item group initialization complete"); MagicMod.LOGGER.info("Item group initialization complete");
@@ -22,17 +22,18 @@ public class ModItems {
.food(ModFoodComponents.TESTING_POWDER_COMPONENT, ModConsumableComponents.TESTING_POWDER_NIGHTVIS) .food(ModFoodComponents.TESTING_POWDER_COMPONENT, ModConsumableComponents.TESTING_POWDER_NIGHTVIS)
); );
public static RegistryKey<Item> keyOf(String id) {
return RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MagicMod.MOD_ID, id));
}
public static Item register(String name, @NotNull Function<Item.Settings, Item> factory, Item.Settings settings) { 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's registry key (get it from the ITEM registry)
// Create the item with the specified settings and give it the correct registry key // 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) // 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)); RegistryKey<Item> itemKey = keyOf(name);
Item item = factory.apply(settings.registryKey(itemKey)); Item item = factory.apply(settings.registryKey(itemKey));
Registry.register(Registries.ITEM, itemKey, item); return Registry.register(Registries.ITEM, itemKey, item);
return item;
} }
/** /**
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "magic_mod:block/magical_crafting_table"
}
}
}
@@ -0,0 +1,6 @@
{
"model": {
"type": "minecraft:model",
"model": "magic_mod:block/magical_crafting_table"
}
}
@@ -1,4 +1,5 @@
{ {
"block.magic_mod.magical_crafting_table": "Magic Crafter",
"item.magic_mod.testing_powder": "Testing Powder (edible)", "item.magic_mod.testing_powder": "Testing Powder (edible)",
"magic_mod.magic_group": "Testing Mod Items" "magic_mod.magic_group": "Testing Mod Items"
} }
@@ -0,0 +1,22 @@
{
"credit": "Made with Blockbench",
"texture_size": [32, 32],
"textures": {
"0": "magic_mod:block/magical_crafting_table",
"particle": "magic_mod:block/magical_crafting_table"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 8, 16],
"faces": {
"north": {"uv": [8, 0, 16, 4], "texture": "#0"},
"east": {"uv": [8, 4, 16, 8], "texture": "#0"},
"south": {"uv": [8, 8, 16, 12], "texture": "#0"},
"west": {"uv": [8, 12, 16, 16], "texture": "#0"},
"up": {"uv": [8, 8, 0, 0], "texture": "#0"},
"down": {"uv": [8, 8, 0, 16], "texture": "#0"}
}
}
]
}
@@ -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"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 277 B

@@ -0,0 +1,19 @@
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "magic_mod:testing_powder"
}
],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
]
}
]
}
@@ -0,0 +1,16 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"$%$",
"###"
],
"key": {
"#": "minecraft:obsidian",
"$": "minecraft:amethyst_shard",
"%": "minecraft:book"
},
"result": {
"id": "magic_mod:magical_crafting_table",
"count": 1
}
}