Skip to content

Commit d15a44f

Browse files
Atilistcalmilamsy
authored andcommitted
New useful Methods and more Examples
Added onActivation and onDeactivation to call Actions precisely at the Start and End of an Event. Made CelestialRegisterEvent extend WorldEvent to get Access to the World. Celestial Events can now directly access the World. Moved the Posting of CelestialRegisterEvent into the WorldMixin. Added all Sorts of Changes to Game Mechanics for the Test Events. Moved some Debug Prints into a Test Mod Class. Started Work on a Saving System for the Activity State, currently unfinished.
1 parent 3344e9a commit d15a44f

12 files changed

Lines changed: 197 additions & 25 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
package net.modificationstation.sltest.block;
22

33
import net.minecraft.block.Material;
4+
import net.minecraft.entity.ItemEntity;
45
import net.minecraft.entity.player.PlayerEntity;
6+
import net.minecraft.item.Item;
7+
import net.minecraft.item.ItemStack;
58
import net.minecraft.world.World;
9+
import net.modificationstation.sltest.celestial.CelestialListener;
610
import net.modificationstation.stationapi.api.template.block.TemplateBlock;
711
import net.modificationstation.stationapi.api.util.Identifier;
812

13+
import java.util.Random;
14+
915
public class TimeMachineBlock extends TemplateBlock {
1016
public TimeMachineBlock(Identifier identifier) {
1117
super(identifier, Material.METAL);
18+
this.setTickRandomly(true);
1219
}
1320

1421
@Override
1522
public boolean onUse(World world, int x, int y, int z, PlayerEntity player) {
1623
world.setTime(world.getTime() + 1000);
1724
return true;
1825
}
26+
27+
@Override
28+
public void onTick(World world, int x, int y, int z, Random random) {
29+
super.onTick(world, x, y, z, random);
30+
if (CelestialListener.flyingDimando.isActive()) {
31+
world.method_287(new ItemEntity(world, x + 0.5, y + 1, z + 0.5, new ItemStack(Item.DIAMOND)));
32+
}
33+
}
1934
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import net.modificationstation.stationapi.api.event.celestial.CelestialRegisterEvent;
99

1010
public class CelestialListener {
11-
12-
private boolean hasRegistered = false; // Workaround to prevent excessive registering
11+
private boolean alreadyRegistered = false;
1312

1413
public static CelestialEvent flyingDimando;
1514
public static CelestialEvent fallingDimando;
@@ -19,14 +18,14 @@ public class CelestialListener {
1918

2019
@EventListener
2120
public void registerCelestialEvents(CelestialRegisterEvent event) {
22-
if (hasRegistered) return;
23-
hasRegistered = true;
21+
if (alreadyRegistered) return;
22+
alreadyRegistered = true;
2423
SLTest.LOGGER.info("Register celestial events for testing");
25-
flyingDimando = new CelestialEvent(4, "Flying Dimando");
26-
fallingDimando = new CelestialEvent(2, "Falling Dimando");
27-
spinningDimando = new CelestialEvent(4, "Spinning Dimando").setDayOffset(1);
28-
burningDimando = new CelestialEvent(2, "Burning Dimando").setDayOffset(1);
29-
longDimando = new CelestialEvent(12, "Long Dimando").setExtraDays(4);
24+
flyingDimando = new FlyingDimando(4, "flying_dimando", event.world);
25+
fallingDimando = new DebugCelestialEvent(2, "falling_dimando", event.world);
26+
spinningDimando = new DebugCelestialEvent(4, "spinning_dimando", event.world).setDayOffset(1);
27+
burningDimando = new DebugCelestialEvent(2, "burning_dimando", event.world).setDayOffset(1);
28+
longDimando = new DebugCelestialEvent(12, "long_dimando", event.world).setExtraDays(4);
3029
flyingDimando.addIncompatibleEvent(fallingDimando);
3130
spinningDimando.addIncompatibleEvent(burningDimando);
3231
CelestialTimeManager.addCelestialEvent(flyingDimando, DayQuarter.MORNING, DayQuarter.MORNING);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.modificationstation.sltest.celestial;
2+
3+
import net.minecraft.world.World;
4+
import net.modificationstation.stationapi.api.celestial.CelestialEvent;
5+
6+
public class DebugCelestialEvent extends CelestialEvent {
7+
public DebugCelestialEvent(int frequency, String name, World world) {
8+
super(frequency, name, world);
9+
}
10+
11+
@Override
12+
public void onActivation() {
13+
super.onActivation();
14+
System.out.println(this.getName() + " has begun");
15+
}
16+
17+
@Override
18+
public void onDeactivation() {
19+
super.onDeactivation();
20+
System.out.println(this.getName() + " is over");
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.modificationstation.sltest.celestial;
2+
3+
import net.minecraft.world.World;
4+
5+
public class FlyingDimando extends DebugCelestialEvent {
6+
public FlyingDimando(int frequency, String name, World world) {
7+
super(frequency, name, world);
8+
}
9+
10+
@Override
11+
public void onActivation() {
12+
super.onActivation();
13+
world.method_262().setRaining(true);
14+
world.method_262().setRainTime(200);
15+
}
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.modificationstation.sltest.mixin;
2+
3+
import net.minecraft.entity.Entity;
4+
import net.modificationstation.sltest.celestial.CelestialListener;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Shadow;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(Entity.class)
12+
public class EntityMixin {
13+
14+
@Shadow public float yaw;
15+
16+
@Shadow public float pitch;
17+
18+
@Shadow public double velocityY;
19+
20+
@Shadow public double velocityX;
21+
22+
@Shadow public double velocityZ;
23+
24+
@Inject(
25+
method = "tick",
26+
at = @At("HEAD")
27+
)
28+
public void spinEntity(CallbackInfo ci) {
29+
if (CelestialListener.spinningDimando.isActive()) {
30+
this.yaw = (this.yaw + 0.1F) % 360.0F;
31+
this.pitch = (this.pitch + 0.1F) % 360.0F;
32+
}
33+
if (CelestialListener.flyingDimando.isActive()) {
34+
if (this.velocityY < 0.5F) {
35+
this.velocityY += 0.05F;
36+
}
37+
}
38+
if (CelestialListener.fallingDimando.isActive()) {
39+
if (this.velocityY > -0.1F) {
40+
this.velocityY -= 0.05F;
41+
}
42+
}
43+
if (CelestialListener.longDimando.isActive()) {
44+
this.velocityX *= 1.1F;
45+
this.velocityZ *= 1.1F;
46+
}
47+
}
48+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.modificationstation.sltest.mixin;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.block.GrassBlock;
5+
import net.minecraft.world.World;
6+
import net.modificationstation.sltest.celestial.CelestialListener;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
12+
import java.util.Random;
13+
14+
@Mixin(GrassBlock.class)
15+
public class GrassBlockMixin {
16+
@Inject(
17+
method = "onTick",
18+
at = @At("TAIL")
19+
)
20+
public void setStuffOnFire(World world, int x, int y, int z, Random random, CallbackInfo ci) {
21+
if (CelestialListener.burningDimando.isActive()) {
22+
if (random.nextInt(100) == 0 && world.getBlockId(x, y + 1, z) == 0) world.method_200(x, y + 1, z, Block.FIRE.id);
23+
}
24+
}
25+
26+
}

src/test/resources/sltest.mixins.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"MixinLevel",
99
"MixinNetherLevelSource",
1010
"MixinObsidian",
11-
"OverworldTestMixin"
11+
"OverworldTestMixin",
12+
"GrassBlockMixin",
13+
"EntityMixin"
1214
],
1315
"server": [
1416
],

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.modificationstation.stationapi.api.celestial;
22

3+
import net.minecraft.world.World;
4+
35
import java.util.LinkedList;
46
import java.util.List;
57
import java.util.Random;
@@ -15,10 +17,12 @@ public class CelestialEvent {
1517
private int extraDays = 0;
1618
private boolean active;
1719
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
20+
public final World world;
1821

19-
public CelestialEvent(int frequency, String name) {
22+
public CelestialEvent(int frequency, String name, World world) {
2023
this.frequency = frequency;
2124
this.name = name;
25+
this.world = world;
2226
}
2327

2428
public CelestialEvent setChance(float chance) {
@@ -54,20 +58,40 @@ public boolean activateEvent(long worldTime, Random random) {
5458
}
5559
long days = worldTime / dayLength + dayOffset;
5660
active = days % frequency == 0 && random.nextFloat() <= chance;
57-
if (active) System.out.println(name + " has begun");
61+
if (active) {
62+
onActivation();
63+
}
5864
return active;
5965
}
6066

67+
public void onActivation() {
68+
}
69+
70+
public void onDeactivation() {
71+
}
72+
6173
public void updateEvent(long worldTime) {
74+
/*
75+
This piece of code for saving the activity state should probably be moved somewhere else. It is also incomplete and not fully functional
76+
CelestialEventActivityState activityState = (CelestialEventActivityState) this.world.getOrCreateState(CelestialEventActivityState.class, this.name);
77+
if (activityState == null) {
78+
activityState = new CelestialEventActivityState(this.name);
79+
this.world.setState(this.name, activityState);
80+
System.out.println("Did not find activity state");
81+
} else {
82+
System.out.println("Found existing activity state");
83+
}
84+
*/
6285
if (!active) return;
6386
worldTime -= startingDaytime;
6487
worldTime += endingDaytime;
6588
long days = worldTime / dayLength + dayOffset;
6689
active = days % frequency <= extraDays;
67-
if (!active) System.out.println(name + " is over");
90+
if (!active) onDeactivation();
6891
}
6992

7093
public void stopEvent() {
94+
onDeactivation();
7195
if (active) {
7296
System.out.println("Stopping event " + name);
7397
active = false;
@@ -83,6 +107,10 @@ public boolean isActive() {
83107
return active;
84108
}
85109

110+
public String getName() {
111+
return this.name;
112+
}
113+
86114
/**
87115
* Adds events to prevent them from happening simultaneously.
88116
* Automatically adds incompatibility for both directions.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.modificationstation.stationapi.api.celestial;
2+
3+
import net.minecraft.nbt.NbtCompound;
4+
import net.minecraft.world.PersistentState;
5+
6+
public class CelestialEventActivityState extends PersistentState {
7+
public boolean active;
8+
9+
public CelestialEventActivityState(String id) {
10+
super(id);
11+
}
12+
13+
@Override
14+
public void readNbt(NbtCompound nbt) {
15+
this.active = nbt.getBoolean("active");
16+
}
17+
18+
@Override
19+
public void writeNbt(NbtCompound nbt) {
20+
nbt.putBoolean("active", active);
21+
}
22+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.modificationstation.stationapi.api.event.celestial;
22

33
import lombok.experimental.SuperBuilder;
4-
import net.mine_diver.unsafeevents.Event;
4+
import net.modificationstation.stationapi.api.event.world.WorldEvent;
55

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

0 commit comments

Comments
 (0)