Skip to content

Commit

Permalink
video 6 done (advanced item)
Browse files Browse the repository at this point in the history
  • Loading branch information
natanmaia95 committed Aug 25, 2024
1 parent b51e85a commit ccf97be
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ModCreativeModeTabs {
output.accept(ModItems.MALACHITE_CHUNK);
output.accept(ModItems.MALACHITE_INGOT);
output.accept(ModItems.NULBERRY);
output.accept(ModItems.CHISEL);
})
.backgroundTexture(ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, "textures/block/earth_crystal_ore.png"))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import com.nateplays.my_neoforge_mod.block.ModBlocks;
import com.nateplays.my_neoforge_mod.item.custom.ChiselItem;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemNameBlockItem;
Expand All @@ -24,7 +25,8 @@ public class ModItems {
public static final DeferredItem<Item> NULBERRY = ITEMS.register("nulberry",
() -> new NulberryItem(new Item.Properties()));


public static final DeferredItem<Item> CHISEL = ITEMS.register("chisel",
() -> new ChiselItem(new Item.Properties().durability(32)));

public static void register(IEventBus eventBus) {
ITEMS.register(eventBus);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.nateplays.my_neoforge_mod.item.custom;

import com.nateplays.my_neoforge_mod.block.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

import java.util.Map;

public class ChiselItem extends Item {

private static final Map<Block, Block> CHISEL_MAP = Map.of(
Blocks.STONE, Blocks.STONE_BRICKS,
ModBlocks.MACHALITE_ORE.get(), ModBlocks.MACHALITE_BLOCK.get()
);

public ChiselItem(Properties properties) {
super(properties);
}

@Override
public InteractionResult useOn(UseOnContext context) {
Level level = context.getLevel();
Block clickedBlock = level.getBlockState(context.getClickedPos()).getBlock();
BlockPos clickedPos = context.getClickedPos();

if (CHISEL_MAP.containsKey(clickedBlock)) {
if (!level.isClientSide()) { // only change things on server
level.setBlockAndUpdate(clickedPos, CHISEL_MAP.get(clickedBlock).defaultBlockState());

if (context.getPlayer() != null && !context.getPlayer().isCreative()) {
context.getItemInHand().hurtAndBreak(1, ((ServerLevel) level), context.getPlayer(),
item -> context.getPlayer().onEquippedItemBroken(item, EquipmentSlot.MAINHAND));
}

level.playSound(null, clickedPos, SoundEvents.GRINDSTONE_USE, SoundSource.BLOCKS);
}

level.addParticle(ParticleTypes.CHERRY_LEAVES, clickedPos.getX(), clickedPos.getY(), clickedPos.getZ(),
0.0, 0.0, 0.0);
}
return InteractionResult.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "my_neoforge_mod:item/chisel"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit ccf97be

Please sign in to comment.