/FileExtraction/Java_unseen/Wind-Development_WindSpigot/WindSpigot-Server/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java
package org.bukkit.craftbukkit.scoreboard;

import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;

import net.minecraft.server.Scoreboard;
import net.minecraft.server.ScoreboardObjective;
import net.minecraft.server.ScoreboardScore;

/**
 * TL;DR: This class is special and lazily grabs a handle... ...because a handle
 * is a full fledged (I think permanent) hashMap for the associated name.
 * <p>
 * Also, as an added perk, a CraftScore will (intentionally) stay a valid
 * reference so long as objective is valid.
 */
final class CraftScore implements Score {
	private final String entry;
	private final CraftObjective objective;

	CraftScore(CraftObjective objective, String entry) {
		this.objective = objective;
		this.entry = entry;
	}

	@Override
	public OfflinePlayer getPlayer() {
		return Bukkit.getOfflinePlayer(entry);
	}

	@Override
	public String getEntry() {
		return entry;
	}

	@Override
	public Objective getObjective() {
		return objective;
	}

	@Override
	public int getScore() throws IllegalStateException {
		Scoreboard board = objective.checkState().board;

		if (board.getPlayers().contains(entry)) { // Lazy
			Map<ScoreboardObjective, ScoreboardScore> scores = board.getPlayerObjectives(entry);
			ScoreboardScore score = scores.get(objective.getHandle());
			if (score != null) { // Lazy
				return score.getScore();
			}
		}

		return 0; // Lazy
	}

	@Override
	public void setScore(int score) throws IllegalStateException {
		objective.checkState().board.getPlayerScoreForObjective(entry, objective.getHandle()).setScore(score);
	}

	@Override
	public CraftScoreboard getScoreboard() {
		return objective.getScoreboard();
	}

	// Spigot start
	@Override
	public boolean isScoreSet() throws IllegalStateException {
		Scoreboard board = objective.checkState().board;

		return board.getPlayers().contains(entry)
				&& board.getPlayerObjectives(entry).containsKey(objective.getHandle());
	}
	// Spigot end
}
