API reference Obsidian.API.BlockEntities
ContainerBlockEntity class
A block entity holding items, like vanilla's chests, barrels, furnaces and brewing stands: its ContainerBlockEntity.Inventory is what players see when they open its block, and is saved like vanilla's containers.
public class ContainerBlockEntity : BlockEntity, IItemAccessProvider
In Obsidian.API.
Remarks
Saved like vanilla's RandomizableContainerBlockEntity: CustomName, and the loot table it's waiting for (LootTable, LootTableSeed) or else its Items, each with its Slot. Slots past the inventory's size are skipped. Changes to the inventory are saved without calling BlockEntity.SetChanged. Vanilla saves a slot's number as a byte, so an inventory holds at most ContainerBlockEntity.MaxSlots slots.
A container with a loot table, like an unopened dungeon chest, is empty until a player opens it, or it's removed, which fills it from the table.
Other blocks reach its items through ContainerBlockEntity.GetItemAccess, from every side to every slot unless a subclass overrides ContainerBlockEntity.GetSlots, ContainerBlockEntity.CanInsert and ContainerBlockEntity.CanExtract, like vanilla's WorldlyContainer.
A container that derives state from its items overrides ContainerBlockEntity.OnItemsChanged, which the server calls at the end of the level's tick after they changed, rather than comparing its slots every tick.
Constructors
ContainerBlockEntity(BaseContainer)
public ContainerBlockEntity(BaseContainer inventory);Creates a block entity holding inventory, at most ContainerBlockEntity.MaxSlots slots.
Properties
DropsContents
public bool DropsContents { get; init; }Whether the items drop when the block is removed, as vanilla's containers' do; shulker boxes keep them.
Inventory
public BaseContainer Inventory { get; }The items the block entity holds, which opening its block shows.
LootTable
public string LootTable { get; set; }The loot table the container is filled from when it's first opened, like minecraft:chests/simple_dungeon, or null once it was (or when it has none).
LootTableSeed
public long LootTableSeed { get; set; }The seed ContainerBlockEntity.LootTable is rolled with; 0 picks a random one.
Methods
CanExtract(int, ItemStack, BlockFace?)
protected internal virtual bool CanExtract(int slot, ItemStack stack, BlockFace? side);Whether stack, in slot, may be taken out from side (null for no particular side), like vanilla's canTakeItemThroughFace. Called with the inventory's lock held. True by default.
CanInsert(int, ItemStack, BlockFace?)
protected internal virtual bool CanInsert(int slot, ItemStack stack, BlockFace? side);Whether stack may be put into slot from side (null for no particular side), like vanilla's canPlaceItem and canPlaceItemThroughFace. Called with the inventory's lock held: read ContainerBlockEntity.Inventory, don't wait on anything. True by default.
GetItemAccess(BlockFace?)
public virtual IItemAccess GetItemAccess(BlockFace? side);The access other blocks have to the container's items from side: the slots ContainerBlockEntity.GetSlots gives, under the rules of ContainerBlockEntity.CanInsert and ContainerBlockEntity.CanExtract.
Remarks
Each call through it is atomic with players' clicks in the container's windows, and changes nothing once the block entity is removed (its inventory is retired). A real change calls BlockEntity.SetChanged, and the players with the container open see it at the end of the level's tick. Reaching an unopened container rolls its loot table first, as vanilla's hoppers do.
GetSlots(BlockFace?)
protected internal virtual IReadOnlyList<int> GetSlots(BlockFace? side);The slots of ContainerBlockEntity.Inventory reached from side, in the order they're filled and emptied, like vanilla's WorldlyContainer.getSlotsForFace; all of them from no particular side. All of them, in order, by default.
protected internal override void Load(NbtCompound tag);LockInventory()
protected IDisposable LockInventory();Locks this container's inventory against menu clicks for a synchronous part of its tick. Returns null when the container is unavailable, or the caller holds the level's tick lock.
Remarks
Read and change ContainerBlockEntity.Inventory directly until the handle is disposed. Do not await, call item access (which takes the same lock), or acquire other locks while holding it. Changes notify viewers and comparators on disposal. Pending loot is prepared before locking; retired inventories cannot be changed.
OnItemsChanged()
protected internal virtual void OnItemsChanged();Called at the end of the level's tick after ContainerBlockEntity.Inventory's items changed, once however many changes the tick made, and once after the block entity joins its level (placed, loaded with its chunk, or its plugin reloaded), following BlockEntity.OnLoad. Override it to update what the block entity derives from its items, like a machine checking the patterns it was given.
Remarks
It hears every change to the items: players' clicks and shift-clicks in any menu showing the inventory, item access inserting and extracting (hoppers' and other plugins' too), the block entity's own changes under ContainerBlockEntity.LockInventory, and its loot table filling it. A change made to ContainerBlockEntity.Inventory without its lock is heard on the ticks of the block entity's chunk. Calling BlockEntity.SetChanged alone doesn't call it.
Called inside a call into the block entity's plugin, holding no lock, so it may call ContainerBlockEntity.LockInventory; a change it makes calls it again on the next tick. Not called once the block entity was removed or its plugin is unloading. A call that throws is logged.
Save(NbtCompound)
protected internal override void Save(NbtCompound tag);Remarks
Saves the inventory under its lock and ContainerBlockEntity.LootLock, so that the save never has a click half applied, nor a loot table being rolled into the slots (which would save neither the table nor its loot).
SlotLimit(int, ItemStack)
protected internal virtual int SlotLimit(int slot, ItemStack stack);The most of stack's item slot holds through item access or menu clicks: the item's stack size by default. The slot is the inventory's index, not a side's item access index. Called with the inventory's lock held, from any thread: read ContainerBlockEntity.Inventory, don't wait on anything. Return zero to refuse insertion; the item's own stack size remains an upper bound.
Fields
MaxSlots
public const int MaxSlots = 256;The most slots a container block entity holds: vanilla saves each slot's number as a byte.