从昨天起我开始制作我的第一个插件,并出现了一些问题。我有记分板课。还有一些方法可以制作记分牌,加载和改变分数。
这是:
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
}
答案 0 :(得分:1)
编辑:
如下更改您的课堂投票
1)使记分板和分数参考静态
public class Votes implements Listener {
public static Scoreboard board;
public static Score scores[] = new Score[6];
2)将方法sBoard更改为
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];
}
}
现在,您可以使用投票从其他课程访问主板。主板