|
| 1 | +package com.github.teraprath.tinylib.game.api; |
| 2 | + |
| 3 | +import com.github.teraprath.tinylib.game.api.event.GameRetireEvent; |
| 4 | +import com.github.teraprath.tinylib.game.api.event.GameStateChangeEvent; |
| 5 | +import com.github.teraprath.tinylib.game.api.listener.GamePingListener; |
| 6 | +import com.github.teraprath.tinylib.game.api.listener.PlayerJoinListener; |
| 7 | +import com.github.teraprath.tinylib.game.api.listener.PlayerQuitListener; |
| 8 | +import com.github.teraprath.tinylib.game.api.listener.WaitingListener; |
| 9 | +import com.github.teraprath.tinylib.game.api.team.GameTeamManager; |
| 10 | +import com.github.teraprath.tinylib.game.api.timer.RunningTimer; |
| 11 | +import com.github.teraprath.tinylib.game.api.timer.ShutdownTimer; |
| 12 | +import com.github.teraprath.tinylib.game.api.timer.WaitingTimer; |
| 13 | +import com.github.teraprath.tinylib.game.state.GameState; |
| 14 | +import com.github.teraprath.tinylib.game.state.GameStateManager; |
| 15 | +import org.bukkit.GameMode; |
| 16 | +import org.bukkit.entity.Player; |
| 17 | +import org.bukkit.plugin.PluginManager; |
| 18 | +import org.bukkit.plugin.java.JavaPlugin; |
| 19 | + |
| 20 | +import javax.annotation.Nonnull; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class GameAPI extends GameStateManager { |
| 25 | + |
| 26 | + private final JavaPlugin plugin; |
| 27 | + private final GameSettings settings; |
| 28 | + private final GameTeamManager teamManager; |
| 29 | + private final WaitingTimer waitingTimer; |
| 30 | + private final RunningTimer runningTimer; |
| 31 | + private final ShutdownTimer shutdownTimer; |
| 32 | + private final List<Player> alive; |
| 33 | + |
| 34 | + public GameAPI(@Nonnull GameSettings settings, @Nonnull JavaPlugin plugin) { |
| 35 | + this.plugin = plugin; |
| 36 | + this.settings = settings; |
| 37 | + this.teamManager = new GameTeamManager(); |
| 38 | + this.waitingTimer = new WaitingTimer(this); |
| 39 | + this.runningTimer = new RunningTimer(this); |
| 40 | + this.shutdownTimer = new ShutdownTimer(this); |
| 41 | + this.alive = new ArrayList<>(); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + public GameAPI init() { |
| 46 | + registerEvents(); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + private void registerEvents() { |
| 51 | + final PluginManager pm = plugin.getServer().getPluginManager(); |
| 52 | + pm.registerEvents(new GamePingListener(this), this.plugin); |
| 53 | + pm.registerEvents(new PlayerJoinListener(this), this.plugin); |
| 54 | + pm.registerEvents(new PlayerQuitListener(this), this.plugin); |
| 55 | + pm.registerEvents(new WaitingListener(this), this.plugin); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onChange(GameState gameState) { |
| 60 | + if (gameState.equals(GameState.RUNNING)) { |
| 61 | + this.alive.addAll(plugin.getServer().getOnlinePlayers()); |
| 62 | + } |
| 63 | + plugin.getServer().getPluginManager().callEvent(new GameStateChangeEvent(this)); |
| 64 | + } |
| 65 | + |
| 66 | + public GameSettings getSettings() { |
| 67 | + return settings; |
| 68 | + } |
| 69 | + |
| 70 | + public GameTeamManager getTeamManager() { |
| 71 | + return teamManager; |
| 72 | + } |
| 73 | + |
| 74 | + public WaitingTimer getWaitingTimer() { |
| 75 | + return waitingTimer; |
| 76 | + } |
| 77 | + |
| 78 | + public RunningTimer getRunningTimer() { |
| 79 | + return runningTimer; |
| 80 | + } |
| 81 | + |
| 82 | + public ShutdownTimer getShutdownTimer() { |
| 83 | + return shutdownTimer; |
| 84 | + } |
| 85 | + |
| 86 | + public List<Player> getAlive() { |
| 87 | + return this.alive; |
| 88 | + } |
| 89 | + |
| 90 | + public void retire(@Nonnull Player player) { |
| 91 | + this.alive.remove(player); |
| 92 | + player.setGameMode(GameMode.SPECTATOR); |
| 93 | + plugin.getServer().getPluginManager().callEvent(new GameRetireEvent(player, this)); |
| 94 | + } |
| 95 | + |
| 96 | + public JavaPlugin getPlugin() { |
| 97 | + return this.plugin; |
| 98 | + } |
| 99 | +} |
0 commit comments