Skip to main content

ResourceHandler

Trait ResourceHandler 

pub trait ResourceHandler<R> {
    // Required methods
    fn size(&self, host: &mut dyn ServerHost) -> usize;
    fn resource(&self, host: &mut dyn ServerHost, index: usize) -> Option<R>;
    fn amount(&self, host: &mut dyn ServerHost, index: usize) -> u32;
    fn capacity(
        &self,
        host: &mut dyn ServerHost,
        index: usize,
        resource: &R,
    ) -> u32;
    fn insert(
        &self,
        host: &mut dyn ServerHost,
        tx: &Transaction,
        index: usize,
        resource: &R,
        amount: u32,
    ) -> u32;
    fn extract(
        &self,
        host: &mut dyn ServerHost,
        tx: &Transaction,
        index: usize,
        resource: &R,
        amount: u32,
    ) -> u32;

    // Provided methods
    fn set_max_stack_size(
        &self,
        _host: &mut dyn ServerHost,
        _tx: &Transaction,
        _limit: u32,
    ) -> bool { ... }
    fn is_valid(
        &self,
        host: &mut dyn ServerHost,
        index: usize,
        resource: &R,
    ) -> bool { ... }
    fn insert_any(
        &self,
        host: &mut dyn ServerHost,
        tx: &Transaction,
        resource: &R,
        amount: u32,
    ) -> u32 { ... }
    fn extract_any(
        &self,
        host: &mut dyn ServerHost,
        tx: &Transaction,
        resource: &R,
        amount: u32,
    ) -> u32 { ... }
}
Expand description

The contents of one holder (a block entity as one side reaches it, an item where it lives), numbered from 0, as NeoForge’s ResourceHandler: a provider makes one for its holder, so it knows where its contents live. It holds no borrow of the game: each call is given the host, and its state is the game’s (slots, a saved compound, attachments, a stack’s components), so the transaction tx a write runs in puts it back on an abort. Amounts never exceed what was asked; a handler answering more is a mod bug, and the move it was part of is aborted.

Required Methods§

fn size(&self, host: &mut dyn ServerHost) -> usize

How many indices it has.

fn resource(&self, host: &mut dyn ServerHost, index: usize) -> Option<R>

What index index holds, if anything.

fn amount(&self, host: &mut dyn ServerHost, index: usize) -> u32

How much of it.

fn capacity(&self, host: &mut dyn ServerHost, index: usize, resource: &R) -> u32

How much of resource index index holds when it holds that, whatever it holds now (getCapacityAsLong). Capacity is not free space or permission to insert: validity, current contents and access rules can still refuse it.

fn insert( &self, host: &mut dyn ServerHost, tx: &Transaction, index: usize, resource: &R, amount: u32, ) -> u32

Puts up to amount of resource into index index: how much went in.

fn extract( &self, host: &mut dyn ServerHost, tx: &Transaction, index: usize, resource: &R, amount: u32, ) -> u32

Takes up to amount of resource out of index index: how much came out. None of another resource.

Provided Methods§

fn set_max_stack_size( &self, _host: &mut dyn ServerHost, _tx: &Transaction, _limit: u32, ) -> bool

Set the container limit transactionally, accepting 1..=99 (vanilla’s upper bound). Existing oversized stacks remain extractable. The game persists this with block entity data. Unsupported handlers return false without mutation.

fn is_valid( &self, host: &mut dyn ServerHost, index: usize, resource: &R, ) -> bool

Whether resource may go in index index at all (isValid). The default uses capacity; a handler with separate acceptance rules overrides this and enforces them in insert.

fn insert_any( &self, host: &mut dyn ServerHost, tx: &Transaction, resource: &R, amount: u32, ) -> u32

Puts up to amount of resource in wherever it goes (NeoForge’s insert without an index): by default index by index from 0. How much went in.

fn extract_any( &self, host: &mut dyn ServerHost, tx: &Transaction, resource: &R, amount: u32, ) -> u32

Takes up to amount of resource out from wherever it is (extract without an index): by default index by index from 0. How much came out.

Trait Implementations§

§

impl Wire for Box<dyn ResourceHandler<ItemResource>>

§

const MIN: usize = 8

Minimum encoded size, used to reject impossible collection lengths.
§

fn put(&self, out: &mut Writer)

Encodes this value, recording failures on the writer.
§

fn put_owned(self, out: &mut Writer)

Consumes owned data when a resource’s ownership must cross the boundary.
§

fn take( input: &mut Reader<'_>, ) -> Result<Box<dyn ResourceHandler<ItemResource>>, WireError>

Decodes one value, leaving any following bytes for the caller.
§

impl Wire for Box<dyn ResourceHandler<FluidResource>>

§

const MIN: usize = 8

Minimum encoded size, used to reject impossible collection lengths.
§

fn put(&self, out: &mut Writer)

Encodes this value, recording failures on the writer.
§

fn put_owned(self, out: &mut Writer)

Consumes owned data when a resource’s ownership must cross the boundary.
§

fn take( input: &mut Reader<'_>, ) -> Result<Box<dyn ResourceHandler<FluidResource>>, WireError>

Decodes one value, leaving any following bytes for the caller.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§