Skip to content

Commit 7933881

Browse files
committed
Event Dependency Feature and important Bug Fix
Events can now have Dependencies. Events only activate if their Dependencies are active. Fixed a Bug that caused attempted Activations to never be reset, leading to Events never happening again.
1 parent 9148337 commit 7933881

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class CelestialListener {
1111

1212
public static CelestialEvent flyingDimando;
1313
public static CelestialEvent fallingDimando;
14+
public static CelestialEvent crashingDimando;
1415
public static CelestialEvent spinningDimando;
1516
public static CelestialEvent burningDimando;
1617
public static CelestialEvent longDimando;
@@ -20,13 +21,16 @@ public void registerCelestialEvents(CelestialRegisterEvent event) {
2021
SLTest.LOGGER.info("Register celestial events for testing");
2122
flyingDimando = new FlyingDimando(4, "flying_dimando", event.world);
2223
fallingDimando = new DebugCelestialEvent(2, "falling_dimando", event.world);
24+
crashingDimando = new DebugCelestialEvent(2, "crashing_dimando", event.world);
2325
spinningDimando = new DebugCelestialEvent(4, "spinning_dimando", event.world).setDayOffset(1);
2426
burningDimando = new DebugCelestialEvent(2, "burning_dimando", event.world).setDayOffset(1);
2527
longDimando = new DebugCelestialEvent(12, "long_dimando", event.world).setExtraDays(4);
2628
flyingDimando.addIncompatibleEvent(fallingDimando);
2729
spinningDimando.addIncompatibleEvent(burningDimando);
30+
crashingDimando.addDependency(longDimando);
2831
CelestialTimeManager.addCelestialEvent(flyingDimando, DayQuarter.MORNING, DayQuarter.MORNING);
2932
CelestialTimeManager.addCelestialEvent(fallingDimando, DayQuarter.NOON, DayQuarter.MIDNIGHT);
33+
CelestialTimeManager.addCelestialEvent(crashingDimando, DayQuarter.NOON, DayQuarter.NOON);
3034
CelestialTimeManager.addCelestialEvent(spinningDimando, DayQuarter.EVENING, DayQuarter.NOON);
3135
CelestialTimeManager.addCelestialEvent(burningDimando, DayQuarter.MIDNIGHT, DayQuarter.EVENING);
3236
CelestialTimeManager.addCelestialEvent(longDimando, DayQuarter.MIDNIGHT, DayQuarter.MIDNIGHT);

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class CelestialEvent {
2626
private boolean active;
2727
private boolean initializationNeeded = true;
2828
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
29+
private final List<CelestialEvent> dependencies = new LinkedList<>();
2930
public final World world;
3031

3132
/**
@@ -118,9 +119,22 @@ public boolean activateEvent(long worldTime, Random random) {
118119
return false;
119120
}
120121
}
121-
long days = worldTime / dayLength + dayOffset;
122+
for (CelestialEvent otherEvent : dependencies) {
123+
if (otherEvent == null) continue;
124+
if (!otherEvent.isActive()) {
125+
activityState.active = false;
126+
activityState.attemptedActivation = true;
127+
activityState.markDirty();
128+
return false;
129+
}
130+
}
131+
long days = (worldTime / dayLength) + dayOffset;
132+
if (days % frequency > extraDays) {
133+
activityState.attemptedActivation = false;
134+
return false;
135+
}
122136
if (!activityState.attemptedActivation) {
123-
active = days % frequency == 0 && random.nextFloat() <= chance;
137+
active = days % frequency <= extraDays && random.nextFloat() <= chance;
124138
}
125139
if (active) {
126140
activityState.active = true;
@@ -162,7 +176,7 @@ public void updateEvent(long worldTime) {
162176
if (!active && !activityState.active) return;
163177
worldTime -= startingDaytime;
164178
worldTime += endingDaytime;
165-
long days = worldTime / dayLength + dayOffset;
179+
long days = (worldTime / dayLength) + dayOffset;
166180
active = days % frequency <= extraDays;
167181
activityState.active = active;
168182
if (!active) {
@@ -234,6 +248,17 @@ public void addIncompatibleEvent(CelestialEvent otherEvent) {
234248
otherEvent.addIncompatibleEvent(this);
235249
}
236250

251+
/**
252+
* Adds dependency to an event, meaning another event has to happen for this one to happen.
253+
* Dependency only gets added into one direction.
254+
*
255+
* @param dependency Event to be added to the dependencies list.
256+
*/
257+
public void addDependency(CelestialEvent dependency) {
258+
if (dependencies.contains(dependency)) return;
259+
dependencies.add(dependency);
260+
}
261+
237262
/**
238263
* Used by CelestialTimeManager to handle an edge-case where events would not be loaded when reloading the same world.
239264
*/

0 commit comments

Comments
 (0)