mirror of
https://github.com/architectury/architectury-api.git
synced 2026-03-28 11:57:01 -05:00
Simplify applicator
This commit is contained in:
@@ -27,6 +27,8 @@ import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface BlockTransferAccess<T, C> extends TransferAccess<T> {
|
||||
@Nullable
|
||||
T get(Level level, BlockPos pos, C context);
|
||||
@@ -39,12 +41,6 @@ public interface BlockTransferAccess<T, C> extends TransferAccess<T> {
|
||||
@FunctionalInterface
|
||||
interface BlockAccessProvider<T, C> {
|
||||
@Nullable
|
||||
BlockAccessApplicator<T, C> get(Level level, BlockPos pos, BlockState state, BlockEntity blockEntity);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface BlockAccessApplicator<T, C> {
|
||||
@Nullable
|
||||
T get(Level level, BlockPos pos, BlockState state, BlockEntity blockEntity, @Nullable C context);
|
||||
Function<C, T> get(Level level, BlockPos pos, BlockState state, BlockEntity blockEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ import net.minecraftforge.common.util.LazyOptional;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ForgeBlockTransferAccess<T, C> extends BlockTransferAccess<T, Direction> {
|
||||
Capability<C> getCapability();
|
||||
|
||||
@@ -39,14 +41,14 @@ public interface ForgeBlockTransferAccess<T, C> extends BlockTransferAccess<T, D
|
||||
CapabilitiesAttachListeners.add(event -> {
|
||||
if (event.getObject() instanceof BlockEntity) {
|
||||
BlockEntity blockEntity = (BlockEntity) event.getObject();
|
||||
BlockAccessApplicator<T, Direction> applicator = provider.get(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity);
|
||||
Function<Direction, T> applicator = provider.get(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity);
|
||||
if (applicator != null) {
|
||||
event.addCapability(id, new ICapabilityProvider() {
|
||||
@NotNull
|
||||
@Override
|
||||
public <S> LazyOptional<S> getCapability(@NotNull Capability<S> capability, @Nullable Direction arg) {
|
||||
if (capability == ForgeBlockTransferAccess.this.getCapability()) {
|
||||
T handler = applicator.get(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity, arg);
|
||||
T handler = applicator.apply(arg);
|
||||
|
||||
return LazyOptional.of(() -> from(handler)).cast();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user