For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server/classes/Scoreboard.md.

Class: Scoreboard

scoreboard

Beta

表示记分板。其上包含了记分项和分数持有者。

Contains objectives and participants for the scoreboard.

Example

updateScoreboard.ts

import { world, DisplaySlotId, ObjectiveSortOrder, DimensionLocation } from '@minecraft/server';

function updateScoreboard(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const scoreboardObjectiveId = 'scoreboard_demo_objective';
  const scoreboardObjectiveDisplayName = 'Demo Objective';

  const players = world.getPlayers();

  // Ensure a new objective.
  let objective = world.scoreboard.getObjective(scoreboardObjectiveId);

  if (!objective) {
    objective = world.scoreboard.addObjective(scoreboardObjectiveId, scoreboardObjectiveDisplayName);
  }

  // get the scoreboard identity for player 0
  const player0Identity = players[0].scoreboardIdentity;

  if (player0Identity === undefined) {
    log('Could not get a scoreboard identity for player 0.');
    return -1;
  }

  // initialize player score to 100;
  objective.setScore(player0Identity, 100);

  world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {
    objective: objective,
    sortOrder: ObjectiveSortOrder.Descending,
  });

  const playerScore = objective.getScore(player0Identity) ?? 0;

  // score should now be 110.
  objective.setScore(player0Identity, playerScore + 10);
}

Methods

addObjective()

addObjective(objectiveId, displayName?): ScoreboardObjective

World Mutation Beta

Parameters

objectiveId

string

记分项名称。

displayName?

string

记分项的显示名称。

Returns

ScoreboardObjective

创建的记分项对象。

Remarks

在记分板上添加一个新的记分项。

Adds a new objective to the scoreboard.

Throws

若同名记分项已存在时,抛出 "Failed to add objective 'objectiveId' as it is already being tracked"

Example

updateScoreboard.ts

import { world, DisplaySlotId, ObjectiveSortOrder, DimensionLocation } from '@minecraft/server';

function updateScoreboard(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const scoreboardObjectiveId = 'scoreboard_demo_objective';
  const scoreboardObjectiveDisplayName = 'Demo Objective';

  const players = world.getPlayers();

  // Ensure a new objective.
  let objective = world.scoreboard.getObjective(scoreboardObjectiveId);

  if (!objective) {
    objective = world.scoreboard.addObjective(scoreboardObjectiveId, scoreboardObjectiveDisplayName);
  }

  // get the scoreboard identity for player 0
  const player0Identity = players[0].scoreboardIdentity;

  if (player0Identity === undefined) {
    log('Could not get a scoreboard identity for player 0.');
    return -1;
  }

  // initialize player score to 100;
  objective.setScore(player0Identity, 100);

  world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {
    objective: objective,
    sortOrder: ObjectiveSortOrder.Descending,
  });

  const playerScore = objective.getScore(player0Identity) ?? 0;

  // score should now be 110.
  objective.setScore(player0Identity, playerScore + 10);
}

clearObjectiveAtDisplaySlot()

clearObjectiveAtDisplaySlot(displaySlotId): ScoreboardObjective | undefined

World Mutation Beta

Parameters

displaySlotId

DisplaySlotId

显示位置。

Returns

ScoreboardObjective | undefined

先前正显示的记分项,为空时返回 null

Remarks

清除显示位置上正在显示的记分项。

Clears the objective that occupies a display slot.


getObjective()

getObjective(objectiveId): ScoreboardObjective | undefined

Beta

Parameters

objectiveId

string

记分项名称。

Identifier of the objective.

Returns

ScoreboardObjective | undefined

指定的记分项对象。不存在时返回 null

Remarks

获取名称为 objectiveId 的记分项对象。

Returns a specific objective (by id).


getObjectiveAtDisplaySlot()

getObjectiveAtDisplaySlot(displaySlotId): ScoreboardObjectiveDisplayOptions | undefined

Beta

Parameters

displaySlotId

DisplaySlotId

显示位置。

Returns

ScoreboardObjectiveDisplayOptions | undefined

位于指定显示位置的记分项显示配置。为空时返回 null

Remarks

获取位于指定显示位置上的记分项对象与显示配置。

Returns an objective that occupies the specified display slot.


getObjectives()

getObjectives(): ScoreboardObjective[]

Beta

Returns

ScoreboardObjective[]

所有记分项对象组成的数组。

Remarks

获取记分板上的已定义的所有记分项。

Returns all defined objectives.


getParticipants()

getParticipants(): ScoreboardIdentity[]

Beta

Returns

ScoreboardIdentity[]

所有分数持有者对象组成的数组。

Remarks

获取所有已经定义的分数持有者。

Returns all defined scoreboard identities.


removeObjective()

removeObjective(objectiveId): boolean

World Mutation Beta

Parameters

objectiveId

string | ScoreboardObjective

记分项对象或名称。

Returns

boolean

总是返回 true

Remarks

从记分板上移除指定的记分项。

Removes an objective from the scoreboard.

Throws

若指定的记分项不存在时,抛出 "Failed to find the objective specified"


setObjectiveAtDisplaySlot()

setObjectiveAtDisplaySlot(displaySlotId, objectiveDisplaySetting): ScoreboardObjective | undefined

World Mutation Beta

Parameters

displaySlotId

DisplaySlotId

显示位置。

objectiveDisplaySetting

ScoreboardObjectiveDisplayOptions

记分项显示配置。

Returns

ScoreboardObjective | undefined

显示位上先前显示的记分项对象。先前未显示记分项时,返回 undefined

Returns the previous ScoreboardObjective set at the display slot, if no objective was previously set it returns undefined.

Remarks

设置指定的显示位置显示的记分项与其他显示配置。

Sets an objective into a display slot with specified additional display settings.

Throws

若记分项无效,抛出 "Failed to set invalid objective at DisplaySlot"

同领域相关