Skip to content

Commit

Permalink
advanced item
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaupenjoe committed Aug 11, 2024
1 parent b7032c7 commit 80603a0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ModItemGroups {
.entries((displayContext, entries) -> {
entries.add(ModItems.PINK_GARNET);
entries.add(ModItems.RAW_PINK_GARNET);

entries.add(ModItems.CHISEL);
}).build());

public static final ItemGroup PINK_GARNET_BLOCKS_GROUP = Registry.register(Registries.ITEM_GROUP,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.kaupenjoe.tutorialmod.TutorialMod;
import net.kaupenjoe.tutorialmod.item.custom.ChiselItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.registry.Registries;
Expand All @@ -12,6 +13,9 @@ public class ModItems {
public static final Item PINK_GARNET = registerItem("pink_garnet", new Item(new Item.Settings()));
public static final Item RAW_PINK_GARNET = registerItem("raw_pink_garnet", new Item(new Item.Settings()));

public static final Item CHISEL = registerItem("chisel", new ChiselItem(new Item.Settings().maxDamage(32)));


private static Item registerItem(String name, Item item) {
return Registry.register(Registries.ITEM, Identifier.of(TutorialMod.MOD_ID, name), item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.kaupenjoe.tutorialmod.item.custom;

import net.kaupenjoe.tutorialmod.block.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;

import java.util.Map;

public class ChiselItem extends Item {
private static final Map<Block, Block> CHISEL_MAP =
Map.of(
Blocks.STONE, Blocks.STONE_BRICKS,
Blocks.END_STONE, Blocks.END_STONE_BRICKS,
Blocks.OAK_LOG, ModBlocks.PINK_GARNET_BLOCK,
Blocks.GOLD_BLOCK, Blocks.NETHERITE_BLOCK
);

public ChiselItem(Settings settings) {
super(settings);
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
Block clickedBlock = world.getBlockState(context.getBlockPos()).getBlock();

if(CHISEL_MAP.containsKey(clickedBlock)) {
if(!world.isClient()) {
world.setBlockState(context.getBlockPos(), CHISEL_MAP.get(clickedBlock).getDefaultState());

context.getStack().damage(1, ((ServerWorld) world), ((ServerPlayerEntity) context.getPlayer()),
item -> context.getPlayer().sendEquipmentBreakStatus(item, EquipmentSlot.MAINHAND));

world.playSound(null, context.getBlockPos(), SoundEvents.BLOCK_GRINDSTONE_USE, SoundCategory.BLOCKS);
}
}

return ActionResult.SUCCESS;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/tutorialmod/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"item.tutorialmod.pink_garnet": "Pink Garnet",
"item.tutorialmod.raw_pink_garnet": "Raw Pink Garnet",
"item.tutorialmod.chisel": "Chisel",


"block.tutorialmod.pink_garnet_block": "Block of Pink Garnet",
"block.tutorialmod.raw_pink_garnet_block": "Block of Raw Pink Garnet",
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/assets/tutorialmod/models/item/chisel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "tutorialmod:item/chisel"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 80603a0

Please sign in to comment.