Scoreboards are often used to display dynamically changing information to players, and allay has a build-in scoreboard
system. In this section, you will learn how to use scoreboards in your plugin.
importorg.allaymc.api.eventbus.EventHandler;importorg.allaymc.api.eventbus.event.server.PlayerJoinEvent;importorg.allaymc.api.eventbus.event.server.PlayerQuitEvent;importorg.allaymc.api.scoreboard.Scoreboard;importorg.allaymc.api.scoreboard.data.DisplaySlot;importorg.allaymc.api.server.Server;importjava.util.List;publicclassMyScoreboard{protectedstaticfinalScoreboardinfo=newScoreboard("INFO");static{update();}protectedstaticvoidupdate(){varnetworkSettings=Server.SETTINGS.networkSettings();info.setLines(List.of("Welcome to the server!",networkSettings.ip()+":"+networkSettings.port()));}@EventHandlerpublicvoidonPlayerJoin(PlayerJoinEventevent){info.addViewer(event.getPlayer(),DisplaySlot.SIDEBAR);/*(1)!*/}@EventHandlerpublicvoidonPlayerQuit(PlayerQuitEventevent){info.removeViewer(event.getPlayer(),DisplaySlot.SIDEBAR);/*(2)!*/}}
We choose to display the information in the sidebar, so that the player can see the information on the right
side of the screen
Don't forget to remove the player from the scoreboard when they quit the server because the player will not be
removed automatically. DisplaySlot should be same as the one you added (DisplaySlot.SIDEBAR in this case).
And that's it! Now the player will see the server information when they join the server. Here we
listened to PlayerJoinEvent and PlayerQuitEvent. For how to use event handler, please refer
to here.
importorg.allaymc.api.eventbus.EventHandler;importorg.allaymc.api.eventbus.event.server.PlayerJoinEvent;importorg.allaymc.api.eventbus.event.server.PlayerQuitEvent;importorg.allaymc.api.scoreboard.Scoreboard;importorg.allaymc.api.scoreboard.data.DisplaySlot;importorg.allaymc.api.server.Server;importjava.util.List;publicclassMyScoreboard{protectedstaticfinalScoreboardinfo=newScoreboard("INFO");protectedstaticvoidupdate(){varnetworkSettings=Server.SETTINGS.networkSettings();info.setLines(List.of("Welcome to the server!","Online: "+Server.getInstance().getPlayerManager().getPlayerCount(),networkSettings.ip()+":"+networkSettings.port()));}@EventHandlerpublicvoidonPlayerJoin(PlayerJoinEventevent){info.addViewer(event.getPlayer(),DisplaySlot.SIDEBAR);update();}@EventHandlerpublicvoidonPlayerQuit(PlayerQuitEventevent){info.removeViewer(event.getPlayer(),DisplaySlot.SIDEBAR);update();}}
We just update the scoreboard when player joins or quits the server by using Scoreboard#setLines() method. And the
scoreboard will be resent to the existing viewers automatically.
In the above examples, we used DisplaySlot.SIDEBAR to display the scoreboard in the sidebar. Allay provides three
types of display slots:
Type
Description
SIDEBAR
Located on the right side of the player's screen, this is the most commonly used slot type.
LIST
Located on the online player list, if you want the player screen to stay clean, you can use this slot type.
BELOW_NAME
A special slot type which will be shown below player's name tag. Notice that this slot type is very different from the other two types, in most cases you don't need to use this slot type.