| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.DustParticleOptions;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.util.valueproviders.UniformInt;
|
| 9 | import net.minecraft.world.InteractionHand;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.item.BlockItem;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 20 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 21 | import net.minecraft.world.phys.BlockHitResult;
|
| 22 |
|
| 23 | public class RedStoneOreBlock extends Block {
|
| 24 | public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;
|
| 25 |
|
| 26 | public RedStoneOreBlock(final BlockBehaviour.Properties properties) {
|
| 27 | super(properties);
|
| 28 | this.registerDefaultState(this.defaultBlockState().setValue(LIT, false));
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected void attack(final BlockState state, final Level level, final BlockPos pos, final Player player) {
|
| 33 | interact(state, level, pos);
|
| 34 | super.attack(state, level, pos, player);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void stepOn(final Level level, final BlockPos pos, final BlockState onState, final Entity entity) {
|
| 39 | if (!entity.isSteppingCarefully()) {
|
| 40 | interact(onState, level, pos);
|
| 41 | }
|
| 42 |
|
| 43 | super.stepOn(level, pos, onState, entity);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected InteractionResult useItemOn(
|
| 48 | final ItemStack itemStack,
|
| 49 | final BlockState state,
|
| 50 | final Level level,
|
| 51 | final BlockPos pos,
|
| 52 | final Player player,
|
| 53 | final InteractionHand hand,
|
| 54 | final BlockHitResult hitResult
|
| 55 | ) {
|
| 56 | if (level.isClientSide()) {
|
| 57 | spawnParticles(level, pos);
|
| 58 | } else {
|
| 59 | interact(state, level, pos);
|
| 60 | }
|
| 61 |
|
| 62 | return itemStack.getItem() instanceof BlockItem && new BlockPlaceContext(player, hand, itemStack, hitResult).canPlace()
|
| 63 | ? InteractionResult.PASS
|
| 64 | : InteractionResult.SUCCESS;
|
| 65 | }
|
| 66 |
|
| 67 | private static void interact(final BlockState state, final Level level, final BlockPos pos) {
|
| 68 | spawnParticles(level, pos);
|
| 69 | if (!state.getValue(LIT)) {
|
| 70 | level.setBlockAndUpdate(pos, state.setValue(LIT, true));
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 76 | return state.getValue(LIT);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 81 | if (state.getValue(LIT)) {
|
| 82 | level.setBlockAndUpdate(pos, state.setValue(LIT, false));
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected void spawnAfterBreak(final BlockState state, final ServerLevel level, final BlockPos pos, final ItemStack tool, final boolean dropExperience) {
|
| 88 | super.spawnAfterBreak(state, level, pos, tool, dropExperience);
|
| 89 | if (dropExperience) {
|
| 90 | this.tryDropExperience(level, pos, tool, UniformInt.of(1, 5));
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 96 | if (state.getValue(LIT)) {
|
| 97 | spawnParticles(level, pos);
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private static void spawnParticles(final Level level, final BlockPos pos) {
|
| 102 | double offset = 0.5625;
|
| 103 | RandomSource random = level.getRandom();
|
| 104 |
|
| 105 | for (Direction direction : Direction.values()) {
|
| 106 | BlockPos relative = pos.relative(direction);
|
| 107 | if (!level.getBlockState(relative).isSolidRender()) {
|
| 108 | Direction.Axis axis = direction.getAxis();
|
| 109 | double dx = axis == Direction.Axis.X ? 0.5 + 0.5625 * direction.getStepX() : random.nextFloat();
|
| 110 | double dy = axis == Direction.Axis.Y ? 0.5 + 0.5625 * direction.getStepY() : random.nextFloat();
|
| 111 | double dz = axis == Direction.Axis.Z ? 0.5 + 0.5625 * direction.getStepZ() : random.nextFloat();
|
| 112 | level.addParticle(DustParticleOptions.REDSTONE, pos.getX() + dx, pos.getY() + dy, pos.getZ() + dz, 0.0, 0.0, 0.0);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 119 | builder.add(LIT);
|
| 120 | }
|
| 121 | }
|
| 122 |
|