-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathGameActivityEvents.java
More file actions
168 lines (152 loc) · 5.85 KB
/
Copy pathGameActivityEvents.java
File metadata and controls
168 lines (152 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package xyz.nucleoid.plasmid.game.event;
import net.minecraft.resource.LifecycledResourceManager;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.game.GameActivity;
import xyz.nucleoid.plasmid.game.GameCloseReason;
import xyz.nucleoid.plasmid.game.GameResult;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.stimuli.event.StimulusEvent;
import java.util.function.Consumer;
/**
* Events relating to the lifecycle of a {@link GameActivity} within a {@link GameSpace}.
*/
public final class GameActivityEvents {
/**
* Called when a {@link GameActivity} is set on a {@link GameSpace} through {@link GameSpace#setActivity(Consumer)}.
* <p>
* This event should be used for any start logic needed to be done by a game.
* <p>
* This event is called after {@link GameActivityEvents#CREATE} as well as after {@link GamePlayerEvents#ADD} which
* will have been called for all players in this {@link GameSpace}.
*/
public static final StimulusEvent<Enable> ENABLE = StimulusEvent.create(Enable.class, ctx -> () -> {
try {
for (var listener : ctx.getListeners()) {
listener.onEnable();
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called after datapacks are reloaded.
*/
public static final StimulusEvent<Reload> RELOAD = StimulusEvent.create(Reload.class, ctx -> (resourceManager, success) -> {
try {
for (var listener : ctx.getListeners()) {
listener.onReload(resourceManager, success);
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called when a {@link GameActivity} should be disabled. This happens when a {@link GameActivity} is replaced by
* another on a {@link GameSpace} or when a {@link GameSpace} is closed.
* <p>
* This event should be used for any closing logic needed to be done by a game.
* <p>
* This event is called before {@link GameActivityEvents#DESTROY} as well as before {@link GamePlayerEvents#REMOVE}
* which will still be called for all players in this {@link GameSpace}.
*/
public static final StimulusEvent<Disable> DISABLE = StimulusEvent.create(Disable.class, ctx -> () -> {
try {
for (var listener : ctx.getListeners()) {
listener.onDisable();
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called immediately when a {@link GameActivity} is created.
* <p>
* This event should be used for any early setup logic, but generally {@link GameActivityEvents#ENABLE} is more
* useful.
* <p>
* This event is called before {@link GameActivityEvents#ENABLE} as well as before {@link GamePlayerEvents#ADD}
* which will still be called for all players in this {@link GameSpace}.
*/
public static final StimulusEvent<Create> CREATE = StimulusEvent.create(Create.class, ctx -> () -> {
try {
for (var listener : ctx.getListeners()) {
listener.onCreate();
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called when a {@link GameActivity} is finally destroyed. This can happen as a result of the {@link GameActivity}
* being replaced or the {@link GameSpace} closing.
* <p>
* This event should be used for any final tear-down logic needed to be done by a game.
* <p>
* This event is called after {@link GameActivityEvents#DISABLE} as well as after {@link GamePlayerEvents#REMOVE}
* which will have been called for all players in this {@link GameSpace}.
*/
public static final StimulusEvent<Destroy> DESTROY = StimulusEvent.create(Destroy.class, ctx -> reason -> {
try {
for (var listener : ctx.getListeners()) {
listener.onDestroy(reason);
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called every tick while a {@link GameActivity} is active.
*/
public static final StimulusEvent<Tick> TICK = StimulusEvent.create(Tick.class, ctx -> () -> {
try {
for (var listener : ctx.getListeners()) {
listener.onTick();
}
} catch (Throwable throwable) {
ctx.handleException(throwable);
}
});
/**
* Called when the {@code /game start} command is run by a player.
* <p>
* This event should be used to run actual starting logic as well as to return any errors if starting is not
* currently possible.
*/
public static final StimulusEvent<RequestStart> REQUEST_START = StimulusEvent.create(RequestStart.class, ctx -> () -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onRequestStart();
if (result != null) {
return result;
}
}
return null;
} catch (Throwable throwable) {
ctx.handleException(throwable);
return GameResult.error(Text.translatable("text.plasmid.game.start_result.error"));
}
});
public interface Enable {
void onEnable();
}
public interface Disable {
void onDisable();
}
public interface Create {
void onCreate();
}
public interface Destroy {
void onDestroy(GameCloseReason reason);
}
public interface Tick {
void onTick();
}
public interface Reload {
void onReload(LifecycledResourceManager resourceManager, boolean success);
}
public interface RequestStart {
@Nullable
GameResult onRequestStart();
}
}