Skip to content

Commit 2b9d7fc

Browse files
authored
Update timer.ytag (#176)
Update timer tag to use events, as the mixin is quite overkill now
1 parent 9c88d12 commit 2b9d7fc

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

tags/guide/timer.ytag

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,28 @@ DO NOT use threads or `java.util.Timer`. (This can cause a crash!) Instead:
88

99
- If you are making a block do something in the future: `world.scheduleBlockTick` + override `scheduledTick` in your `Block`.
1010
- If you are making a custom tickable stuff (usually block entity/entity) do something in the future/periodically: see below, but instead of Mixin just implement yourself
11-
- If you are making a vanilla tickable thing (world, server, etc) do something in the future/periodically: use the following mixin.
11+
- If you are making a vanilla tickable thing (world, server, etc) do something in the future/periodically: use the relevant tick event similar to the following.
1212

1313
```java
14-
@Mixin(StuffToTick.class) // ServerWorld, MinecraftServer, etc
15-
public class StuffTimer implements StuffTimerAccess {
16-
@Unique
17-
private long ticksUntilSomething;
18-
19-
@Inject(method = "tick", at = @At("TAIL"))
20-
private void onTick(CallbackInfo ci) { // Fix parameters as needed
21-
if (--this.ticksUntilSomething == 0L) {
22-
doSomething();
23-
// If you want to repeat this, reset ticksUntilSomething here.
14+
public static class StuffTimer implements ServerTickEvents.EndTick {
15+
public static final StuffTimer INSTANCE = new StuffTimer();
16+
private long ticksUntilSomething;
17+
public void setTimer(long ticksUntilSomething) {
18+
this.ticksUntilSomething = ticksUntilSomething;
19+
}
20+
@Override
21+
public void onEndTick(MinecraftServer server) {
22+
if (--this.ticksUntilSomething == 0L) {
23+
doSomething();
24+
// If you want to repeat this, reset ticksUntilSomething here.
25+
}
26+
}
27+
public static void register() {
28+
ServerTickEvents.END_SERVER_TICK.register(INSTANCE);
2429
}
2530
}
26-
27-
@Override
28-
public void yourmod_setTimer(long ticksUntilSomething) {
29-
this.ticksUntilSomething = ticksUntilSomething;
30-
}
31-
}
32-
```
33-
```java
34-
public interface StuffTimerAccess {
35-
void yourmod_setTimer(long ticksUntilSomething);
36-
}
3731
```
3832
Usage:
3933
```java
40-
MinecraftServer server;
41-
((StuffTimerAccess) server).yourmod_setTimer(100L); // do something after 100 ticks
34+
StuffTimer.INSTANCE.setTimer(100L); // do something after 100 ticks
4235
```

0 commit comments

Comments
 (0)