summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java')
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java196
1 files changed, 3 insertions, 193 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java b/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java
index 1d8698a..d50ee77 100644
--- a/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java
+++ b/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java
@@ -4,144 +4,23 @@ import jakarta.annotation.Nonnull;
4import jakarta.annotation.Nullable; 4import jakarta.annotation.Nullable;
5import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry; 5import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry;
6import lv.enes.mc.eris_alchemy.block.entity.AlchemicalChestBlockEntity; 6import lv.enes.mc.eris_alchemy.block.entity.AlchemicalChestBlockEntity;
7import lv.enes.mc.eris_alchemy.menu.AlchemicalChestMenu;
8import net.minecraft.core.BlockPos; 7import net.minecraft.core.BlockPos;
9import net.minecraft.core.Direction;
10import net.minecraft.network.chat.Component; 8import net.minecraft.network.chat.Component;
11import net.minecraft.world.*;
12import net.minecraft.world.entity.monster.piglin.PiglinAi;
13import net.minecraft.world.entity.player.Player;
14import net.minecraft.world.item.context.BlockPlaceContext;
15import net.minecraft.world.level.BlockGetter;
16import net.minecraft.world.level.Level;
17import net.minecraft.world.level.LevelAccessor;
18import net.minecraft.world.level.block.*;
19import net.minecraft.world.level.block.entity.BlockEntity; 9import net.minecraft.world.level.block.entity.BlockEntity;
20import net.minecraft.world.level.block.entity.BlockEntityTicker;
21import net.minecraft.world.level.block.entity.BlockEntityType;
22import net.minecraft.world.level.block.entity.ChestBlockEntity;
23import net.minecraft.world.level.block.state.BlockState; 10import net.minecraft.world.level.block.state.BlockState;
24import net.minecraft.world.level.block.state.StateDefinition;
25import net.minecraft.world.level.block.state.properties.BlockStateProperties;
26import net.minecraft.world.level.block.state.properties.BooleanProperty;
27import net.minecraft.world.level.block.state.properties.DirectionProperty;
28import net.minecraft.world.level.material.FluidState;
29import net.minecraft.world.level.material.Fluids;
30import net.minecraft.world.level.pathfinder.PathComputationType;
31import net.minecraft.world.phys.BlockHitResult;
32import net.minecraft.world.phys.shapes.CollisionContext;
33import net.minecraft.world.phys.shapes.VoxelShape;
34 11
35public class AlchemicalChestBlock 12public class AlchemicalChestBlock extends ChestLikeBlock<AlchemicalChestBlockEntity>
36 extends AbstractChestBlock<AlchemicalChestBlockEntity>
37 implements SimpleWaterloggedBlock
38{ 13{
39 public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
40 public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
41
42 public static final Component CONTAINER_TITLE = Component.translatable("container.eris_alchemy.alchemical_chest"); 14 public static final Component CONTAINER_TITLE = Component.translatable("container.eris_alchemy.alchemical_chest");
43 public static final VoxelShape SHAPE = Block.box(
44 1.0, 0.0, 1.0,
45 15.0, 14.0, 15.0
46 );
47
48 public static Container getContainer(Level world, BlockPos pos) {
49 if (world.getBlockEntity(pos) instanceof AlchemicalChestBlockEntity container) {
50 return container;
51 }
52 return null;
53 }
54 15
55 public AlchemicalChestBlock(Properties properties) { 16 public AlchemicalChestBlock(Properties properties) {
56 super(properties, () -> ErisAlchemyRegistry.BlockEntities.ALCHEMICAL_CHEST); 17 super(properties, () -> ErisAlchemyRegistry.BlockEntities.ALCHEMICAL_CHEST);
57 registerDefaultState(getStateDefinition().any()
58 .setValue(FACING, Direction.NORTH)
59 .setValue(WATERLOGGED, false)
60 );
61 }
62
63 @Nonnull
64 @Override
65 public DoubleBlockCombiner.NeighborCombineResult<? extends ChestBlockEntity> combine(
66 BlockState state,
67 Level world,
68 BlockPos pos,
69 boolean ignoreBlocked
70 ) {
71 return DoubleBlockCombiner.Combiner::acceptNone;
72 }
73
74 @SuppressWarnings("deprecation")
75 @Override
76 public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
77 return AlchemicalChestMenu.getRedstoneSignalFromContainer(getContainer(world, pos));
78 }
79
80 @SuppressWarnings("deprecation")
81 @Nonnull
82 @Override
83 public FluidState getFluidState(BlockState state) {
84 return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
85 }
86
87 @Override
88 public MenuProvider getMenuProvider(BlockState state, Level world, BlockPos pos) {
89 if (world.getBlockEntity(pos) instanceof AlchemicalChestBlockEntity entity) {
90 return new SimpleMenuProvider(entity, CONTAINER_TITLE);
91 }
92 return null;
93 } 18 }
94 19
95 @Nonnull
96 @Override 20 @Override
97 public RenderShape getRenderShape(BlockState state) {
98 return RenderShape.ENTITYBLOCK_ANIMATED;
99 }
100
101 @SuppressWarnings("deprecation")
102 @Nonnull 21 @Nonnull
103 @Override 22 protected Component getContainerTitle() {
104 public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext ctx) { 23 return CONTAINER_TITLE;
105 return SHAPE;
106 }
107
108 @Override
109 public BlockState getStateForPlacement(BlockPlaceContext ctx) {
110 return defaultBlockState()
111 .setValue(FACING, ctx.getHorizontalDirection().getOpposite())
112 .setValue(WATERLOGGED, ctx.getLevel().getFluidState(ctx.getClickedPos()).is(Fluids.WATER));
113 }
114
115 @Override
116 public <T extends BlockEntity> BlockEntityTicker<T> getTicker(
117 Level ignoredWorld,
118 BlockState ignoredState,
119 BlockEntityType<T> type
120 ) {
121 return createTickerHelper(
122 type,
123 blockEntityType.get(),
124 (world, pos, state, entity) -> entity.tick(world, pos, state)
125 );
126 }
127
128 @SuppressWarnings("deprecation")
129 @Override
130 public boolean hasAnalogOutputSignal(BlockState state) {
131 return true;
132 }
133
134 @SuppressWarnings("deprecation")
135 @Override
136 public boolean isPathfindable(BlockState state, BlockGetter world, BlockPos pos, PathComputationType type) {
137 return false;
138 }
139
140 @SuppressWarnings("deprecation")
141 @Nonnull
142 @Override
143 public BlockState mirror(BlockState state, Mirror mirror) {
144 return state.rotate(mirror.getRotation(state.getValue(FACING)));
145 } 24 }
146 25
147 @Nullable 26 @Nullable
@@ -149,73 +28,4 @@ public class AlchemicalChestBlock
149 public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { 28 public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
150 return new AlchemicalChestBlockEntity(pos, state); 29 return new AlchemicalChestBlockEntity(pos, state);
151 } 30 }
152
153 @SuppressWarnings("deprecation")
154 @Override
155 public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean moved) {
156 if (!state.is(newState.getBlock())) {
157 var entity = world.getBlockEntity(pos);
158 if (entity instanceof Container container) {
159 Containers.dropContents(world, pos, container);
160 world.updateNeighbourForOutputSignal(pos, this);
161 }
162
163 super.onRemove(state, world, pos, newState, moved);
164 }
165 }
166
167 @SuppressWarnings("deprecation")
168 @Nonnull
169 @Override
170 public BlockState rotate(BlockState state, Rotation rotation) {
171 return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
172 }
173
174 @SuppressWarnings("deprecation")
175 @Nonnull
176 @Override
177 public BlockState updateShape(
178 BlockState state,
179 Direction direction,
180 BlockState neighborState,
181 LevelAccessor world,
182 BlockPos pos,
183 BlockPos neighborPos
184 ) {
185 if (state.getValue(WATERLOGGED)) {
186 world.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
187 }
188
189 return super.updateShape(state, direction, neighborState, world, pos, neighborPos);
190 }
191
192 @SuppressWarnings("deprecation")
193 @Nonnull
194 @Override
195 public InteractionResult use(
196 BlockState state,
197 Level world,
198 BlockPos pos,
199 Player player,
200 InteractionHand hand,
201 BlockHitResult hit
202 ) {
203 if (world.isClientSide) {
204 return InteractionResult.SUCCESS;
205 }
206
207 var provider = getMenuProvider(state, world, pos);
208 if (provider != null) {
209 player.openMenu(provider);
210 // TODO: player.awardStat(getOpenChestStat);
211 PiglinAi.angerNearbyPiglins(player, true);
212 }
213
214 return InteractionResult.CONSUME;
215 }
216
217 @Override
218 protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
219 builder.add(FACING, WATERLOGGED);
220 }
221} 31}