3

Since yesterday I started to make my first plugin and there appeared some issues. I have class for scoreboard. And there is methods to make scoreboard, load it and change scores.

Here it is:

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

}

The problem is that I don't know how to change scores or load it to the player in different classes. Example:

//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
}
Anat0m
  • 37
  • 6
  • Just throwing this out there. Your `vote` reference is inside of the class Main and is not accessible from a different class such as JoinDiscsEvents in the way you show. You could pass the reference into JoinDiscsEvents if you create an instance of that class inside of the Main class. – takendarkk Jul 12 '14 at 17:55
  • Just use `Votes vote = new Votes(); vote.sBoard();` in any class you want to initialize the scoreboard, or, if you were to make the method `static`, you could just use `Votes.sBoard();`. – Jojodmo Jul 12 '14 at 17:58
  • But if I need use that scoreboard in three different classes that means I have to create three scoreboards which I don't want to do. What I need to do is use **created** scoreboard – Anat0m Jul 12 '14 at 18:01
  • The best solution may be dependency injection, but I'm not 100% clear on your problem to fully recommend this. Where do you create JoinDicsEvents relative to your Main class? – Hovercraft Full Of Eels Jul 12 '14 at 20:55
  • I'm not quite sure what are you asking for, but in the same package. – Anat0m Jul 12 '14 at 21:03
  • Do you create a `new Main()` anywhere in your code? Do you call `new JoinDicsEvents()` anywhere in your code? – Hovercraft Full Of Eels Jul 12 '14 at 21:06
  • No it isn't necessary, because those classes have methods which uses bukkit. – Anat0m Jul 12 '14 at 21:10

1 Answers1

1

EDIT: Change your class vote as follows

1) Make Scoreboard and scores reference static

public class Votes implements Listener {

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

2) Change your method sBoard to

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

Now you could access the board from other classes using Votes.board

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
humblerookie
  • 4,717
  • 4
  • 25
  • 40
  • Sorry I'm newbie, but I don't understand why I need ScoreboadManager class? And what does the singleton do? – Anat0m Jul 12 '14 at 18:45
  • Singleton is when you create just one object of a class and have it shared among all,which seems to be what you need. YOu can get this done by having a static variable. YOu have imported the scoreboardManager whose source code I presumed was available to you . In case it isnt then just create a static instance of the Scoreboard in any class and access it from there. – humblerookie Jul 12 '14 at 18:49
  • `board=new Scoreboard();` <--- this part throws: Cannot instantiate the type Scoreboard – Anat0m Jul 12 '14 at 19:04
  • Thank you very much! I fixed some mistakes myself and now this class works! You're he best :) – Anat0m Jul 12 '14 at 19:45
  • I know, but I can't find any solutions :/. – Anat0m Jul 12 '14 at 20:31
  • 1
    @Hovercraft :I concur..I suggested a Singleton pattern but It seems hez using a 3rd party library and the only way to use the same would be this .Personally I'm not fond of this approach either. – humblerookie Jul 12 '14 at 20:52
  • Then I remove my down-vote. I must claim ignorance for use of Bukkit. – Hovercraft Full Of Eels Jul 12 '14 at 22:45