|
| 1 | +package juuxel.adorn.platform.forge; |
| 2 | + |
| 3 | +import juuxel.adorn.block.AdornBlockEntities; |
| 4 | +import juuxel.adorn.block.variant.BlockKind; |
| 5 | +import juuxel.adorn.block.variant.BlockVariantSets; |
| 6 | +import juuxel.adorn.platform.forge.block.entity.BlockEntityWithFluidTank; |
| 7 | +import net.minecraft.block.entity.BlockEntity; |
| 8 | +import net.minecraft.inventory.Inventory; |
| 9 | +import net.minecraft.inventory.SidedInventory; |
| 10 | +import net.minecraft.util.math.Direction; |
| 11 | +import net.neoforged.neoforge.capabilities.Capabilities; |
| 12 | +import net.neoforged.neoforge.capabilities.IBlockCapabilityProvider; |
| 13 | +import net.neoforged.neoforge.capabilities.ICapabilityProvider; |
| 14 | +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; |
| 15 | +import net.neoforged.neoforge.fluids.capability.IFluidHandler; |
| 16 | +import net.neoforged.neoforge.items.IItemHandler; |
| 17 | +import net.neoforged.neoforge.items.wrapper.InvWrapper; |
| 18 | +import net.neoforged.neoforge.items.wrapper.SidedInvWrapper; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +public final class AdornCapabilities { |
| 22 | + private static final IBlockCapabilityProvider<IItemHandler, @Nullable Direction> INVENTORY_WRAPPER_FOR_BLOCK = |
| 23 | + (world, pos, state, blockEntity, side) -> blockEntity instanceof Inventory inventory ? getInventoryWrapper(inventory, side) : null; |
| 24 | + private static final ICapabilityProvider<BlockEntity, @Nullable Direction, IItemHandler> INVENTORY_WRAPPER_FOR_BLOCK_ENTITY = |
| 25 | + (blockEntity, side) -> blockEntity instanceof Inventory inventory ? getInventoryWrapper(inventory, side) : null; |
| 26 | + private static final IBlockCapabilityProvider<IFluidHandler, @Nullable Direction> FLUID_TANK_FOR_BLOCK = |
| 27 | + (world, pos, state, blockEntity, side) -> blockEntity instanceof BlockEntityWithFluidTank withTank ? withTank.getTank() : null; |
| 28 | + private static final ICapabilityProvider<BlockEntity, @Nullable Direction, IFluidHandler> FLUID_TANK_FOR_BLOCK_ENTITY = |
| 29 | + (blockEntity, side) -> blockEntity instanceof BlockEntityWithFluidTank withTank ? withTank.getTank() : null; |
| 30 | + |
| 31 | + public static void register(RegisterCapabilitiesEvent event) { |
| 32 | + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, AdornBlockEntities.BREWER.get(), INVENTORY_WRAPPER_FOR_BLOCK_ENTITY); |
| 33 | + |
| 34 | + var containerBlockKinds = new BlockKind[] { |
| 35 | + BlockKind.DRAWER, |
| 36 | + BlockKind.KITCHEN_CUPBOARD, |
| 37 | + BlockKind.SHELF, |
| 38 | + }; |
| 39 | + |
| 40 | + for (var kind : containerBlockKinds) { |
| 41 | + for (var block : BlockVariantSets.get(kind)) { |
| 42 | + event.registerBlock(Capabilities.ItemHandler.BLOCK, INVENTORY_WRAPPER_FOR_BLOCK, block.get()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + event.registerBlockEntity(Capabilities.FluidHandler.BLOCK, AdornBlockEntities.BREWER.get(), FLUID_TANK_FOR_BLOCK_ENTITY); |
| 47 | + |
| 48 | + for (var kitchenSink : BlockVariantSets.get(BlockKind.KITCHEN_SINK)) { |
| 49 | + event.registerBlock(Capabilities.FluidHandler.BLOCK, FLUID_TANK_FOR_BLOCK, kitchenSink.get()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static IItemHandler getInventoryWrapper(Inventory inventory, @Nullable Direction side) { |
| 54 | + return side != null && inventory instanceof SidedInventory sided ? new SidedInvWrapper(sided, side) : new InvWrapper(inventory); |
| 55 | + } |
| 56 | +} |
0 commit comments