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 Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final boolean
     
    private final net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level>
     
    private final int
     
    private WeakReference<net.minecraft.world.level.LevelAccessor>
     
    private static final org.apache.logging.log4j.Logger
     
    private final @Nullable net.minecraft.nbt.CompoundTag
     
    private final net.minecraft.core.BlockPos
     
    private final net.minecraft.world.level.block.state.BlockState
     
    private @Nullable String
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    BlockSnapshot(net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim, net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState state, @Nullable net.minecraft.nbt.CompoundTag nbt, int flags)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    create(net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim, net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos)
    Creates a new snapshot with the default block flags (and Block#UPDATE_CLIENTS.
    create(net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim, net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos, int flag)
    Creates a new snapshot of the data at the given position.
    boolean
     
    private static @Nullable net.minecraft.nbt.CompoundTag
    getBlockEntityTag(net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos)
    Checks for a block entity at a given position, and saves it to NBT with full metadata if it exists.
    net.minecraft.world.level.block.state.BlockState
    Returns the current (live) block state at the recorded position, not the snapshot's recorded state.
    net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level>
    Returns the recorded dimension key.
    int
     
    @Nullable net.minecraft.world.level.LevelAccessor
    Returns the stored level, attempting to resolve it from the current server if it has gone out of scope.
    net.minecraft.core.BlockPos
    Returns the recorded position.
    net.minecraft.world.level.block.state.BlockState
    Returns the snapshot's recorded block state.
    @Nullable net.minecraft.nbt.CompoundTag
    Returns the recorded block entity NBT data, if one was present.
    int
     
    @Nullable net.minecraft.world.level.block.entity.BlockEntity
    recreateBlockEntity(net.minecraft.core.HolderLookup.Provider provider)
    Recreates a block entity from the stored data (pos/state/NBT) of this block snapshot.
    boolean
    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.
    boolean
    restoreBlockEntity(net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos)
    Loads the stored BlockEntity data if one exists at the given position.
    boolean
    restoreToLocation(net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos, int flags)
    Restores this block snapshot to the target level and position with the specified flags.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEBUG

      private static final boolean DEBUG
    • LOGGER

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

      private final net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim
    • pos

      private final net.minecraft.core.BlockPos pos
    • flags

      private final int flags
    • state

      private final net.minecraft.world.level.block.state.BlockState state
    • nbt

      @Nullable private final @Nullable net.minecraft.nbt.CompoundTag nbt
    • level

      private WeakReference<net.minecraft.world.level.LevelAccessor> level
    • toString

      @Nullable private @Nullable String toString
  • Constructor Details

    • BlockSnapshot

      private BlockSnapshot(net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim, net.minecraft.world.level.LevelAccessor level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState state, @Nullable @Nullable net.minecraft.nbt.CompoundTag nbt, int flags)
  • Method Details

    • create

      public static BlockSnapshot create(net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> dim, net.minecraft.world.level.LevelAccessor level, net.minecraft.core.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

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

      public net.minecraft.resources.ResourceKey<net.minecraft.world.level.Level> getDimension()
      Returns the recorded dimension key.
      Returns:
      the recorded dimension key
    • getPos

      public net.minecraft.core.BlockPos getPos()
      Returns the recorded position.
      Returns:
      the recorded position
    • getFlags

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

      @Nullable public @Nullable net.minecraft.nbt.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

      public net.minecraft.world.level.block.state.BlockState getState()
      Returns the snapshot's recorded block state.
      Returns:
      the snapshot's recorded block state
    • getLevel

      @Nullable public @Nullable net.minecraft.world.level.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

      public net.minecraft.world.level.block.state.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

      @Nullable public @Nullable net.minecraft.world.level.block.entity.BlockEntity recreateBlockEntity(net.minecraft.core.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

      public boolean restoreToLocation(net.minecraft.world.level.LevelAccessor level, net.minecraft.core.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

      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

      public boolean restore()
    • restoreBlockEntity

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

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

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

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

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