Line 118:
Line 118:
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
−
public static Capability<IItemHandler> ITEM_HANDLER = CapabilityManager.get(new CapabilityToken<>(){});
+
public static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>(){});
</syntaxhighlight>
</syntaxhighlight>
−
The above code will let Forge know that the field <code>ITEM_HANDLER</code> should be analogous with the <code>IItemHandler</code> capability. Note that this does not mean the capability is accessible or registered. To check if it is, call <code>Capability#isRegistered</code>.
+
The above code will let Forge know that the field <code>ITEM_HANDLER_CAPABILITY</code> should be analogous with the <code>IItemHandler</code> capability. Note that this does not mean the capability is accessible or registered. To check if it is, call <code>Capability#isRegistered</code>.
−
This is, for obvious reasons, redundant, since all capabilities are available through
+
This is, for obvious reasons, redundant, since that capability is also available through
−
<code>ForgeCapabilities</code>.
+
<code>CapabilityItemHandler</code>.
=== Exposing a Capability ===
=== Exposing a Capability ===
Line 153:
Line 153:
// suppose the presence of a field 'inventory' of type 'IItemHandler'
// suppose the presence of a field 'inventory' of type 'IItemHandler'
−
private LazyOptional<IItemhandler> inventoryOptional = LazyOptional.empty();
+
private final LazyOptional<IItemhandler> inventoryOptional = LazyOptional.of(() -> this.inventory);
@Override
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction direction) {
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction direction) {
−
if (capability == ForgeCapabilities.ITEM_HANDLER
+
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY
&& (direction == null || direction == Direction.UP || direction == Direction.DOWN)) {
&& (direction == null || direction == Direction.UP || direction == Direction.DOWN)) {
return this.inventoryOptional.cast();
return this.inventoryOptional.cast();
}
}
return super.getCapability(capability, direction); // See note after snippet
return super.getCapability(capability, direction); // See note after snippet
−
}
−
−
@Override
−
public void invalidateCaps()
−
{
−
this.inventoryOptional = LazyOptional.of(() -> this.inventory);
}
}
Line 228:
Line 222:
@Override
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction direction) {
public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction direction) {
−
if (cap == ForgeCapabilities.ENERGY) {
+
if (cap == CapabilityEnergy.ENERGY) {
return optionalStorage.cast();
return optionalStorage.cast();
}
}
Line 262:
Line 256:
EnergyStorage backend = new EnergyStorage(((EnergyBasedBlockEntity) event.getObject()).capacity);
EnergyStorage backend = new EnergyStorage(((EnergyBasedBlockEntity) event.getObject()).capacity);
LazyOptional<IEnergyStorage> optionalStorage = LazyOptional.of(() -> backend);
LazyOptional<IEnergyStorage> optionalStorage = LazyOptional.of(() -> backend);
−
Capability<IEnergyStorage> capability = ForgeCapabilities.ENERGY;
+
Capability<IEnergyStorage> capability = CapabilityEnergy.ENERGY;
ICapabilityProvider provider = new ICapabilitySerializable<IntTag>() {
ICapabilityProvider provider = new ICapabilitySerializable<IntTag>() {
Line 322:
Line 316:
if (targetCapability == null) {
if (targetCapability == null) {
ICapabilityProvider provider = level.getBlockEntity(pos.relative(direction));
ICapabilityProvider provider = level.getBlockEntity(pos.relative(direction));
−
targetCapability = provider.getCapability(ForgeCapabilities.ENERGY, direction.getOpposite());
+
targetCapability = provider.getCapability(CapabilityEnergy.ENERGY, direction.getOpposite());
cache.put(direction, targetCapability);
cache.put(direction, targetCapability);
targetCapability.addListener(self -> cache.put(direction, null));
targetCapability.addListener(self -> cache.put(direction, null));