From 77aacef8cbd29c5c2716c29a53bfa88ab6c09890 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Sun, 24 Mar 2024 03:05:26 -0400 Subject: [PATCH] Add metal detector item from youtube guide. --- README.md | 5 +- .../test/item/CustomItemGroups.java | 2 + .../kelsoncraft/test/item/CustomItems.java | 3 + .../test/item/MetalDetectorItem.java | 67 ++++++++++++++++++ .../assets/kelsoncraft-test/lang/en_us.json | 1 + .../models/item/metal_detector.json | 6 ++ .../textures/item/metal_detector.png | Bin 0 -> 314 bytes 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/kelsoncraft/test/item/MetalDetectorItem.java create mode 100644 src/main/resources/assets/kelsoncraft-test/models/item/metal_detector.json create mode 100644 src/main/resources/assets/kelsoncraft-test/textures/item/metal_detector.png diff --git a/README.md b/README.md index 492cc63..5041a15 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +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 \ No newline at end of file diff --git a/src/main/java/net/kelsoncraft/test/item/CustomItemGroups.java b/src/main/java/net/kelsoncraft/test/item/CustomItemGroups.java index 117a430..bc58431 100644 --- a/src/main/java/net/kelsoncraft/test/item/CustomItemGroups.java +++ b/src/main/java/net/kelsoncraft/test/item/CustomItemGroups.java @@ -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); diff --git a/src/main/java/net/kelsoncraft/test/item/CustomItems.java b/src/main/java/net/kelsoncraft/test/item/CustomItems.java index 1bfd907..1259163 100644 --- a/src/main/java/net/kelsoncraft/test/item/CustomItems.java +++ b/src/main/java/net/kelsoncraft/test/item/CustomItems.java @@ -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); diff --git a/src/main/java/net/kelsoncraft/test/item/MetalDetectorItem.java b/src/main/java/net/kelsoncraft/test/item/MetalDetectorItem.java new file mode 100644 index 0000000..4d56ca1 --- /dev/null +++ b/src/main/java/net/kelsoncraft/test/item/MetalDetectorItem.java @@ -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); + } +} diff --git a/src/main/resources/assets/kelsoncraft-test/lang/en_us.json b/src/main/resources/assets/kelsoncraft-test/lang/en_us.json index 07f6cdc..2489b00 100644 --- a/src/main/resources/assets/kelsoncraft-test/lang/en_us.json +++ b/src/main/resources/assets/kelsoncraft-test/lang/en_us.json @@ -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", diff --git a/src/main/resources/assets/kelsoncraft-test/models/item/metal_detector.json b/src/main/resources/assets/kelsoncraft-test/models/item/metal_detector.json new file mode 100644 index 0000000..2e3ec44 --- /dev/null +++ b/src/main/resources/assets/kelsoncraft-test/models/item/metal_detector.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "kelsoncraft-test:item/metal_detector" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/kelsoncraft-test/textures/item/metal_detector.png b/src/main/resources/assets/kelsoncraft-test/textures/item/metal_detector.png new file mode 100644 index 0000000000000000000000000000000000000000..6bf27812321799665b95572b5d35e7a1da113b54 GIT binary patch literal 314 zcmV-A0mc4_P)Px#^hrcPR5*>*ku6WdP!xur@*$SGktVmIom}NG)f30`m&~D2^$S0Ozc7cXpcVrp z6?KGV$!ahpWC`Y=?oF3$-JRrB_nh44Jzwx|RpgNZT$~Ho_xyHDmWJwOV1d5L$94xm zFa(N-UVpzcF(aPezL?K$`0RErZr4A@c@a@n)wXRlOG5?7(omO+sf)efx~|n2;{YER z5jhBZsmLQ;E~aXXaj=LSHQBpDkw-c)BOaeNlw}ztNiw+PJp&QZ*YzcDcz(EtDd literal 0 HcmV?d00001