LetThemEatCake/src/main/java/org/lunamc/letthemeatcake/Game.java
2022-12-21 14:00:43 -05:00

101 lines
2.7 KiB
Java

package org.lunamc.letthemeatcake;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.ArrayList;
import java.util.List;
public class Game {
private JavaPlugin plugin;
private boolean openStatus;
private List<Player> players;
private BukkitTask queueTimer;
public Game(JavaPlugin plugin) {
this.plugin = plugin;
openStatus = true;
players = new ArrayList<>();
startQueueTimer();
queueTimer.cancel();
}
public List<Player> getPlayers() {
return players;
}
/**
* Adds player to the game queue
* @param player player that will join the game queue
* @return 0 if the player has been added, 1 if the game is full
*/
public int addPlayer(Player player) {
if (players.size() >= 2 || queueTimer.isCancelled()) {
startQueueTimer();
}
players.add(player);
if (players.size() == 8) {
openStatus = false;
return 0;
}
return 1;
}
/**
* Adds multiple players to the game queue
* @param players array of players to be added
* @return 0 if the players have been added, 1 if the game is full, or 2 if the players have been added AND the queue is full (use to trigger start early)
*/
public int addPlayers(Player ... players) {
if (players.length <= (8 - this.players.size())) {
this.players.addAll(List.of(players));
if (this.players.size() == 8) {
openStatus = false;
return 2;
}
return 0;
}
return 1;
}
/**
* Adds multiple players to the game queue
* @param players array of players to be added
* @return 0 if the players have been added, 1 if the game is full, or 2 if the players have been added AND the queue is full (use to trigger start early)
*/
public int addPlayers(List<Player> players) {
if (players.size() <= (8 - this.players.size())) {
this.players.addAll(players);
if (this.players.size() == 8) {
openStatus = false;
return 2;
}
return 0;
}
return 1;
}
public void startQueueTimer() {
queueTimer = plugin.getServer().getScheduler().runTaskLater(
plugin,
() -> {
openStatus = false;
start();
},
200L
);
}
public void start() {
openStatus = false;
}
public boolean isOpen() {
return openStatus;
}
}