Skip to content

Commit

Permalink
Improved Initialization Logic
Browse files Browse the repository at this point in the history
Moved Initializing Process to CelestialInitializer.
All Events are automatically added to it.
Now Events without a Time Manager are initialized properly.
  • Loading branch information
Atilist committed Jun 25, 2024
1 parent 55c57c3 commit 9148337
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CelestialEvent(int frequency, String name, World world) {
this.frequency = frequency;
this.name = name;
this.world = world;
CelestialInitializer.addEvent(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.modificationstation.stationapi.api.celestial;

import java.util.LinkedList;
import java.util.List;

/**
* Automatically handles initialization of celestial events.
* Ensures that all celestial events are loaded correctly.
*/
public class CelestialInitializer {
private static final List<CelestialEvent> ALL_EVENTS = new LinkedList<>();

/**
* Called automatically by every celestial event.
* @param celestialEvent The event to be added.
*/
public static void addEvent(CelestialEvent celestialEvent) {
ALL_EVENTS.add(celestialEvent);
}

/**
* Prevents duplicates when worlds are switched.
*/
public static void clearList() {
ALL_EVENTS.clear();
}

/**
* Initializes all events when the world is loaded, ensures correct loading of active events.
*/
public static void initializeEvents() {
for (CelestialEvent celestialEvent : ALL_EVENTS) {
if (celestialEvent == null) continue;
celestialEvent.markForInitialization();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,4 @@ public static void clearLists() {
MIDNIGHT_START.clear();
ALL_EVENTS.clear();
}

/**
* Initializes all events when the world is loaded, ensures correct loading of active events.
*/
public static void initializeEvents() {
for (CelestialEvent celestialEvent : ALL_EVENTS) {
if (celestialEvent == null) continue;
celestialEvent.markForInitialization();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.world.World;
import net.modificationstation.stationapi.api.StationAPI;
import net.modificationstation.stationapi.api.celestial.CelestialInitializer;
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
import net.modificationstation.stationapi.api.event.world.WorldEvent;
Expand All @@ -22,9 +23,10 @@ class WorldMixin {
)
private void stationapi_onCor1(CallbackInfo ci) {
CelestialTimeManager.clearLists();
CelestialInitializer.clearList();
StationAPI.EVENT_BUS.post(WorldEvent.Init.builder().world(World.class.cast(this)).build());
StationAPI.EVENT_BUS.post(CelestialRegisterEvent.builder().world(World.class.cast(this)).build());
CelestialTimeManager.initializeEvents();
CelestialInitializer.initializeEvents();
}

@Inject(
Expand Down

0 comments on commit 9148337

Please sign in to comment.