summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java
blob: 0ffbe9b08d89b9db367bcbb9482c5256b4779616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.bukkit.craftbukkit.scoreboard;

import java.util.Map;

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

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

/**
 * 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 playerName;
    private final CraftObjective objective;

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

    public OfflinePlayer getPlayer() {
        return Bukkit.getOfflinePlayer(playerName);
    }

    public Objective getObjective() {
        return objective;
    }

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

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

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

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