Add metal detector item from youtube guide.

This commit is contained in:
kelson8 2024-03-24 03:05:26 -04:00
parent 906334f998
commit 77aacef8cb
Signed by: kelson8
GPG Key ID: 3D738D9DD7239E13
7 changed files with 83 additions and 1 deletions

View File

@ -1,3 +1,6 @@
# KelsonCraftFabricTest
This repo will contain a test mod that I am working on for Minecraft fabric, mostly just learning the basics and playing around with it.
This repo will contain a test mod that I am working on for Minecraft fabric, mostly just learning the basics and playing around with it.
I have added the metal detector item from this fabric guide:
https://www.youtube.com/watch?v=gEYUeu_wraQ&list=PLKGarocXCE1EO43Dlf5JGh7Yk-kRAXUEJ

View File

@ -22,6 +22,8 @@ public class CustomItemGroups {
entries.add(CustomItems.RUBY);
entries.add(CustomItems.RAW_RUBY);
entries.add(CustomItems.CUSTOM_ITEM);
// Metal detector
entries.add(CustomItems.METAL_DETECTOR);
// Custom blocks
entries.add(CustomBlocks.RUBY_BLOCK);

View File

@ -26,6 +26,9 @@ public class CustomItems extends Item {
public static final Item RAW_RUBY = registerItem("raw_ruby", new Item(new FabricItemSettings()));
public static final Item CUSTOM_ITEM = registerItem("custom_item", new Item(new FabricItemSettings()));
// Metal detector, I forgot to add maxDamage(64), that is what makes the item break after 64 uses.
public static final Item METAL_DETECTOR = registerItem("metal_detector", new MetalDetectorItem(new FabricItemSettings().maxDamage(64)));
private static void addItemsToIngredientItemGroup(FabricItemGroupEntries entries){
entries.add(RUBY);
entries.add(RAW_RUBY);

View File

@ -0,0 +1,67 @@
package net.kelsoncraft.test.item;
import net.kelsoncraft.test.util.Messages;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
// https://www.youtube.com/watch?v=gEYUeu_wraQ&list=PLKGarocXCE1EO43Dlf5JGh7Yk-kRAXUEJ&index=6
public class MetalDetectorItem extends Item {
public MetalDetectorItem(Settings settings) {
super(settings);
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if(!context.getWorld().isClient()) {
BlockPos positionClicked = context.getBlockPos();
PlayerEntity player = context.getPlayer();
boolean foundBlock = false;
for (int i = 0; i <= positionClicked.getY() + 64; i++) {
BlockState state = context.getWorld().getBlockState(positionClicked.down(i));
// Show if a block is valuable
if(isValuableBlock(state)) {
outputValuableCoordinates(positionClicked.down(), player, state.getBlock());
foundBlock = true;
break;
}
}
if(!foundBlock) {
player.sendMessage(Text.literal(Messages.Kbp_Fabric() + "No Valuables found!"));
}
}
// Send the player a message.
//context.getPlayer().sendMessage();
// https://youtu.be/gEYUeu_wraQ?list=PLKGarocXCE1EO43Dlf5JGh7Yk-kRAXUEJ&t=364
// This seems to damage the item by 1, hmm I'm going to change this to 20 to see if it gets damaged quicker.
// Yes it does change the damage to the item.
context.getStack().damage(1, context.getPlayer(),
playerEntity -> playerEntity.sendToolBreakStatus(playerEntity.getActiveHand()));
return ActionResult.SUCCESS;
}
private void outputValuableCoordinates(BlockPos blockPos, PlayerEntity player, Block block) {
player.sendMessage(Text.literal(Messages.Kbp_Fabric() + "Found " + block.asItem().getName().getString() +
" at " + "(" + blockPos.getX() + ", " + blockPos.getY() + ", " + blockPos.getZ() + ")"), false);
}
private boolean isValuableBlock(BlockState state) {
return state.isOf(Blocks.IRON_ORE) || state.isOf(Blocks.DIAMOND_ORE);
}
}

View File

@ -2,6 +2,7 @@
"item.kelsoncraft-test.ruby": "Ruby",
"item.kelsoncraft-test.raw_ruby": "Raw Ruby",
"item.kelsoncraft-test.custom_item": "Custom item",
"item.kelsoncraft-test.metal_detector": "Metal Detector",
"block.kelsoncraft-test.ruby_block": "Ruby Block",
"block.kelsoncraft-test.raw_ruby_block": "Raw Ruby Block",

View File

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "kelsoncraft-test:item/metal_detector"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 B