6金属探测器
这一节我们主要做一个金属探测器,该金属探测器可以向玩家脚下64个高度探测是否有钻石或者铁,如果有的话,会告诉具体的坐标,并且会减少一点耐久
金属探测器代码
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));
if(isValuableBlock(state)) {
outputValuableCoordinates(positionClicked.down(i), player, state.getBlock());
foundBlock = true;
break;
}
}
if(!foundBlock) {
player.sendMessage(Text.literal("没有找到任何有价值的矿石!"));
}
}
context.getStack().damage(1, context.getPlayer(),
EquipmentSlot.MAINHAND);
return ActionResult.SUCCESS;
}
private void outputValuableCoordinates(BlockPos blockPos, PlayerEntity player, Block block) {
player.sendMessage(Text.literal("找到" + 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);
}
注册物品
public class ModItems {
public static final Item RUBY = registerItem("ruby", new Item(new Item.Settings()));
public static final Item RAW_RUBY = registerItem("raw_ruby", new Item(new Item.Settings()));
// //这里是注册金属探测器
public static final Item METAL_DETECTOR = registerItem("metal_detector", new MetalDetectorItem(new Item.Settings().maxCount(1).maxDamage(64)));
public static void registerModItems() {
DemoMod1.LOGGER.info("Registering items");
ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS)
.register(ModItems::addItemsToIngredientTabItemGroup);
}
public static void addItemsToIngredientTabItemGroup(FabricItemGroupEntries entries) {
entries.add(RUBY);
entries.add(RAW_RUBY);
entries.add(METAL_DETECTOR); //这里是注册金属探测器
}
public static Item registerItem(String name, Item item) {
DemoMod1.LOGGER.info("Registering item " + name);
Registry.register(Registries.ITEM, new Identifier(DemoMod1.MOD_ID, name), item);
return item;
}
将其添加到我们的物品组中
public class ModItemGroups {
public static final ItemGroup RUBY_GROUP = Registry.register(Registries.ITEM_GROUP,
new Identifier(DemoMod1.MOD_ID, "ruby"),
FabricItemGroup.builder().displayName(Text.translatable("itemgroup.ruby"))
.icon(() -> new ItemStack(ModItems.RUBY)).entries((displayContext, entries) -> {
entries.add(ModItems.RUBY);
entries.add(ModItems.RAW_RUBY);
entries.add(Items.DIAMOND);
entries.add(ModBlocks.RUBY_BLOCK);
entries.add(ModBlocks.RAW_RUBY_BLOCK);
entries.add(ModBlocks.RUBY_ORE);
entries.add(ModBlocks.DEEPSLATE_RUBY_ORE);
entries.add(ModBlocks.NETHER_RUBY_ORE);
entries.add(ModBlocks.END_STONE_RUBY_ORE);
entries.add(ModItems.METAL_DETECTOR);
}).build());
public static void registerItemGroups() {
DemoMod1.LOGGER.info("Registering Item Groups for " + DemoMod1.MOD_ID);
}
}
效果展示
当有铁的时候,会给出提示