类 Scoreboard实验性

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

Contains objectives and participants for the scoreboard.

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);
}

方法

  • 实验性

    参数

    • objectiveId: string

      记分项名称。

    • 可选displayName: string

      记分项的显示名称。

    返回 ScoreboardObjective

    创建的记分项对象。

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

    Adds a new objective to the scoreboard.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

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

    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);
    }
  • 实验性

    参数

    • objectiveId: string

      记分项名称。

      Identifier of the objective.

    返回 ScoreboardObjective

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

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

    Returns a specific objective (by id).

  • 实验性

    参数

    返回 boolean

    总是返回 true

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

    Removes an objective from the scoreboard.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

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

  • 实验性

    参数

    返回 ScoreboardObjective

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

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

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

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

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

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