Skip to content

Commit 528720e

Browse files
committed
Use notification callback for when CasingComponent updates
1 parent 07da29e commit 528720e

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package aztech.modern_industrialization.machines.components;
2525

2626
import aztech.modern_industrialization.api.energy.CableTier;
27-
import aztech.modern_industrialization.api.energy.CableTierHolder;
2827
import aztech.modern_industrialization.machines.IComponent;
2928
import aztech.modern_industrialization.machines.MachineBlockEntity;
3029
import aztech.modern_industrialization.machines.models.MachineCasing;
@@ -43,15 +42,31 @@
4342
import net.minecraft.world.level.block.Blocks;
4443
import org.jetbrains.annotations.Nullable;
4544

46-
public class CasingComponent implements IComponent, DropableComponent, CableTierHolder {
45+
public class CasingComponent implements IComponent, DropableComponent {
4746

48-
protected ItemStack casingStack = ItemStack.EMPTY;
49-
protected CableTier currentTier = CableTier.LV;
47+
@FunctionalInterface
48+
public interface UpdatedCallback {
49+
void onUpdated(CableTier from, CableTier to);
50+
}
51+
52+
@Nullable
53+
private final UpdatedCallback callback;
54+
55+
private ItemStack casingStack = ItemStack.EMPTY;
56+
private CableTier currentTier = CableTier.LV;
57+
58+
public CasingComponent(@Nullable UpdatedCallback callback) {
59+
this.callback = callback;
60+
}
61+
62+
public CasingComponent() {
63+
this(null);
64+
}
5065

5166
/**
5267
* Sets the current casing stack and update {@link #currentTier} accordingly.
5368
*/
54-
protected void setCasingStack(ItemStack stack) {
69+
private void setCasingStack(ItemStack stack) {
5570
casingStack = stack;
5671

5772
// Compute tier
@@ -81,7 +96,6 @@ public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
8196
currentTier = CableTier.getTier(tag.getString("casing"));
8297
}
8398

84-
@Override
8599
public CableTier getCableTier() {
86100
return currentTier;
87101
}
@@ -93,6 +107,7 @@ public void dropCasing(Level world, BlockPos pos) {
93107
public ItemInteractionResult onUse(MachineBlockEntity be, Player player, InteractionHand hand) {
94108
ItemStack stackInHand = player.getItemInHand(hand);
95109
if (stackInHand.getCount() >= 1) {
110+
var previousTier = currentTier;
96111
var newTier = getCasingTier(stackInHand.getItem());
97112
if (newTier != null && newTier != currentTier) {
98113
if (currentTier != CableTier.LV) {
@@ -109,13 +124,16 @@ public ItemInteractionResult onUse(MachineBlockEntity be, Player player, Interac
109124
be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR);
110125
// Play a nice sound :)
111126
playCasingPlaceSound(be);
127+
if (callback != null) {
128+
callback.onUpdated(previousTier, currentTier);
129+
}
112130
return ItemInteractionResult.sidedSuccess(be.getLevel().isClientSide);
113131
}
114132
}
115133
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
116134
}
117135

118-
protected void playCasingPlaceSound(MachineBlockEntity be) {
136+
private void playCasingPlaceSound(MachineBlockEntity be) {
119137
var blockKey = currentTier.itemKey;
120138
if (blockKey == null) {
121139
return; // no sound for LV
@@ -148,11 +166,15 @@ public ItemStack getDrop() {
148166
}
149167

150168
public void setCasingServer(MachineBlockEntity be, ItemStack casing) {
169+
var previousTier = currentTier;
151170
setCasingStack(casing);
152171
be.setChanged();
153172
be.sync();
154173
be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR);
155174
playCasingPlaceSound(be);
175+
if (callback != null && previousTier != currentTier) {
176+
callback.onUpdated(previousTier, currentTier);
177+
}
156178
}
157179

158180
public MachineCasing getCasing() {

0 commit comments

Comments
 (0)