com.sky8the2flies.KOTH.util.board.SkyEntry.java Source code

Java tutorial

Introduction

Here is the source code for com.sky8the2flies.KOTH.util.board.SkyEntry.java

Source

package com.sky8the2flies.KOTH.util.board;

import java.util.Iterator;

import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Team;

import com.google.common.base.Splitter;

/**
 * ======================================================================
 * Copyright sky8the2flies {c} 2014. All Rights Reserved.
 * Any code contained within this document, and any associated APIs with similar branding
 * are the sole property of sky8the2flies. Distribution, reproduction, taking snippets, or
 * claiming any contents as your own will break the terms of the License, and void any
 * agreements with you, the third party.
 * Thanks, sky8the2flies LLC
 * ======================================================================
 */
public class SkyEntry {

    private String key;
    private SkyBoard skyboard;
    private String name;
    private Team team;
    private Score score;
    private int value;

    private String origName;
    private int count;

    public SkyEntry(String key, SkyBoard spigboard, int value) {
        this.key = key;
        this.skyboard = spigboard;
        this.value = value;
        this.count = 0;
    }

    public SkyEntry(String key, SkyBoard spigboard, int value, String origName, int count) {
        this.key = key;
        this.skyboard = spigboard;
        this.value = value;
        this.origName = origName;
        this.count = count;
    }

    public String getKey() {
        return key;
    }

    public SkyBoard getSkyboard() {
        return skyboard;
    }

    public String getName() {
        return name;
    }

    public Team getTeam() {
        return team;
    }

    public Score getScore() {
        return score;
    }

    public int getValue() {
        return score != null ? (value = score.getScore()) : value;
    }

    public void setValue(int value) {
        if (!score.isScoreSet()) {
            score.setScore(-1);
        }

        score.setScore(value);
    }

    public void update(String newName) {
        int value = getValue();
        if (origName != null && newName.equals(origName)) {
            for (int i = 0; i < count; i++) {
                newName = ChatColor.RESET + newName;
            }

        }

        create(newName);
        setValue(value);
    }

    void remove() {
        if (score != null) {
            try {
                score.getScoreboard().resetScores(score.getEntry());
            } catch (Exception npe) {

            }
        }

        if (team != null) {
            try {
                team.unregister();
            } catch (Exception npe) {

            }
        }
    }

    private void create(String name) {
        this.name = name;
        remove();

        if (name.length() <= 16) {
            int value = getValue();
            score = skyboard.getObjective().getScore(name);
            score.setScore(value);
            return;
        }

        team = skyboard.getScoreboard().registerNewTeam("skyboard-" + skyboard.getTeamId());
        Iterator<String> iterator = Splitter.fixedLength(16).split(name).iterator();
        if (name.length() > 16)
            team.setPrefix(iterator.next());
        String entry = iterator.next();
        score = skyboard.getObjective().getScore(entry);
        if (name.length() > 32)
            team.setSuffix(iterator.next());

        team.addEntry(entry);
    }

}