23
23
import net .minecraft .core .component .DataComponentMap ;
24
24
import net .minecraft .core .component .DataComponents ;
25
25
import net .minecraft .nbt .CompoundTag ;
26
+ import net .minecraft .nbt .Tag ;
26
27
import net .minecraft .network .chat .Component ;
27
28
import net .minecraft .network .protocol .game .ClientboundBlockEntityDataPacket ;
28
29
import net .minecraft .world .Container ;
@@ -43,10 +44,12 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements
43
44
private static final String NBT_ID = "ComputerId" ;
44
45
private static final String NBT_LABEL = "Label" ;
45
46
private static final String NBT_ON = "On" ;
47
+ private static final String NBT_CAPACITY = "Capacity" ;
46
48
47
49
private @ Nullable UUID instanceID = null ;
48
50
private int computerID = -1 ;
49
51
protected @ Nullable String label = null ;
52
+ protected long storageCapacity = -1 ;
50
53
private boolean on = false ;
51
54
boolean startOn = false ;
52
55
private boolean fresh = false ;
@@ -143,6 +146,7 @@ public void saveAdditional(CompoundTag nbt, HolderLookup.Provider registries) {
143
146
// Save ID, label and power state
144
147
if (computerID >= 0 ) nbt .putInt (NBT_ID , computerID );
145
148
if (label != null ) nbt .putString (NBT_LABEL , label );
149
+ if (storageCapacity > 0 ) nbt .putLong (NBT_CAPACITY , storageCapacity );
146
150
nbt .putBoolean (NBT_ON , on );
147
151
148
152
lockCode .addToTag (nbt );
@@ -164,6 +168,7 @@ protected void loadServer(CompoundTag nbt, HolderLookup.Provider registries) {
164
168
// Load ID, label and power state
165
169
computerID = nbt .contains (NBT_ID ) ? nbt .getInt (NBT_ID ) : -1 ;
166
170
label = nbt .contains (NBT_LABEL ) ? nbt .getString (NBT_LABEL ) : null ;
171
+ storageCapacity = nbt .contains (NBT_CAPACITY , Tag .TAG_ANY_NUMERIC ) ? nbt .getLong (NBT_CAPACITY ) : -1 ;
167
172
on = startOn = nbt .getBoolean (NBT_ON );
168
173
169
174
lockCode = LockCode .fromTag (nbt );
@@ -174,6 +179,7 @@ protected void applyImplicitComponents(DataComponentInput component) {
174
179
super .applyImplicitComponents (component );
175
180
label = DataComponentUtil .getCustomName (component .get (DataComponents .CUSTOM_NAME ));
176
181
computerID = NonNegativeId .getId (component .get (ModRegistry .DataComponents .COMPUTER_ID .get ()));
182
+ storageCapacity = StorageCapacity .getOrDefault (component .get (ModRegistry .DataComponents .STORAGE_CAPACITY .get ()), -1 );
177
183
lockCode = component .getOrDefault (DataComponents .LOCK , LockCode .NO_LOCK );
178
184
}
179
185
@@ -182,6 +188,7 @@ protected void collectImplicitComponents(DataComponentMap.Builder builder) {
182
188
super .collectImplicitComponents (builder );
183
189
builder .set (ModRegistry .DataComponents .COMPUTER_ID .get (), NonNegativeId .of (computerID ));
184
190
builder .set (DataComponents .CUSTOM_NAME , label == null ? null : Component .literal (label ));
191
+ builder .set (ModRegistry .DataComponents .STORAGE_CAPACITY .get (), storageCapacity > 0 ? new StorageCapacity (storageCapacity ) : null );
185
192
if (lockCode != LockCode .NO_LOCK ) builder .set (DataComponents .LOCK , lockCode );
186
193
}
187
194
@@ -191,6 +198,7 @@ public void removeComponentsFromTag(CompoundTag tag) {
191
198
super .removeComponentsFromTag (tag );
192
199
tag .remove (NBT_ID );
193
200
tag .remove (NBT_LABEL );
201
+ tag .remove (NBT_CAPACITY );
194
202
tag .remove (LockCode .TAG_LOCK );
195
203
}
196
204
@@ -397,14 +405,16 @@ public final ClientboundBlockEntityDataPacket getUpdatePacket() {
397
405
public CompoundTag getUpdateTag (HolderLookup .Provider registries ) {
398
406
// We need this for pick block on the client side.
399
407
var nbt = super .getUpdateTag (registries );
400
- if (label != null ) nbt .putString (NBT_LABEL , label );
401
408
if (computerID >= 0 ) nbt .putInt (NBT_ID , computerID );
409
+ if (label != null ) nbt .putString (NBT_LABEL , label );
410
+ if (storageCapacity > 0 ) nbt .putLong (NBT_CAPACITY , storageCapacity );
402
411
return nbt ;
403
412
}
404
413
405
414
protected void loadClient (CompoundTag nbt , HolderLookup .Provider registries ) {
406
- label = nbt .contains (NBT_LABEL ) ? nbt .getString (NBT_LABEL ) : null ;
407
415
computerID = nbt .contains (NBT_ID ) ? nbt .getInt (NBT_ID ) : -1 ;
416
+ label = nbt .contains (NBT_LABEL ) ? nbt .getString (NBT_LABEL ) : null ;
417
+ storageCapacity = nbt .contains (NBT_CAPACITY , Tag .TAG_ANY_NUMERIC ) ? nbt .getLong (NBT_CAPACITY ) : -1 ;
408
418
}
409
419
410
420
protected void transferStateFrom (AbstractComputerBlockEntity copy ) {
@@ -413,6 +423,7 @@ protected void transferStateFrom(AbstractComputerBlockEntity copy) {
413
423
instanceID = copy .instanceID ;
414
424
computerID = copy .computerID ;
415
425
label = copy .label ;
426
+ storageCapacity = copy .storageCapacity ;
416
427
on = copy .on ;
417
428
startOn = copy .startOn ;
418
429
lockCode = copy .lockCode ;
0 commit comments