4040import com .gregtechceu .gtceu .syncsystem .annotations .SaveField ;
4141import com .gregtechceu .gtceu .syncsystem .annotations .SyncToClient ;
4242
43+ import com .gregtechceu .gtceu .utils .GTUtil ;
4344import com .lowdragmc .lowdraglib .gui .texture .IGuiTexture ;
4445import com .lowdragmc .lowdraglib .gui .texture .ResourceTexture ;
4546import com .lowdragmc .lowdraglib .utils .DummyWorld ;
5253import net .minecraft .core .BlockPos ;
5354import net .minecraft .core .Direction ;
5455import net .minecraft .locale .Language ;
56+ import net .minecraft .nbt .CompoundTag ;
5557import net .minecraft .network .chat .Component ;
58+ import net .minecraft .server .level .ServerPlayer ;
5659import net .minecraft .util .RandomSource ;
5760import net .minecraft .world .InteractionHand ;
5861import net .minecraft .world .InteractionResult ;
62+ import net .minecraft .world .entity .LivingEntity ;
5963import net .minecraft .world .entity .player .Player ;
6064import net .minecraft .world .item .ItemStack ;
6165import net .minecraft .world .item .context .UseOnContext ;
102106@ ParametersAreNonnullByDefault
103107@ MethodsReturnNonnullByDefault
104108public class MetaMachine extends ManagedSyncBlockEntity implements IGregtechBlockEntity , IToolable , IToolGridHighlight ,
105- IFancyTooltip , IPaintable {
109+ IFancyTooltip , IPaintable , IMachineFeature {
106110
107111 public static final ModelProperty <BlockAndTintGetter > MODEL_DATA_LEVEL = new ModelProperty <>();
108112 public static final ModelProperty <BlockPos > MODEL_DATA_POS = new ModelProperty <>();
@@ -130,9 +134,6 @@ public class MetaMachine extends ManagedSyncBlockEntity implements IGregtechBloc
130134 @ Getter
131135 protected final List <MachineTrait > traits ;
132136
133- private final List <TickableSubscription > serverTicks ;
134- private final List <TickableSubscription > waitingToAdd ;
135-
136137 @ Getter
137138 @ Setter
138139 @ SaveField
@@ -152,58 +153,27 @@ public MetaMachine(BlockEntityCreationInfo info) {
152153 }
153154
154155 //////////////////////////////////////
155- // ***** Initialization ******//
156+ // ***** Machine Lifecycle ******//
156157 //////////////////////////////////////
157158
158- @ Override
159- public final void setRemoved () {
160- super .setRemoved ();
161- onUnload ();
162- }
163-
164- public @ UnknownNullability Level getLevel () {
165- return super .getLevel ();
166- }
167-
168- public void setOwnerUUID (UUID uuid ) {
169- ownerUUID = uuid ;
170- syncDataHolder .markClientSyncFieldDirty ("ownerUUID" );
171- }
172-
173- @ Override
174- public boolean triggerEvent (int id , int para ) {
175- if (id == 1 ) { // chunk re render
176- if (level != null && level .isClientSide ) {
177- scheduleRenderUpdate ();
178- }
179- return true ;
159+ public void onMachinePlaced (@ Nullable LivingEntity player , ItemStack stack ) {
160+ if (player instanceof ServerPlayer sPlayer ) {
161+ ownerUUID = sPlayer .getUUID ();
180162 }
181- return false ;
182- }
183163
184- public void setPaintingColor (int color ) {
185- if (color == this .paintingColor ) return ;
186-
187- this .paintingColor = color ;
188- syncDataHolder .markClientSyncFieldDirty ("paintingColor" );
189- this .onPaintingColorChanged (color );
190-
191- MachineRenderState renderState = getRenderState ();
192- if (renderState .hasProperty (GTMachineModelProperties .IS_PAINTED )) {
193- setRenderState (renderState .setValue (GTMachineModelProperties .IS_PAINTED , this .isPainted ()));
164+ if (this instanceof IDropSaveMachine dropSaveMachine ) {
165+ CompoundTag tag = stack .getTag ();
166+ if (tag != null ) {
167+ dropSaveMachine .loadFromItem (tag );
168+ }
194169 }
195170 }
196171
197- public void onPaintingColorChanged (int color ) {}
198-
199- @ OverridingMethodsMustInvokeSuper
200- public void onUnload () {
201- traits .forEach (MachineTrait ::onMachineUnLoad );
202- coverContainer .onUnload ();
203- for (TickableSubscription serverTick : serverTicks ) {
204- serverTick .unsubscribe ();
172+ public void onRemoved () {
173+ for (Direction direction : GTUtil .DIRECTIONS ) {
174+ getCoverContainer ().removeCover (direction , null );
205175 }
206- serverTicks . clear ();
176+ if ( this instanceof IMachineLife l ) l . onMachineRemoved ();
207177 }
208178
209179 @ OverridingMethodsMustInvokeSuper
@@ -219,16 +189,29 @@ public void onLoad() {
219189 }
220190 }
221191
222- public void notifyBlockUpdate () {
223- if (getLevel () != null ) {
224- getLevel ().updateNeighborsAt (getBlockPos (), getLevel ().getBlockState (getBlockPos ()).getBlock ());
192+ @ Override
193+ public final void setRemoved () {
194+ super .setRemoved ();
195+ onUnload ();
196+ }
197+
198+ @ OverridingMethodsMustInvokeSuper
199+ public void onUnload () {
200+ traits .forEach (MachineTrait ::onMachineUnLoad );
201+ coverContainer .onUnload ();
202+ for (TickableSubscription serverTick : serverTicks ) {
203+ serverTick .unsubscribe ();
225204 }
205+ serverTicks .clear ();
226206 }
227207
228208 //////////////////////////////////////
229209 // ***** Tickable Manager ****//
230210 //////////////////////////////////////
231211
212+ private final List <TickableSubscription > serverTicks ;
213+ private final List <TickableSubscription > waitingToAdd ;
214+
232215 /**
233216 * For initialization. To get level and property fields after auto sync, you can subscribe it in {@link #onLoad()}
234217 * event.
@@ -469,6 +452,47 @@ public static MetaMachine getMachine(BlockGetter level, BlockPos pos) {
469452 return null ;
470453 }
471454
455+ public void notifyBlockUpdate () {
456+ if (getLevel () != null ) {
457+ getLevel ().updateNeighborsAt (getBlockPos (), getLevel ().getBlockState (getBlockPos ()).getBlock ());
458+ }
459+ }
460+
461+ public @ UnknownNullability Level getLevel () {
462+ return super .getLevel ();
463+ }
464+
465+ public void setOwnerUUID (UUID uuid ) {
466+ ownerUUID = uuid ;
467+ syncDataHolder .markClientSyncFieldDirty ("ownerUUID" );
468+ }
469+
470+ @ Override
471+ public boolean triggerEvent (int id , int para ) {
472+ if (id == 1 ) { // chunk re render
473+ if (level != null && level .isClientSide ) {
474+ scheduleRenderUpdate ();
475+ }
476+ return true ;
477+ }
478+ return false ;
479+ }
480+
481+ public void setPaintingColor (int color ) {
482+ if (color == this .paintingColor ) return ;
483+
484+ this .paintingColor = color ;
485+ syncDataHolder .markClientSyncFieldDirty ("paintingColor" );
486+ this .onPaintingColorChanged (color );
487+
488+ MachineRenderState renderState = getRenderState ();
489+ if (renderState .hasProperty (GTMachineModelProperties .IS_PAINTED )) {
490+ setRenderState (renderState .setValue (GTMachineModelProperties .IS_PAINTED , this .isPainted ()));
491+ }
492+ }
493+
494+ public void onPaintingColorChanged (int color ) {}
495+
472496 /**
473497 * All traits should be initialized while MetaMachine is creating. you cannot add them on the fly.
474498 */
@@ -722,7 +746,6 @@ public boolean canConnectRedstone(@NotNull Direction side) {
722746 return cover .canConnectRedstone ();
723747 }
724748
725-
726749 //////////////////////////////////////
727750 // ****** Ownership ********//
728751 //////////////////////////////////////
0 commit comments