Check for incorrect FluidBucketWrapper usage

Items that use FluidBucketWrapper incorrectly:
- Will not get filling recipes
- Cannot be filled with the spout
This commit is contained in:
PepperBell 2021-05-07 13:33:18 -07:00
parent da6f2652e7
commit e05db3cd76
2 changed files with 34 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import com.simibubi.create.AllBlocks;
import com.simibubi.create.Create; import com.simibubi.create.Create;
import com.simibubi.create.compat.jei.category.animations.AnimatedSpout; import com.simibubi.create.compat.jei.category.animations.AnimatedSpout;
import com.simibubi.create.content.contraptions.fluids.actors.FillingRecipe; import com.simibubi.create.content.contraptions.fluids.actors.FillingRecipe;
import com.simibubi.create.content.contraptions.fluids.actors.GenericItemFilling;
import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler; import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler;
import com.simibubi.create.content.contraptions.processing.ProcessingRecipeBuilder; import com.simibubi.create.content.contraptions.processing.ProcessingRecipeBuilder;
import com.simibubi.create.foundation.fluid.FluidIngredient; import com.simibubi.create.foundation.fluid.FluidIngredient;
@ -71,6 +72,8 @@ public class SpoutCategory extends CreateRecipeCategory<FillingRecipe> {
ItemStack copy = stack.copy(); ItemStack copy = stack.copy();
copy.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY) copy.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY)
.ifPresent(fhi -> { .ifPresent(fhi -> {
if (!GenericItemFilling.isFluidHandlerValid(copy, fhi))
return;
FluidStack fluidCopy = fluidStack.copy(); FluidStack fluidCopy = fluidStack.copy();
fluidCopy.setAmount(1000); fluidCopy.setAmount(1000);
fhi.fill(fluidCopy, FluidAction.EXECUTE); fhi.fill(fluidCopy, FluidAction.EXECUTE);

View file

@ -5,9 +5,11 @@ import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler
import com.simibubi.create.foundation.fluid.FluidHelper; import com.simibubi.create.foundation.fluid.FluidHelper;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.item.MilkBucketItem;
import net.minecraft.potion.PotionUtils; import net.minecraft.potion.PotionUtils;
import net.minecraft.potion.Potions; import net.minecraft.potion.Potions;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -20,6 +22,33 @@ import net.minecraftforge.fluids.capability.wrappers.FluidBucketWrapper;
public class GenericItemFilling { public class GenericItemFilling {
/**
* Checks if an ItemStack's IFluidHandlerItem is valid. Ideally, this check would
* not be necessary. Unfortunately, some mods that copy the functionality of the
* MilkBucketItem copy the FluidBucketWrapper capability that is patched in by
* Forge without looking into what it actually does. In all cases this is
* incorrect because having a non-bucket item turn into a bucket item does not
* make sense.
*
* <p>This check is only necessary for filling since a FluidBucketWrapper will be
* empty if it is initialized with a non-bucket item.
*
* @param stack The ItemStack.
* @param fluidHandler The IFluidHandlerItem instance retrieved from the ItemStack.
* @return If the IFluidHandlerItem is valid for the passed ItemStack.
*/
public static boolean isFluidHandlerValid(ItemStack stack, IFluidHandlerItem fluidHandler) {
// Not instanceof in case a correct subclass is made
if (fluidHandler.getClass() == FluidBucketWrapper.class) {
Item item = stack.getItem();
// Forge does not patch the FluidBucketWrapper onto subclasses of BucketItem
if (item.getClass() != BucketItem.class && !(item instanceof MilkBucketItem)) {
return false;
}
}
return true;
}
public static boolean canItemBeFilled(World world, ItemStack stack) { public static boolean canItemBeFilled(World world, ItemStack stack) {
if (stack.getItem() == Items.GLASS_BOTTLE) if (stack.getItem() == Items.GLASS_BOTTLE)
return true; return true;
@ -31,6 +60,8 @@ public class GenericItemFilling {
IFluidHandlerItem tank = capability.orElse(null); IFluidHandlerItem tank = capability.orElse(null);
if (tank == null) if (tank == null)
return false; return false;
if (!isFluidHandlerValid(stack, tank))
return false;
for (int i = 0; i < tank.getTanks(); i++) { for (int i = 0; i < tank.getTanks(); i++) {
if (tank.getFluidInTank(i) if (tank.getFluidInTank(i)
.getAmount() < tank.getTankCapacity(i)) .getAmount() < tank.getTankCapacity(i))