-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePhase.java
More file actions
78 lines (69 loc) · 2.44 KB
/
GamePhase.java
File metadata and controls
78 lines (69 loc) · 2.44 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
package net.onelitefeather.cygnus.phase;
import net.minestom.server.event.EventDispatcher;
import net.onelitefeather.cygnus.event.GameStartEvent;
import net.onelitefeather.cygnus.view.event.ViewUpdateEvent;
import net.theevilreaper.xerus.api.phase.TickDirection;
import net.theevilreaper.xerus.api.phase.TimedPhase;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.player.PlayerTickEvent;
import net.onelitefeather.cygnus.event.GameFinishEvent;
import net.onelitefeather.cygnus.listener.player.CygnusPlayerTickListener;
import net.onelitefeather.cygnus.view.GameView;
import org.jetbrains.annotations.Nullable;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
/**
* @author theEvilReaper
* @version 1.0.0
* @since 1.0.0
**/
public final class GamePhase extends TimedPhase {
private final GameView gameView;
private @Nullable GameFinishEvent finishEvent;
/**
* Creates a new instance from the {@link GamePhase}.
*
* @param gameView the view to update
* @param endRunnable the runnable to execute on end
*/
public GamePhase(
GameView gameView,
Runnable endRunnable,
int gameTime
) {
super("GamePhase", ChronoUnit.SECONDS, 1);
this.setCurrentTicks(gameTime);
this.setTickDirection(TickDirection.DOWN);
this.setEndTicks(0);
this.gameView = gameView;
this.setFinishedCallback(endRunnable);
}
/**
* Set's the reason why a game has ended.
*
* @param finishEvent the reason to set
*/
public void setFinishEvent(GameFinishEvent finishEvent) {
if (this.finishEvent != null) return;
this.finishEvent = finishEvent;
}
@Override
public void onStart() {
super.onStart();
addListener(PlayerTickEvent.class, new CygnusPlayerTickListener());
EventDispatcher.call(new GameStartEvent());
}
@Override
protected void onFinish() {
finishEvent = finishEvent == null ? new GameFinishEvent(GameFinishEvent.Reason.TIME_OVER) : finishEvent;
MinecraftServer.getGlobalEventHandler().call(finishEvent);
this.gameView.removePlayers(new HashSet<>(MinecraftServer.getConnectionManager().getOnlinePlayers()));
}
/**
* Contains the logic which should be exacted on each tick.
*/
@Override
public void onUpdate() {
EventDispatcher.call(new ViewUpdateEvent(getCurrentTicks()));
}
}