Skip to content

Commit fef6ae6

Browse files
Atilistcalmilamsy
authored andcommitted
API Documentation and small Fixes.
Documented all important API Components down to the Details. Introduced Lombok Getters. Added NBT Data Updating to the stopEvent() Method in CelestialEvent. Removed some redundant Code. Added Newline to CelestialRegisterEvent as requested.
1 parent d5d9f10 commit fef6ae6

5 files changed

Lines changed: 154 additions & 11 deletions

File tree

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

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package net.modificationstation.stationapi.api.celestial;
22

3+
import lombok.Getter;
34
import net.minecraft.world.World;
45

56
import java.util.LinkedList;
67
import java.util.List;
78
import java.util.Random;
89

10+
/**
11+
* Contains the most important logic for automatically starting and stopping the event.
12+
* It is recommended to add the event to CelestialTimeManger for automatic management.
13+
* Inheritance is possible to add custom logic.
14+
*/
915
public class CelestialEvent {
16+
@Getter
1017
private final String name;
1118
private final int frequency;
1219
private float chance = 1;
@@ -15,37 +22,81 @@ public class CelestialEvent {
1522
private int startingDaytime = 0;
1623
private int endingDaytime = 0;
1724
private int extraDays = 0;
25+
@Getter
1826
private boolean active;
1927
private boolean initializationNeeded = true;
2028
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
2129
public final World world;
2230

31+
/**
32+
* Primary constructor for the event, containing the required parameters.
33+
*
34+
* @param frequency Specifies how many day apart the events should be. The bigger the number, the longer the distance.
35+
* @param name Event name, snake_casing is recommended.
36+
* @param world World object, obtained from the register event.
37+
*/
2338
public CelestialEvent(int frequency, String name, World world) {
2439
this.frequency = frequency;
2540
this.name = name;
2641
this.world = world;
2742
}
2843

44+
/**
45+
* Builder method for optional chance value.
46+
*
47+
* @param chance Value ranging from 0.0F to 1.0F, representing percentage chance.
48+
* @return The object itself.
49+
*/
2950
public CelestialEvent setChance(float chance) {
3051
this.chance = chance;
3152
return this;
3253
}
3354

55+
/**
56+
* Builder method for optional day length value.
57+
* Intended to be used in dimensions where day time is different.
58+
* This will however need a custom time manager to account for the different day length.
59+
*
60+
* @param dayLength Length of day specified in ticks.
61+
* @return The object itself.
62+
*/
3463
public CelestialEvent setDayLength(int dayLength) {
3564
this.dayLength = dayLength;
3665
return this;
3766
}
3867

68+
/**
69+
* Builder method for optional day offset.
70+
*
71+
* @param dayOffset Offsets the frequency by the specified number of days.
72+
* @return The object itself.
73+
*/
3974
public CelestialEvent setDayOffset(int dayOffset) {
4075
this.dayOffset = dayOffset;
4176
return this;
4277
}
4378

79+
/**
80+
* Builder method for optional extra days.
81+
*
82+
* @param extraDays Extends the event duration by the specified number of days.
83+
* @return The object itself.
84+
*/
4485
public CelestialEvent setExtraDays(int extraDays) {
4586
this.extraDays = extraDays;
4687
return this;
4788
}
4889

90+
/**
91+
* Attempts to activate the event. Checks if the activation already was attempted within the valid time frame.
92+
* Takes activation chance and incompatible events into account.
93+
* Method gets called by CelestialTimeManager in case the event is added to it.
94+
* Can be called manually as well.
95+
*
96+
* @param worldTime Current time in the world measured in ticks.
97+
* @param random Locally used randomizer, seed is irrelevant.
98+
* @return True if the event met the criteria to be activated, otherwise false.
99+
*/
49100
public boolean activateEvent(long worldTime, Random random) {
50101
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
51102
if (initializationNeeded) {
@@ -60,7 +111,6 @@ public boolean activateEvent(long worldTime, Random random) {
60111
for (CelestialEvent otherEvent : incompatibleEvents) {
61112
if (otherEvent == null) continue;
62113
if (otherEvent.isActive()) {
63-
active = false;
64114
activityState.active = false;
65115
activityState.attemptedActivation = true;
66116
activityState.markDirty();
@@ -80,12 +130,29 @@ public boolean activateEvent(long worldTime, Random random) {
80130
return active;
81131
}
82132

133+
/**
134+
* Called precisely on event activation.
135+
* Can be customized by inheriting from this class.
136+
* Has access to the world object for more versatility.
137+
*/
83138
public void onActivation() {
84139
}
85140

141+
/**
142+
* Called precisely on event deactivation.
143+
* Can be customized by inheriting from this class.
144+
* Has access to the world object for more versatility.
145+
*/
86146
public void onDeactivation() {
87147
}
88148

149+
/**
150+
* Updates the activity status of an already active event.
151+
* Used by CelestialTimeManager 4 times per day.
152+
* Checks for validity of the time interval but not for chance and incompatibility.
153+
*
154+
* @param worldTime Current time in the world measured in ticks.
155+
*/
89156
public void updateEvent(long worldTime) {
90157
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
91158
if (initializationNeeded) {
@@ -104,6 +171,13 @@ public void updateEvent(long worldTime) {
104171
activityState.markDirty();
105172
}
106173

174+
/**
175+
* Initializes the event by synchronizing it with NBT data.
176+
* Only used internally.
177+
*
178+
* @param activityState CelestialEventActivityState (custom PersistentState) used for accessing NBT data.
179+
* @return The initialized CelestialEventActivityState.
180+
*/
107181
private CelestialEventActivityState initializeEvent(CelestialEventActivityState activityState) {
108182
initializationNeeded = false;
109183
activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
@@ -116,38 +190,52 @@ private CelestialEventActivityState initializeEvent(CelestialEventActivityState
116190
return activityState;
117191
}
118192

193+
/**
194+
* Manual way of stopping event.
195+
* Intended for command systems.
196+
*/
119197
public void stopEvent() {
120198
onDeactivation();
121199
if (active) {
200+
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
201+
if (initializationNeeded) {
202+
activityState = initializeEvent(activityState);
203+
}
204+
activityState.active = false;
205+
activityState.markDirty();
122206
System.out.println("Stopping event " + name);
123207
active = false;
124208
}
125209
}
126210

211+
/**
212+
* Called by CelestialTimeManager to specify the valid time frame of the event.
213+
* Time values can be specified as 4 values representing ticks in a day:
214+
* Morning: 0, Noon: 6000, Evening: 12000, Midnight: 18000
215+
*
216+
* @param startingDaytime Beginning daytime of the event.
217+
* @param endingDaytime Ending daytime of the event.
218+
*/
127219
public void setInterval(int startingDaytime, int endingDaytime) {
128220
this.startingDaytime = startingDaytime;
129221
this.endingDaytime = endingDaytime;
130222
}
131223

132-
public boolean isActive() {
133-
return active;
134-
}
135-
136-
public String getName() {
137-
return this.name;
138-
}
139-
140224
/**
141225
* Adds events to prevent them from happening simultaneously.
142226
* Automatically adds incompatibility for both directions.
143-
* @param otherEvent
227+
*
228+
* @param otherEvent Event to be added to the incompatibility list.
144229
*/
145230
public void addIncompatibleEvent(CelestialEvent otherEvent) {
146231
if (incompatibleEvents.contains(otherEvent)) return;
147232
incompatibleEvents.add(otherEvent);
148233
otherEvent.addIncompatibleEvent(this);
149234
}
150235

236+
/**
237+
* Used by CelestialTimeManager to handle an edge-case where events would not be loaded when reloading the same world.
238+
*/
151239
public void markForInitialization() {
152240
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
153241
initializeEvent(activityState);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import net.minecraft.nbt.NbtCompound;
44
import net.minecraft.world.PersistentState;
55

6+
/**
7+
* Manages important NBT data to ensure correct saving and loading of active celestial events.
8+
*/
69
public class CelestialEventActivityState extends PersistentState {
710
public boolean active;
811
public boolean attemptedActivation;

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
import java.util.List;
55
import java.util.Random;
66

7+
/**
8+
* Manages the activation and deactivation of celestial events.
9+
* Has been injected into WorldProperties using the WorldPropertiesMixin in station-world-events, does not need to be handled by mods.
10+
* Events need to be added during the registering process.
11+
* Uses lists to schedule event updates during the correct time of day.
12+
* Called four times per day.
13+
*/
714
public class CelestialTimeManager {
815
private static final List<CelestialEvent> MORNING_START = new LinkedList<>();
916
private static final List<CelestialEvent> NOON_START = new LinkedList<>();
@@ -20,6 +27,13 @@ public class CelestialTimeManager {
2027

2128
private static final Random RANDOM = new Random();
2229

30+
/**
31+
* Add a celestial event to the time manager.
32+
*
33+
* @param celestialEvent Event to be added.
34+
* @param start Time of day at which the event begins.
35+
* @param stop Time of day at which the event ends.
36+
*/
2337
public static void addCelestialEvent(CelestialEvent celestialEvent, DayQuarter start, DayQuarter stop) {
2438
switch (start) {
2539
case MORNING -> MORNING_START.add(celestialEvent);
@@ -31,6 +45,12 @@ public static void addCelestialEvent(CelestialEvent celestialEvent, DayQuarter s
3145
celestialEvent.setInterval(start.ordinal() * 6000, Math.abs(stop.ordinal() * 6000 - start.ordinal() * 6000));
3246
}
3347

48+
/**
49+
* Attempts to start all morning events. Called once per day.
50+
*
51+
* @param time World time in ticks.
52+
* @param currentDay Current day in the world.
53+
*/
3454
public static void startMorningEvents(long time, long currentDay) {
3555
if (morningActivation && lastCheckedDay == currentDay) return;
3656
lastCheckedDay = currentDay;
@@ -45,6 +65,12 @@ public static void startMorningEvents(long time, long currentDay) {
4565
}
4666
}
4767

68+
/**
69+
* Attempts to start all noon events. Called once per day.
70+
*
71+
* @param time World time in ticks.
72+
* @param currentDay Current day in the world.
73+
*/
4874
public static void startNoonEvents(long time, long currentDay) {
4975
if (noonActivation && lastCheckedDay == currentDay) return;
5076
lastCheckedDay = currentDay;
@@ -59,6 +85,12 @@ public static void startNoonEvents(long time, long currentDay) {
5985
}
6086
}
6187

88+
/**
89+
* Attempts to start all evening events. Called once per day.
90+
*
91+
* @param time World time in ticks.
92+
* @param currentDay Current day in the world.
93+
*/
6294
public static void startEveningEvents(long time, long currentDay) {
6395
if (eveningActivation && lastCheckedDay == currentDay) return;
6496
lastCheckedDay = currentDay;
@@ -73,6 +105,12 @@ public static void startEveningEvents(long time, long currentDay) {
73105
}
74106
}
75107

108+
/**
109+
* Attempts to start all midnight events. Called once per day.
110+
*
111+
* @param time World time in ticks.
112+
* @param currentDay Current day in the world.
113+
*/
76114
public static void startMidnightEvents(long time, long currentDay) {
77115
if (midnightActivation && lastCheckedDay == currentDay) return;
78116
lastCheckedDay = currentDay;
@@ -87,13 +125,21 @@ public static void startMidnightEvents(long time, long currentDay) {
87125
}
88126
}
89127

128+
/**
129+
* Updates activity state of all events. Called four times per day.
130+
*
131+
* @param time World time in ticks.
132+
*/
90133
public static void updateEvents(long time) {
91134
for (CelestialEvent celestialEvent : ALL_EVENTS) {
92135
if (celestialEvent == null) continue;
93136
celestialEvent.updateEvent(time);
94137
}
95138
}
96139

140+
/**
141+
* Clears all lists. Ensures that no duplicates show up after switching worlds.
142+
*/
97143
public static void clearLists() {
98144
MORNING_START.clear();
99145
NOON_START.clear();
@@ -102,6 +148,9 @@ public static void clearLists() {
102148
ALL_EVENTS.clear();
103149
}
104150

151+
/**
152+
* Initializes all events when the world is loaded, ensures correct loading of active events.
153+
*/
105154
public static void initializeEvents() {
106155
for (CelestialEvent celestialEvent : ALL_EVENTS) {
107156
if (celestialEvent == null) continue;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.modificationstation.stationapi.api.celestial;
22

3+
/**
4+
* Used for celestial event time management.
5+
*/
36
public enum DayQuarter {
47
MORNING, NOON, EVENING, MIDNIGHT
58
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
import net.modificationstation.stationapi.api.event.world.WorldEvent;
55

66
@SuperBuilder
7-
public class CelestialRegisterEvent extends WorldEvent {}
7+
public class CelestialRegisterEvent extends WorldEvent {}

0 commit comments

Comments
 (0)