Skip to content

Commit 3d6dc17

Browse files
Atilistcalmilamsy
authored andcommitted
Improvements to Saving and Loading
Fixed incorrect Loading for Edge Cases. Fixed Activation Method not properly saving the Activity State. Added State for attempted Activation to ensure that Events do not activate again when it already was attempted in the valid Time Frame. Event Registering and Time Management now automatically clean themselves up.
1 parent 21c511f commit 3d6dc17

5 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/test/java/net/modificationstation/sltest/celestial/CelestialListener.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
99

1010
public class CelestialListener {
11-
private boolean alreadyRegistered = false;
1211

1312
public static CelestialEvent flyingDimando;
1413
public static CelestialEvent fallingDimando;
@@ -18,8 +17,6 @@ public class CelestialListener {
1817

1918
@EventListener
2019
public void registerCelestialEvents(CelestialRegisterEvent event) {
21-
if (alreadyRegistered) return;
22-
alreadyRegistered = true;
2320
SLTest.LOGGER.info("Register celestial events for testing");
2421
flyingDimando = new FlyingDimando(4, "flying_dimando", event.world);
2522
fallingDimando = new DebugCelestialEvent(2, "falling_dimando", event.world);

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,34 @@ public CelestialEvent setExtraDays(int extraDays) {
4747
}
4848

4949
public boolean activateEvent(long worldTime, Random random) {
50+
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
51+
if (initializationNeeded) {
52+
activityState = initializeEvent(activityState);
53+
}
5054
if (active) {
55+
activityState.active = true;
56+
activityState.attemptedActivation = true;
57+
activityState.markDirty();
5158
return true;
5259
}
5360
for (CelestialEvent otherEvent : incompatibleEvents) {
5461
if (otherEvent == null) continue;
5562
if (otherEvent.isActive()) {
5663
active = false;
64+
activityState.active = false;
65+
activityState.attemptedActivation = true;
66+
activityState.markDirty();
5767
return false;
5868
}
5969
}
6070
long days = worldTime / dayLength + dayOffset;
61-
active = days % frequency == 0 && random.nextFloat() <= chance;
71+
if (!activityState.attemptedActivation) {
72+
active = days % frequency == 0 && random.nextFloat() <= chance;
73+
}
6274
if (active) {
75+
activityState.active = true;
76+
activityState.attemptedActivation = true;
77+
activityState.markDirty();
6378
onActivation();
6479
}
6580
return active;
@@ -76,14 +91,17 @@ public void updateEvent(long worldTime) {
7691
if (initializationNeeded) {
7792
activityState = initializeEvent(activityState);
7893
}
79-
if (!active) return;
94+
if (!active && !activityState.active) return;
8095
worldTime -= startingDaytime;
8196
worldTime += endingDaytime;
8297
long days = worldTime / dayLength + dayOffset;
8398
active = days % frequency <= extraDays;
8499
activityState.active = active;
100+
if (!active) {
101+
onDeactivation();
102+
activityState.attemptedActivation = false;
103+
}
85104
activityState.markDirty();
86-
if (!active) onDeactivation();
87105
}
88106

89107
private CelestialEventActivityState initializeEvent(CelestialEventActivityState activityState) {
@@ -129,4 +147,10 @@ public void addIncompatibleEvent(CelestialEvent otherEvent) {
129147
incompatibleEvents.add(otherEvent);
130148
otherEvent.addIncompatibleEvent(this);
131149
}
150+
151+
public void markForInitialization() {
152+
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
153+
initializeEvent(activityState);
154+
initializationNeeded = false;
155+
}
132156
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class CelestialEventActivityState extends PersistentState {
77
public boolean active;
8+
public boolean attemptedActivation;
89

910
public CelestialEventActivityState(String id) {
1011
super(id);
@@ -13,10 +14,12 @@ public CelestialEventActivityState(String id) {
1314
@Override
1415
public void readNbt(NbtCompound nbt) {
1516
this.active = nbt.getBoolean("active");
17+
this.attemptedActivation = nbt.getBoolean("attemptedActivation");
1618
}
1719

1820
@Override
1921
public void writeNbt(NbtCompound nbt) {
2022
nbt.putBoolean("active", active);
23+
nbt.putBoolean("attemptedActivation", attemptedActivation);
2124
}
2225
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,19 @@ public static void updateEvents(long time) {
9393
celestialEvent.updateEvent(time);
9494
}
9595
}
96+
97+
public static void clearLists() {
98+
MORNING_START.clear();
99+
NOON_START.clear();
100+
EVENING_START.clear();
101+
MIDNIGHT_START.clear();
102+
ALL_EVENTS.clear();
103+
}
104+
105+
public static void initializeEvents() {
106+
for (CelestialEvent celestialEvent : ALL_EVENTS) {
107+
if (celestialEvent == null) continue;
108+
celestialEvent.markForInitialization();
109+
}
110+
}
96111
}

station-world-events-v0/src/main/java/net/modificationstation/stationapi/mixin/world/WorldMixin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.minecraft.world.World;
44
import net.modificationstation.stationapi.api.StationAPI;
5+
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
56
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
67
import net.modificationstation.stationapi.api.event.world.WorldEvent;
78
import org.spongepowered.asm.mixin.Mixin;
@@ -20,10 +21,10 @@ class WorldMixin {
2021
at = @At("RETURN")
2122
)
2223
private void stationapi_onCor1(CallbackInfo ci) {
24+
CelestialTimeManager.clearLists();
2325
StationAPI.EVENT_BUS.post(WorldEvent.Init.builder().world(World.class.cast(this)).build());
24-
StationAPI.EVENT_BUS.post(
25-
CelestialRegisterEvent.builder().world(World.class.cast(this)).build()
26-
);
26+
StationAPI.EVENT_BUS.post(CelestialRegisterEvent.builder().world(World.class.cast(this)).build());
27+
CelestialTimeManager.initializeEvents();
2728
}
2829

2930
@Inject(

0 commit comments

Comments
 (0)