从昨天开始我的第一个插件,出现了一些问题。我有记分牌课。还有制作记分板、加载记分板和更改记分板的方法。
这里是:

package eu.anavicius.TomTom1997.TomTowerDefence;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;

public class Votes implements Listener {

    public Scoreboard board;
    public Score scores[] = new Score[6];

    public void sBoard () {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();

        Objective obj = board.registerNewObjective("VoteMap", "dummy");
        obj.setDisplayName("Vote for maps!");
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);

        Score scores[] = new Score[6];

        for (int i = 1; i<6; i++) {
            scores[i] = obj.getScore("Map " + i);
            scores[i].setScore(0);
            this.scores[i] = scores[i];
        }
    }

    public void setScore(int i) {
        int sc = scores[i].getScore();
        scores[i].setScore(sc+=1);

        for (Player p : Bukkit.getOnlinePlayers()) {
            p.setScoreboard(board);
        }
    }

    public void showTo (Player player) {
        player.setScoreboard(this.board);
    }

}

问题是,我不知道如何改变分数,也不知道如何将分数加载到不同级别的球员身上。例子:
//On my main class

public class Main extends Javaplugin {
    Votes vote = new Votes();

    public void onEnable() {
        vote.sBoard();// Initialized scoreboard
    }
}

// another class

public class JoinDicsEvents {
    // what should i write here to acces my initialized
    // scoreboard (I mean write vote.showTo(player){})
    // Everytime when I try something there appears NPE
}

最佳答案

编辑:
改变你的班级投票如下
1)使记分牌和分数参考静止

public class Votes implements Listener {

  public static Scoreboard board;
  public static Score scores[] = new Score[6];

2)将方法板更改为
   public static void sBoard () {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        if(board==null)
        board = manager.getNewScoreboard();

        Objective obj = board.registerNewObjective("VoteMap", "dummy");
        obj.setDisplayName("Vote for maps!");
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);

        Score scores[] = new Score[6];

        for (int i = 1; i<6; i++) {
            scores[i] = obj.getScore("Map " + i);
            scores[i].setScore(0);
            this.scores[i] = scores[i];
        }
    }

现在您可以使用votes.board从其他类访问该板。

07-24 20:19