| 1 | package net.minecraft.world.scores;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2IntMaps;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
| 10 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 11 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
| 12 | import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.Collection;
|
| 15 | import java.util.Collections;
|
| 16 | import java.util.EnumMap;
|
| 17 | import java.util.List;
|
| 18 | import java.util.Map;
|
| 19 | import java.util.Objects;
|
| 20 | import java.util.function.Consumer;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.network.chat.numbers.NumberFormat;
|
| 23 | import net.minecraft.world.entity.Entity;
|
| 24 | import net.minecraft.world.entity.player.Player;
|
| 25 | import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
| 26 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class Scoreboard {
|
| 31 | public static final String HIDDEN_SCORE_PREFIX = "#";
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 | private final Object2ObjectMap<String, Objective> objectivesByName = new Object2ObjectOpenHashMap<>(16, 0.5F);
|
| 34 | private final Reference2ObjectMap<ObjectiveCriteria, List<Objective>> objectivesByCriteria = new Reference2ObjectOpenHashMap<>();
|
| 35 | private final Map<String, PlayerScores> playerScores = new Object2ObjectOpenHashMap<>(16, 0.5F);
|
| 36 | private final Map<DisplaySlot, Objective> displayObjectives = new EnumMap<>(DisplaySlot.class);
|
| 37 | private final Object2ObjectMap<String, PlayerTeam> teamsByName = new Object2ObjectOpenHashMap<>();
|
| 38 | private final Object2ObjectMap<String, PlayerTeam> teamsByPlayer = new Object2ObjectOpenHashMap<>();
|
| 39 |
|
| 40 | public @Nullable Objective getObjective(final @Nullable String name) {
|
| 41 | return this.objectivesByName.get(name);
|
| 42 | }
|
| 43 |
|
| 44 | public Objective addObjective(
|
| 45 | final String name,
|
| 46 | final ObjectiveCriteria criteria,
|
| 47 | final Component displayName,
|
| 48 | final ObjectiveCriteria.RenderType renderType,
|
| 49 | final boolean displayAutoUpdate,
|
| 50 | final @Nullable NumberFormat numberFormat
|
| 51 | ) {
|
| 52 | if (this.objectivesByName.containsKey(name)) {
|
| 53 | throw new IllegalArgumentException("An objective with the name '" + name + "' already exists!");
|
| 54 | }
|
| 55 |
|
| 56 | Objective objective = new Objective(this, name, criteria, displayName, renderType, displayAutoUpdate, numberFormat);
|
| 57 | this.objectivesByCriteria.computeIfAbsent(criteria, k -> Lists.newArrayList()).add(objective);
|
| 58 | this.objectivesByName.put(name, objective);
|
| 59 | this.onObjectiveAdded(objective);
|
| 60 | return objective;
|
| 61 | }
|
| 62 |
|
| 63 | public final void forAllObjectives(final ObjectiveCriteria criteria, final ScoreHolder name, final Consumer<ScoreAccess> operation) {
|
| 64 | this.objectivesByCriteria.getOrDefault(criteria, Collections.emptyList()).forEach(o -> operation.accept(this.getOrCreatePlayerScore(name, o, true)));
|
| 65 | }
|
| 66 |
|
| 67 | private PlayerScores getOrCreatePlayerInfo(final String name) {
|
| 68 | return this.playerScores.computeIfAbsent(name, k -> new PlayerScores());
|
| 69 | }
|
| 70 |
|
| 71 | public ScoreAccess getOrCreatePlayerScore(final ScoreHolder holder, final Objective objective) {
|
| 72 | return this.getOrCreatePlayerScore(holder, objective, false);
|
| 73 | }
|
| 74 |
|
| 75 | public ScoreAccess getOrCreatePlayerScore(final ScoreHolder scoreHolder, final Objective objective, final boolean forceWritable) {
|
| 76 | final boolean canModify = forceWritable || !objective.getCriteria().isReadOnly();
|
| 77 | PlayerScores playerScore = this.getOrCreatePlayerInfo(scoreHolder.getScoreboardName());
|
| 78 | final MutableBoolean requiresSync = new MutableBoolean();
|
| 79 | final Score score = playerScore.getOrCreate(objective, newScore -> requiresSync.setTrue());
|
| 80 | return new ScoreAccess() {
|
| 81 | @Override
|
| 82 | public int get() {
|
| 83 | return score.value();
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void set(final int value) {
|
| 88 | if (!canModify) {
|
| 89 | throw new IllegalStateException("Cannot modify read-only score");
|
| 90 | }
|
| 91 |
|
| 92 | boolean hasChanged = requiresSync.isTrue();
|
| 93 | if (objective.displayAutoUpdate()) {
|
| 94 | Component newDisplay = scoreHolder.getDisplayName();
|
| 95 | if (newDisplay != null && !newDisplay.equals(score.display())) {
|
| 96 | score.display(newDisplay);
|
| 97 | hasChanged = true;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | if (value != score.value()) {
|
| 102 | score.value(value);
|
| 103 | hasChanged = true;
|
| 104 | }
|
| 105 |
|
| 106 | if (hasChanged) {
|
| 107 | this.sendScoreToPlayers();
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public @Nullable Component display() {
|
| 113 | return score.display();
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void display(final @Nullable Component display) {
|
| 118 | if (requiresSync.isTrue() || !Objects.equals(display, score.display())) {
|
| 119 | score.display(display);
|
| 120 | this.sendScoreToPlayers();
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void numberFormatOverride(final @Nullable NumberFormat numberFormat) {
|
| 126 | score.numberFormat(numberFormat);
|
| 127 | this.sendScoreToPlayers();
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean locked() {
|
| 132 | return score.isLocked();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void unlock() {
|
| 137 | this.setLocked(false);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void lock() {
|
| 142 | this.setLocked(true);
|
| 143 | }
|
| 144 |
|
| 145 | private void setLocked(final boolean locked) {
|
| 146 | score.setLocked(locked);
|
| 147 | if (requiresSync.isTrue()) {
|
| 148 | this.sendScoreToPlayers();
|
| 149 | }
|
| 150 |
|
| 151 | Scoreboard.this.onScoreLockChanged(scoreHolder, objective);
|
| 152 | }
|
| 153 |
|
| 154 | private void sendScoreToPlayers() {
|
| 155 | Scoreboard.this.onScoreChanged(scoreHolder, objective, score);
|
| 156 | requiresSync.setFalse();
|
| 157 | }
|
| 158 | };
|
| 159 | }
|
| 160 |
|
| 161 | public @Nullable ReadOnlyScoreInfo getPlayerScoreInfo(final ScoreHolder name, final Objective objective) {
|
| 162 | PlayerScores playerScore = this.playerScores.get(name.getScoreboardName());
|
| 163 | return playerScore != null ? playerScore.get(objective) : null;
|
| 164 | }
|
| 165 |
|
| 166 | public Collection<PlayerScoreEntry> listPlayerScores(final Objective objective) {
|
| 167 | List<PlayerScoreEntry> result = new ArrayList<>();
|
| 168 | this.playerScores.forEach((player, scores) -> {
|
| 169 | Score score = scores.get(objective);
|
| 170 | if (score != null) {
|
| 171 | result.add(new PlayerScoreEntry(player, score.value(), score.display(), score.numberFormat()));
|
| 172 | }
|
| 173 | });
|
| 174 | return result;
|
| 175 | }
|
| 176 |
|
| 177 | public Collection<Objective> getObjectives() {
|
| 178 | return this.objectivesByName.values();
|
| 179 | }
|
| 180 |
|
| 181 | public Collection<String> getObjectiveNames() {
|
| 182 | return this.objectivesByName.keySet();
|
| 183 | }
|
| 184 |
|
| 185 | public Collection<ScoreHolder> getTrackedPlayers() {
|
| 186 | return this.playerScores.keySet().stream().map(ScoreHolder::forNameOnly).toList();
|
| 187 | }
|
| 188 |
|
| 189 | public void resetAllPlayerScores(final ScoreHolder player) {
|
| 190 | PlayerScores removed = this.playerScores.remove(player.getScoreboardName());
|
| 191 | if (removed != null) {
|
| 192 | this.onPlayerRemoved(player);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | public void resetSinglePlayerScore(final ScoreHolder player, final Objective objective) {
|
| 197 | PlayerScores scores = this.playerScores.get(player.getScoreboardName());
|
| 198 | if (scores != null) {
|
| 199 | boolean hasRemoved = scores.remove(objective);
|
| 200 | if (!scores.hasScores()) {
|
| 201 | PlayerScores removedPlayer = this.playerScores.remove(player.getScoreboardName());
|
| 202 | if (removedPlayer != null) {
|
| 203 | this.onPlayerRemoved(player);
|
| 204 | }
|
| 205 | } else if (hasRemoved) {
|
| 206 | this.onPlayerScoreRemoved(player, objective);
|
| 207 | }
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | public Object2IntMap<Objective> listPlayerScores(final ScoreHolder player) {
|
| 212 | PlayerScores scores = this.playerScores.get(player.getScoreboardName());
|
| 213 | return scores != null ? scores.listScores() : Object2IntMaps.emptyMap();
|
| 214 | }
|
| 215 |
|
| 216 | public void removeObjective(final Objective objective) {
|
| 217 | this.objectivesByName.remove(objective.getName());
|
| 218 |
|
| 219 | for (DisplaySlot value : DisplaySlot.values()) {
|
| 220 | if (this.getDisplayObjective(value) == objective) {
|
| 221 | this.setDisplayObjective(value, null);
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | List<Objective> objectives = this.objectivesByCriteria.get(objective.getCriteria());
|
| 226 | if (objectives != null) {
|
| 227 | objectives.remove(objective);
|
| 228 | }
|
| 229 |
|
| 230 | for (PlayerScores playerScore : this.playerScores.values()) {
|
| 231 | playerScore.remove(objective);
|
| 232 | }
|
| 233 |
|
| 234 | this.onObjectiveRemoved(objective);
|
| 235 | }
|
| 236 |
|
| 237 | public void setDisplayObjective(final DisplaySlot slot, final @Nullable Objective objective) {
|
| 238 | this.displayObjectives.put(slot, objective);
|
| 239 | }
|
| 240 |
|
| 241 | public @Nullable Objective getDisplayObjective(final DisplaySlot slot) {
|
| 242 | return this.displayObjectives.get(slot);
|
| 243 | }
|
| 244 |
|
| 245 | public @Nullable PlayerTeam getPlayerTeam(final String name) {
|
| 246 | return this.teamsByName.get(name);
|
| 247 | }
|
| 248 |
|
| 249 | public PlayerTeam addPlayerTeam(final String name) {
|
| 250 | PlayerTeam team = this.getPlayerTeam(name);
|
| 251 | if (team != null) {
|
| 252 | LOGGER.warn("Requested creation of existing team '{}'", name);
|
| 253 | return team;
|
| 254 | } else {
|
| 255 | team = new PlayerTeam(this, name);
|
| 256 | this.teamsByName.put(name, team);
|
| 257 | this.onTeamAdded(team);
|
| 258 | return team;
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | public void removePlayerTeam(final PlayerTeam team) {
|
| 263 | this.teamsByName.remove(team.getName());
|
| 264 |
|
| 265 | for (String player : team.getPlayers()) {
|
| 266 | this.teamsByPlayer.remove(player);
|
| 267 | }
|
| 268 |
|
| 269 | this.onTeamRemoved(team);
|
| 270 | }
|
| 271 |
|
| 272 | public boolean addPlayerToTeam(final String player, final PlayerTeam team) {
|
| 273 | if (this.getPlayersTeam(player) != null) {
|
| 274 | this.removePlayerFromTeam(player);
|
| 275 | }
|
| 276 |
|
| 277 | this.teamsByPlayer.put(player, team);
|
| 278 | return team.getPlayers().add(player);
|
| 279 | }
|
| 280 |
|
| 281 | public boolean removePlayerFromTeam(final String player) {
|
| 282 | PlayerTeam team = this.getPlayersTeam(player);
|
| 283 | if (team != null) {
|
| 284 | this.removePlayerFromTeam(player, team);
|
| 285 | return true;
|
| 286 | } else {
|
| 287 | return false;
|
| 288 | }
|
| 289 | }
|
| 290 |
|
| 291 | public void removePlayerFromTeam(final String player, final PlayerTeam team) {
|
| 292 | if (this.getPlayersTeam(player) != team) {
|
| 293 | throw new IllegalStateException("Player is either on another team or not on any team. Cannot remove from team '" + team.getName() + "'.");
|
| 294 | }
|
| 295 |
|
| 296 | this.teamsByPlayer.remove(player);
|
| 297 | team.getPlayers().remove(player);
|
| 298 | }
|
| 299 |
|
| 300 | public Collection<String> getTeamNames() {
|
| 301 | return this.teamsByName.keySet();
|
| 302 | }
|
| 303 |
|
| 304 | public Collection<PlayerTeam> getPlayerTeams() {
|
| 305 | return this.teamsByName.values();
|
| 306 | }
|
| 307 |
|
| 308 | public @Nullable PlayerTeam getPlayersTeam(final String name) {
|
| 309 | return this.teamsByPlayer.get(name);
|
| 310 | }
|
| 311 |
|
| 312 | public void onObjectiveAdded(final Objective objective) {
|
| 313 | }
|
| 314 |
|
| 315 | public void onObjectiveChanged(final Objective objective) {
|
| 316 | }
|
| 317 |
|
| 318 | public void onObjectiveRemoved(final Objective objective) {
|
| 319 | }
|
| 320 |
|
| 321 | protected void onScoreChanged(final ScoreHolder owner, final Objective objective, final Score score) {
|
| 322 | }
|
| 323 |
|
| 324 | protected void onScoreLockChanged(final ScoreHolder owner, final Objective objective) {
|
| 325 | }
|
| 326 |
|
| 327 | public void onPlayerRemoved(final ScoreHolder player) {
|
| 328 | }
|
| 329 |
|
| 330 | public void onPlayerScoreRemoved(final ScoreHolder player, final Objective objective) {
|
| 331 | }
|
| 332 |
|
| 333 | public void onTeamAdded(final PlayerTeam team) {
|
| 334 | }
|
| 335 |
|
| 336 | public void onTeamChanged(final PlayerTeam team) {
|
| 337 | }
|
| 338 |
|
| 339 | public void onTeamRemoved(final PlayerTeam team) {
|
| 340 | }
|
| 341 |
|
| 342 | public void entityRemoved(final Entity entity) {
|
| 343 | if (!(entity instanceof Player) && !entity.isAlive()) {
|
| 344 | this.resetAllPlayerScores(entity);
|
| 345 | this.removePlayerFromTeam(entity.getScoreboardName());
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | protected List<Scoreboard.PackedScore> packPlayerScores() {
|
| 350 | return this.playerScores
|
| 351 | .entrySet()
|
| 352 | .stream()
|
| 353 | .flatMap(
|
| 354 | playerEntry -> {
|
| 355 | String player = playerEntry.getKey();
|
| 356 | return playerEntry.getValue()
|
| 357 | .listRawScores()
|
| 358 | .entrySet()
|
| 359 | .stream()
|
| 360 | .map(entry -> new Scoreboard.PackedScore(player, entry.getKey().getName(), entry.getValue().pack()));
|
| 361 | }
|
| 362 | )
|
| 363 | .toList();
|
| 364 | }
|
| 365 |
|
| 366 | protected void loadPlayerScore(final Scoreboard.PackedScore score) {
|
| 367 | Objective objective = this.getObjective(score.objective);
|
| 368 | if (objective == null) {
|
| 369 | LOGGER.error("Unknown objective {} for name {}, ignoring", score.objective, score.owner);
|
| 370 | } else {
|
| 371 | this.getOrCreatePlayerInfo(score.owner).setScore(objective, new Score(score.score));
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | protected List<PlayerTeam.Packed> packPlayerTeams() {
|
| 376 | return this.getPlayerTeams().stream().map(PlayerTeam::pack).toList();
|
| 377 | }
|
| 378 |
|
| 379 | protected void loadPlayerTeam(final PlayerTeam.Packed packed) {
|
| 380 | PlayerTeam team = this.addPlayerTeam(packed.name());
|
| 381 | packed.displayName().ifPresent(team::setDisplayName);
|
| 382 | team.setColor(packed.color());
|
| 383 | team.setAllowFriendlyFire(packed.allowFriendlyFire());
|
| 384 | team.setSeeFriendlyInvisibles(packed.seeFriendlyInvisibles());
|
| 385 | team.setPlayerPrefix(packed.memberNamePrefix());
|
| 386 | team.setPlayerSuffix(packed.memberNameSuffix());
|
| 387 | team.setNameTagVisibility(packed.nameTagVisibility());
|
| 388 | team.setDeathMessageVisibility(packed.deathMessageVisibility());
|
| 389 | team.setCollisionRule(packed.collisionRule());
|
| 390 |
|
| 391 | for (String player : packed.players()) {
|
| 392 | this.addPlayerToTeam(player, team);
|
| 393 | }
|
| 394 | }
|
| 395 |
|
| 396 | protected List<Objective.Packed> packObjectives() {
|
| 397 | return this.getObjectives().stream().map(Objective::pack).toList();
|
| 398 | }
|
| 399 |
|
| 400 | protected void loadObjective(final Objective.Packed objective) {
|
| 401 | this.addObjective(
|
| 402 | objective.name(),
|
| 403 | objective.criteria(),
|
| 404 | objective.displayName(),
|
| 405 | objective.renderType(),
|
| 406 | objective.displayAutoUpdate(),
|
| 407 | objective.numberFormat().orElse(null)
|
| 408 | );
|
| 409 | }
|
| 410 |
|
| 411 | protected Map<DisplaySlot, String> packDisplaySlots() {
|
| 412 | Map<DisplaySlot, String> displaySlots = new EnumMap<>(DisplaySlot.class);
|
| 413 |
|
| 414 | for (DisplaySlot slot : DisplaySlot.values()) {
|
| 415 | Objective objective = this.getDisplayObjective(slot);
|
| 416 | if (objective != null) {
|
| 417 | displaySlots.put(slot, objective.getName());
|
| 418 | }
|
| 419 | }
|
| 420 |
|
| 421 | return displaySlots;
|
| 422 | }
|
| 423 |
|
| 424 | public record PackedScore(String owner, String objective, Score.Packed score) {
|
| 425 | public static final Codec<Scoreboard.PackedScore> CODEC = RecordCodecBuilder.create(
|
| 426 | i -> i.group(
|
| 427 | Codec.STRING.fieldOf("Name").forGetter(Scoreboard.PackedScore::owner),
|
| 428 | Codec.STRING.fieldOf("Objective").forGetter(Scoreboard.PackedScore::objective),
|
| 429 | Score.Packed.MAP_CODEC.forGetter(Scoreboard.PackedScore::score)
|
| 430 | )
|
| 431 | .apply(i, Scoreboard.PackedScore::new)
|
| 432 | );
|
| 433 | }
|
| 434 | }
|
| 435 |
|