76 lines
2.2 KiB
Java
76 lines
2.2 KiB
Java
package xyz.twovb.sgm.games;
|
|
|
|
/*
|
|
* Created by 2vb - 4/6/2024
|
|
*/
|
|
|
|
import lombok.Getter;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import xyz.twovb.sgm.SGM;
|
|
import xyz.twovb.toolbox.managers.PlaceholderManager;
|
|
import xyz.twovb.toolbox.utils.ChatUtils;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
public class GameManager {
|
|
|
|
@Getter
|
|
private static final Map<UUID, Minigame> activeGames = new HashMap<UUID, Minigame>();
|
|
@Getter
|
|
private final ArrayList<String> registeredGames = new ArrayList<String>();
|
|
|
|
public static Minigame findGame(UUID gameId) {
|
|
return activeGames.get(gameId);
|
|
}
|
|
|
|
public Minigame findGame(Player player) {
|
|
for (Minigame game : activeGames.values()) {
|
|
if (game.getPlayers().contains(player)) {
|
|
return game;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static boolean isInGame(Player player) {
|
|
for (Minigame game : activeGames.values()) {
|
|
if (game.getPlayers().contains(player)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void initGame(Minigame game, CommandSender owner, String level) {
|
|
try {
|
|
game.init(owner, level);
|
|
} catch (IOException e) {
|
|
SGM.getInstance().getCLogger().error(e.getMessage());
|
|
owner.sendMessage(ChatUtils.translate(PlaceholderManager.setPlaceholders(SGM.getInstance().getMessages().getString("sgm.game.cant-start"), owner)));
|
|
}
|
|
activeGames.put(game.getGameId(), game);
|
|
}
|
|
|
|
public void addPlayerToGame(Player player, UUID gameId) {
|
|
Minigame game = findGame(gameId);
|
|
if (game.getState() == Minigame.GameState.READY && !game.getPlayers().contains(player) && !isInGame(player)) {
|
|
game.addPlayer(player);
|
|
}
|
|
}
|
|
|
|
public void removePlayerFromGame(Player player, UUID gameId) {
|
|
Minigame game = findGame(gameId);
|
|
game.removePlayer(player);
|
|
}
|
|
|
|
public void registerGame(Minigame game) {
|
|
registeredGames.add(game.getName().toLowerCase());
|
|
SGM.getInstance().getCLogger().log("Registered game " + game.getName());
|
|
}
|
|
|
|
} |