29
29
import net .fabricmc .fabric .api .transfer .v1 .transaction .TransactionContext ;
30
30
import net .minecraft .nbt .ListTag ;
31
31
import net .minecraft .network .RegistryFriendlyByteBuf ;
32
+ import net .minecraft .world .level .block .entity .BlockEntity ;
32
33
import org .jetbrains .annotations .NotNull ;
33
34
import org .jetbrains .annotations .Nullable ;
34
35
35
36
public abstract class ResourceStorageImpl <Resource , Slot extends ResourceSlot <Resource >> extends BaseSlottedStorage <Resource , Slot > implements ResourceStorage <Resource , Slot >, TransactionContext .CloseCallback {
36
37
private final LongList transactions = new LongArrayList ();
37
38
private long modifications = 1 ;
38
- private Runnable listener ;
39
+ private @ Nullable BlockEntity parent ;
39
40
40
41
public ResourceStorageImpl (@ NotNull Slot @ NotNull [] slots ) {
41
42
super (slots );
@@ -45,8 +46,13 @@ public ResourceStorageImpl(@NotNull Slot @NotNull [] slots) {
45
46
}
46
47
47
48
@ Override
48
- public void setListener (Runnable listener ) {
49
- this .listener = listener ;
49
+ public void setParent (@ Nullable BlockEntity parent ) {
50
+ this .parent = parent ;
51
+ }
52
+
53
+ @ Override
54
+ public boolean isValid () {
55
+ return this .parent == null || !this .parent .isRemoved ();
50
56
}
51
57
52
58
@ Override
@@ -57,7 +63,7 @@ public long getModifications() {
57
63
@ Override
58
64
public void markModified () {
59
65
this .modifications ++;
60
- if (this .listener != null ) this .listener . run ();
66
+ if (this .parent != null ) this .parent . setChanged ();
61
67
}
62
68
63
69
@ Override
@@ -92,7 +98,7 @@ public void onClose(TransactionContext transaction, TransactionContext.Result re
92
98
this .transactions .clear ();
93
99
transaction .addOuterCloseCallback ((res ) -> {
94
100
assert res .wasCommitted ();
95
- if (this .listener != null ) this .listener . run ();
101
+ if (this .parent != null ) this .parent . setChanged ();
96
102
});
97
103
}
98
104
}
0 commit comments