Skip to content

Commit 3344e9a

Browse files
Atilistcalmilamsy
authored andcommitted
Time Management Overhaul
Events are now fully flexible with Start and End Time of Day. Events are still updated correctly when Time Jumps happen. It is now possible to make Events last for multiple Days. Added Time Machine to quickly change Time. Added Enumeration for different Times of Day.
1 parent d639f06 commit 3344e9a

9 files changed

Lines changed: 91 additions & 67 deletions

File tree

src/test/java/net/modificationstation/sltest/block/Blocks.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public enum Blocks {
2222
FREEZER("freezer", "freezer", id -> new BlockFreezer(id).setHardness(2.5F).setSoundGroup(TemplateBlock.DEFAULT_SOUND_GROUP)),
2323
ALTAR("altar", "altar", id -> new BlockAltar(id, Material.STONE).setHardness(3)),
2424
VARIATION_BLOCK("variation_block", "variationBlock", id -> new VariationBlock(id, Material.STONE).setHardness(.5F).setSoundGroup(Block.DEFAULT_SOUND_GROUP).disableAutoItemRegistration()),
25-
EMISSION_CHECKER("emission_checker", "emissionChecker", LampBlock::new);
25+
EMISSION_CHECKER("emission_checker", "emissionChecker", LampBlock::new),
26+
INDISPENSABLE_BLOCK("indispensable_block", "indispensableBlock", IndispensableBlock::new),
27+
TIME_MACHINE_BLOCK("time_machine_block", "timeMachineBlock", TimeMachineBlock::new);
2628

2729
private final Runnable register;
2830
private Block block;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.modificationstation.sltest.block;
2+
3+
import net.minecraft.block.Material;
4+
import net.minecraft.entity.player.PlayerEntity;
5+
import net.minecraft.world.World;
6+
import net.modificationstation.stationapi.api.template.block.TemplateBlock;
7+
import net.modificationstation.stationapi.api.util.Identifier;
8+
9+
public class TimeMachineBlock extends TemplateBlock {
10+
public TimeMachineBlock(Identifier identifier) {
11+
super(identifier, Material.METAL);
12+
}
13+
14+
@Override
15+
public boolean onUse(World world, int x, int y, int z, PlayerEntity player) {
16+
world.setTime(world.getTime() + 1000);
17+
return true;
18+
}
19+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.modificationstation.sltest.SLTest;
55
import net.modificationstation.stationapi.api.celestial.CelestialEvent;
66
import net.modificationstation.stationapi.api.celestial.CelestialTimeManager;
7+
import net.modificationstation.stationapi.api.celestial.DayQuarter;
78
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
89

910
public class CelestialListener {
@@ -14,6 +15,7 @@ public class CelestialListener {
1415
public static CelestialEvent fallingDimando;
1516
public static CelestialEvent spinningDimando;
1617
public static CelestialEvent burningDimando;
18+
public static CelestialEvent longDimando;
1719

1820
@EventListener
1921
public void registerCelestialEvents(CelestialRegisterEvent event) {
@@ -24,10 +26,13 @@ public void registerCelestialEvents(CelestialRegisterEvent event) {
2426
fallingDimando = new CelestialEvent(2, "Falling Dimando");
2527
spinningDimando = new CelestialEvent(4, "Spinning Dimando").setDayOffset(1);
2628
burningDimando = new CelestialEvent(2, "Burning Dimando").setDayOffset(1);
29+
longDimando = new CelestialEvent(12, "Long Dimando").setExtraDays(4);
2730
flyingDimando.addIncompatibleEvent(fallingDimando);
28-
CelestialTimeManager.addMorningEvent(flyingDimando);
29-
CelestialTimeManager.addNoonEvent(fallingDimando);
30-
CelestialTimeManager.addEveningEvent(spinningDimando);
31-
CelestialTimeManager.addMidnightEvent(burningDimando);
31+
spinningDimando.addIncompatibleEvent(burningDimando);
32+
CelestialTimeManager.addCelestialEvent(flyingDimando, DayQuarter.MORNING, DayQuarter.MORNING);
33+
CelestialTimeManager.addCelestialEvent(fallingDimando, DayQuarter.NOON, DayQuarter.MIDNIGHT);
34+
CelestialTimeManager.addCelestialEvent(spinningDimando, DayQuarter.EVENING, DayQuarter.NOON);
35+
CelestialTimeManager.addCelestialEvent(burningDimando, DayQuarter.MIDNIGHT, DayQuarter.EVENING);
36+
CelestialTimeManager.addCelestialEvent(longDimando, DayQuarter.MIDNIGHT, DayQuarter.MIDNIGHT);
3237
}
3338
}

src/test/java/net/modificationstation/sltest/texture/TextureListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void registerTextures(TextureRegisterEvent event) {
2525
TEST_ANIMATED_BLOCK.get().textureId = terrain.addTexture(of(NAMESPACE, "blocks/testAnimatedBlock")).index;
2626
FREEZER.get().textureId = terrain.addTexture(of(NAMESPACE, "blocks/FreezerTop")).index;
2727
((BlockFreezer) FREEZER.get()).sideTexture = terrain.addTexture(of(NAMESPACE, "blocks/FreezerSide")).index;
28+
TIME_MACHINE_BLOCK.get().textureId = terrain.addTexture(of(NAMESPACE, "blocks/timeMachineBlock")).index;
2829

2930
altarTextures[Direction.DOWN.ordinal()] = terrain.addTexture(of(NAMESPACE, "blocks/altar_bottom")).index;
3031
altarTextures[Direction.UP.ordinal()] = terrain.addTexture(of(NAMESPACE, "blocks/altar_top")).index;

src/test/resources/assets/sltest/stationapi/lang/en_US.lang

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ item.@.variationBlockPassive.name=Variation Block Passive
1717
item.@.variationBlockActive.name=Variation Block Active
1818
item.@.test_shears.name=Test Shears
1919
item.@.pacifist_sword.name=Pacifist Sword
20-
item.@.dull_pickaxe.name=Dull Pickaxe
20+
item.@.dull_pickaxe.name=Dull Pickaxe
21+
tile.@.timeMachineBlock.name=Time Machine
312 Bytes
Loading

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class CelestialEvent {
1010
private float chance = 1;
1111
private int dayLength = 24000;
1212
private int dayOffset = 0;
13+
private int startingDaytime = 0;
14+
private int endingDaytime = 0;
15+
private int extraDays = 0;
1316
private boolean active;
1417
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
1518

@@ -33,6 +36,11 @@ public CelestialEvent setDayOffset(int dayOffset) {
3336
return this;
3437
}
3538

39+
public CelestialEvent setExtraDays(int extraDays) {
40+
this.extraDays = extraDays;
41+
return this;
42+
}
43+
3644
public boolean activateEvent(long worldTime, Random random) {
3745
if (active) {
3846
return true;
@@ -46,13 +54,29 @@ public boolean activateEvent(long worldTime, Random random) {
4654
}
4755
long days = worldTime / dayLength + dayOffset;
4856
active = days % frequency == 0 && random.nextFloat() <= chance;
49-
if (active) System.out.println("Starting event " + name);
57+
if (active) System.out.println(name + " has begun");
5058
return active;
5159
}
5260

61+
public void updateEvent(long worldTime) {
62+
if (!active) return;
63+
worldTime -= startingDaytime;
64+
worldTime += endingDaytime;
65+
long days = worldTime / dayLength + dayOffset;
66+
active = days % frequency <= extraDays;
67+
if (!active) System.out.println(name + " is over");
68+
}
69+
5370
public void stopEvent() {
54-
if (active) System.out.println("Stopping event " + name);
55-
active = false;
71+
if (active) {
72+
System.out.println("Stopping event " + name);
73+
active = false;
74+
}
75+
}
76+
77+
public void setInterval(int startingDaytime, int endingDaytime) {
78+
this.startingDaytime = startingDaytime;
79+
this.endingDaytime = endingDaytime;
5680
}
5781

5882
public boolean isActive() {

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

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

77
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<>();
8+
private static final List<CelestialEvent> MORNING_START = new LinkedList<>();
9+
private static final List<CelestialEvent> NOON_START = new LinkedList<>();
10+
private static final List<CelestialEvent> EVENING_START = new LinkedList<>();
11+
private static final List<CelestialEvent> MIDNIGHT_START = new LinkedList<>();
12+
private static final List<CelestialEvent> ALL_EVENTS = new LinkedList<>();
1213

1314
private static boolean morningActivation = false;
1415
private static boolean noonActivation = false;
@@ -19,111 +20,77 @@ public class CelestialTimeManager {
1920

2021
private static final Random RANDOM = new Random();
2122

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);
23+
public static void addCelestialEvent(CelestialEvent celestialEvent, DayQuarter start, DayQuarter stop) {
24+
switch (start) {
25+
case MORNING -> MORNING_START.add(celestialEvent);
26+
case NOON -> NOON_START.add(celestialEvent);
27+
case EVENING -> EVENING_START.add(celestialEvent);
28+
case MIDNIGHT -> MIDNIGHT_START.add(celestialEvent);
29+
}
30+
ALL_EVENTS.add(celestialEvent);
31+
celestialEvent.setInterval(start.ordinal() * 6000, Math.abs(stop.ordinal() * 6000 - start.ordinal() * 6000));
3632
}
3733

3834
public static void startMorningEvents(long time, long currentDay) {
3935
if (morningActivation && lastCheckedDay == currentDay) return;
40-
stopMorningEvents();
4136
lastCheckedDay = currentDay;
4237
morningActivation = true;
4338
noonActivation = false;
4439
eveningActivation = false;
4540
midnightActivation = false;
46-
for (CelestialEvent celestialEvent : MORNING_LIST) {
41+
updateEvents(time);
42+
for (CelestialEvent celestialEvent : MORNING_START) {
4743
if (celestialEvent == null) continue;
4844
celestialEvent.activateEvent(time, RANDOM);
4945
}
50-
stopNoonEvents();
51-
stopEveningEvents();
5246
}
5347

5448
public static void startNoonEvents(long time, long currentDay) {
5549
if (noonActivation && lastCheckedDay == currentDay) return;
56-
stopNoonEvents();
5750
lastCheckedDay = currentDay;
5851
morningActivation = false;
5952
noonActivation = true;
6053
eveningActivation = false;
6154
midnightActivation = false;
62-
for (CelestialEvent celestialEvent : NOON_LIST) {
55+
updateEvents(time);
56+
for (CelestialEvent celestialEvent : NOON_START) {
6357
if (celestialEvent == null) continue;
6458
celestialEvent.activateEvent(time, RANDOM);
6559
}
66-
stopEveningEvents();
67-
stopMidnightEvents();
6860
}
6961

7062
public static void startEveningEvents(long time, long currentDay) {
7163
if (eveningActivation && lastCheckedDay == currentDay) return;
72-
stopEveningEvents();
7364
lastCheckedDay = currentDay;
7465
morningActivation = false;
7566
noonActivation = false;
7667
eveningActivation = true;
7768
midnightActivation = false;
78-
for (CelestialEvent celestialEvent : EVENING_LIST) {
69+
updateEvents(time);
70+
for (CelestialEvent celestialEvent : EVENING_START) {
7971
if (celestialEvent == null) continue;
8072
celestialEvent.activateEvent(time, RANDOM);
8173
}
82-
stopMidnightEvents();
83-
stopMorningEvents();
8474
}
8575

8676
public static void startMidnightEvents(long time, long currentDay) {
8777
if (midnightActivation && lastCheckedDay == currentDay) return;
88-
stopMidnightEvents();
8978
lastCheckedDay = currentDay;
9079
morningActivation = false;
9180
noonActivation = false;
9281
eveningActivation = false;
9382
midnightActivation = true;
94-
for (CelestialEvent celestialEvent : MIDNIGHT_LIST) {
83+
updateEvents(time);
84+
for (CelestialEvent celestialEvent : MIDNIGHT_START) {
9585
if (celestialEvent == null) continue;
9686
celestialEvent.activateEvent(time, RANDOM);
9787
}
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-
}
12188
}
12289

123-
public static void stopMidnightEvents() {
124-
for (CelestialEvent celestialEvent : MIDNIGHT_LIST) {
90+
public static void updateEvents(long time) {
91+
for (CelestialEvent celestialEvent : ALL_EVENTS) {
12592
if (celestialEvent == null) continue;
126-
celestialEvent.stopEvent();
93+
celestialEvent.updateEvent(time);
12794
}
12895
}
12996
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.modificationstation.stationapi.api.celestial;
2+
3+
public enum DayQuarter {
4+
MORNING, NOON, EVENING, MIDNIGHT
5+
}

0 commit comments

Comments
 (0)