| 1 | package net.minecraft.core.dispenser;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.entity.monster.cubemob.SulfurCube;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.level.block.BaseFireBlock;
|
| 10 | import net.minecraft.world.level.block.CampfireBlock;
|
| 11 | import net.minecraft.world.level.block.CandleBlock;
|
| 12 | import net.minecraft.world.level.block.CandleCakeBlock;
|
| 13 | import net.minecraft.world.level.block.DispenserBlock;
|
| 14 | import net.minecraft.world.level.block.TntBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 17 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 18 | import net.minecraft.world.phys.AABB;
|
| 19 |
|
| 20 | public class FlintAndSteelDispenseItemBehavior extends OptionalDispenseItemBehavior {
|
| 21 | @Override
|
| 22 | protected ItemStack execute(final BlockSource source, final ItemStack dispensed) {
|
| 23 | ServerLevel level = source.level();
|
| 24 | this.setSuccess(true);
|
| 25 | Direction facing = source.state().getValue(DispenserBlock.FACING);
|
| 26 | BlockPos targetPos = source.pos().relative(facing);
|
| 27 | BlockState target = level.getBlockState(targetPos);
|
| 28 | if (!tryIgniteExplosiveEntities(level, targetPos)) {
|
| 29 | if (BaseFireBlock.canBePlacedAt(level, targetPos, facing)) {
|
| 30 | level.setBlockAndUpdate(targetPos, BaseFireBlock.getState(level, targetPos));
|
| 31 | level.gameEvent(null, GameEvent.BLOCK_PLACE, targetPos);
|
| 32 | } else if (CampfireBlock.canLight(target) || CandleBlock.canLight(target) || CandleCakeBlock.canLight(target)) {
|
| 33 | level.setBlockAndUpdate(targetPos, target.setValue(BlockStateProperties.LIT, true));
|
| 34 | level.gameEvent(null, GameEvent.BLOCK_CHANGE, targetPos);
|
| 35 | } else if (target.getBlock() instanceof TntBlock) {
|
| 36 | if (TntBlock.prime(level, targetPos, null, dispensed)) {
|
| 37 | level.removeBlock(targetPos, false);
|
| 38 | } else {
|
| 39 | this.setSuccess(false);
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | if (this.isSuccess()) {
|
| 45 | dispensed.hurtAndBreak(1, level, null, item -> {});
|
| 46 | }
|
| 47 |
|
| 48 | return dispensed;
|
| 49 | }
|
| 50 |
|
| 51 | private static boolean tryIgniteExplosiveEntities(final ServerLevel level, final BlockPos pos) {
|
| 52 | List<SulfurCube> entities = level.getEntitiesOfClass(SulfurCube.class, new AABB(pos), SulfurCube::canExplode);
|
| 53 | if (entities.isEmpty()) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | entities.getFirst().primeTime(false);
|
| 58 | return true;
|
| 59 | }
|
| 60 | }
|
| 61 |
|