|
| 1 | +package galena.oreganized.content.block; |
| 2 | + |
| 3 | +import galena.oreganized.index.OBlockEntities; |
| 4 | +import galena.oreganized.index.OBlocks; |
| 5 | +import galena.oreganized.index.ODamageSources; |
| 6 | +import galena.oreganized.world.IDoorProgressHolder; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.world.InteractionResult; |
| 9 | +import net.minecraft.world.entity.player.Player; |
| 10 | +import net.minecraft.world.level.Level; |
| 11 | +import net.minecraft.world.level.LevelAccessor; |
| 12 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 13 | +import net.minecraft.world.level.block.entity.BlockEntityTicker; |
| 14 | +import net.minecraft.world.level.block.entity.BlockEntityType; |
| 15 | +import net.minecraft.world.level.block.state.BlockState; |
| 16 | +import net.minecraft.world.level.block.state.properties.BlockSetType; |
| 17 | +import net.minecraft.world.level.gameevent.GameEvent; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import static galena.oreganized.content.block.LeadDoorBlock.ANIMATED; |
| 23 | +import static net.minecraft.world.level.block.state.properties.BlockStateProperties.OPEN; |
| 24 | + |
| 25 | +public class HeavyDoorBlockEntity extends BlockEntity { |
| 26 | + |
| 27 | + private int pressure = 0; |
| 28 | + |
| 29 | + private final BlockSetType set = OBlocks.LEAD_BLOCK_SET; |
| 30 | + |
| 31 | + private static final int REQUIRED_PRESSURE_OPEN = 20; |
| 32 | + |
| 33 | + public HeavyDoorBlockEntity(BlockPos pos, BlockState state) { |
| 34 | + super(OBlockEntities.HEAVY_DOOR.get(), pos, state); |
| 35 | + } |
| 36 | + |
| 37 | + public static Optional<HeavyDoorBlockEntity> getAt(LevelAccessor level, BlockPos pos) { |
| 38 | + var be = level.getBlockEntity(pos); |
| 39 | + if (be instanceof HeavyDoorBlockEntity door) return Optional.of(door); |
| 40 | + return Optional.empty(); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("unchecked") |
| 44 | + public static @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) { |
| 45 | + if (type == OBlockEntities.HEAVY_DOOR.get()) { |
| 46 | + BlockEntityTicker<HeavyDoorBlockEntity> ticker = (l, p, s, be) -> be.tick(s, l, p); |
| 47 | + return (BlockEntityTicker<T>) ticker; |
| 48 | + } |
| 49 | + |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + public void tick(BlockState state, Level level, BlockPos pos) { |
| 54 | + if (pressure <= 0) return; |
| 55 | + |
| 56 | + pressure--; |
| 57 | + |
| 58 | + if (pressure == 0) { |
| 59 | + if (state.getValue(OPEN)) { |
| 60 | + state = state.setValue(OPEN, false); |
| 61 | + level.setBlock(pos, state, 10); |
| 62 | + level.gameEvent(GameEvent.BLOCK_CLOSE, pos, GameEvent.Context.of(state)); |
| 63 | + if (state.getBlock() instanceof IHeavyDoor heavy) heavy.sound(null, level, pos, false); |
| 64 | + } |
| 65 | + |
| 66 | + stopUsing(state, level, pos, null); |
| 67 | + } else if (pressure < (REQUIRED_PRESSURE_OPEN - 5)) { |
| 68 | + setAnimationState(level, pos, state, true); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void setAnimationState(Level level, BlockPos pos, BlockState state, boolean animationState) { |
| 73 | + if (state.hasProperty(ANIMATED) && state.getValue(ANIMATED) != animationState) { |
| 74 | + level.setBlockAndUpdate(pos, state.setValue(ANIMATED, animationState)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void stopUsing(BlockState state, Level level, BlockPos pos, @Nullable Player player) { |
| 79 | + setAnimationState(level, pos, state, false); |
| 80 | + } |
| 81 | + |
| 82 | + public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player) { |
| 83 | + var progressHolder = (IDoorProgressHolder) player; |
| 84 | + progressHolder.oreganised$incrementOpeningProgress(); |
| 85 | + |
| 86 | + if (state.getBlock() instanceof IMeltableBlock meltable) { |
| 87 | + var goopyness = meltable.getGoopyness(state); |
| 88 | + if (goopyness > 0) { |
| 89 | + player.hurt(level.damageSources().source(ODamageSources.MOLTEN_LEAD), 1F); |
| 90 | + } |
| 91 | + if (goopyness > 1) return InteractionResult.FAIL; |
| 92 | + } |
| 93 | + |
| 94 | + if (pressure == 0) { |
| 95 | + setAnimationState(level, pos, state, true); |
| 96 | + } |
| 97 | + |
| 98 | + if (!level.isClientSide) System.out.println(pressure); |
| 99 | + |
| 100 | + if (pressure < REQUIRED_PRESSURE_OPEN) { |
| 101 | + pressure += 6; |
| 102 | + } |
| 103 | + |
| 104 | + if (pressure > REQUIRED_PRESSURE_OPEN && !state.getValue(OPEN)) { |
| 105 | + state = state.setValue(OPEN, true); |
| 106 | + level.setBlock(pos, state, 10); |
| 107 | + level.gameEvent(GameEvent.BLOCK_OPEN, pos, GameEvent.Context.of(state)); |
| 108 | + if (state.getBlock() instanceof IHeavyDoor heavy) heavy.sound(player, level, pos, true); |
| 109 | + stopUsing(state, level, pos, player); |
| 110 | + } |
| 111 | + |
| 112 | + return InteractionResult.sidedSuccess(level.isClientSide); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments