Skip to content

Commit 34d9789

Browse files
committed
fix data storage for smartglass and save distance detector data in smartglass
1 parent 17d99c3 commit 34d9789

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/main/java/de/srendi/advancedperipherals/common/items/SmartGlassesItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public SmartGlassesComputer getOrCreateComputer(ServerLevel level, Entity entity
248248
setComputerID(stack, computerID);
249249
}
250250

251-
computer = new SmartGlassesComputer(level, getComputerID(stack), getLabel(stack), getFamily(), stack.getOrCreateTag().getCompound(SmartGlassesComputer.UPGRADE_DATAS_TAG).copy());
251+
computer = new SmartGlassesComputer(level, getComputerID(stack), getLabel(stack), getFamily(), stack.getOrCreateTag().getCompound(SmartGlassesComputer.UPGRADE_DATAS_TAG));
252252

253253
setInstanceID(stack, computer.register());
254254
setSessionID(stack, registry.getSessionID());

src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesAccess.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ public void setLight(int colour) {
4343

4444
@Override
4545
public CompoundTag getUpgradeNBTData() {
46-
return new CompoundTag();
46+
return computer.getUpgradeNBTData();
4747
}
4848

4949
@Override
5050
public void updateUpgradeNBTData() {
51+
computer.updateUpgradeNBTData();
5152
}
5253

5354
@Override
5455
public void invalidatePeripheral() {
56+
computer.invalidatePeripheral();
5557
}
5658

5759
@Override

src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesComputer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,18 @@ private void updatePeripheralsAndModules(SmartGlassesItemHandler itemHandler) {
212212
public void tickServer() {
213213
super.tickServer();
214214

215+
boolean shouldUpdateInventory = this.peripheralOutdated || this.isDirty;
215216
if (this.peripheralOutdated && this.itemHandler != null) {
216217
this.peripheralOutdated = false;
217218
this.updatePeripheralsAndModules(this.itemHandler);
218219
}
219-
220220
if (this.isDirty) {
221221
this.isDirty = false;
222222
CompoundTag data = this.stack.getOrCreateTag();
223223
data.put(UPGRADE_DATAS_TAG, this.upgradeDatas.copy());
224-
if (entity instanceof Player player) {
225-
player.getInventory().setChanged();
226-
}
224+
}
225+
if (shouldUpdateInventory && entity instanceof Player player) {
226+
player.getInventory().setChanged();
227227
}
228228

229229
this.modules.values().forEach(module -> {

0 commit comments

Comments
 (0)