Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ID

An ID from Minecraft, which will have a path and may have a namespace.

Hierarchy

  • ID

Index

Constructors

constructor

  • new ID(path: string, namespace?: undefined | string): ID
  • Create a new ID.

    Examples

    These are all equivalent (mostly):

    • new ID("stone")
    • new ID("minecraft:stone")
    • new ID("stone", "minecraft")

    Parameters

    • path: string

      The path of the ID. If namespace is not passed this will be parsed (see ID.parseString).

    • Optional namespace: undefined | string

      The namespace of the ID. If this is included, path will be used as-is.

    Returns ID

Properties

Optional namespace

namespace: undefined | string

The namespace of this ID, such as minecraft in minecraft:stone. If undefined, e.g. when parsed from stone, it will be treated as if it was specified as "minecraft" (ID.DEFAULT_NAMESPACE) in most cases.

path

path: string

Static DEFAULT_NAMESPACE

DEFAULT_NAMESPACE: "minecraft" = "minecraft"

Static SEGMENT_REGEX

SEGMENT_REGEX: RegExp = /[0-9a-z_/\.-]/

Static SEP

SEP: ":" = ":"

Methods

equals

  • equals(other: ID): boolean
  • Test if this is the same ID as other. This treats a missing namespace as if it was minecraft.

    Example

    import { ID } from "minecraft-id";
    import * as assert from "assert";
    
    assert.ok(new ID("stone").equals(new ID("minecraft:stone")));
    assert.ok(!(new ID("dirt").equals(new ID("minecraft:stone"))));
    assert.ok(!(new ID("minecraft:stone").equals(new ID("namespace:stone"))));

    Parameters

    • other: ID

    Returns boolean

isNamespaceDefault

  • isNamespaceDefault(): boolean
  • Test if this has the DEFAULT_NAMESPACE. If the namespace is unspecified, this returns true

    Returns boolean

sameNamespace

  • sameNamespace(other: ID): boolean
  • Test if this has the same namespace as other. This ignores a missing namespace

    Example

    import { ID } from "minecraft-id";
    import * as assert from "assert";
    
    assert.ok(new ID("stone").sameNamespace(new ID("minecraft:stone")));
    assert.ok(new ID("dirt").sameNamespace(new ID("minecraft:stone")));
    assert.ok(!(new ID("minecraft:stone").sameNamespace(new ID("namespace:stone"))));
    assert.ok(new ID("namespace:stone").sameNamespace(new ID("namespace:dirt")));

    Parameters

    • other: ID

    Returns boolean

toString

  • toString(): string
  • Convert this ID into a string. This also allows IDs to be used in certain places where strings are expected, most notably in template literals.

    Example

    import * as assert from "assert";
    import {ID} from "minecraft-id";
    
    assert.strictEqual(`This is an ID - ${new ID("stone")}`, "This is an ID - minecraft:stone");

    Returns string

Static Private parseSegmentsFromString

  • parseSegmentsFromString(input: string, sep?: string): object
  • Parameters

    • input: string
    • Default value sep: string = ID.SEP

    Returns object

Static parseString

  • parseString(input: string, sep?: string): ID
  • Convert a string into an ID, with an optional seperator parameter. This has the same behaviour as the constructor called with a single argument if the seperator is not defined.

    If the seperator is defined, it is used to overwrite the seperator between the namespace and the tag, instead of using the default :.

    Examples

    • ID.fromString("stone") is equivalent to new ID("stone").
    • ID.fromString("minecraft:stone") is equivalent to new ID("minecraft:stone") and new ID("stone", "minecraft")
    • ID.fromString("minecraft.stone", ".") is equivalent to new ID("stone", "minecraft")

    Validation

    This does not validate the input, and instead naïvely splits based on the first occurance of sep. If validation is required is should be implemented manually, using ID.SEGMENT_REGEX to ensure that the segments (path and namespace) are valid

    Parameters

    • input: string
    • Default value sep: string = ID.SEP

      The seperator to use (defaults to :)

    Returns ID

Generated using TypeDoc