| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.stats.Stats;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.projectile.Projectile;
|
| 13 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 14 | import net.minecraft.world.level.BlockGetter;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.LevelAccessor;
|
| 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.BlockStateProperties;
|
| 21 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | public class TargetBlock extends Block {
|
| 26 | private static final IntegerProperty OUTPUT_POWER = BlockStateProperties.POWER;
|
| 27 | private static final int ACTIVATION_TICKS_ARROWS = 20;
|
| 28 | private static final int ACTIVATION_TICKS_OTHER = 8;
|
| 29 |
|
| 30 | public TargetBlock(final BlockBehaviour.Properties properties) {
|
| 31 | super(properties);
|
| 32 | this.registerDefaultState(this.stateDefinition.any().setValue(OUTPUT_POWER, 0));
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult hitResult, final Projectile projectile) {
|
| 37 | int outputStrength = updateRedstoneOutput(level, state, hitResult, projectile);
|
| 38 | if (projectile.getOwner() instanceof ServerPlayer playerOwner) {
|
| 39 | playerOwner.awardStat(Stats.TARGET_HIT);
|
| 40 | CriteriaTriggers.TARGET_BLOCK_HIT.trigger(playerOwner, projectile, hitResult.getLocation(), outputStrength);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | private static int updateRedstoneOutput(final LevelAccessor level, final BlockState state, final BlockHitResult hitResult, final Entity entity) {
|
| 45 | int redstoneStrength = getRedstoneStrength(hitResult, hitResult.getLocation());
|
| 46 | int duration = entity instanceof AbstractArrow ? 20 : 8;
|
| 47 | if (!level.getBlockTicks().hasScheduledTick(hitResult.getBlockPos(), state.getBlock())) {
|
| 48 | setOutputPower(level, state, redstoneStrength, hitResult.getBlockPos(), duration);
|
| 49 | }
|
| 50 |
|
| 51 | return redstoneStrength;
|
| 52 | }
|
| 53 |
|
| 54 | private static int getRedstoneStrength(final BlockHitResult hitResult, final Vec3 hitLocation) {
|
| 55 | Direction hitDirection = hitResult.getDirection();
|
| 56 | double distX = Math.abs(Mth.frac(hitLocation.x) - 0.5);
|
| 57 | double distY = Math.abs(Mth.frac(hitLocation.y) - 0.5);
|
| 58 | double distZ = Math.abs(Mth.frac(hitLocation.z) - 0.5);
|
| 59 | Direction.Axis axis = hitDirection.getAxis();
|
| 60 | double distance;
|
| 61 | if (axis == Direction.Axis.Y) {
|
| 62 | distance = Math.max(distX, distZ);
|
| 63 | } else if (axis == Direction.Axis.Z) {
|
| 64 | distance = Math.max(distX, distY);
|
| 65 | } else {
|
| 66 | distance = Math.max(distY, distZ);
|
| 67 | }
|
| 68 |
|
| 69 | return Math.max(1, Mth.ceil(15.0 * Mth.clamp((0.5 - distance) / 0.5, 0.0, 1.0)));
|
| 70 | }
|
| 71 |
|
| 72 | private static void setOutputPower(final LevelAccessor level, final BlockState state, final int outputStrength, final BlockPos pos, final int duration) {
|
| 73 | level.setBlockAndUpdate(pos, state.setValue(OUTPUT_POWER, outputStrength));
|
| 74 | level.scheduleTick(pos, state.getBlock(), duration);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 79 | if (state.getValue(OUTPUT_POWER) != 0) {
|
| 80 | level.setBlockAndUpdate(pos, state.setValue(OUTPUT_POWER, 0));
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 86 | return state.getValue(OUTPUT_POWER);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected boolean isSignalSource(final BlockState state) {
|
| 91 | return true;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 96 | builder.add(OUTPUT_POWER);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 101 | if (!level.isClientSide() && !state.is(oldState.getBlock())) {
|
| 102 | if (state.getValue(OUTPUT_POWER) > 0 && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 103 | level.setBlock(pos, state.setValue(OUTPUT_POWER, 0), 18);
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|