gpt4 book ai didi

java - 需要帮助将对象传递给其他类(记分牌、bukkit)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:52 24 4
gpt4 key购买 nike

从昨天开始,我开始制作我的第一个插件,但出现了一些问题。我有记分牌类(class)。并且有制作记分板、加载记分板和更改分数的方法。

这里是:

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
}

最佳答案

编辑:按如下方式更改您的类(class)投票

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];
}
}

现在您可以使用投票从其他类(class)访问boardboard

关于java - 需要帮助将对象传递给其他类(记分牌、bukkit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24715714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com