Class BlockSnapshot

java.lang.Object
net.neoforged.neoforge.common.util.BlockSnapshot

public class BlockSnapshot extends Object
Represents a captured snapshot of a block, including the level, position, state, BE data, and setBlock flags.

Used to record the prior state and unwind changes if the change was denied, such as during BlockEvent.BreakEvent.

  • Field Details Link icon

    • DEBUG Link icon

      private static final boolean DEBUG
    • LOGGER Link icon

      private static final org.apache.logging.log4j.Logger LOGGER
    • dim Link icon

      private final ResourceKey<Level> dim
    • pos Link icon

      private final BlockPos pos
    • flags Link icon

      private final int flags
    • state Link icon

      private final BlockState state
    • nbt Link icon

      @Nullable private final @Nullable CompoundTag nbt
    • level Link icon

      private WeakReference<LevelAccessor> level
    • toString Link icon

      @Nullable private @Nullable String toString
  • Constructor Details Link icon

  • Method Details Link icon

    • create Link icon

      public static BlockSnapshot create(ResourceKey<Level> dim, LevelAccessor level, BlockPos pos, int flag)
      Creates a new snapshot of the data at the given position.
      Parameters:
      dim - The dimension of the changed block
      level - The level of the changed block
      pos - The position of the changed block
      flag - The Level.setBlock(BlockPos, BlockState, int) flags that the block was changed with.
      Returns:
      A captured block snapshot, containing the state and BE data from the given position.
    • create Link icon

      public static BlockSnapshot create(ResourceKey<Level> dim, LevelAccessor level, BlockPos pos)
      Creates a new snapshot with the default block flags (and Block#UPDATE_CLIENTS.
      See Also:
    • getDimension Link icon

      public ResourceKey<Level> getDimension()
      Returns the recorded dimension key.
      Returns:
      the recorded dimension key
    • getPos Link icon

      public BlockPos getPos()
      Returns the recorded position.
      Returns:
      the recorded position
    • getFlags Link icon

      public int getFlags()
      Returns:
      the recorded Level.setBlock(BlockPos, BlockState, int) flags
    • getTag Link icon

      @Nullable public @Nullable CompoundTag getTag()
      Returns the recorded block entity NBT data, if one was present.
      Returns:
      the recorded block entity NBT data, if one was present
    • getState Link icon

      public BlockState getState()
      Returns the snapshot's recorded block state.
      Returns:
      the snapshot's recorded block state
    • getLevel Link icon

      @Nullable public @Nullable LevelAccessor getLevel()
      Returns the stored level, attempting to resolve it from the current server if it has gone out of scope.
      Returns:
      the stored level, attempting to resolve it from the current server if it has gone out of scope
    • getCurrentState Link icon

      public BlockState getCurrentState()
      Returns the current (live) block state at the recorded position, not the snapshot's recorded state.
      Returns:
      the current (live) block state at the recorded position, not the snapshot's recorded state
    • recreateBlockEntity Link icon

      @Nullable public @Nullable BlockEntity recreateBlockEntity(HolderLookup.Provider provider)
      Recreates a block entity from the stored data (pos/state/NBT) of this block snapshot.
      Returns:
      The newly created block entity, or null if no NBT data was present, or it was invalid.
    • restoreToLocation Link icon

      public boolean restoreToLocation(LevelAccessor level, BlockPos pos, int flags)
      Restores this block snapshot to the target level and position with the specified flags.
      Returns:
      true if the block was successfully updated, false otherwise.
    • restore Link icon

      public boolean restore(int flags)
      Calls restoreToLocation(net.minecraft.world.level.LevelAccessor, net.minecraft.core.BlockPos, int) with the stored level, position, but custom block flags.
    • restore Link icon

      public boolean restore()
    • restoreBlockEntity Link icon

      public boolean restoreBlockEntity(LevelAccessor level, BlockPos pos)
      Loads the stored BlockEntity data if one exists at the given position.
      Returns:
      true if any data was loaded
    • equals Link icon

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode Link icon

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • getBlockEntityTag Link icon

      @Nullable private static @Nullable CompoundTag getBlockEntityTag(LevelAccessor level, BlockPos pos)
      Checks for a block entity at a given position, and saves it to NBT with full metadata if it exists.