Skip to content

Commit d20e200

Browse files
Atilistcalmilamsy
authored andcommitted
Start of Celestial Events
Added CelestialEvent. Added the Register Event. Added Test Items. Current Features: - Adjustable Intervals - Day Offset to offset the Interval - Custom Day Length - Custom Chance for successful Activation - Mutually exclusive Events
1 parent 5830d44 commit d20e200

8 files changed

Lines changed: 173 additions & 3 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.modificationstation.sltest.celestial;
2+
3+
import net.mine_diver.unsafeevents.listener.EventListener;
4+
import net.modificationstation.sltest.SLTest;
5+
import net.modificationstation.stationapi.api.celestial.CelestialEvent;
6+
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
7+
8+
public class CelestialListener {
9+
10+
private boolean hasRegistered = false; // Workaround to prevent excessive registering
11+
12+
public static CelestialEvent flyingDimando;
13+
public static CelestialEvent fallingDimando;
14+
15+
@EventListener
16+
public void registerCelestialEvents(CelestialRegisterEvent event) {
17+
if (hasRegistered) return;
18+
hasRegistered = true;
19+
SLTest.LOGGER.info("Register celestial events for testing");
20+
flyingDimando = new CelestialEvent(2);
21+
fallingDimando = new CelestialEvent(4);
22+
flyingDimando.addIncompatibleEvent(fallingDimando);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.modificationstation.sltest.item;
2+
3+
import net.minecraft.entity.player.PlayerEntity;
4+
import net.minecraft.item.ItemStack;
5+
import net.minecraft.world.World;
6+
import net.modificationstation.sltest.celestial.CelestialListener;
7+
import net.modificationstation.stationapi.api.template.item.TemplateItem;
8+
import net.modificationstation.stationapi.api.util.Identifier;
9+
10+
public class CelestialTestItem extends TemplateItem {
11+
public CelestialTestItem(Identifier identifier) {
12+
super(identifier);
13+
}
14+
15+
@Override
16+
public ItemStack use(ItemStack item, World world, PlayerEntity player) {
17+
if (CelestialListener.flyingDimando.isActive()) System.out.println("Event is happening");
18+
else System.out.println("No event");
19+
return super.use(item, world, player);
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.modificationstation.sltest.item;
2+
3+
import net.minecraft.entity.player.PlayerEntity;
4+
import net.minecraft.item.ItemStack;
5+
import net.minecraft.world.World;
6+
import net.modificationstation.sltest.celestial.CelestialListener;
7+
import net.modificationstation.stationapi.api.template.item.TemplateItem;
8+
import net.modificationstation.stationapi.api.util.Identifier;
9+
10+
public class CelestialToggleItem extends TemplateItem {
11+
public CelestialToggleItem(Identifier identifier) {
12+
super(identifier);
13+
}
14+
15+
@Override
16+
public ItemStack use(ItemStack item, World world, PlayerEntity player) {
17+
if (CelestialListener.fallingDimando.isActive()) {
18+
CelestialListener.fallingDimando.stopEvent();
19+
System.out.println("Stopping Falling Dimando");
20+
} else if (CelestialListener.fallingDimando.activateEvent(world.getTime(), random)) {
21+
System.out.println("Activating Falling Dimando");
22+
} else {
23+
System.out.println("Falling Dimando not activated");
24+
}
25+
if (CelestialListener.flyingDimando.isActive()) {
26+
CelestialListener.flyingDimando.stopEvent();
27+
System.out.println("Stopping Flying Dimando");
28+
} else if (CelestialListener.flyingDimando.activateEvent(world.getTime(), random)) {
29+
System.out.println("Activating Flying Dimando");
30+
} else {
31+
System.out.println("Flying Dimando not activated");
32+
}
33+
return super.use(item, world, player);
34+
}
35+
}

src/test/java/net/modificationstation/sltest/item/ItemListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void registerItems(ItemRegistryEvent event) {
3838
testShears = new TestShearsItem(NAMESPACE.id("test_shears")).setTranslationKey(NAMESPACE, "test_shears");
3939
pacifistSword = new PacifistSwordItem(NAMESPACE.id("pacifist_sword")).setTranslationKey(NAMESPACE, "pacifist_sword");
4040
dullPickaxe = new DullPickaxeItem(NAMESPACE.id("dull_pickaxe")).setTranslationKey(NAMESPACE, "dull_pickaxe");
41-
42-
41+
celestialTestItem = new CelestialTestItem(NAMESPACE.id("test_celestial")).setTranslationKey(NAMESPACE, "test_celestial");
42+
celestialToggleItem = new CelestialToggleItem(NAMESPACE.id("toggle_celestial")).setTranslationKey(NAMESPACE, "toggle_celestial");
4343
}
4444

4545
public static Item testItem;
@@ -55,4 +55,6 @@ public void registerItems(ItemRegistryEvent event) {
5555
public static Item testShears;
5656
public static Item pacifistSword;
5757
public static Item dullPickaxe;
58+
public static Item celestialTestItem;
59+
public static Item celestialToggleItem;
5860
}

src/test/resources/fabric.mod.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"net.modificationstation.sltest.item.tool.ToolListener",
3333
"net.modificationstation.sltest.datafixer.DataFixerListener",
3434
"net.modificationstation.sltest.worldgen.TestWorldgenListener",
35-
"net.modificationstation.sltest.bonemeal.BonemealListener"
35+
"net.modificationstation.sltest.bonemeal.BonemealListener",
36+
"net.modificationstation.sltest.dispenser.DispenserListener",
37+
"net.modificationstation.sltest.celestial.CelestialListener"
3638
],
3739
"stationapi:event_bus_client": [
3840
"net.modificationstation.sltest.gui.GuiListener",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 CelestialEvent {
8+
private final int frequency;
9+
private final float chance;
10+
private final int dayLength;
11+
private final int dayOffset;
12+
private boolean active;
13+
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
14+
15+
public CelestialEvent(int frequency, float chance, int dayLength, int dayOffset) {
16+
this.frequency = frequency;
17+
this.chance = chance;
18+
this.dayLength = dayLength;
19+
this.dayOffset = dayOffset;
20+
}
21+
22+
public CelestialEvent(int frequency, float chance, int dayLength) {
23+
this(frequency, chance, dayLength, 0);
24+
}
25+
26+
public CelestialEvent(int frequency, float chance) {
27+
this(frequency, chance, 24000);
28+
}
29+
30+
public CelestialEvent(int frequency) {
31+
this(frequency, 1);
32+
}
33+
34+
public boolean activateEvent(long worldTime, Random random) {
35+
if (active) {
36+
return true;
37+
}
38+
for (CelestialEvent otherEvent : incompatibleEvents) {
39+
if (otherEvent == null) continue;
40+
if (otherEvent.isActive()) {
41+
active = false;
42+
return false;
43+
}
44+
}
45+
long days = worldTime / dayLength + dayOffset;
46+
active = days % frequency == 0 && random.nextFloat() <= chance;
47+
return active;
48+
}
49+
50+
public void stopEvent() {
51+
active = false;
52+
}
53+
54+
public boolean isActive() {
55+
return active;
56+
}
57+
58+
/**
59+
* Adds events to prevent them from happening simultaneously.
60+
* Automatically adds incompatibility for both directions.
61+
* @param otherEvent
62+
*/
63+
public void addIncompatibleEvent(CelestialEvent otherEvent) {
64+
if (incompatibleEvents.contains(otherEvent)) return;
65+
incompatibleEvents.add(otherEvent);
66+
otherEvent.addIncompatibleEvent(this);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.modificationstation.stationapi.api.event.celestial;
2+
3+
import lombok.experimental.SuperBuilder;
4+
import net.mine_diver.unsafeevents.Event;
5+
6+
@SuperBuilder
7+
public class CelestialRegisterEvent extends Event {}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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.event.celestial.CelestialRegisterEvent;
67
import net.modificationstation.stationapi.api.event.world.WorldPropertiesEvent;
78
import org.spongepowered.asm.mixin.Mixin;
89
import org.spongepowered.asm.mixin.injection.At;
@@ -37,4 +38,14 @@ private void stationapi_onSaveToTag(NbtCompound arg, NbtCompound arg1, CallbackI
3738
.build()
3839
);
3940
}
41+
42+
@Inject(
43+
method = "setTime",
44+
at = @At("HEAD")
45+
)
46+
private void stationapi_postCelestialEvent(CallbackInfo ci) { // This gets called more than once. Does not really make sense with the way the events work
47+
StationAPI.EVENT_BUS.post(
48+
CelestialRegisterEvent.builder().build()
49+
);
50+
}
4051
}

0 commit comments

Comments
 (0)