| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.Entity;
|
| 7 | import net.minecraft.world.level.block.SculkSensorBlock;
|
| 8 | import net.minecraft.world.level.block.state.BlockState;
|
| 9 | import net.minecraft.world.level.gameevent.BlockPositionSource;
|
| 10 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 11 | import net.minecraft.world.level.gameevent.GameEventListener;
|
| 12 | import net.minecraft.world.level.gameevent.PositionSource;
|
| 13 | import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
|
| 14 | import net.minecraft.world.level.storage.ValueInput;
|
| 15 | import net.minecraft.world.level.storage.ValueOutput;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class SculkSensorBlockEntity extends BlockEntity implements GameEventListener.Provider<VibrationSystem.Listener>, VibrationSystem {
|
| 19 | private static final int DEFAULT_LAST_VIBRATION_FREQUENCY = 0;
|
| 20 | private VibrationSystem.Data vibrationData;
|
| 21 | private final VibrationSystem.Listener vibrationListener;
|
| 22 | private final VibrationSystem.User vibrationUser;
|
| 23 | private int lastVibrationFrequency = 0;
|
| 24 |
|
| 25 | protected SculkSensorBlockEntity(final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState) {
|
| 26 | super(type, worldPosition, blockState);
|
| 27 | this.vibrationUser = this.createVibrationUser();
|
| 28 | this.vibrationData = new VibrationSystem.Data();
|
| 29 | this.vibrationListener = new VibrationSystem.Listener(this);
|
| 30 | }
|
| 31 |
|
| 32 | public SculkSensorBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 33 | this(BlockEntityTypes.SCULK_SENSOR, worldPosition, blockState);
|
| 34 | }
|
| 35 |
|
| 36 | public VibrationSystem.User createVibrationUser() {
|
| 37 | return new SculkSensorBlockEntity.VibrationUser(this.getBlockPos());
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void loadAdditional(final ValueInput input) {
|
| 42 | super.loadAdditional(input);
|
| 43 | this.lastVibrationFrequency = input.getIntOr("last_vibration_frequency", 0);
|
| 44 | this.vibrationData = input.read("listener", VibrationSystem.Data.CODEC).orElseGet(VibrationSystem.Data::new);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void saveAdditional(final ValueOutput output) {
|
| 49 | super.saveAdditional(output);
|
| 50 | output.putInt("last_vibration_frequency", this.lastVibrationFrequency);
|
| 51 | output.store("listener", VibrationSystem.Data.CODEC, this.vibrationData);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public VibrationSystem.Data getVibrationData() {
|
| 56 | return this.vibrationData;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public VibrationSystem.User getVibrationUser() {
|
| 61 | return this.vibrationUser;
|
| 62 | }
|
| 63 |
|
| 64 | public int getLastVibrationFrequency() {
|
| 65 | return this.lastVibrationFrequency;
|
| 66 | }
|
| 67 |
|
| 68 | public void setLastVibrationFrequency(final int lastVibrationFrequency) {
|
| 69 | this.lastVibrationFrequency = lastVibrationFrequency;
|
| 70 | }
|
| 71 |
|
| 72 | public VibrationSystem.Listener getListener() {
|
| 73 | return this.vibrationListener;
|
| 74 | }
|
| 75 |
|
| 76 | protected class VibrationUser implements VibrationSystem.User {
|
| 77 | public static final int LISTENER_RANGE = 8;
|
| 78 | protected final BlockPos blockPos;
|
| 79 | private final PositionSource positionSource;
|
| 80 |
|
| 81 | public VibrationUser(final BlockPos blockPos) {
|
| 82 | this.blockPos = blockPos;
|
| 83 | this.positionSource = new BlockPositionSource(blockPos);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public int getListenerRadius() {
|
| 88 | return 8;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public PositionSource getPositionSource() {
|
| 93 | return this.positionSource;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean canTriggerAvoidVibration() {
|
| 98 | return true;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public boolean canReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final GameEvent.@Nullable Context context) {
|
| 103 | if (!pos.equals(this.blockPos) || !event.is(GameEvent.BLOCK_DESTROY) && !event.is(GameEvent.BLOCK_PLACE)) {
|
| 104 | return VibrationSystem.getGameEventFrequency(event) == 0 ? false : SculkSensorBlock.canActivate(SculkSensorBlockEntity.this.getBlockState());
|
| 105 | } else {
|
| 106 | return false;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void onReceiveVibration(
|
| 112 | final ServerLevel level,
|
| 113 | final BlockPos pos,
|
| 114 | final Holder<GameEvent> event,
|
| 115 | final @Nullable Entity sourceEntity,
|
| 116 | final @Nullable Entity projectileOwner,
|
| 117 | final float receivingDistance
|
| 118 | ) {
|
| 119 | BlockState state = SculkSensorBlockEntity.this.getBlockState();
|
| 120 | if (SculkSensorBlock.canActivate(state)) {
|
| 121 | int eventFrequency = VibrationSystem.getGameEventFrequency(event);
|
| 122 | SculkSensorBlockEntity.this.setLastVibrationFrequency(eventFrequency);
|
| 123 | int calculatedPower = VibrationSystem.getRedstoneStrengthForDistance(receivingDistance, this.getListenerRadius());
|
| 124 | if (state.getBlock() instanceof SculkSensorBlock sculkSensorBlock) {
|
| 125 | sculkSensorBlock.activate(sourceEntity, level, this.blockPos, state, calculatedPower, eventFrequency);
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public void onDataChanged() {
|
| 132 | SculkSensorBlockEntity.this.setChanged();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean requiresAdjacentChunksToBeTicking() {
|
| 137 | return true;
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|