Skip to content

Commit 9b63928

Browse files
committed
god class metamachine
1 parent d377968 commit 9b63928

File tree

3 files changed

+77
-65
lines changed

3 files changed

+77
-65
lines changed

src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,7 @@ public void setPlacedBy(Level pLevel, BlockPos pPos, BlockState pState, @Nullabl
123123
if (!pLevel.isClientSide) {
124124
var machine = MetaMachine.getMachine(pLevel, pPos);
125125
if (machine != null) {
126-
if (player instanceof ServerPlayer sPlayer) {
127-
machine.setOwnerUUID(sPlayer.getUUID());
128-
}
129-
}
130-
if (machine instanceof IDropSaveMachine dropSaveMachine) {
131-
CompoundTag tag = pStack.getTag();
132-
if (tag != null) {
133-
dropSaveMachine.loadFromItem(tag);
134-
}
135-
}
136-
if (machine instanceof IMachineLife machineLife) {
137-
machineLife.onMachinePlaced(player, pStack);
126+
machine.onMachinePlaced(player, pStack);
138127
}
139128
}
140129
}

src/main/java/com/gregtechceu/gtceu/api/machine/MetaMachine.java

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.gregtechceu.gtceu.syncsystem.annotations.SaveField;
4141
import com.gregtechceu.gtceu.syncsystem.annotations.SyncToClient;
4242

43+
import com.gregtechceu.gtceu.utils.GTUtil;
4344
import com.lowdragmc.lowdraglib.gui.texture.IGuiTexture;
4445
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
4546
import com.lowdragmc.lowdraglib.utils.DummyWorld;
@@ -52,10 +53,13 @@
5253
import net.minecraft.core.BlockPos;
5354
import net.minecraft.core.Direction;
5455
import net.minecraft.locale.Language;
56+
import net.minecraft.nbt.CompoundTag;
5557
import net.minecraft.network.chat.Component;
58+
import net.minecraft.server.level.ServerPlayer;
5659
import net.minecraft.util.RandomSource;
5760
import net.minecraft.world.InteractionHand;
5861
import net.minecraft.world.InteractionResult;
62+
import net.minecraft.world.entity.LivingEntity;
5963
import net.minecraft.world.entity.player.Player;
6064
import net.minecraft.world.item.ItemStack;
6165
import net.minecraft.world.item.context.UseOnContext;
@@ -102,7 +106,7 @@
102106
@ParametersAreNonnullByDefault
103107
@MethodsReturnNonnullByDefault
104108
public 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
//////////////////////////////////////

src/main/java/com/gregtechceu/gtceu/common/machine/storage/CrateMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public InteractionResult onUse(BlockState state, Level world, BlockPos pos, Play
108108

109109
@Override
110110
public void onMachinePlaced(@Nullable LivingEntity player, ItemStack stack) {
111-
IMachineLife.super.onMachinePlaced(player, stack);
111+
super.onMachinePlaced(player, stack);
112112
CompoundTag tag = stack.getTag();
113113
if (tag != null) {
114114
if (tag.contains("taped") && tag.getBoolean("taped")) {

0 commit comments

Comments
 (0)