Skip to content

Commit

Permalink
Small cleanup to cauldron recipes
Browse files Browse the repository at this point in the history
Using new `splitStack` method, mostly to keep all stack splitting in one place. Will let me make that and `shrinkStack` both copy if we decide stacks should be effectively immutable
  • Loading branch information
KnightMiner committed Sep 17, 2020
1 parent a1b0b0c commit bd5145c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
Expand Up @@ -58,6 +58,19 @@ default boolean shrinkStack(int amount) {
return stack.isEmpty();
}

/**
* Splits off a stack with the given amount
* @param amount Amount to split into
* @return Split stack instance
*/
default ItemStack splitStack(int amount) {
ItemStack stack = getStack();
if (stack.isEmpty()) {
return ItemStack.EMPTY;
}
return stack.split(amount);
}

/**
* Sets the given stack if the stack is empty, gives otherwise
* @param stack New stack to give
Expand Down
Expand Up @@ -65,16 +65,8 @@ public boolean matches(ICauldronInventory inv, World worldIn) {

@Override
public void handleRecipe(IModifyableCauldronInventory inventory) {
// if the stack contains multiple items, update just one
ICauldronContents contents = inventory.getContents();
ItemStack stack = inventory.getStack();
if (stack.getCount() > 1) {
stack = stack.split(1);
inventory.giveStack(updateColor(contents, stack));
} else {
// if one, update the one
inventory.setStack(updateColor(contents, stack));
}
// update a single item from the stack
inventory.setOrGiveStack(updateColor(inventory.getContents(), inventory.splitStack(1)));

// remove a level of dye
inventory.addLevel(-THIRD);
Expand Down
Expand Up @@ -5,6 +5,7 @@
import knightminer.inspirations.library.recipe.cauldron.inventory.ICauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.inventory.IModifyableCauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.tags.FluidTags;
Expand All @@ -16,6 +17,7 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;

/**
* Recipe that fills a bucket from cauldron contents. Supports any generic fluid handler item.
Expand All @@ -26,6 +28,17 @@ public FillBucketCauldronRecipe(ResourceLocation id) {
this.id = id;
}

/**
* Attempts to fill a fluid handler with the given fluid
* @param handler Fluid handler
* @param fluid Fluid amount
* @param action Whether to simulate or execute the action
* @return True if successful, false if failed
*/
private static boolean tryFill(IFluidHandlerItem handler, Fluid fluid, FluidAction action) {
return handler.fill(new FluidStack(fluid, FluidAttributes.BUCKET_VOLUME), action) == FluidAttributes.BUCKET_VOLUME;
}

@Override
public boolean matches(ICauldronInventory inv, World worldIn) {
// must be full or no fill bucket
Expand All @@ -44,21 +57,17 @@ public boolean matches(ICauldronInventory inv, World worldIn) {
// must be a fluid
return inv.getContents()
.get(CauldronContentTypes.FLUID)
// must have a fluid handler, I really wish you could flatmap a lazy optional
.map(fluid -> FluidUtil.getFluidHandler(stack)
// handler must be fillable with the given fluid and must take 1000mb
.map(handler -> handler.fill(new FluidStack(fluid, FluidAttributes.BUCKET_VOLUME), FluidAction.SIMULATE) == FluidAttributes.BUCKET_VOLUME)
.orElse(false))
.orElse(false);
// handler must be fillable with the given fluid and must take 1000mb
.flatMap(fluid -> FluidUtil.getFluidHandler(stack).filter(handler -> tryFill(handler, fluid, FluidAction.SIMULATE)))
.isPresent();
}

@Override
public void handleRecipe(IModifyableCauldronInventory inv) {
// must have a fluid handler, I really wish you could flatmap a lazy optional
ItemStack stack = inv.getStack().split(1);
ItemStack stack = inv.splitStack(1);
inv.getContents().get(CauldronContentTypes.FLUID).ifPresent(fluid -> FluidUtil.getFluidHandler(stack).ifPresent(handler -> {
// if we successfully fill the handler, update the cauldron
if (handler.fill(new FluidStack(fluid, FluidAttributes.BUCKET_VOLUME), FluidAction.EXECUTE) == FluidAttributes.BUCKET_VOLUME) {
if (tryFill(handler, fluid, FluidAction.EXECUTE)) {
inv.setLevel(0);
inv.setOrGiveStack(handler.getContainer());

Expand Down
Expand Up @@ -35,7 +35,7 @@ public boolean matches(ICauldronInventory inv, World worldIn) {
@Override
public void handleRecipe(IModifyableCauldronInventory inv) {
// remove patterns
ItemStack stack = inv.getStack().split(1);
ItemStack stack = inv.splitStack(1);
BannerTileEntity.removeBannerData(stack);
inv.setOrGiveStack(stack);
// use one level of water
Expand Down

0 comments on commit bd5145c

Please sign in to comment.