mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 18:45:09 +00:00
Co-authored-by: RealMangoRage <[email protected]> Co-authored-by: Daniel Norris <[email protected]> Co-authored-by: Ven <[email protected]>
107 lines
3.9 KiB
Java
107 lines
3.9 KiB
Java
/*
|
|
* Copyright (c) Forge Development LLC and contributors
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
|
*/
|
|
|
|
package net.minecraftforge.fluids;
|
|
|
|
import net.minecraft.world.level.block.DispenserBlock;
|
|
import net.minecraft.core.dispenser.BlockSource;
|
|
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.block.entity.DispenserBlockEntity;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
|
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
/**
|
|
* Fills or drains a fluid container item using a Dispenser.
|
|
*/
|
|
public class DispenseFluidContainer extends DefaultDispenseItemBehavior {
|
|
private static final DispenseFluidContainer INSTANCE = new DispenseFluidContainer();
|
|
|
|
public static DispenseFluidContainer getInstance() {
|
|
return INSTANCE;
|
|
}
|
|
|
|
private DispenseFluidContainer() {}
|
|
|
|
private final DefaultDispenseItemBehavior dispenseBehavior = new DefaultDispenseItemBehavior();
|
|
|
|
@Override
|
|
@NotNull
|
|
public ItemStack execute(@NotNull BlockSource source, @NotNull ItemStack stack) {
|
|
if (FluidUtil.getFluidContained(stack).isPresent())
|
|
return dumpContainer(source, stack);
|
|
else
|
|
return fillContainer(source, stack);
|
|
}
|
|
|
|
/**
|
|
* Picks up fluid in front of a Dispenser and fills a container with it.
|
|
*/
|
|
@NotNull
|
|
private ItemStack fillContainer(@NotNull BlockSource source, @NotNull ItemStack stack) {
|
|
Level level = source.level();
|
|
Direction dispenserFacing = source.state().getValue(DispenserBlock.FACING);
|
|
BlockPos blockpos = source.pos().relative(dispenserFacing);
|
|
|
|
FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, level, blockpos, dispenserFacing.getOpposite());
|
|
ItemStack resultStack = actionResult.getResult();
|
|
|
|
if (!actionResult.isSuccess() || resultStack.isEmpty())
|
|
return super.execute(source, stack);
|
|
|
|
if (stack.getCount() == 1)
|
|
return resultStack;
|
|
|
|
source.blockEntity().insertItem(resultStack);
|
|
|
|
if (!resultStack.isEmpty())
|
|
this.dispenseBehavior.dispense(source, resultStack);
|
|
|
|
ItemStack stackCopy = stack.copy();
|
|
stackCopy.shrink(1);
|
|
return stackCopy;
|
|
}
|
|
|
|
/**
|
|
* Drains a filled container and places the fluid in front of the Dispenser.
|
|
*/
|
|
@NotNull
|
|
private ItemStack dumpContainer(BlockSource source, @NotNull ItemStack stack) {
|
|
ItemStack singleStack = stack.copy();
|
|
singleStack.setCount(1);
|
|
IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(singleStack).orElse(null);
|
|
if (fluidHandler == null)
|
|
return super.execute(source, stack);
|
|
|
|
FluidStack fluidStack = fluidHandler.drain(FluidType.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
|
|
Direction dispenserFacing = source.state().getValue(DispenserBlock.FACING);
|
|
BlockPos blockpos = source.pos().relative(dispenserFacing);
|
|
FluidActionResult result = FluidUtil.tryPlaceFluid(null, source.level(), InteractionHand.MAIN_HAND, blockpos, stack, fluidStack);
|
|
|
|
if (result.isSuccess()) {
|
|
ItemStack drainedStack = result.getResult();
|
|
|
|
if (drainedStack.getCount() == 1)
|
|
return drainedStack;
|
|
|
|
if (!drainedStack.isEmpty()) {
|
|
source.blockEntity().insertItem(drainedStack);
|
|
this.dispenseBehavior.dispense(source, drainedStack);
|
|
}
|
|
|
|
ItemStack stackCopy = drainedStack.copy();
|
|
stackCopy.shrink(1);
|
|
return stackCopy;
|
|
} else {
|
|
return this.dispenseBehavior.dispense(source, stack);
|
|
}
|
|
}
|
|
}
|