Search in sources :

Example 1 with FluidStackSnapshot

use of org.spongepowered.api.extra.fluid.FluidStackSnapshot in project SpongeForge by SpongePowered.

the class ForgeFluidTankDataProcessor method set.

@Override
protected boolean set(TileEntity dataHolder, Map<Direction, List<FluidStackSnapshot>> value) {
    for (EnumFacing enumFacing : EnumFacing.values()) {
        final Direction direction = DirectionFacingProvider.getInstance().getKey(enumFacing).orElseThrow(() -> new IllegalArgumentException("Invalid EnumFacing: " + enumFacing));
        if (dataHolder.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, enumFacing)) {
            final IFluidHandler handler = dataHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, enumFacing);
            final IFluidTankProperties[] oldInfo = handler.getTankProperties();
            if (oldInfo != null) {
                for (IFluidTankProperties old : oldInfo) {
                    if (old != null && old.getContents() != null) {
                        handler.drain(old.getContents(), true);
                    }
                }
            }
            for (FluidStackSnapshot snapshot : value.get(direction)) {
                handler.fill(((net.minecraftforge.fluids.FluidStack) snapshot.createStack()), true);
            }
        }
    }
    return true;
}
Also used : FluidStackSnapshot(org.spongepowered.api.extra.fluid.FluidStackSnapshot) IFluidTankProperties(net.minecraftforge.fluids.capability.IFluidTankProperties) FluidStack(org.spongepowered.api.extra.fluid.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) Direction(org.spongepowered.api.util.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 2 with FluidStackSnapshot

use of org.spongepowered.api.extra.fluid.FluidStackSnapshot in project SpongeForge by SpongePowered.

the class ForgeFluidTankDataProcessor method getVal.

@Override
protected Optional<Map<Direction, List<FluidStackSnapshot>>> getVal(TileEntity dataHolder) {
    Map<Direction, List<FluidStackSnapshot>> map = new HashMap<>();
    for (EnumFacing facing : EnumFacing.values()) {
        final Direction direction = DirectionFacingProvider.getInstance().getKey(facing).get();
        if (dataHolder.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing)) {
            final IFluidHandler handler = dataHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing);
            final IFluidTankProperties[] tankProperties = handler.getTankProperties();
            if (tankProperties != null) {
                ImmutableList.Builder<FluidStackSnapshot> snapshotBuilder = ImmutableList.builder();
                for (IFluidTankProperties info : tankProperties) {
                    if (info != null && info.getContents() != null) {
                        final FluidStack drained = (FluidStack) handler.drain(info.getContents(), false);
                        if (drained != null) {
                            snapshotBuilder.add(drained.createSnapshot());
                        }
                    }
                }
                map.put(direction, snapshotBuilder.build());
            }
        }
    }
    return Optional.of(map);
}
Also used : FluidStackSnapshot(org.spongepowered.api.extra.fluid.FluidStackSnapshot) HashMap(java.util.HashMap) IFluidTankProperties(net.minecraftforge.fluids.capability.IFluidTankProperties) ImmutableList(com.google.common.collect.ImmutableList) FluidStack(org.spongepowered.api.extra.fluid.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Direction(org.spongepowered.api.util.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

EnumFacing (net.minecraft.util.EnumFacing)2 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)2 IFluidTankProperties (net.minecraftforge.fluids.capability.IFluidTankProperties)2 FluidStack (org.spongepowered.api.extra.fluid.FluidStack)2 FluidStackSnapshot (org.spongepowered.api.extra.fluid.FluidStackSnapshot)2 Direction (org.spongepowered.api.util.Direction)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashMap (java.util.HashMap)1 List (java.util.List)1