类 BlockCapability<T,C extends @Nullable Object>
- 类型参数:
T
- type of queried objectsC
- type of the additional context
BlockCapability
gives flexible access to objects of type T
located in the world.
Querying a block capability
To get an object of type T
, use ILevelExtension.getCapability(BlockCapability, BlockPos, Object)
.
For example, to query an item handler in the world, from a specific side:
Level level = ...;
BlockPos pos = ...;
Direction side = ...;
IItemHandler maybeHandler = level.getCapability(Capabilities.ItemHandler.BLOCK, pos, side);
if (maybeHandler != null) {
// Use maybeHandler
}
For repeated queries at a specific position, use BlockCapabilityCache
to improve performance.
Providing a capability for a block entity
To provide objects of type T
, register providers to RegisterCapabilitiesEvent
. For example:
modBus.addListener(RegisterCapabilitiesEvent.class, event -> {
event.registerBlockEntity(
Capabilities.ItemHandler.BLOCK, // capability to register for
MY_BLOCK_ENTITY_TYPE,
(myBlockEntity, side) -> <return the IItemHandler for myBlockEntity and side>);
});
If a previously returned capability is not valid anymore, or if a new capability is available,
ILevelExtension.invalidateCapabilities(BlockPos)
MUST be called to notify the caches (see below).
Providing a capability for a plain block
For blocks without a block entity, we useregisterBlock
instead:
modBus.addListener(RegisterCapabilitiesEvent.class, event -> {
event.registerBlock(
Capabilities.ItemHandler.BLOCK, // capability to register for
(level, pos, state, be, side) -> <return the IItemHandler>,
// blocks to register for
MY_ITEM_HANDLER_BLOCK, MY_OTHER_ITEM_HANDLER_BLOCK);
});
Plain blocks must invalidate their capabilities whenever they change, including on placement and removal. For example:
public class MyBlock extends Block {
@Override
public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
// Invalidate capabilities on block placement or block state change
level.invalidateCapabilities(pos);
}
@Override
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
super.onRemove(state, level, pos, newState, movedByPiston);
// Invalidate capabilities on block removal or block state change
level.invalidateCapabilities(pos);
}
}
-
字段概要
字段 -
构造器概要
构造器限定符构造器说明private
BlockCapability
(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) -
方法概要
修饰符和类型方法说明static <T,
C extends @Nullable Object>
BlockCapability<T, C> create
(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) Creates a new block capability, or gets it if it already exists.static <T> BlockCapability
<T, @Nullable Direction> createSided
(ResourceLocation name, Class<T> typeClass) Creates a new block capability with nullableDirection
context, or gets it if it already exists.static <T> BlockCapability
<T, @Nullable Void> createVoid
(ResourceLocation name, Class<T> typeClass) Creates a new block capability withVoid
context, or gets it if it already exists.static List
<BlockCapability<?, ?>> getAll()
返回 a new immutable copy of all the currently known block capabilities。static List
<BlockCapability<?, ?>> 返回 a new immutable copy of all the currently known proxyable block capabilities。getCapability
(Level level, BlockPos pos, @Nullable BlockState state, @Nullable BlockEntity blockEntity, C context) boolean
Returns whether this capability is proxyable.(专用程序包) void
setProxyable
(boolean proxyable) 从类继承的方法 net.neoforged.neoforge.capabilities.BaseCapability
contextClass, name, typeClass
-
字段详细资料
-
registry
-
providers
-
proxyable
-
-
构造器详细资料
-
BlockCapability
-
-
方法详细资料
-
create
public static <T,C extends @Nullable Object> BlockCapability<T,C> create(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) Creates a new block capability, or gets it if it already exists.- 参数:
name
- name of the capabilitytypeClass
- type of the queried APIcontextClass
- type of the additional context
-
createVoid
public static <T> BlockCapability<T,@Nullable Void> createVoid(ResourceLocation name, Class<T> typeClass) Creates a new block capability withVoid
context, or gets it if it already exists. This should be used for capabilities that do not require any additional context.- 另请参阅:
-
createSided
public static <T> BlockCapability<T,@Nullable Direction> createSided(ResourceLocation name, Class<T> typeClass) Creates a new block capability with nullableDirection
context, or gets it if it already exists. -
getAll
返回 a new immutable copy of all the currently known block capabilities。Mods that want to forward "all" capability requests should likely use
getAllProxyable()
instead.- 返回:
- a new immutable copy of all the currently known block capabilities
-
getAllProxyable
返回 a new immutable copy of all the currently known proxyable block capabilities。This method should typically only be used in the
EventPriority.LOW
orEventPriority.LOWEST
phase ofRegisterCapabilitiesEvent
, or later, to ensure that all proxyable capabilities have beenmarked as such
.- 返回:
- a new immutable copy of all the currently known proxyable block capabilities
- 另请参阅:
-
isProxyable
public boolean isProxyable()Returns whether this capability is proxyable. This information is metadata: it does not change how the capability works internally, but it tells mods whether they should or should not register proxying capability providers.If the capability is proxyable, requests for this capability are safe to forward unilaterally to other blocks.
If the capability is not proxyable, requests for this capability should not be forwarded to other blocks without further information. In that case, refer to documentation of the capability to understand under which circumstances it is safe to forward, if at all. For this reason, mods that forward "all" capabilities should not forward non-proxyable capabilities.
Block capabilities are not proxyable by default. Any call to
RegisterCapabilitiesEvent.setProxyable(BlockCapability)
will mark the capability as proxyable. Any call toRegisterCapabilitiesEvent.setNonProxyable(BlockCapability)
will mark the capability as non-proxyable, and prevent it from being marked as proxyable. -
setProxyable
void setProxyable(boolean proxyable) -
getCapability
@Internal @Nullable public T getCapability(Level level, BlockPos pos, @Nullable @Nullable BlockState state, @Nullable @Nullable BlockEntity blockEntity, C context)
-