11package eu .pb4 .polydecorations .block .item ;
22
33import com .mojang .serialization .MapCodec ;
4- import eu .pb4 .factorytools .api .block .BarrierBasedWaterloggable ;
54import eu .pb4 .factorytools .api .block .FactoryBlock ;
65import eu .pb4 .factorytools .api .block .QuickWaterloggable ;
76import eu .pb4 .factorytools .api .virtualentity .BlockModel ;
87import eu .pb4 .factorytools .api .virtualentity .ItemDisplayElementUtil ;
98import eu .pb4 .polydecorations .block .DecorationsBlocks ;
9+ import eu .pb4 .polydecorations .util .DecorationSoundEvents ;
1010import eu .pb4 .polydecorations .util .DecorationsUtil ;
1111import eu .pb4 .polymer .blocks .api .PolymerTexturedBlock ;
1212import eu .pb4 .polymer .virtualentity .api .ElementHolder ;
1818import net .minecraft .resources .Identifier ;
1919import net .minecraft .server .level .ServerLevel ;
2020import net .minecraft .server .level .ServerPlayer ;
21+ import net .minecraft .sounds .SoundEvent ;
2122import net .minecraft .util .RandomSource ;
2223import net .minecraft .world .Containers ;
2324import net .minecraft .world .InteractionResult ;
2829import net .minecraft .world .level .Level ;
2930import net .minecraft .world .level .LevelReader ;
3031import net .minecraft .world .level .ScheduledTickAccess ;
31- import net .minecraft .world .level .block .BaseEntityBlock ;
32- import net .minecraft .world .level .block .Block ;
33- import net .minecraft .world .level .block .Blocks ;
34- import net .minecraft .world .level .block .ChainBlock ;
35- import net .minecraft .world .level .block .ShulkerBoxBlock ;
32+ import net .minecraft .world .level .block .*;
3633import net .minecraft .world .level .block .entity .BlockEntity ;
3734import net .minecraft .world .level .block .entity .ShulkerBoxBlockEntity ;
3835import net .minecraft .world .level .block .state .BlockState ;
5350
5451import static eu .pb4 .polydecorations .util .DecorationsUtil .id ;
5552
56- public class BasketBlock extends BaseEntityBlock implements FactoryBlock , QuickWaterloggable , PolymerTexturedBlock {
57- public static final Identifier CONTENTS_DYNAMIC_DROP_ID = ShulkerBoxBlock .CONTENTS ;
58-
59- public static final EnumProperty <Direction > FACING = BlockStateProperties .HORIZONTAL_FACING ;
60- public static final BooleanProperty OPEN = BlockStateProperties .OPEN ;
53+ public class BasketBlock extends PickableItemContainerBlock {
6154 public static final BooleanProperty HANGING = BlockStateProperties .HANGING ;
6255
56+
6357 public BasketBlock (Properties settings ) {
64- super (settings );
65- this .registerDefaultState (this .defaultBlockState ().setValue (WATERLOGGED , false ). setValue ( OPEN , false ). setValue ( HANGING , false ));
58+ super (settings , DecorationSoundEvents . BASKET_OPEN , DecorationSoundEvents . BASKET_CLOSE );
59+ this .registerDefaultState (this .defaultBlockState ().setValue (HANGING , false ));
6660 }
6761
6862 @ Override
6963 protected void createBlockStateDefinition (StateDefinition .Builder <Block , BlockState > builder ) {
70- builder .add (WATERLOGGED , FACING , OPEN , HANGING );
64+ super .createBlockStateDefinition (builder );
65+ builder .add (HANGING );
7166 }
7267
7368 @ Nullable
@@ -76,56 +71,6 @@ public BlockState getStateForPlacement(BlockPlaceContext ctx) {
7671 return waterLog (ctx , this .defaultBlockState ().setValue (FACING , ctx .getHorizontalDirection ().getOpposite ()).setValue (HANGING , canHangingOn (ctx .getLevel (), ctx .getClickedPos ().above ())));
7772 }
7873
79- @ Override
80- public InteractionResult useWithoutItem (BlockState state , Level world , BlockPos pos , Player player , BlockHitResult hit ) {
81- if (!player .isShiftKeyDown () && world .getBlockEntity (pos ) instanceof BasketBlockEntity be ) {
82- be .openGui ((ServerPlayer ) player );
83- return InteractionResult .SUCCESS_SERVER ;
84- }
85-
86- return InteractionResult .PASS ;
87- }
88-
89- @ Override
90- protected void tick (BlockState state , ServerLevel world , BlockPos pos , RandomSource random ) {
91- if (world .getBlockEntity (pos ) instanceof BasketBlockEntity be ) {
92- be .tick ();
93- }
94- }
95-
96-
97- @ Override
98- public BlockState playerWillDestroy (Level world , BlockPos pos , BlockState state , Player player ) {
99- if (world .getBlockEntity (pos ) instanceof BasketBlockEntity be ) {
100- if (!world .isClientSide () && player .preventsBlockDrops () && !be .isEmpty ()) {
101- var itemStack = this .asItem ().getDefaultInstance ();
102- itemStack .applyComponents (be .collectComponents ());
103- ItemEntity itemEntity = new ItemEntity (world , (double ) pos .getX () + 0.5 , (double ) pos .getY () + 0.5 , (double ) pos .getZ () + 0.5 , itemStack );
104- itemEntity .setDefaultPickUpDelay ();
105- world .addFreshEntity (itemEntity );
106- } else {
107- //be.generateLoot(player);
108- }
109- }
110-
111- return super .playerWillDestroy (world , pos , state , player );
112- }
113-
114- @ Override
115- protected List <ItemStack > getDrops (BlockState state , LootParams .Builder builder ) {
116- BlockEntity blockEntity = builder .getOptionalParameter (LootContextParams .BLOCK_ENTITY );
117- if (blockEntity instanceof ShulkerBoxBlockEntity be ) {
118- builder = builder .withDynamicDrop (CONTENTS_DYNAMIC_DROP_ID , (lootConsumer ) -> {
119- for (int i = 0 ; i < be .getContainerSize (); ++i ) {
120- lootConsumer .accept (be .getItem (i ));
121- }
122-
123- });
124- }
125-
126- return super .getDrops (state , builder );
127- }
128-
12974 @ Override
13075 public BlockState getPolymerBlockState (BlockState blockState , PacketContext packetContext ) {
13176 return blockState .getValue (WATERLOGGED ) ? DecorationsUtil .CAMPFIRE_WATERLOGGED_STATE : DecorationsUtil .CAMPFIRE_STATE ;
@@ -148,45 +93,21 @@ protected BlockState updateShape(BlockState state, LevelReader world, ScheduledT
14893
14994 private boolean canHangingOn (LevelReader world , BlockPos up ) {
15095 var state = world .getBlockState (up );
151-
15296 return state .is (DecorationsBlocks .ROPE ) || state .getBlock () instanceof ChainBlock && state .getValue (ChainBlock .AXIS ) == Direction .Axis .Y ;
15397 }
15498
155- public FluidState getFluidState (BlockState state ) {
156- return state .getValue (WATERLOGGED ) ? Fluids .WATER .getSource (false ) : super .getFluidState (state );
157- }
158-
159- @ Override
160- protected void affectNeighborsAfterRemoval (BlockState state , ServerLevel world , BlockPos pos , boolean moved ) {
161- Containers .updateNeighboursAfterDestroy (state , world , pos );
162- super .affectNeighborsAfterRemoval (state , world , pos , moved );
163- }
164-
165- @ Override
166- protected MapCodec <? extends BaseEntityBlock > codec () {
167- return null ;
168- }
169-
17099 @ Override
171100 public @ Nullable ElementHolder createElementHolder (ServerLevel world , BlockPos pos , BlockState initialBlockState ) {
172101 return new Model (world , initialBlockState );
173102 }
174103
175- @ Nullable
176- @ Override
177- public BlockEntity newBlockEntity (BlockPos pos , BlockState state ) {
178- return new BasketBlockEntity (pos , state );
179- }
180-
181- public static final class Model extends BlockModel {
182- public static final ItemStack BASKET_OPEN = ItemDisplayElementUtil .getModel (id ("block/basket_open" ));
183- public static final ItemStack BASKET_CLOSE = ItemDisplayElementUtil .getModel (id ("block/basket_closed" ));
104+ public final class Model extends BlockModel {
184105 private final ItemDisplayElement main ;
185106
186107 public Model (ServerLevel world , BlockState state ) {
187108 var direction = state .getValue (FACING ).toYRot ();
188109
189- this .main = ItemDisplayElementUtil .createSimple (state .getValue (OPEN ) ? BASKET_OPEN : BASKET_CLOSE );
110+ this .main = ItemDisplayElementUtil .createSimple (state .getValue (OPEN ) ? modelOpen : modelClosed );
190111 this .main .setScale (new Vector3f (2 ));
191112 this .main .setTranslation (new Vector3f (0 , state .getValue (HANGING ) ? 3 / 16f - 0.005f : 0.005f , 0 ));
192113 this .main .setYaw (direction );
@@ -200,7 +121,7 @@ public void notifyUpdate(HolderAttachment.UpdateType updateType) {
200121 var direction = state .getValue (FACING ).toYRot ();
201122 this .main .setYaw (direction );
202123 this .main .setTranslation (new Vector3f (0 , state .getValue (HANGING ) ? 3 / 16f - 0.005f : 0.005f , 0 ));
203- this .main .setItem (state .getValue (OPEN ) ? BASKET_OPEN : BASKET_CLOSE );
124+ this .main .setItem (state .getValue (OPEN ) ? modelOpen : modelClosed );
204125 this .tick ();
205126 }
206127 }
0 commit comments