Skip to content

Commit c798dd1

Browse files
Atilistcalmilamsy
authored andcommitted
Time Manager
Added Mixin for CelestialTimeManager. Celestial Events now automatically start and stop if added to the Time Manager. Starting Point can be customized to 4 Times of the Day: - Morning - Noon - Evening - Midnight Events always stop half a Day after they start. Time Manager has been designed to work with abrupt Time Jumps. Added Debug Messages for Event Starts and Stops. Added Name Parameter to CelestialEvent.
1 parent 94629ef commit c798dd1

4 files changed

Lines changed: 169 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.mine_diver.unsafeevents.listener.EventListener;
44
import net.modificationstation.sltest.SLTest;
55
import net.modificationstation.stationapi.api.celestial.CelestialEvent;
6+
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
67
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
78

89
public class CelestialListener {
@@ -17,8 +18,10 @@ public void registerCelestialEvents(CelestialRegisterEvent event) {
1718
if (hasRegistered) return;
1819
hasRegistered = true;
1920
SLTest.LOGGER.info("Register celestial events for testing");
20-
flyingDimando = new CelestialEvent(2);
21-
fallingDimando = new CelestialEvent(4);
21+
flyingDimando = new CelestialEvent(2, "Flying Dimando");
22+
fallingDimando = new CelestialEvent(4, "Falling Dimando");
2223
flyingDimando.addIncompatibleEvent(fallingDimando);
24+
CelestialTimeManager.addMorningEvent(flyingDimando);
25+
CelestialTimeManager.addNoonEvent(fallingDimando);
2326
}
2427
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@
55
import java.util.Random;
66

77
public class CelestialEvent {
8+
private final String name;
89
private final int frequency;
910
private final float chance;
1011
private final int dayLength;
1112
private final int dayOffset;
1213
private boolean active;
1314
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
1415

15-
public CelestialEvent(int frequency, float chance, int dayLength, int dayOffset) {
16+
public CelestialEvent(int frequency, float chance, int dayLength, int dayOffset, String name) {
1617
this.frequency = frequency;
1718
this.chance = chance;
1819
this.dayLength = dayLength;
1920
this.dayOffset = dayOffset;
21+
this.name = name;
2022
}
2123

22-
public CelestialEvent(int frequency, float chance, int dayLength) {
23-
this(frequency, chance, dayLength, 0);
24+
public CelestialEvent(int frequency, float chance, int dayLength, String name) {
25+
this(frequency, chance, dayLength, 0, name);
2426
}
2527

26-
public CelestialEvent(int frequency, float chance) {
27-
this(frequency, chance, 24000);
28+
public CelestialEvent(int frequency, float chance, String name) {
29+
this(frequency, chance, 24000, name);
2830
}
2931

30-
public CelestialEvent(int frequency) {
31-
this(frequency, 1);
32+
public CelestialEvent(int frequency, String name) {
33+
this(frequency, 1, name);
3234
}
3335

3436
public boolean activateEvent(long worldTime, Random random) {
37+
System.out.println("Attempt activation for event " + name);
3538
if (active) {
3639
return true;
3740
}
@@ -48,6 +51,7 @@ public boolean activateEvent(long worldTime, Random random) {
4851
}
4952

5053
public void stopEvent() {
54+
System.out.println("Stopping event " + name);
5155
active = false;
5256
}
5357

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package net.modificationstation.stationapi.api.celestial;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
public class CelestialTimeManager {
8+
private static final List<CelestialEvent> MORNING_LIST = new LinkedList<>();
9+
private static final List<CelestialEvent> NOON_LIST = new LinkedList<>();
10+
private static final List<CelestialEvent> EVENING_LIST = new LinkedList<>();
11+
private static final List<CelestialEvent> MIDNIGHT_LIST = new LinkedList<>();
12+
13+
private static boolean morningActivation = false;
14+
private static boolean noonActivation = false;
15+
private static boolean eveningActivation = false;
16+
private static boolean midnightActivation = false;
17+
18+
private static long lastCheckedDay = 0;
19+
20+
private static final Random RANDOM = new Random();
21+
22+
public static void addMorningEvent(CelestialEvent celestialEvent) {
23+
MORNING_LIST.add(celestialEvent);
24+
}
25+
26+
public static void addNoonEvent(CelestialEvent celestialEvent) {
27+
NOON_LIST.add(celestialEvent);
28+
}
29+
30+
public static void addEveningEvent(CelestialEvent celestialEvent) {
31+
EVENING_LIST.add(celestialEvent);
32+
}
33+
34+
public static void addMidnightEvent(CelestialEvent celestialEvent) {
35+
MIDNIGHT_LIST.add(celestialEvent);
36+
}
37+
38+
public static void startMorningEvents(long time, long currentDay) {
39+
if (morningActivation && lastCheckedDay == currentDay) return;
40+
stopMorningEvents();
41+
lastCheckedDay = currentDay;
42+
morningActivation = true;
43+
noonActivation = false;
44+
eveningActivation = false;
45+
midnightActivation = false;
46+
for (CelestialEvent celestialEvent : MORNING_LIST) {
47+
if (celestialEvent == null) continue;
48+
celestialEvent.activateEvent(time, RANDOM);
49+
}
50+
stopNoonEvents();
51+
stopEveningEvents();
52+
}
53+
54+
public static void startNoonEvents(long time, long currentDay) {
55+
if (noonActivation && lastCheckedDay == currentDay) return;
56+
stopNoonEvents();
57+
lastCheckedDay = currentDay;
58+
morningActivation = false;
59+
noonActivation = true;
60+
eveningActivation = false;
61+
midnightActivation = false;
62+
for (CelestialEvent celestialEvent : NOON_LIST) {
63+
if (celestialEvent == null) continue;
64+
celestialEvent.activateEvent(time, RANDOM);
65+
}
66+
stopEveningEvents();
67+
stopMidnightEvents();
68+
}
69+
70+
public static void startEveningEvents(long time, long currentDay) {
71+
if (eveningActivation && lastCheckedDay == currentDay) return;
72+
stopEveningEvents();
73+
lastCheckedDay = currentDay;
74+
morningActivation = false;
75+
noonActivation = false;
76+
eveningActivation = true;
77+
midnightActivation = false;
78+
for (CelestialEvent celestialEvent : EVENING_LIST) {
79+
if (celestialEvent == null) continue;
80+
celestialEvent.activateEvent(time, RANDOM);
81+
}
82+
stopMidnightEvents();
83+
stopMorningEvents();
84+
}
85+
86+
public static void startMidnightEvents(long time, long currentDay) {
87+
if (midnightActivation && lastCheckedDay == currentDay) return;
88+
stopMidnightEvents();
89+
lastCheckedDay = currentDay;
90+
morningActivation = false;
91+
noonActivation = false;
92+
eveningActivation = false;
93+
midnightActivation = true;
94+
for (CelestialEvent celestialEvent : MIDNIGHT_LIST) {
95+
if (celestialEvent == null) continue;
96+
celestialEvent.activateEvent(time, RANDOM);
97+
}
98+
stopMorningEvents();
99+
stopNoonEvents();
100+
}
101+
102+
public static void stopMorningEvents() {
103+
for (CelestialEvent celestialEvent : MORNING_LIST) {
104+
if (celestialEvent == null) continue;
105+
celestialEvent.stopEvent();
106+
}
107+
}
108+
109+
public static void stopNoonEvents() {
110+
for (CelestialEvent celestialEvent : NOON_LIST) {
111+
if (celestialEvent == null) continue;
112+
celestialEvent.stopEvent();
113+
}
114+
}
115+
116+
public static void stopEveningEvents() {
117+
for (CelestialEvent celestialEvent : EVENING_LIST) {
118+
if (celestialEvent == null) continue;
119+
celestialEvent.stopEvent();
120+
}
121+
}
122+
123+
public static void stopMidnightEvents() {
124+
for (CelestialEvent celestialEvent : MIDNIGHT_LIST) {
125+
if (celestialEvent == null) continue;
126+
celestialEvent.stopEvent();
127+
}
128+
}
129+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
import net.minecraft.nbt.NbtCompound;
44
import net.minecraft.world.WorldProperties;
55
import net.modificationstation.stationapi.api.StationAPI;
6+
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
67
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
78
import net.modificationstation.stationapi.api.event.world.WorldPropertiesEvent;
89
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
911
import org.spongepowered.asm.mixin.injection.At;
1012
import org.spongepowered.asm.mixin.injection.Inject;
1113
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1214

1315
@Mixin(WorldProperties.class)
1416
public class WorldPropertiesMixin {
17+
@Shadow private long time;
18+
19+
@Shadow private long lastPlayed;
20+
1521
@Inject(
1622
method = "<init>(Lnet/minecraft/nbt/NbtCompound;)V",
1723
at = @At("RETURN")
@@ -48,4 +54,22 @@ private void stationapi_postCelestialEvent(CallbackInfo ci) { // This gets calle
4854
CelestialRegisterEvent.builder().build()
4955
);
5056
}
57+
58+
@Inject(
59+
method = "setTime",
60+
at = @At("HEAD")
61+
)
62+
private void stationapi_celestialEventTimeManager(CallbackInfo ci) {
63+
long daytime = time % 24000;
64+
long currentDay = time / 24000;
65+
if (daytime >= 0 && daytime < 6000) {
66+
CelestialTimeManager.startMorningEvents(time, currentDay);
67+
} else if (daytime >= 6000 && daytime < 12000) {
68+
CelestialTimeManager.startNoonEvents(time, currentDay);
69+
} else if (daytime >= 12000 && daytime < 18000) {
70+
CelestialTimeManager.startEveningEvents(time, currentDay);
71+
} else if (daytime >= 18000) {
72+
CelestialTimeManager.startMidnightEvents(time, currentDay);
73+
}
74+
}
5175
}

0 commit comments

Comments
 (0)