ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/util/CapHelper.java
2024-03-04 13:38:11 -06:00

34 lines
1.3 KiB
Java

package de.ellpeck.actuallyadditions.mod.util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.items.IItemHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Optional;
public class CapHelper {
@Nonnull
public static Optional<IItemHandler> getItemHandler(@Nonnull Level level, @Nonnull BlockPos pos, @Nullable Direction side) {
BlockState blockState = level.getBlockState(pos);
if (blockState.hasBlockEntity()) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity != null) {
return Optional.ofNullable(level.getCapability(Capabilities.ItemHandler.BLOCK, pos, level.getBlockState(pos), blockEntity, side));
}
}
return Optional.empty();
}
@Nonnull
public static Optional<IItemHandler> getItemHandler(ItemStack stack) {
return Optional.ofNullable(stack.getCapability(Capabilities.ItemHandler.ITEM));
}
}