Class VanillaContainerWrapper.SlotWrapper

All Implemented Interfaces:
ValueIOSerializable, ResourceHandler<ItemResource>
Direct Known Subclasses:
PlayerInventoryWrapper.ArmorSlotWrapper
Enclosing class:
VanillaContainerWrapper

class VanillaContainerWrapper.SlotWrapper extends ItemStackResourceHandler
  • Field Details

    • index

      private final int index
  • Constructor Details

    • SlotWrapper

      SlotWrapper(int index)
  • Method Details

    • getStack

      protected ItemStack getStack()
      Description copied from class: ItemStackResourceHandler
      Return the stack of this handler. It will be modified directly sometimes to avoid needless copies. However, any mutation of the stack will directly be followed by a call to ItemStackResourceHandler.setStack(ItemStack). This means that either returning the backing stack directly or a copy is safe. If returning a copy, consider overriding ItemStackResourceHandler.getAmountAsLong(int) and ItemStackResourceHandler.getResource(int) to avoid unnecessary copies.
      Specified by:
      getStack in class ItemStackResourceHandler
      Returns:
      The current stack.
    • setStack

      protected void setStack(ItemStack item)
      Description copied from class: ItemStackResourceHandler
      Set the stack of this handler.
      Specified by:
      setStack in class ItemStackResourceHandler
    • isValid

      protected boolean isValid(ItemResource resource)
      Description copied from class: ItemStackResourceHandler
      Return true if the passed non-empty item resource can fit in this handler, false otherwise.

      The result of this function is used in the provided implementations of:

      Overrides:
      isValid in class ItemStackResourceHandler
    • getCapacity

      protected int getCapacity(ItemResource resource)
      Special cases because vanilla checks the current stack in the following functions (which it shouldn't):

      Should these extra checks cause performance issues, we can switch to subclasses.

      Overrides:
      getCapacity in class ItemStackResourceHandler
      Returns:
      The maximum capacity of this handler for the passed item resource.
    • insert

      public int insert(int index, ItemResource resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Inserts up to the given amount of a resource into the handler at the given index.

      Changes to the handler are made in the context of a transaction.

      Specified by:
      insert in interface ResourceHandler<ItemResource>
      Overrides:
      insert in class ItemStackResourceHandler
      Parameters:
      index - The index to insert the resource into.
      resource - The resource to insert. Must be non-empty.
      amount - The maximum amount of the resource to insert. Must be non-negative.
      transaction - The transaction that this operation is part of.
      Returns:
      The amount that was inserted. Between 0 (inclusive, nothing was inserted) and amount (inclusive, everything was inserted).
      See Also:
    • extract

      public int extract(int index, ItemResource resource, int amount, TransactionContext transaction)
      Description copied from interface: ResourceHandler
      Extracts up to the given amount of a resource from the handler at the given index.

      Changes to the handler are made in the context of a transaction.

      Specified by:
      extract in interface ResourceHandler<ItemResource>
      Overrides:
      extract in class ItemStackResourceHandler
      Parameters:
      index - The index to extract the resource from.
      resource - The resource to extract. Must be non-empty.
      amount - The maximum amount of the resource to extract. Must be non-negative.
      transaction - The transaction that this operation is part of.
      Returns:
      The amount that was extracted. Between 0 (inclusive, nothing was extracted) and amount (inclusive, everything was extracted).
      See Also:
    • updateSnapshots

      public void updateSnapshots(TransactionContext transaction)
      Description copied from class: SnapshotJournal
      Update the stored snapshots so that the changes happening as part of the passed transaction can be correctly committed or rolled back. This function should be called every time the journal is about to change its internal state as part of a transaction. However, only the first snapshot taken of that depth will be taken.
      Overrides:
      updateSnapshots in class SnapshotJournal<ItemStack>
    • onRootCommit

      protected void onRootCommit(ItemStack original)
      Description copied from class: SnapshotJournal
      Called after the root transaction was successfully committed, to perform irreversible actions such as setChanged() or neighbor updates.

      When a root transaction is being closed, all journals for which onRootCommit will be called are stored in a global thread-local queue. The processing of this queue starts immediately after the root transaction is closed. As such, new root transactions can safely be opened from this method.

      When a root transaction is opened from onRootCommit, any journal might be modified, leading to more onRootCommit callbacks being enqueued:

      • A journal that is already enqueued for onRootCommit will not be enqueued a second time. It will thus be notified a single time for changes that spanned multiple transactions. The originalState will be the state at the beginning of the first of these transactions.
      • A journal whose onRootCommit was already processed will be enqueued again. The journal will be notified a second time, with originalState the state at the beginning of the second transaction.
      • In particular, a journal is removed from the queue immediately before onRootCommit is called. Should the journal be modified again from its own onRootCommit, it will be added to the queue, and onRootCommit will be called again later.

      Given the large amount of actions that can happen between the last modification and the call to onRootCommit, journals should not depend on onRootCommit being called immediately for correctness, and implementations of this method should be careful (e.g. in case the journal got removed from the level). For example, skipping block change notifications because the block was removed from the level is preferable than crashing or silently overwriting the block.

      Overrides:
      onRootCommit in class SnapshotJournal<ItemStack>
      Parameters:
      original - state of this journal before the transactional operations. This corresponds to the first snapshot that was created in the transactional operations.
    • toString

      public String toString()
      Overrides:
      toString in class ItemStackResourceHandler