Plugin Help Help with scoreboard

Discussion in 'Plugin Help/Development/Requests' started by ShownPC, Jul 22, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ShownPC

    I created a new scoreboard inside onEnable with

    ScoreboardManager Manager = Bukkit.getScoreboardManager();
    Scoreboard board = Manager.getNewScoreboard();

    I want to know how to access this scoreboard from a different class.
     
  2. Offline

    Machine Maker

    @ShownPC
    Create what's called a getter. Initialize the variable board outside of the onEnable() method, then set board to a new Scoreboard. Then create a new method (can be called anything) called getBoard() and have that method return the board instance. Now all you have to do in your other class is

    Code:
    plugin.getBoard().doSomething();
    (plugin is the instance of your Main class)
     
  3. Offline

    ShownPC

    how do I create the instance of the main class?
     
  4. Offline

    Machine Maker

    @ShownPC Don't create an instance of it. The server already creates one for you. It depends on what kind of class file you want the instance in. If this is an event class, just add a constructor for the class that takes your main class as an argument.
    Code:
    private Plugin p;
    public EventClass(Plugin plugin) {
            this.p = plugin;
    }
    Then, when you register the event in your main class,

    Code:
    Bukkit.getPluginManager().registerEvents(new EventClass(this), this);
    You just pass this (the instance of your main class) to the event constructor.

    Now* in your class you have a variable p, that is your is main class.

    *changed Not to Now
     
    Last edited: Jul 22, 2017
  5. Offline

    aceMlg

    this is doesnt working , in the console it says there is a problem in our main class in line 63
    and what we have in that line is:
    Scoreboard board = Manager.getNewScoreboard();
     
  6. Offline

    Machine Maker

    @aceMlg
    Make sure you are just initializing the Scoreboard board object outside of the onEnable() method.

    Code:
    Scoreboard board;
    ScoreboardManager manager;
    public void onEnable() {
            //other stuff
            manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
    }
    
    If there are any errors this time, please post the error log using [URL]https://pastebin.com[/URL].
     
  7. Offline

    aceMlg

    already did that..
    this is the method:
    ScoreboardManager Manager = Bukkit.getScoreboardManager();
    Scoreboard board = Manager.getNewScoreboard();
    public Scoreboard getBoard(){

    return board;}
     
  8. Offline

    Machine Maker

    @aceMlg If what you showed me right there is exactly what you have in your code, you did not already do that. I have part of that code in the onEnable() method and you don't have anything in there. I define the variables just above the onEnable() method and don't set them to any value until the onEnable() method is called on server start.

    Main class​

    Code:
    package me.x1machinemaker1x.arierajail.objects;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    public class Main extends JavaPlugin {
       
        private ScoreboardManager manager;
        private Scoreboard board;
        public void onEnable() {
           
            //set commands/events
           
            Bukkit.getPluginManager().registerEvents(new TestEvent(this), this);
           
            manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
        }
       
        public Scoreboard getBoard() {
            return board;
        }
    
    }
    
    Event class​
    Code:
    package me.x1machinemaker1x.arierajail.objects;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.scoreboard.Scoreboard;
    
    public class TestEvent implements Listener {
       
        private Main main;
       
        public TestEvent(Main main) {
            this.main = main;
        }
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Scoreboard board = main.getBoard();
        }
    
    }
     
    Last edited: Jul 22, 2017
Thread Status:
Not open for further replies.

Share This Page