Added Metal Detector
This commit is contained in:
parent
228981be16
commit
cda9741cc8
6 changed files with 80 additions and 0 deletions
|
@ -23,6 +23,7 @@ public class ModCreativeTabs {
|
||||||
.title(Component.translatable("creativetab.tutorial_tab")).displayItems((pParams, pOut) -> {
|
.title(Component.translatable("creativetab.tutorial_tab")).displayItems((pParams, pOut) -> {
|
||||||
pOut.accept(ModItems.SAPPHIRE.get());
|
pOut.accept(ModItems.SAPPHIRE.get());
|
||||||
pOut.accept(ModItems.RAW_SAPPHIRE.get());
|
pOut.accept(ModItems.RAW_SAPPHIRE.get());
|
||||||
|
pOut.accept(ModItems.METAL_DETECTOR.get());
|
||||||
pOut.accept(ModBlocks.SAPPHIRE_BLOCK.get());
|
pOut.accept(ModBlocks.SAPPHIRE_BLOCK.get());
|
||||||
pOut.accept(ModBlocks.RAW_SAPPHIRE_BLOCK.get());
|
pOut.accept(ModBlocks.RAW_SAPPHIRE_BLOCK.get());
|
||||||
pOut.accept(ModBlocks.SAPPHIRE_ORE.get());
|
pOut.accept(ModBlocks.SAPPHIRE_ORE.get());
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cc.toph.tutorialmod.item;
|
package cc.toph.tutorialmod.item;
|
||||||
|
|
||||||
import cc.toph.tutorialmod.TutorialMod;
|
import cc.toph.tutorialmod.TutorialMod;
|
||||||
|
import cc.toph.tutorialmod.item.custom.MetalDetectorItem;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraftforge.eventbus.api.IEventBus;
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
import net.minecraftforge.registries.DeferredRegister;
|
import net.minecraftforge.registries.DeferredRegister;
|
||||||
|
@ -18,4 +19,7 @@ public class ModItems {
|
||||||
public static final RegistryObject<Item> SAPPHIRE = ITEMS.register("sapphire", () -> new Item(new Item.Properties()));
|
public static final RegistryObject<Item> SAPPHIRE = ITEMS.register("sapphire", () -> new Item(new Item.Properties()));
|
||||||
public static final RegistryObject<Item> RAW_SAPPHIRE = ITEMS.register("raw_sapphire",
|
public static final RegistryObject<Item> RAW_SAPPHIRE = ITEMS.register("raw_sapphire",
|
||||||
() -> new Item(new Item.Properties()));
|
() -> new Item(new Item.Properties()));
|
||||||
|
|
||||||
|
public static final RegistryObject<Item> METAL_DETECTOR = ITEMS.register("metal_detector",
|
||||||
|
() -> new MetalDetectorItem(new Item.Properties().durability(100)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package cc.toph.tutorialmod.item.custom;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.world.InteractionResult;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
|
import net.minecraft.world.item.Item;
|
||||||
|
import net.minecraft.world.item.context.UseOnContext;
|
||||||
|
import net.minecraft.world.level.block.Block;
|
||||||
|
import net.minecraft.world.level.block.Blocks;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
public class MetalDetectorItem extends Item {
|
||||||
|
|
||||||
|
public MetalDetectorItem(Properties pProperties) {
|
||||||
|
super(pProperties);
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionResult useOn(UseOnContext pContext) {
|
||||||
|
|
||||||
|
if (!pContext.getLevel().isClientSide()) {
|
||||||
|
BlockPos positionClicked = pContext.getClickedPos();
|
||||||
|
Player player = pContext.getPlayer();
|
||||||
|
var foundBlock = false;
|
||||||
|
|
||||||
|
for (int i = 0; i <= positionClicked.getY() + 64; i++) {
|
||||||
|
var state = pContext.getLevel().getBlockState(positionClicked.below(i));
|
||||||
|
if (isValuableBlock(state)) {
|
||||||
|
notifyCords(positionClicked.below(i), player, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!foundBlock) {
|
||||||
|
player.sendSystemMessage(Component.literal("Loser Loser"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pContext.getItemInHand().hurtAndBreak(1, pContext.getPlayer(),
|
||||||
|
(player) -> player.broadcastBreakEvent(player.getUsedItemHand()));
|
||||||
|
|
||||||
|
return InteractionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValuableBlock(BlockState state) {
|
||||||
|
|
||||||
|
final var valuables = new ArrayList<Block>();
|
||||||
|
|
||||||
|
valuables.add(Blocks.COAL_ORE);
|
||||||
|
valuables.add(Blocks.IRON_ORE);
|
||||||
|
valuables.add(Blocks.GOLD_ORE);
|
||||||
|
valuables.add(Blocks.DIAMOND_ORE);
|
||||||
|
valuables.add(Blocks.LAPIS_ORE);
|
||||||
|
valuables.add(Blocks.REDSTONE_ORE);
|
||||||
|
valuables.add(Blocks.EMERALD_ORE);
|
||||||
|
|
||||||
|
return valuables.contains(state.getBlock());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyCords(BlockPos below, Player player, BlockState state) {
|
||||||
|
var messg = Component.literal("Found " + state.getBlock().getName().getString() + " at " + below.getX() + " "
|
||||||
|
+ below.getY() + " " + below.getZ());
|
||||||
|
player.sendSystemMessage(messg);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"item.tutorialmod.sapphire": "Sapphire",
|
"item.tutorialmod.sapphire": "Sapphire",
|
||||||
"item.tutorialmod.raw_sapphire": "Raw Sapphire",
|
"item.tutorialmod.raw_sapphire": "Raw Sapphire",
|
||||||
|
"item.tutorialmod.metal_detector": "Metal Detector",
|
||||||
|
|
||||||
"block.tutorialmod.sapphire_block": "Block of Sapphire",
|
"block.tutorialmod.sapphire_block": "Block of Sapphire",
|
||||||
"block.tutorialmod.raw_sapphire_block": "Block of Raw Sapphire",
|
"block.tutorialmod.raw_sapphire_block": "Block of Raw Sapphire",
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"parent": "item/generated",
|
||||||
|
"textures": {
|
||||||
|
"layer0": "tutorialmod:item/metal_detector"
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 305 B |
Loading…
Add table
Reference in a new issue