@@ -26,6 +26,7 @@ public class DistanceDetectorPeripheral extends BasePeripheral<IPeripheralOwner>
2626
2727 public static final String PERIPHERAL_TYPE = "distance_detector" ;
2828
29+ private final AtomicBoolean isDirty = new AtomicBoolean (false );
2930 private final DistanceDetectorEntity tileEntity ;
3031 private final AtomicInteger maxRange ;
3132 private volatile float currentDistance ;
@@ -37,6 +38,7 @@ public class DistanceDetectorPeripheral extends BasePeripheral<IPeripheralOwner>
3738 public DistanceDetectorPeripheral (DistanceDetectorEntity tileEntity ) {
3839 super (PERIPHERAL_TYPE , new BlockEntityPeripheralOwner <>(tileEntity ));
3940 this .tileEntity = tileEntity ;
41+ // TODO: let distance detector block also use data storage
4042 this .maxRange = new AtomicInteger (Float .floatToRawIntBits (this .tileEntity .getMaxRange ()));
4143 this .currentDistance = this .tileEntity .getCurrentDistance ();
4244 this .showLaser = new AtomicBoolean (this .tileEntity .getShowLaser ());
@@ -45,7 +47,6 @@ public DistanceDetectorPeripheral(DistanceDetectorEntity tileEntity) {
4547 this .detectionType = new AtomicReference <>(this .tileEntity .getDetectionType ());
4648 }
4749
48- // TODO: thread safely save data
4950 protected DistanceDetectorPeripheral (IPeripheralOwner owner ) {
5051 super (PERIPHERAL_TYPE , owner );
5152 this .tileEntity = null ;
@@ -89,24 +90,20 @@ public void setMaxRange(float maxRange) {
8990 this .tileEntity .setMaxRange (maxRange );
9091 this .tileEntity .sendUpdate ();
9192 }
92- this .owner . markDataStorageDirty ( );
93+ this .isDirty . set ( true );
9394 }
9495
9596 public float getCurrentDistance () {
9697 return this .currentDistance ;
9798 }
9899
99100 public void setCurrentDistance (float currentDistance ) {
100- // Since setCurrentDistance should only invokes from main thread, volatile field should be safe here.
101- if (this .currentDistance == currentDistance ) {
102- return ;
103- }
104101 this .currentDistance = currentDistance ;
105102 if (this .tileEntity != null ) {
106103 this .tileEntity .setCurrentDistance (currentDistance );
107104 this .tileEntity .sendUpdate ();
108105 }
109- this .owner . markDataStorageDirty ( );
106+ this .isDirty . set ( true );
110107 }
111108
112109 public boolean getCalculatePeriodically () {
@@ -118,7 +115,7 @@ public void setCalculatePeriodically(boolean calculatePeriodically) {
118115 if (this .tileEntity != null ) {
119116 this .tileEntity .setCalculatePeriodically (calculatePeriodically );
120117 }
121- this .owner . markDataStorageDirty ( );
118+ this .isDirty . set ( true );
122119 }
123120
124121 public boolean getShowLaser () {
@@ -133,7 +130,7 @@ public void setShowLaser(boolean showLaser) {
133130 this .tileEntity .setShowLaser (showLaser );
134131 this .tileEntity .sendUpdate ();
135132 }
136- this .owner . markDataStorageDirty ( );
133+ this .isDirty . set ( true );
137134 }
138135
139136 public boolean getIgnoreTransparent () {
@@ -145,7 +142,7 @@ public void setIgnoreTransparent(boolean ignoreTransparent) {
145142 if (this .tileEntity != null ) {
146143 this .tileEntity .setIgnoreTransparent (ignoreTransparent );
147144 }
148- this .owner . markDataStorageDirty ( );
145+ this .isDirty . set ( true );
149146 }
150147
151148 public DetectionType getDetectionType () {
@@ -159,7 +156,7 @@ public void setDetectionType(DetectionType detectionType) {
159156 if (this .tileEntity != null ) {
160157 this .tileEntity .setDetectionType (detectionType );
161158 }
162- this .owner . markDataStorageDirty ( );
159+ this .isDirty . set ( true );
163160 }
164161
165162 @ LuaFunction
@@ -286,6 +283,19 @@ public void update() {
286283 // It should be okay to run that function every 2 ticks, calculating it does not take too much time.
287284 this .calculateAndUpdateDistance ();
288285 }
286+
287+ if (this .isDirty .getAndSet (false )) {
288+ if (this .tileEntity == null ) {
289+ CompoundTag data = this .owner .getDataStorage ();
290+ data .putFloat ("maxRange" , this .getMaxRange ());
291+ data .putFloat ("currentDistance" , this .getCurrentDistance ());
292+ data .putBoolean ("showLaser" , this .getShowLaser ());
293+ data .putBoolean ("calculatePeriodically" , this .getCalculatePeriodically ());
294+ data .putBoolean ("ignoreTransparent" , this .getIgnoreTransparent ());
295+ data .putByte ("detectionType" , (byte ) this .getDetectionType ().ordinal ());
296+ }
297+ this .owner .markDataStorageDirty ();
298+ }
289299 }
290300
291301 protected HitResult getHitResult (Vec3 from , Vec3 to ) {
0 commit comments