API reference Obsidian.API.BlockEntities
BlockEntity class
The data and behaviour the world keeps for one block besides its state, like vanilla's and NeoForge's block entities: a chest's items, a spawner's timers, or a plugin's machine. Subclass it and register the subclass for blocks with IPluginRegistry.RegisterBlockEntity.
public abstract class BlockEntity : IBlockEntity
In Obsidian.API.
Remarks
The server creates a block entity when one of its type's blocks is set, by any means (a player placing it, plugin code, world generation), and removes it when the position changes to a block its type doesn't cover. Loading its chunk loads it from the chunk's saved block_entities through BlockEntity.Load. A plugin's block entities in chunks being generated or loaded are kept as saved data until the level's next tick once the chunk is complete, which creates and loads them.
A plugin's block entity hears when it joins and leaves the level's live set, like NeoForge's block entities: BlockEntity.OnLoad once it's placed, its chunk loaded or its plugin reloaded, then BlockEntity.OnRemoved when its block is removed or BlockEntity.OnChunkUnloaded when its chunk unloads. Its plugin unloading tells it nothing.
The server saves what BlockEntity.Save writes as of the last time the block entity called BlockEntity.SetChanged, taken at the end of the level's tick: call it whenever its saved data changes, like vanilla's setChanged. The server calls BlockEntity.Load, BlockEntity.Save, BlockEntity.TickAsync, BlockEntity.GetUpdateData, BlockEntity.GetShapes, BlockEntity.CollectComponents, BlockEntity.OnLoad, BlockEntity.OnRemoved and BlockEntity.OnChunkUnloaded on the level's tick (or between two ticks), one at a time, besides creating a new one for a block set elsewhere, which BlockEntity.ApplyComponents initializes; code that changes it on the level's tick needs no locking. Chunks are saved and sent from copies of what those calls returned.
Constructors
protected BlockEntity();Properties
BlockPosition
public Vector BlockPosition { get; }The position of its block.
Id
public string Id { get; }The block entity type's identifier, like minecraft:chest.
IsRemoved
public bool IsRemoved { get; }Whether the block entity was removed from its level: its block changed to one without its type, its chunk unloaded, or its plugin unloaded. A removed block entity isn't ticked, saved or sent again.
Level
public ILevel Level { get; }The level the block entity is in, or null while it's in a chunk that isn't part of a level yet (being generated), or it was created but not placed. Always null on the client; use the GetClientWorld() extension in Obsidian.API.Client for the client's separate read-only world view.
Server
public IServer Server { get; }The server as the code running now sees it, like event arguments' Server: in the calls into the block entity's plugin, such as BlockEntity.TickAsync, the plugin's own IServer, which finds its recipes (IServer.FindRecipe) and keeps what's added through it the plugin's. Null while the block entity isn't in a level, and for the server's own block entities.
Methods
ApplyComponents(ItemStack)
protected internal virtual void ApplyComponents(ItemStack item);Reads back what BlockEntity.CollectComponents copied into the item a player places the block with, like vanilla's applyImplicitComponents: read the plugin's component values with ItemStack.TryGet and set the block entity's fields from them. The server saves the block entity afterwards.
Parameters
item- A copy of one of the stack the block is placed with.
Remarks
Called on the new block entity before anything else can reach it, as the block is placed, on any thread; BlockEntity.Level is null then. A call that throws is logged, and the block is placed without the data.
ClientTick()
protected internal virtual void ClientTick();Runs once a client tick (20 a second) on the Obsidian client, like vanilla's client-side block entity tickers: for what only players see, such as animations, particles or sounds. The server never calls it.
Remarks
A mod whose type is registered on the client too (with IPluginRegistry.RegisterBlockEntity in ConfigureRegistry) gets a live instance of its class for each of its block entities in the chunks the client has, created when the server sends it and loaded with BlockEntity.Load from what BlockEntity.GetUpdateData returned rather than saved data: again, on the same instance, on each update. It's dropped (BlockEntity.IsRemoved) when its block changes to one without its type, its chunk unloads, the player leaves the server, or the mod unloads.
It runs on the client's game thread while the player is in a world that isn't paused, one block entity at a time, in turn with its BlockEntity.Load calls. It may use the block entity's own fields, BlockEntity.Id, BlockEntity.BlockPosition, and client state the mod kept from ConfigureClient; BlockEntity.Level is null. The GetClientWorld() extension in Obsidian.API.Client reads neighbouring blocks, synced block entity data and tracked entities; its live instance and entity queries require this game thread. The view is also available during client Load, and detached when the instance is dropped. BlockEntity.SetChanged and BlockEntity.SendUpdate do nothing. Keep it short, as the game waits for it. A tick that throws is logged, and the block entity isn't ticked again until the server updates it.
CollectComponents(ItemStack)
protected internal virtual void CollectComponents(ItemStack item);Copies the block entity's data that its block's item keeps into item, like vanilla's collectImplicitComponents: set values of the plugin's own component types on it (see ItemStack.Set), which BlockEntity.ApplyComponents reads back when the item is placed again.
Parameters
item- A stack of the block's item, to set components on.
Remarks
Called as the block breaks (by a player or an explosion), before it's removed, on the level's tick or between two ticks. What it sets goes to the block's loot, like a vanilla block entity's components: a plugin block without a loot table drops its item with the plugin's component values it set, and a loot table copies them with minecraft:copy_components ("source": "block_entity"). Not called once the plugin started unloading.
Keep it to reading the block entity's own state: what a container holds still drops as its items. A call that throws is logged, and the item gets none of the block entity's data.
GetShapes()
protected internal virtual BlockShapes GetShapes();Overrides this block's collision and outline boxes from its data, or returns null for the state's shapes. Called when created or loaded, after a player's break hook, and at tick end after SetChanged or SendUpdate. Call both when a shape change must be saved and shown to clients; GetUpdateData includes the client's data.
GetUpdateData()
protected internal virtual NbtCompound GetUpdateData();The data clients get for the block entity (vanilla's getUpdateTag), with the chunk and from BlockEntity.SendUpdate; null, the default, sends none.
Remarks
A client mod's shape and rendering code reads it, and its client instance loads it (see BlockEntity.ClientTick), so send only what clients show.
Load(NbtCompound)
protected internal virtual void Load(NbtCompound tag);Reads the block entity's saved data, as BlockEntity.Save wrote it, when its chunk loads, or when a structure places it with data. Fields missing from tag mean their defaults. On the client it reads the update data instead (see BlockEntity.ClientTick).
Parameters
tag- The saved data, without the id and position.
OnChunkUnloaded()
protected internal virtual void OnChunkUnloaded();Called when the block entity's chunk unloads, like NeoForge's onChunkUnloaded: after the chunk's final save, between two of the level's ticks, holding no lock. The block entity is gone from then on (its block stays saved, and its chunk loading again creates a new one), so drop what refers to it.
Remarks
BlockEntity.IsRemoved is already true, a container's inventory no longer takes clicks, and BlockEntity.Level is still set for this call. Don't change the world from it: its chunk is saved. Not called when its block is removed (see BlockEntity.OnRemoved), its plugin unloads, or the server stops (which saves its chunk without unloading it). A call that throws is logged.
OnLoad()
protected internal virtual void OnLoad();Called once a plugin's block entity is live in its level, like NeoForge's onLoad: after it was placed, its chunk loaded, or its plugin loaded again, on the level's next tick, whether or not its chunk ticks, and before its first BlockEntity.TickAsync. Join what spans several block entities here, such as a network (see ILevel.AddTicker), and leave it in BlockEntity.OnRemoved and BlockEntity.OnChunkUnloaded.
Remarks
BlockEntity.Level is set. One removed or unloaded before that tick gets neither this nor BlockEntity.OnChunkUnloaded, but still gets BlockEntity.OnRemoved. A call that throws is logged.
OnRemoved()
protected internal virtual void OnRemoved();Called when the block entity's block is removed from the level, before what a container holds drops, like vanilla's preRemoveSideEffects: drop what it holds, say. Not called when its chunk unloads (see BlockEntity.OnChunkUnloaded), or its plugin does. Runs on the level's tick after the removal, which may come from elsewhere; BlockEntity.IsRemoved is already true, and a container's inventory no longer takes clicks.
Save(NbtCompound)
protected internal virtual void Save(NbtCompound tag);Writes the block entity's data into tag, which the server saves with the id and position.
Parameters
tag- An empty compound to write to.
SendUpdate()
public void SendUpdate();Sends what BlockEntity.GetUpdateData returns at the end of the level's tick to the players who have the block entity's chunk, like vanilla's block entity data packet; nothing when it returns null. Calls in one tick send one update.
SetChanged()
public void SetChanged();Tells the server the block entity's saved data changed, so that the next save has it. The server takes the change in at the end of the level's tick, or before a save that comes first, calling BlockEntity.Save.
TickAsync()
protected internal virtual ValueTask TickAsync();Runs once a tick while the block entity's chunk ticks (is within the simulation distance of a player), like vanilla's block entity tickers.
public override string ToString();