Skip to content

Commit a0829db

Browse files
Atilistcalmilamsy
authored andcommitted
Functional Saving and Loading
Celestial Events now save and load their Activity Status from NBT, meaning that their Activity gets preserved after closing the Game. Added Initialization Logic to ensure correct Loading of NBT Data when the Game is started.
1 parent d15a44f commit a0829db

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

  • station-world-events-v0/src/main/java/net/modificationstation/stationapi/api/celestial

station-world-events-v0/src/main/java/net/modificationstation/stationapi/api/celestial/CelestialEvent.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class CelestialEvent {
1616
private int endingDaytime = 0;
1717
private int extraDays = 0;
1818
private boolean active;
19+
private boolean initializationNeeded = true;
1920
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
2021
public final World world;
2122

@@ -71,25 +72,32 @@ public void onDeactivation() {
7172
}
7273

7374
public void updateEvent(long worldTime) {
74-
/*
75-
This piece of code for saving the activity state should probably be moved somewhere else. It is also incomplete and not fully functional
7675
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
77-
if (activityState == null) {
78-
activityState = new CelestialEventActivityState(this.name);
79-
this.world.setState(this.name, activityState);
80-
System.out.println("Did not find activity state");
81-
} else {
82-
System.out.println("Found existing activity state");
76+
if (initializationNeeded) {
77+
activityState = initializeEvent(activityState);
8378
}
84-
*/
8579
if (!active) return;
8680
worldTime -= startingDaytime;
8781
worldTime += endingDaytime;
8882
long days = worldTime / dayLength + dayOffset;
8983
active = days % frequency <= extraDays;
84+
activityState.active = active;
85+
activityState.markDirty();
9086
if (!active) onDeactivation();
9187
}
9288

89+
private CelestialEventActivityState initializeEvent(CelestialEventActivityState activityState) {
90+
initializationNeeded = false;
91+
activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
92+
if (activityState == null) {
93+
activityState = new CelestialEventActivityState(this.name);
94+
this.world.setState(this.name, activityState);
95+
} else {
96+
active = activityState.active;
97+
}
98+
return activityState;
99+
}
100+
93101
public void stopEvent() {
94102
onDeactivation();
95103
if (active) {

0 commit comments

Comments
 (0)