|
| 1 | +--- |
| 2 | +order: 1 |
| 3 | +authors: |
| 4 | + - JorelAli |
| 5 | + - DerEchtePilz |
| 6 | + - willkroboth |
| 7 | + - misode |
| 8 | +--- |
| 9 | + |
| 10 | +# Scoreboard arguments |
| 11 | + |
| 12 | +The CommandAPI uses two classes to provide information about a scoreboard: |
| 13 | + |
| 14 | +- The `ScoreHolderArgument` class represents **score holder** - a player's name or an entity's UUID that has scores in an objective. This is described in more detail [on the Minecraft Wiki](https://minecraft.wiki/w/Scoreboard#Objectives). |
| 15 | +- The `ScoreboardSlotArgument` class represents a **display slot** (sidebar, list or belowName) as well as the team color if the display is the sidebar. This is described in more detail [on the Minecraft Wiki](https://minecraft.wiki/w/Scoreboard#Display_slots). |
| 16 | + |
| 17 | +## Score holder argument |
| 18 | + |
| 19 | +The score holder argument can accept either a single entity or a collection of multiple entities. To specify which one to use, you must use the `ScoreHolderArgument.Single` or `ScoreHolderArgument.Multiple` constructor respectively: |
| 20 | + |
| 21 | +```java |
| 22 | +new ScoreHolderArgument.Single(nodeName); |
| 23 | +new ScoreHolderArgument.Multiple(nodeName); |
| 24 | +``` |
| 25 | + |
| 26 | +Depending on which constructor is used, the cast type changes. If you use `ScoreHolderArgument.Single`, the argument must be cast to a `String`. Otherwise, if you use `ScoreHolderArgument.Multiple`, the argument must be cast to a `Collection<String>`. |
| 27 | + |
| 28 | +::::tip Example – Rewarding players with scoreboard objectives |
| 29 | + |
| 30 | +Say we want to reward all players that fit certain criteria. We want a command with the following syntax: |
| 31 | + |
| 32 | +```mccmd |
| 33 | +/reward <players> |
| 34 | +``` |
| 35 | + |
| 36 | +Since we could have multiple players that fit a certain criterion, we want to use `ScoreHolderArgument.Multiple` constructor. |
| 37 | + |
| 38 | +To give this example a bit more context, let's say we want to reward all players that have died less than 10 times on the server. To do this, we will use the following command: |
| 39 | + |
| 40 | +```mccmd |
| 41 | +/reward @e[type=player,scores={deaths=..9}] |
| 42 | +``` |
| 43 | + |
| 44 | +Note how we use `..9` to represent nine or fewer deaths (since ranges are inclusive). Also note how we restrict our input to players via the command using `type=player`. We can now implement our command: |
| 45 | + |
| 46 | +:::tabs |
| 47 | +===Java |
| 48 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/scoreboard/ScoreboardArguments.java#scoreHolderArgumentExample |
| 49 | +===Kotlin |
| 50 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/scoreboard/ScoreboardArguments.kt#scoreHolderArgumentExample |
| 51 | +===Kotlin DSL |
| 52 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/scoreboard/ScoreboardArguments.kt#scoreHolderArgumentExampleDSL |
| 53 | +::: |
| 54 | + |
| 55 | +:::: |
| 56 | + |
| 57 | +:::info |
| 58 | + |
| 59 | +In the example above, we have our user use the `@e[type=player]` entity selector to restrict the `Collection<String>` so it only returns player names, which allows us to use `Bukkit.getPlayer(playerName)`. In practice, we can’t guarantee that such a selector will be used, so we could update the code to accept both entities and players. For example, we can differentiate between players and entities by using the `UUID.fromString(String)` method: |
| 60 | + |
| 61 | +```java |
| 62 | +Collection<String> entitiesAndPlayers = (Collection<String>) args.get(0); |
| 63 | +for(String str : entitiesAndPlayers) { |
| 64 | + try { |
| 65 | + UUID uuid = UUID.fromString(str); |
| 66 | + // Is a UUID, so it must by an entity |
| 67 | + Bukkit.getEntity(uuid); |
| 68 | + } catch(IllegalArgumentException exception) { |
| 69 | + // Not a UUID, so it must be a player name |
| 70 | + Bukkit.getPlayer(str); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +::: |
| 76 | + |
| 77 | +## Scoreboard slot argument |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +The `ScoreboardSlotArgument` represents where scoreboard information is displayed. Since the Bukkit scoreboard `DisplaySlot` is not able to represent the case where team colors are provided, the CommandAPI uses the `ScoreboardSlot` wrapper class as the representation of the `ScoreboardSlotArgument`. |
| 82 | + |
| 83 | +### `ScoreboardSlot` wrapper |
| 84 | + |
| 85 | +The `ScoreboardSlot` wrapper class has three main methods: |
| 86 | + |
| 87 | +```java |
| 88 | +enum ScoreboardSlot { |
| 89 | + PLAYER_LIST, |
| 90 | + SIDEBAR, // Unused, use the other SIDEBAR_TEAM_### values below |
| 91 | + BELOW_NAME, |
| 92 | + SIDEBAR_TEAM_BLACK, |
| 93 | + SIDEBAR_TEAM_DARK_BLUE, |
| 94 | + SIDEBAR_TEAM_DARK_GREEN, |
| 95 | + SIDEBAR_TEAM_DARK_AQUA, |
| 96 | + SIDEBAR_TEAM_DARK_RED, |
| 97 | + SIDEBAR_TEAM_DARK_PURPLE, |
| 98 | + SIDEBAR_TEAM_GOLD, |
| 99 | + SIDEBAR_TEAM_GRAY, |
| 100 | + SIDEBAR_TEAM_DARK_GRAY, |
| 101 | + SIDEBAR_TEAM_BLUE, |
| 102 | + SIDEBAR_TEAM_GREEN, |
| 103 | + SIDEBAR_TEAM_AQUA, |
| 104 | + SIDEBAR_TEAM_RED, |
| 105 | + SIDEBAR_TEAM_LIGHT_PURPLE, |
| 106 | + SIDEBAR_TEAM_YELLOW, |
| 107 | + SIDEBAR_TEAM_WHITE; |
| 108 | + |
| 109 | + public DisplaySlot getDisplaySlot(); |
| 110 | + public ChatColor getTeamColor(); |
| 111 | + public boolean hasTeamColor(); |
| 112 | + |
| 113 | + public NamespacedKey getKey(); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +The `getDisplaySlot()` method returns the display slot that was chosen. Sidebar scoreboard colors can be accessed via `ScoreboardSlot.SIDEBAR_TEAM_###`. You can also retrieve the color using the `getTeamColor()` method. |
| 118 | + |
| 119 | +::::tip Example – Clearing objectives in a scoreboard slot |
| 120 | + |
| 121 | +Say we want to clear all objectives in a specific scoreboard slot. In this example, we will use the main server scoreboard, which is accessed using `Bukkit.getScoreboardManager.getMainScoreboard()`. We want a command with the following syntax: |
| 122 | + |
| 123 | +```mccmd |
| 124 | +/clearobjectives <slot> |
| 125 | +``` |
| 126 | + |
| 127 | +We implement this simply by using the `ScoreboardSlotArgument` as our argument, and then we can clear the slot using the scoreboard `clearSlot(DisplaySlot)` method. |
| 128 | + |
| 129 | +:::tabs |
| 130 | +===Java |
| 131 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/scoreboard/ScoreboardArguments.java#scoreboardSlotArgumentExample |
| 132 | +===Kotlin |
| 133 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/scoreboard/ScoreboardArguments.kt#scoreboardSlotArgumentExample |
| 134 | +===Kotlin DSL |
| 135 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/scoreboard/ScoreboardArguments.kt#scoreboardSlotArgumentExampleDSL |
| 136 | +::: |
| 137 | + |
| 138 | +:::: |
0 commit comments