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.java162
1 files changed, 162 insertions, 0 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
new file mode 100644
index 0000000..c013df6
--- /dev/null
+++ b/src/main/java/lv/enes/mc/eris_alchemy/block/AlchemicalChestBlock.java
@@ -0,0 +1,162 @@
1package lv.enes.mc.eris_alchemy.block;
2
3import jakarta.annotation.Nonnull;
4import jakarta.annotation.Nullable;
5import lv.enes.mc.eris_alchemy.block.entity.AlchemicalChestBlockEntity;
6import lv.enes.mc.eris_alchemy.block.entity.ErisAlchemyBlockEntities;
7import lv.enes.mc.eris_alchemy.menu.AlchemicalChestMenu;
8import net.minecraft.core.BlockPos;
9import net.minecraft.core.Direction;
10import 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.block.*;
18import net.minecraft.world.level.block.entity.BlockEntity;
19import net.minecraft.world.level.block.entity.BlockEntityTicker;
20import net.minecraft.world.level.block.entity.BlockEntityType;
21import net.minecraft.world.level.block.entity.ChestBlockEntity;
22import net.minecraft.world.level.block.state.BlockState;
23import net.minecraft.world.level.block.state.StateDefinition;
24import net.minecraft.world.level.block.state.properties.BlockStateProperties;
25import net.minecraft.world.level.block.state.properties.DirectionProperty;
26import net.minecraft.world.level.pathfinder.PathComputationType;
27import net.minecraft.world.phys.BlockHitResult;
28import net.minecraft.world.phys.shapes.CollisionContext;
29import net.minecraft.world.phys.shapes.VoxelShape;
30
31public class AlchemicalChestBlock extends AbstractChestBlock<AlchemicalChestBlockEntity> {
32 public static final Component CONTAINER_TITLE = Component.translatable("container.eris_alchemy.alchemical_chest");
33 public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
34 public static final VoxelShape SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 14.0, 15.0);
35
36 public static Container getContainer(Level world, BlockPos pos) {
37 if (world.getBlockEntity(pos) instanceof AlchemicalChestBlockEntity container) {
38 return container;
39 }
40 return null;
41 }
42
43 public AlchemicalChestBlock(Properties properties) {
44 super(properties, () -> ErisAlchemyBlockEntities.ALCHEMICAL_CHEST);
45 registerDefaultState(getStateDefinition().any()
46 .setValue(FACING, Direction.NORTH)
47 );
48 }
49
50 @Nonnull
51 @Override
52 public DoubleBlockCombiner.NeighborCombineResult<? extends ChestBlockEntity> combine(BlockState state, Level world, BlockPos pos, boolean ignoreBlocked) {
53 return DoubleBlockCombiner.Combiner::acceptNone;
54 }
55
56 @Override
57 protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
58 builder.add(FACING);
59 }
60
61 @SuppressWarnings("deprecation")
62 @Override
63 public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
64 return AlchemicalChestMenu.getRedstoneSignalFromContainer(getContainer(world, pos));
65 }
66
67 @Override
68 public MenuProvider getMenuProvider(BlockState state, Level world, BlockPos pos) {
69 if (world.getBlockEntity(pos) instanceof AlchemicalChestBlockEntity entity) {
70 return new SimpleMenuProvider(entity, CONTAINER_TITLE);
71 }
72 return null;
73 }
74
75 @Nonnull
76 @Override
77 public RenderShape getRenderShape(BlockState state) {
78 return RenderShape.ENTITYBLOCK_ANIMATED;
79 }
80
81 @SuppressWarnings("deprecation")
82 @Nonnull
83 @Override
84 public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext ctx) {
85 return SHAPE;
86 }
87
88 @Override
89 public BlockState getStateForPlacement(BlockPlaceContext ctx) {
90 return defaultBlockState()
91 .setValue(FACING, ctx.getHorizontalDirection().getOpposite());
92 }
93
94 @Override
95 public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level ignoredWorld, BlockState ignoredState, BlockEntityType<T> type) {
96 return createTickerHelper(type, blockEntityType.get(), (world, pos, state, entity) -> entity.tick(world, pos, state));
97 }
98
99 @SuppressWarnings("deprecation")
100 @Override
101 public boolean hasAnalogOutputSignal(BlockState state) {
102 return true;
103 }
104
105 @SuppressWarnings("deprecation")
106 @Override
107 public boolean isPathfindable(BlockState state, BlockGetter world, BlockPos pos, PathComputationType type) {
108 return false;
109 }
110
111 @SuppressWarnings("deprecation")
112 @Nonnull
113 @Override
114 public BlockState mirror(BlockState state, Mirror mirror) {
115 return state.rotate(mirror.getRotation(state.getValue(FACING)));
116 }
117
118 @Nullable
119 @Override
120 public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
121 return new AlchemicalChestBlockEntity(pos, state);
122 }
123
124 @SuppressWarnings("deprecation")
125 @Override
126 public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean moved) {
127 if (!state.is(newState.getBlock())) {
128 var entity = world.getBlockEntity(pos);
129 if (entity instanceof Container container) {
130 Containers.dropContents(world, pos, container);
131 world.updateNeighbourForOutputSignal(pos, this);
132 }
133
134 super.onRemove(state, world, pos, newState, moved);
135 }
136 }
137
138 @SuppressWarnings("deprecation")
139 @Nonnull
140 @Override
141 public BlockState rotate(BlockState state, Rotation rotation) {
142 return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
143 }
144
145 @SuppressWarnings("deprecation")
146 @Nonnull
147 @Override
148 public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
149 if (world.isClientSide) {
150 return InteractionResult.SUCCESS;
151 }
152
153 var provider = getMenuProvider(state, world, pos);
154 if (provider != null) {
155 player.openMenu(provider);
156 // player.awardStat(getOpenChestStat);
157 PiglinAi.angerNearbyPiglins(player, true);
158 }
159
160 return InteractionResult.CONSUME;
161 }
162}