| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerPlayer;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.entity.player.Player;
|
| 10 | import net.minecraft.world.item.context.UseOnContext;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.block.BaseFireBlock;
|
| 13 | import net.minecraft.world.level.block.CampfireBlock;
|
| 14 | import net.minecraft.world.level.block.CandleBlock;
|
| 15 | import net.minecraft.world.level.block.CandleCakeBlock;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 19 |
|
| 20 | public class FlintAndSteelItem extends Item {
|
| 21 | public FlintAndSteelItem(final Item.Properties properties) {
|
| 22 | super(properties);
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public InteractionResult useOn(final UseOnContext context) {
|
| 27 | Player player = context.getPlayer();
|
| 28 | Level level = context.getLevel();
|
| 29 | BlockPos pos = context.getClickedPos();
|
| 30 | BlockState state = level.getBlockState(pos);
|
| 31 | if (!CampfireBlock.canLight(state) && !CandleBlock.canLight(state) && !CandleCakeBlock.canLight(state)) {
|
| 32 | BlockPos relativePos = pos.relative(context.getClickedFace());
|
| 33 | if (BaseFireBlock.canBePlacedAt(level, relativePos, context.getHorizontalDirection())) {
|
| 34 | level.playSound(player, relativePos, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.4F + 0.8F);
|
| 35 | BlockState fireState = BaseFireBlock.getState(level, relativePos);
|
| 36 | level.setBlock(relativePos, fireState, 11);
|
| 37 | level.gameEvent(player, GameEvent.BLOCK_PLACE, pos);
|
| 38 | ItemStack itemStack = context.getItemInHand();
|
| 39 | if (player instanceof ServerPlayer serverPlayer) {
|
| 40 | CriteriaTriggers.PLACED_BLOCK.trigger(serverPlayer, relativePos, itemStack);
|
| 41 | itemStack.hurtAndBreak(1, player, context.getHand().asEquipmentSlot());
|
| 42 | }
|
| 43 |
|
| 44 | return InteractionResult.SUCCESS;
|
| 45 | } else {
|
| 46 | return InteractionResult.FAIL;
|
| 47 | }
|
| 48 | } else {
|
| 49 | level.playSound(player, pos, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.4F + 0.8F);
|
| 50 | level.setBlock(pos, state.setValue(BlockStateProperties.LIT, true), 11);
|
| 51 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 52 | if (player != null) {
|
| 53 | context.getItemInHand().hurtAndBreak(1, player, context.getHand().asEquipmentSlot());
|
| 54 | }
|
| 55 |
|
| 56 | return InteractionResult.SUCCESS;
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|