Created
December 24, 2025 22:02
-
-
Save Geolykt/2fce5a47416a49c5ede35dce58bfe961 to your computer and use it in GitHub Desktop.
Modern age example on how to use Starloader-API's RollingChartData (as of SLAPI 2.0.0-a20251224)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package de.geolykt.graphs.science; | |
| import java.util.Optional; | |
| import org.jetbrains.annotations.NotNull; | |
| import de.geolykt.starloader.api.Galimulator; | |
| import de.geolykt.starloader.api.NamespacedKey; | |
| import de.geolykt.starloader.api.dimension.Empire; | |
| import de.geolykt.starloader.api.event.EventHandler; | |
| import de.geolykt.starloader.api.event.EventManager; | |
| import de.geolykt.starloader.api.event.Listener; | |
| import de.geolykt.starloader.api.event.lifecycle.GalaxyGeneratedEvent; | |
| import de.geolykt.starloader.api.event.lifecycle.GalaxyLoadingEndEvent; | |
| import de.geolykt.starloader.api.event.lifecycle.GalaxySavingEvent; | |
| import de.geolykt.starloader.api.event.lifecycle.LogicalTickEvent; | |
| import de.geolykt.starloader.api.event.lifecycle.LogicalTickEvent.Phase; | |
| import de.geolykt.starloader.api.gui.canvas.Canvas; | |
| import de.geolykt.starloader.api.gui.canvas.CanvasManager; | |
| import de.geolykt.starloader.api.gui.canvas.CanvasSettings; | |
| import de.geolykt.starloader.api.gui.graph.LineChartCanvasContext; | |
| import de.geolykt.starloader.api.gui.graph.RollingChartData; | |
| import de.geolykt.starloader.api.serial.references.PersistentEmpireReference; | |
| import de.geolykt.starloader.mod.Extension; | |
| public class ScienceChartManager { | |
| private static final int VALIDITY_PERIOD = 1_000; | |
| @NotNull | |
| static RollingChartData<PersistentEmpireReference> scienceData = new RollingChartData<>(ScienceChartManager.VALIDITY_PERIOD); | |
| public static void onGameInit(@NotNull Extension instance) { | |
| EventManager.registerListener(new Listener() { | |
| @EventHandler | |
| public void onTick(@NotNull LogicalTickEvent event) { | |
| if (event.getPhase() != Phase.PRE_LOGICAL || (Galimulator.getGameYear() % 100) != 0) { | |
| return; | |
| } | |
| ScienceChartManager.scienceData.incrementPosition(); | |
| for (Empire e : Galimulator.getUniverse().getEmpiresView()) { | |
| ScienceChartManager.scienceData.addNode(new PersistentEmpireReference(e), e.getTechnologyLevel()); | |
| } | |
| } | |
| @EventHandler | |
| public void onLoad(@NotNull GalaxyLoadingEndEvent event) { | |
| Optional<@NotNull RollingChartData<PersistentEmpireReference>> data = event.getState().getDeserializedForm(new NamespacedKey(instance, "history_science")); | |
| if (data.isPresent()) { | |
| ScienceChartManager.scienceData = data.get(); | |
| } else { | |
| ScienceChartManager.scienceData = new RollingChartData<>(ScienceChartManager.VALIDITY_PERIOD); | |
| } | |
| } | |
| @EventHandler | |
| public void onSave(@NotNull GalaxySavingEvent event) { | |
| event.getMetadataCollector().put(new NamespacedKey(instance, "history_science"), ScienceChartManager.scienceData); | |
| } | |
| @EventHandler | |
| public void onGeneration(@NotNull GalaxyGeneratedEvent event) { | |
| ScienceChartManager.scienceData = new RollingChartData<>(ScienceChartManager.VALIDITY_PERIOD); | |
| } | |
| }); | |
| } | |
| public static void showUI() { | |
| CanvasManager cmgr = CanvasManager.getInstance(); | |
| Canvas canvas = cmgr.newCanvas(new LineChartCanvasContext<>(800, 600, ScienceChartManager.scienceData), new CanvasSettings("Science history")); | |
| canvas.openCanvas(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment