Search in sources :

Example 6 with VoxelGameLibException

use of com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.

the class MojangUtil method getDisplayName.

// TODO we need to do some caching to not break mojang api rate limits
/**
 * Tries to fetch the current display name for the user
 *
 * @param id the id of the user to check
 * @return the current display name of that user
 * @throws IOException           if something goes wrong
 * @throws VoxelGameLibException if the user has no display name
 */
@Nonnull
public static String getDisplayName(@Nonnull UUID id) throws IOException, VoxelGameLibException {
    URL url = new URL(NAME_HISTORY_URL.replace("%1", id.toString().replace("-", "")));
    System.out.println(url.toString());
    Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(url.openStream())));
    if (scanner.hasNext()) {
        String json = scanner.nextLine();
        try {
            JSONArray jsonArray = (JSONArray) new JSONParser().parse(json);
            if (json.length() > 0) {
                return (String) ((JSONObject) jsonArray.get(0)).get("name");
            }
        } catch (ParseException ignore) {
        }
    }
    throw new VoxelGameLibException("User has no name! " + id);
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) BufferedReader(java.io.BufferedReader) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) URL(java.net.URL) Nonnull(javax.annotation.Nonnull)

Example 7 with VoxelGameLibException

use of com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method checkEnd.

private void checkEnd() {
    // check all victory conditions
    User winner = null;
    Team winnerTeam = null;
    for (VictoryCondition victoryCondition : victoryConditions) {
        if (!victoryCondition.completed()) {
            return;
        }
        if (victoryCondition.getWinner() != null) {
            if (!victoryCondition.getWinner().equals(winner)) {
                if (winner == null) {
                    if (winnerTeam != null && !winnerTeam.contains(victoryCondition.getWinner())) {
                        throw new VoxelGameLibException(victoryCondition.getName() + " defined a winner even tho we already have a winning team!");
                    }
                    winner = victoryCondition.getWinner();
                } else {
                    throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winner than one of the conditions before it!");
                }
            }
        }
        if (victoryCondition.getWinnerTeam() != null) {
            if (!victoryCondition.getWinnerTeam().equals(winnerTeam)) {
                if (winnerTeam == null) {
                    if (winner != null && !victoryCondition.getWinnerTeam().contains(winner)) {
                        throw new VoxelGameLibException(victoryCondition.getName() + " defined a winning team even tho we already have a winning user!");
                    } else {
                        winnerTeam = victoryCondition.getWinnerTeam();
                    }
                } else {
                    throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winning team than one of the conditions before it!");
                }
            }
        }
    }
    // all done, end this game
    getGame().endGame(winnerTeam, winner);
}
Also used : User(com.voxelgameslib.voxelgameslib.components.user.User) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) Team(com.voxelgameslib.voxelgameslib.components.team.Team) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.conditions.EmptyVictoryCondition) VictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.VictoryCondition)

Aggregations

VoxelGameLibException (com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException)7 Nonnull (javax.annotation.Nonnull)4 PlayerProfile (com.destroystokyo.paper.profile.PlayerProfile)2 User (com.voxelgameslib.voxelgameslib.components.user.User)2 BaseCommand (co.aikar.commands.BaseCommand)1 CommandAlias (co.aikar.commands.annotation.CommandAlias)1 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Default (co.aikar.commands.annotation.Default)1 Subcommand (co.aikar.commands.annotation.Subcommand)1 Syntax (co.aikar.commands.annotation.Syntax)1 VictoryCondition (com.voxelgameslib.voxelgameslib.api.condition.VictoryCondition)1 EmptyVictoryCondition (com.voxelgameslib.voxelgameslib.api.condition.conditions.EmptyVictoryCondition)1 GameStartException (com.voxelgameslib.voxelgameslib.api.exception.GameStartException)1 DefaultGameData (com.voxelgameslib.voxelgameslib.api.game.DefaultGameData)1 MapHandler (com.voxelgameslib.voxelgameslib.components.map.MapHandler)1 MarkerDefinition (com.voxelgameslib.voxelgameslib.components.map.MarkerDefinition)1 Team (com.voxelgameslib.voxelgameslib.components.team.Team)1 Lang (com.voxelgameslib.voxelgameslib.internal.lang.Lang)1 LangKey (com.voxelgameslib.voxelgameslib.internal.lang.LangKey)1 TextureHandler (com.voxelgameslib.voxelgameslib.internal.texture.TextureHandler)1