Skip to content

Commit 9148337

Browse files
committed
Improved Initialization Logic
Moved Initializing Process to CelestialInitializer. All Events are automatically added to it. Now Events without a Time Manager are initialized properly.
1 parent 55c57c3 commit 9148337

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public CelestialEvent(int frequency, String name, World world) {
3939
this.frequency = frequency;
4040
this.name = name;
4141
this.world = world;
42+
CelestialInitializer.addEvent(this);
4243
}
4344

4445
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.modificationstation.stationapi.api.celestial;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Automatically handles initialization of celestial events.
8+
* Ensures that all celestial events are loaded correctly.
9+
*/
10+
public class CelestialInitializer {
11+
private static final List<CelestialEvent> ALL_EVENTS = new LinkedList<>();
12+
13+
/**
14+
* Called automatically by every celestial event.
15+
* @param celestialEvent The event to be added.
16+
*/
17+
public static void addEvent(CelestialEvent celestialEvent) {
18+
ALL_EVENTS.add(celestialEvent);
19+
}
20+
21+
/**
22+
* Prevents duplicates when worlds are switched.
23+
*/
24+
public static void clearList() {
25+
ALL_EVENTS.clear();
26+
}
27+
28+
/**
29+
* Initializes all events when the world is loaded, ensures correct loading of active events.
30+
*/
31+
public static void initializeEvents() {
32+
for (CelestialEvent celestialEvent : ALL_EVENTS) {
33+
if (celestialEvent == null) continue;
34+
celestialEvent.markForInitialization();
35+
}
36+
}
37+
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,4 @@ public static void clearLists() {
147147
MIDNIGHT_START.clear();
148148
ALL_EVENTS.clear();
149149
}
150-
151-
/**
152-
* Initializes all events when the world is loaded, ensures correct loading of active events.
153-
*/
154-
public static void initializeEvents() {
155-
for (CelestialEvent celestialEvent : ALL_EVENTS) {
156-
if (celestialEvent == null) continue;
157-
celestialEvent.markForInitialization();
158-
}
159-
}
160150
}

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

Lines changed: 3 additions & 1 deletion
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.CelestialInitializer;
56
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
67
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
78
import net.modificationstation.stationapi.api.event.world.WorldEvent;
@@ -22,9 +23,10 @@ class WorldMixin {
2223
)
2324
private void stationapi_onCor1(CallbackInfo ci) {
2425
CelestialTimeManager.clearLists();
26+
CelestialInitializer.clearList();
2527
StationAPI.EVENT_BUS.post(WorldEvent.Init.builder().world(World.class.cast(this)).build());
2628
StationAPI.EVENT_BUS.post(CelestialRegisterEvent.builder().world(World.class.cast(this)).build());
27-
CelestialTimeManager.initializeEvents();
29+
CelestialInitializer.initializeEvents();
2830
}
2931

3032
@Inject(

0 commit comments

Comments
 (0)