Scoreboard Help

I cannot seem to find anywhere on the forums or any tutorials that contains a tutorial on how to create a Scoreboard. By any chance, can anyone help me on this? Thank you!

@Aaron1011 if he has the time.

1 Like

Is there a reason why displaySlot(DisplaySlot displaySlot) method was removed in 2.1-SNAPSHOT when making a new Objective using ObjectiveBuilder class? That method was in 2.0 version of SpongeAPI.

@Atomicbeast101: The reason that was removed is because one objective can be added to multiple scoreboards. Instead, the display slot is set on each scoreboard, through the addObjective(Objective objective, DisplaySlot displaySlot) method on Scoreboard.

This allows the same objective to be set in a different display slot in each scoreboard it’s present on.

1 Like

Ah that makes sense. Thanks!!!

Glad to be able to help! :smiley:

I’m responsible for the scoreboard API and implementation, so feel free to ask me about any questions or problems you have.

My solution:

Scoreboard board = game.getRegistry().getScoreboardBuilder().build();
Objective obj = game.getRegistry().getObjectiveBuilder().name("OnlineStatus").criterion(Criteria.DUMMY).displayName(Texts.of(TextColors.YELLOW, "Online Status").build();
obj.getScore(Texts.of(TextColors.GREEN, TextStyles.BOLD, "Online:")).setScore(1);
obj.getScore(Texts.of(TextColors.WHITE, game.getServer().getOnlinePlayers().size() + "/10")).setScore(0);
board.addObjective(obj);
board.addObjective(obj, DisplaySlots.SIDEBAR);

Hello !
I have a question about the scoreboard because I tried to display a scoreboard with the following code:

@Subscribe
public void playerEvent(PlayerJoinEvent event)
{
    ...
    event.getUser().setScoreboard(board);
}

The fact is that the scoreboard is not displayed after this event. Is there a way to display the scoreboard at PlayerJoinEvent ? Because if we try we another event (ex: PlayerBreakBlockEvent), it works :smiley:

Can you please display the whole code there please? That way I will be able to help you more.

Sure,

@Plugin(id = "Scoreboard test", name = "Scoreboard test", version = "1.0")
public class SBTest
{
    @Inject
    private Game game;  
    private Scoreboard board;

    @Subscribe
    public void playerEvent(PlayerJoinEvent event)
    {
        event.getUser().sendMessage( Texts.of("PlayerJointEvent") );        
        
        /* Scoreboard build */
        /********************/
        // 1. Create an objective
        obj = game.getRegistry().getObjectiveBuilder()
                  .name("MyObjective").criterion(Criteria.DUMMY)
                  .displayName("My title").build();
        
        // 2. Add scores to the objective
        obj.getScore(Texts.of("My first score")).setScore(0);
        
        // 3. Add objective to the scoreboard
        board = game.getRegistry().getScoreboardBuilder().build();
        board.addObjective(obj);
        board.addObjective(obj, DisplaySlots.SIDEBAR);

        // 4. Display scoreboard to player
        event.getUser().setScoreboard(board);
    }     
}

But if we replace the event by PlayerBreakBlockEvent, it works.

1 Like

Instead of getUser(), use getSource(). Works for me. Make sure you registered the event as well.

@ezpzcraft: This is an issue with the implementation of PlayerJoinEvent. Currently, it’s fired before the player is sent the ‘Join Game’ packet, which causes the client to create a world instance.

Until that’s changed, you’ll need to either schedule setScoreboard to run 1 tick later, or use EntitySpawnEvent.

2 Likes

Oh okay ! Thank’s :slight_smile:

Could you not register a callback for player join instead of acting when the event is called?

Yes, but callbacks aren’t currently implemented ATM.