Struct minecraft_assets::api::ResourceIdentifier [−][src]
pub struct ResourceIdentifier<'a> { /* fields omitted */ }
Expand description
A namespaced, typed resource identifier.
ResourceIdentifiers
reference blocks, items, entity types, recipes,
functions, advancements, tags, and various other objects in Minecraft.
A valid resource identifier has a format of "namespace:path"
. If the
namespace
portion is left out, then "minecraft"
is the implied
namespace.
Some examples:
"block/stone"
"minecraft:diamond_hoe"
"foo:bar/baz"
Read more on the wiki.
Borrowing / Ownership
To avoid cloning / String
construction when not necessary, this type can
either borrow or take ownership of the underlying string.
By default, no copying or allocating is done. You must call
to_owned()
to get an owned identifier.
Strong Typing
The string representation of a ResourceIdentifier
does not provide
enough information to figure out the full path to a given resource. For
example, "block/kelp"
may refer to any of the following:
assets/minecraft/models/block/kelp.json
assets/minecraft/textures/block/kelp.png
assets/minecraft/textures/block/kelp.png.mcmeta
Because of this, ResourceIdentifier
requires you to specify a
ResourceKind
upon construction. Thus, the identifier is “strongly
typed.”
This also solves an issue with how model identifiers are formatted in
resource packs prior to 1.13. See the ModelIdentifier
docs for more
information.
Implementations
Constructs a new ResourceIdentifier
from the given type and id.
The id
string will be borrowed. You can either use to_owned()
to convert the id to an owned representation, or construct on
directly using new_owned()
.
Example
let id = ResourceIdentifier::new(ResourceKind::BlockModel, "oak_stairs");
Like new()
, but returns a ResourceIdentifier
that owns its
internal string.
Constructs a new ResourceIdentifier
referencing the BlockStates
of
the given block id.
Example
let id = ResourceIdentifier::blockstates("stone");
let id = ResourceIdentifier::blockstates("minecraft:dirt");
Constructs a new ResourceIdentifier
referencing the BlockModel
of
the given block id.
Constructs a new ResourceIdentifier
referencing the ItemModel
of
the given item id.
Constructs a new ResourceIdentifier
referencing the Texture
located at the given path.
Example
let id = ResourceIdentifier::texture("block/stone");
let id = ResourceIdentifier::texture("item/diamond_hoe");
Returns the underlying identifier as a string slice.
Example
let id = ResourceIdentifier::blockstates("stone");
assert_eq!(id.as_str(), "stone");
let id = ResourceIdentifier::blockstates("minecraft:dirt");
assert_eq!(id.as_str(), "minecraft:dirt");
Returns whether or not this resource id includes an explicit namespace.
Example
let id = ResourceIdentifier::blockstates("foo:bar");
assert!(id.has_namespace());
let id = ResourceIdentifier::blockstates("bar");
assert!(!id.has_namespace());
Returns the namespace portion of the resource identifier, or
"minecraft"
if it does not have an explicit namespace.
Example
let id = ResourceIdentifier::blockstates("foo:bar");
assert_eq!(id.namespace(), "foo");
let id = ResourceIdentifier::blockstates("bar");
assert_eq!(id.namespace(), "minecraft");
let id = ResourceIdentifier::blockstates(":bar");
assert_eq!(id.namespace(), "");
Returns the path portion of the resource id.
Note on Models
For BlockModel
or ItemModel
resources, the name will not
include any leading prefix like block/
or item/
. See the
ModelIdentifier
documentation for more information.
Returns what kind of resource is referenced by this id.
Returns true if the resource id refers to a built-in resource.
If true
, then there is no corresponding file that contains the
resource.
Example
let id = ResourceIdentifier::item_model("builtin/generated");
assert!(id.is_builtin());
Returns a new id with a canonical representation (i.e., containing an explicit namespace).
This will involve allocating a new String
if self
does not already
contain an explicit namespace.
Examples
Prepends the default namespace when one is not present:
let id = ResourceIdentifier::blockstates("stone");
let canonical = id.to_canonical();
assert_eq!(canonical.as_str(), "minecraft:stone");
Performs a shallow copy when a namespace is already present:
let id = ResourceIdentifier::blockstates("foo:bar");
let canonical = id.to_canonical();
assert_eq!(canonical.as_str(), "foo:bar");
// Prove that it was a cheap copy.
assert_eq!(
id.as_str().as_ptr() as usize,
canonical.as_str().as_ptr() as usize,
);
Prepends block/
or item/
for models if missing:
let id = ResourceIdentifier::item_model("minecraft:diamond_hoe");
let canonical = id.to_canonical();
assert_eq!(canonical.as_str(), "minecraft:item/diamond_hoe");
Returns a new ResourceIdentifier
that owns the underlying string.
This is useful for, e.g., storing the id in a data structure or passing it to another thread.
By default, all ResourceIdentifier
s borrow the string they are
constructed with, so no copying will occur unless you call this
function.
Examples
Constructing an id using From
simply borrows the data:
let string = String::new("my:resource");
let id = ResourceIdentifier::from(&string);
// Location borrows data from `string`, cannot be sent across threads.
std::thread::spawn(move || println!("{}", id));
Calling to_owned()
on the id allows it to be
sent to the thread:
let string = "my:resource".to_string();
let id = ResourceIdentifier::blockstates(&string);
let id = id.to_owned();
std::thread::spawn(move || println!("{}", id));
Trait Implementations
Auto Trait Implementations
impl<'a> RefUnwindSafe for ResourceIdentifier<'a>
impl<'a> Send for ResourceIdentifier<'a>
impl<'a> Sync for ResourceIdentifier<'a>
impl<'a> Unpin for ResourceIdentifier<'a>
impl<'a> UnwindSafe for ResourceIdentifier<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more