ResourceLocation

Struct ResourceLocation 

Source
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>

Source

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");
Source

pub fn new_owned(kind: ResourceKind, id: String) -> ResourceLocation<'static>

Like new(), but returns a ResourceLocation that owns its internal string.

Source

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");
Source

pub fn block_model(block_id: &'a str) -> Self

Constructs a new ResourceLocation referencing the BlockModel of the given block id.

Source

pub fn item_model(item_id: &'a str) -> Self

Constructs a new ResourceLocation referencing the ItemModel of the given item id.

Source

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");
Source

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");
Source

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());
Source

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(), "");
Source

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.

Source

pub fn kind(&self) -> ResourceKind

Returns what kind of resource is referenced by this location.

Source

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());
Source

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,
);
Source

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>

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for ResourceLocation<'a>

Source§

fn clone(&self) -> ResourceLocation<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ResourceLocation<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for ResourceLocation<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Hash for ResourceLocation<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for ResourceLocation<'a>

Source§

fn eq(&self, other: &ResourceLocation<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for ResourceLocation<'a>

Source§

impl<'a> StructuralPartialEq for ResourceLocation<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for ResourceLocation<'a>

§

impl<'a> RefUnwindSafe for ResourceLocation<'a>

§

impl<'a> Send for ResourceLocation<'a>

§

impl<'a> Sync for ResourceLocation<'a>

§

impl<'a> Unpin for ResourceLocation<'a>

§

impl<'a> UnwindSafe for ResourceLocation<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.