66import com .gregtechceu .gtceu .api .item .MetaMachineItem ;
77import com .gregtechceu .gtceu .api .item .tool .GTToolType ;
88import com .gregtechceu .gtceu .api .item .tool .ToolHelper ;
9- import com .gregtechceu .gtceu .api .machine .IMachineBlockEntity ;
109import com .gregtechceu .gtceu .api .machine .MachineDefinition ;
1110import com .gregtechceu .gtceu .api .machine .MetaMachine ;
1211import com .gregtechceu .gtceu .api .machine .MultiblockMachineDefinition ;
1312import com .gregtechceu .gtceu .api .machine .feature .*;
1413import com .gregtechceu .gtceu .common .data .GTItems ;
1514import com .gregtechceu .gtceu .common .machine .owner .MachineOwner ;
15+ import com .gregtechceu .gtceu .syncsystem .ManagedSyncBlockEntity ;
1616import com .gregtechceu .gtceu .utils .GTUtil ;
1717
1818import net .minecraft .MethodsReturnNonnullByDefault ;
1919import net .minecraft .core .BlockPos ;
2020import net .minecraft .core .Direction ;
2121import net .minecraft .locale .Language ;
22- import net .minecraft .nbt .CompoundTag ;
2322import net .minecraft .network .chat .Component ;
2423import net .minecraft .server .level .ServerPlayer ;
2524import net .minecraft .util .RandomSource ;
3736import net .minecraft .world .level .BlockGetter ;
3837import net .minecraft .world .level .Level ;
3938import net .minecraft .world .level .block .Block ;
39+ import net .minecraft .world .level .block .EntityBlock ;
4040import net .minecraft .world .level .block .RenderShape ;
4141import net .minecraft .world .level .block .Rotation ;
4242import net .minecraft .world .level .block .entity .BlockEntity ;
43+ import net .minecraft .world .level .block .entity .BlockEntityTicker ;
44+ import net .minecraft .world .level .block .entity .BlockEntityType ;
4345import net .minecraft .world .level .block .state .BlockState ;
4446import net .minecraft .world .level .block .state .StateDefinition ;
4547import net .minecraft .world .level .storage .loot .LootParams ;
6062@ SuppressWarnings ("deprecation" )
6163@ MethodsReturnNonnullByDefault
6264@ ParametersAreNonnullByDefault
63- public class MetaMachineBlock extends Block implements IMachineBlock {
65+ public class MetaMachineBlock extends Block implements EntityBlock {
6466
6567 @ Getter
6668 public final MachineDefinition definition ;
@@ -90,6 +92,10 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
9092 }
9193 }
9294
95+ public RotationState getRotationState () {
96+ return getDefinition ().getRotationState ();
97+ }
98+
9399 @ Override
94100 public VoxelShape getShape (BlockState pState , BlockGetter pLevel , BlockPos pPos , CollisionContext pContext ) {
95101 return getRotationState () == RotationState .NONE ? definition .getShape (Direction .NORTH ) :
@@ -104,7 +110,7 @@ public RenderShape getRenderShape(BlockState state) {
104110 @ Override
105111 public void animateTick (BlockState state , Level level , BlockPos pos , RandomSource random ) {
106112 super .animateTick (state , level , pos , random );
107- var machine = getMachine (level , pos );
113+ var machine = MetaMachine . getMachine (level , pos );
108114 if (machine != null ) {
109115 machine .animateTick (random );
110116 }
@@ -114,20 +120,9 @@ public void animateTick(BlockState state, Level level, BlockPos pos, RandomSourc
114120 public void setPlacedBy (Level pLevel , BlockPos pPos , BlockState pState , @ Nullable LivingEntity player ,
115121 ItemStack pStack ) {
116122 if (!pLevel .isClientSide ) {
117- var machine = getMachine (pLevel , pPos );
123+ var machine = MetaMachine . getMachine (pLevel , pPos );
118124 if (machine != null ) {
119- if (player instanceof ServerPlayer sPlayer ) {
120- machine .setOwnerUUID (sPlayer .getUUID ());
121- }
122- }
123- if (machine instanceof IDropSaveMachine dropSaveMachine ) {
124- CompoundTag tag = pStack .getTag ();
125- if (tag != null ) {
126- dropSaveMachine .loadFromItem (tag );
127- }
128- }
129- if (machine instanceof IMachineLife machineLife ) {
130- machineLife .onMachinePlaced (player , pStack );
125+ machine .onMachinePlaced (player , pStack );
131126 }
132127 }
133128 }
@@ -178,7 +173,8 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
178173 @ Override
179174 public ItemStack getCloneItemStack (BlockGetter level , BlockPos pos , BlockState state ) {
180175 ItemStack itemStack = super .getCloneItemStack (level , pos , state );
181- if (getMachine (level , pos ) instanceof IDropSaveMachine dropSaveMachine && dropSaveMachine .savePickClone ()) {
176+ if (MetaMachine .getMachine (level , pos ) instanceof IDropSaveMachine dropSaveMachine &&
177+ dropSaveMachine .savePickClone ()) {
182178 dropSaveMachine .saveToItem (itemStack .getOrCreateTag ());
183179 }
184180 return itemStack ;
@@ -228,8 +224,7 @@ public BlockState rotate(BlockState pState, Rotation pRotation) {
228224 public List <ItemStack > getDrops (BlockState state , LootParams .Builder builder ) {
229225 BlockEntity tileEntity = builder .getOptionalParameter (LootContextParams .BLOCK_ENTITY );
230226 var drops = super .getDrops (state , builder );
231- if (tileEntity instanceof IMachineBlockEntity holder ) {
232- var machine = holder .getMetaMachine ();
227+ if (tileEntity instanceof MetaMachine machine ) {
233228 if (machine instanceof IMachineModifyDrops machineModifyDrops ) {
234229 machineModifyDrops .onDrops (drops );
235230 }
@@ -250,7 +245,7 @@ public List<ItemStack> getDrops(BlockState state, LootParams.Builder builder) {
250245 public void onRemove (BlockState pState , Level pLevel , BlockPos pPos , BlockState pNewState , boolean pIsMoving ) {
251246 if (pState .hasBlockEntity ()) {
252247 if (!pState .is (pNewState .getBlock ())) { // new block
253- MetaMachine machine = getMachine (pLevel , pPos );
248+ MetaMachine machine = MetaMachine . getMachine (pLevel , pPos );
254249 if (machine instanceof IMachineLife machineLife ) {
255250 machineLife .onMachineRemoved ();
256251 }
@@ -266,7 +261,7 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState
266261 var oldFacing = pState .getValue (getRotationState ().property );
267262 var newFacing = pNewState .getValue (getRotationState ().property );
268263 if (newFacing != oldFacing ) {
269- var machine = getMachine (pLevel , pPos );
264+ var machine = MetaMachine . getMachine (pLevel , pPos );
270265 if (machine != null ) {
271266 machine .onRotated (oldFacing , newFacing );
272267 }
@@ -278,7 +273,7 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState
278273 @ Override
279274 public InteractionResult use (BlockState state , Level world , BlockPos pos , Player player , InteractionHand hand ,
280275 BlockHitResult hit ) {
281- var machine = getMachine (world , pos );
276+ var machine = MetaMachine . getMachine (world , pos );
282277 ItemStack itemStack = player .getItemInHand (hand );
283278 boolean shouldOpenUi = true ;
284279
@@ -325,28 +320,32 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
325320 return shouldOpenUi ? InteractionResult .PASS : InteractionResult .CONSUME ;
326321 }
327322
328- @ Override
329- @ SuppressWarnings ("deprecation" ) // This is fine to override, just not to be called.
330- public int getSignal (BlockState state , BlockGetter level , BlockPos pos , Direction direction ) {
331- return getMachine (level , pos ).getOutputSignal (direction );
323+ //////////////////////////////////////
324+ // ***** Redstone Signals ****//
325+ //////////////////////////////////////
326+
327+ public boolean canConnectRedstone (BlockGetter level , BlockPos pos , Direction side ) {
328+ return MetaMachine .getMachine (level , pos ).canConnectRedstone (side );
332329 }
333330
334331 @ Override
335332 @ SuppressWarnings ("deprecation" ) // This is fine to override, just not to be called.
336- public int getDirectSignal (BlockState state , BlockGetter level , BlockPos pos , Direction direction ) {
337- return getMachine (level , pos ).getOutputDirectSignal (direction );
333+ public int getSignal (BlockState state , BlockGetter level , BlockPos pos , Direction direction ) {
334+ return MetaMachine . getMachine (level , pos ).getOutputSignal (direction );
338335 }
339336
340337 @ Override
341338 @ SuppressWarnings ("deprecation" ) // This is fine to override, just not to be called.
342339 public int getAnalogOutputSignal (BlockState state , Level level , BlockPos pos ) {
343- return getMachine (level , pos ).getAnalogOutputSignal ();
340+ return MetaMachine . getMachine (level , pos ).getAnalogOutputSignal ();
344341 }
345342
343+ /////////
344+
346345 @ Override
347346 public void neighborChanged (BlockState state , Level level , BlockPos pos , Block block , BlockPos fromPos ,
348347 boolean isMoving ) {
349- var machine = getMachine (level , pos );
348+ var machine = MetaMachine . getMachine (level , pos );
350349 if (machine != null ) {
351350 machine .onNeighborChanged (block , fromPos , isMoving );
352351 }
@@ -356,7 +355,7 @@ public void neighborChanged(BlockState state, Level level, BlockPos pos, Block b
356355 @ Override
357356 public BlockState getAppearance (BlockState state , BlockAndTintGetter level , BlockPos pos , Direction side ,
358357 @ Nullable BlockState sourceState , @ Nullable BlockPos sourcePos ) {
359- var machine = getMachine (level , pos );
358+ var machine = MetaMachine . getMachine (level , pos );
360359 if (machine != null ) {
361360 return machine .getBlockAppearance (state , level , pos , side , sourceState , sourcePos );
362361 }
@@ -368,4 +367,51 @@ public boolean isValidSpawn(BlockState state, BlockGetter level, BlockPos pos, S
368367 EntityType <?> entityType ) {
369368 return false ;
370369 }
370+
371+ public Direction getFrontFacing (BlockState state ) {
372+ return getRotationState () == RotationState .NONE ? Direction .NORTH : state .getValue (getRotationState ().property );
373+ }
374+
375+ public static int colorTinted (BlockState blockState , @ Nullable BlockAndTintGetter level , @ Nullable BlockPos pos ,
376+ int index ) {
377+ if (level != null && pos != null ) {
378+ var machine = MetaMachine .getMachine (level , pos );
379+ if (machine != null ) {
380+ return machine .tintColor (index );
381+ }
382+ }
383+ return -1 ;
384+ }
385+
386+ @ Nullable
387+ @ Override
388+ public final BlockEntity newBlockEntity (BlockPos pos , BlockState state ) {
389+ return getDefinition ().getBlockEntityType ().create (pos , state );
390+ }
391+
392+ @ Nullable
393+ @ Override
394+ public <T extends BlockEntity > BlockEntityTicker <T > getTicker (Level level , BlockState state ,
395+ BlockEntityType <T > blockEntityType ) {
396+ if (blockEntityType == getDefinition ().getBlockEntityType ()) {
397+ if (!level .isClientSide ) {
398+ return (pLevel , pPos , pState , pTile ) -> {
399+ pTile .setChanged ();
400+ if (pTile instanceof MetaMachine metaMachine ) {
401+ metaMachine .serverTick ();
402+ }
403+ if (pTile instanceof ManagedSyncBlockEntity syncObj ) {
404+ syncObj .updateTick ();
405+ }
406+ };
407+ } else {
408+ return (pLevel , pPos , pState , pTile ) -> {
409+ if (pTile instanceof MetaMachine metaMachine ) {
410+ metaMachine .clientTick ();
411+ }
412+ };
413+ }
414+ }
415+ return null ;
416+ }
371417}
0 commit comments