Class TransientBlockTracker

java.lang.Object
net.minecraft.world.level.saveddata.SavedData
johnsmith.enchantmentcore.util.TransientBlockTracker

public class TransientBlockTracker extends net.minecraft.world.level.saveddata.SavedData
Manages the temporary modification, decay animation, and restoration of block states within a ServerLevel.

To survive chunk unloads and reloads without losing restoration state or desynchronizing from frozen SpellFieldAnchorEntity instances in unloaded chunks, tracked blocks are partitioned into chunk-keyed buckets (ChunkPos.asLong(BlockPos)) and evaluated using relative tick counters (remainingStableTicks and remainingDecayTicks).

Progression and block restoration are strictly gated behind ServerLevel.shouldTickBlocksAt(long), ensuring that blocks in unloaded or border chunks pause their timers until the chunk re-enters an active block-ticking state.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.mojang.serialization.Codec<TransientBlockTracker>
    Codec responsible for serializing and deserializing all chunk-partitioned transient block records.
    static final net.minecraft.world.level.saveddata.SavedDataType<TransientBlockTracker>
    Persistent data type definition registered with the level's data storage.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addBlock(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState originalState, @Nullable net.minecraft.nbt.CompoundTag blockEntityData, int stableTicks, int decayTicks)
    Registers a block position for temporary modification using relative tick durations.
    void
    addBlock(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState originalState, @Nullable net.minecraft.nbt.CompoundTag blockEntityData, long decayStartTick, long expiryTick)
    Registers a block position for temporary modification using absolute game-time timestamps.
    get(net.minecraft.server.level.ServerLevel level)
    Retrieves the active TransientBlockTracker instance for the specified server level, instantiating and registering a new tracker if one does not already exist.
    @Nullable net.minecraft.world.level.block.state.BlockState
    getOriginalState(net.minecraft.core.BlockPos pos)
    Retrieves the original pre-modification BlockState recorded for the specified coordinate.
    boolean
    isTracked(net.minecraft.core.BlockPos pos)
    Checks whether the specified block coordinate is currently undergoing a temporary modification managed by this tracker.
    void
    tick(net.minecraft.server.level.ServerLevel level)
    Executes the per-tick evaluation loop for all tracked transient blocks in the given level.

    Methods inherited from class net.minecraft.world.level.saveddata.SavedData

    isDirty, setDirty, setDirty

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CODEC

      public static final com.mojang.serialization.Codec<TransientBlockTracker> CODEC
      Codec responsible for serializing and deserializing all chunk-partitioned transient block records.
    • TYPE

      public static final net.minecraft.world.level.saveddata.SavedDataType<TransientBlockTracker> TYPE
      Persistent data type definition registered with the level's data storage.
  • Constructor Details

    • TransientBlockTracker

      public TransientBlockTracker()
  • Method Details

    • get

      public static TransientBlockTracker get(net.minecraft.server.level.ServerLevel level)
      Retrieves the active TransientBlockTracker instance for the specified server level, instantiating and registering a new tracker if one does not already exist.
      Parameters:
      level - The target server level.
      Returns:
      The bound TransientBlockTracker instance.
    • isTracked

      public boolean isTracked(net.minecraft.core.BlockPos pos)
      Checks whether the specified block coordinate is currently undergoing a temporary modification managed by this tracker.
      Parameters:
      pos - The block coordinate to query.
      Returns:
      true if the position is actively tracked and awaiting restoration, false otherwise.
    • getOriginalState

      @Nullable public @Nullable net.minecraft.world.level.block.state.BlockState getOriginalState(net.minecraft.core.BlockPos pos)
      Retrieves the original pre-modification BlockState recorded for the specified coordinate.

      Used by temporary block effects during repeated spell field ticks or post-chunk-reload ticks to prevent overwriting the true original block state with the temporary block's own state.

      Parameters:
      pos - The block coordinate to query.
      Returns:
      The original BlockState scheduled for restoration, or null if the position is not tracked.
    • addBlock

      public void addBlock(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState originalState, @Nullable @Nullable net.minecraft.nbt.CompoundTag blockEntityData, long decayStartTick, long expiryTick)
      Registers a block position for temporary modification using absolute game-time timestamps.

      Converts the provided timestamps into relative tick durations and delegates to addBlock(ServerLevel, BlockPos, BlockState, CompoundTag, int, int).

      Parameters:
      level - The server level containing the block.
      pos - The coordinate position of the modified block.
      originalState - The original block state to restore upon expiration.
      blockEntityData - The serialized NBT metadata of the original block entity, or null if none existed.
      decayStartTick - The absolute game time tick when the block-breaking decay animation should begin.
      expiryTick - The absolute game time tick when the original block state should be restored.
    • addBlock

      public void addBlock(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState originalState, @Nullable @Nullable net.minecraft.nbt.CompoundTag blockEntityData, int stableTicks, int decayTicks)
      Registers a block position for temporary modification using relative tick durations.

      If the position is already tracked, its original BlockState and BlockEntity NBT are preserved while its remaining lifetime and decay timers are refreshed to the maximum of the existing and incoming durations.

      Parameters:
      level - The server level containing the block.
      pos - The coordinate position of the modified block.
      originalState - The original block state to restore upon expiration.
      blockEntityData - The serialized NBT metadata of the original block entity, or null if none existed.
      stableTicks - The number of active chunk ticks before decay animation begins.
      decayTicks - The number of active chunk ticks over which the decay animation plays before restoration.
    • tick

      public void tick(net.minecraft.server.level.ServerLevel level)
      Executes the per-tick evaluation loop for all tracked transient blocks in the given level.

      Chunks that are currently unloaded or outside the active block-ticking simulation distance (ServerLevel.shouldTickBlocksAt(long) returns false) are skipped without decrementing their timers or discarding their restoration entries.

      Parameters:
      level - The executing server level.