pub struct ResourceLocation<'a> { /* private fields */ }Expand description
Represents a Minecraft resource location.
Resource locations are namespaced identifiers referencing blocks, items, entity types, recipes, functions, advancements, tags, and various other objects in vanilla Minecraft.
A valid resource location has a format of "namespace:path". If the
namespace portion is left out, then "minecraft" is the implied
namespace.
§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.
Implementations§
Source§impl<'a> ResourceLocation<'a>
impl<'a> ResourceLocation<'a>
Sourcepub fn new(kind: ResourceKind, id: &'a str) -> Self
pub fn new(kind: ResourceKind, id: &'a str) -> Self
Constructs a new ResourceLocation from the given type and id.
The id string will be borrowed. You can either use to_owned()
to convert the location to an owned representation, or construct on
directly using new_owned().
§Example
let location = ResourceLocation::new(ResourceKind::BlockModel, "oak_stairs");Sourcepub fn new_owned(kind: ResourceKind, id: String) -> ResourceLocation<'static>
pub fn new_owned(kind: ResourceKind, id: String) -> ResourceLocation<'static>
Like new(), but returns a ResourceLocation that owns its
internal string.
Sourcepub fn blockstates(block_id: &'a str) -> Self
pub fn blockstates(block_id: &'a str) -> Self
Constructs a new ResourceLocation referencing the BlockStates of
the given block id.
§Example
let location = ResourceLocation::blockstates("stone");
let location = ResourceLocation::blockstates("minecraft:dirt");Sourcepub fn block_model(block_id: &'a str) -> Self
pub fn block_model(block_id: &'a str) -> Self
Constructs a new ResourceLocation referencing the BlockModel of
the given block id.
Sourcepub fn item_model(item_id: &'a str) -> Self
pub fn item_model(item_id: &'a str) -> Self
Constructs a new ResourceLocation referencing the ItemModel of
the given item id.
Sourcepub fn texture(path: &'a str) -> Self
pub fn texture(path: &'a str) -> Self
Constructs a new ResourceLocation referencing the Texture
located at the given path.
§Example
let location = ResourceLocation::texture("block/stone");
let location = ResourceLocation::texture("item/diamond_hoe");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the underlying identifier as a string slice.
§Example
let location = ResourceLocation::blockstates("stone");
assert_eq!(location.as_str(), "stone");
let location = ResourceLocation::blockstates("minecraft:dirt");
assert_eq!(location.as_str(), "minecraft:dirt");Sourcepub fn has_namespace(&self) -> bool
pub fn has_namespace(&self) -> bool
Returns whether or not this resource location includes an explicit namespace.
§Example
let location = ResourceLocation::blockstates("foo:bar");
assert!(location.has_namespace());
let location = ResourceLocation::blockstates("bar");
assert!(!location.has_namespace());Sourcepub fn namespace(&self) -> &str
pub fn namespace(&self) -> &str
Returns the namespace portion of the resource identifier, or
"minecraft" if it does not have an explicit namespace.
§Example
let location = ResourceLocation::blockstates("foo:bar");
assert_eq!(location.namespace(), "foo");
let location = ResourceLocation::blockstates("bar");
assert_eq!(location.namespace(), "minecraft");
let location = ResourceLocation::blockstates(":bar");
assert_eq!(location.namespace(), "");Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Returns the path portion of the resource location.
§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.
Sourcepub fn kind(&self) -> ResourceKind
pub fn kind(&self) -> ResourceKind
Returns what kind of resource is referenced by this location.
Sourcepub fn is_builtin(&self) -> bool
pub fn is_builtin(&self) -> bool
Returns true if the resource location refers to a built-in resource.
If true, then there is no corresponding file that contains the
resource.
§Example
let loc = ResourceLocation::item_model("builtin/generated");
assert!(loc.is_builtin());Sourcepub fn to_canonical(&self) -> ResourceLocation<'a>
pub fn to_canonical(&self) -> ResourceLocation<'a>
Returns a new location 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 location = ResourceLocation::blockstates("stone");
let canonical = location.to_canonical();
assert_eq!(canonical.as_str(), "minecraft:stone");Performs a shallow copy when a namespace is already present:
let location = ResourceLocation::blockstates("foo:bar");
let canonical = location.to_canonical();
assert_eq!(canonical.as_str(), "foo:bar");
// Prove that it was a cheap copy.
assert_eq!(
location.as_str().as_ptr() as usize,
canonical.as_str().as_ptr() as usize,
);Sourcepub fn to_owned(&self) -> ResourceLocation<'static>
pub fn to_owned(&self) -> ResourceLocation<'static>
Returns a new ResourceLocation that owns the underlying string.
This is useful for, e.g., storing the location in a data structure or passing it to another thread.
By default, all ResourceLocations borrow the string they are
constructed with, so no copying will occur unless you call this
function.
§Examples
Constructing a location using From simply borrows the data:
let string = String::new("my:resource");
let location = ResourceLocation::from(&string);
// Location borrows data from `string`, cannot be sent across threads.
std::thread::spawn(move || println!("{}", location));Calling to_owned() on the location allows it to be
sent to the thread:
let string = "my:resource".to_string();
let location = ResourceLocation::blockstates(&string);
let location = location.to_owned();
std::thread::spawn(move || println!("{}", location));Trait Implementations§
Source§impl<'a> AsRef<str> for ResourceLocation<'a>
impl<'a> AsRef<str> for ResourceLocation<'a>
Source§impl<'a> Clone for ResourceLocation<'a>
impl<'a> Clone for ResourceLocation<'a>
Source§fn clone(&self) -> ResourceLocation<'a>
fn clone(&self) -> ResourceLocation<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more