Skip to content

Fix all recipe types to respect crafting remaining items #8556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: mc1.20.1/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity en
.placeItemBackInInventory(polished);
}
}
if (!toPolish.isEmpty() && toPolish.hasCraftingRemainingItem()) {
if (player instanceof FakePlayer) {
player.drop(toPolish.getCraftingRemainingItem(), false, false);
} else {
player.getInventory()
.placeItemBackInInventory(toPolish.getCraftingRemainingItem());
}
}
tag.remove("Polishing");
stack.hurtAndBreak(1, entityLiving, p -> p.broadcastBreakEvent(p.getUsedItemHand()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void tick() {

float processingSpeed =
Mth.clamp((speed) / (!inventory.appliedRecipe ? (float) Math.log(inventory.getStackInSlot(0)
.getCount())/(float) Math.log(2) : 1), .25f, 20);
.getCount()) / (float) Math.log(2) : 1), .25f, 20);
inventory.remainingTime -= processingSpeed;
spawnParticles(inventory.getStackInSlot(0));

Expand Down Expand Up @@ -296,15 +296,18 @@ private void applyRecipe() {

List<ItemStack> list = new ArrayList<>();
if (recipe.isPresent()) {
int rolls = inventory.getStackInSlot(0)
.getCount();
ItemStack input = inventory.getStackInSlot(0);
int rolls = input.getCount();
inventory.clear();
for (int roll = 0; roll < rolls; roll++) {
List<ItemStack> rolledResults = recipe.get()
.rollResults();
for (ItemStack stack : rolledResults) {
ItemHelper.addToList(stack, list);
}
if (input.hasCraftingRemainingItem()) {
ItemHelper.addToList(input.getCraftingRemainingItem(), list);
}
}
for (int slot = 0; slot < list.size() && slot + 1 < inventory.getSlots(); slot++)
inventory.setStackInSlot(slot + 1, list.get(slot));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -97,7 +98,7 @@ public static void activate(TransportedItemStack transported, TransportedItemSta
DeployerBlockEntity blockEntity, Recipe<?> recipe) {

List<TransportedItemStack> collect =
RecipeApplier.applyRecipeOn(blockEntity.getLevel(), ItemHandlerHelper.copyStackWithSize(transported.stack, 1), recipe)
RecipeApplier.applyRecipeOn(blockEntity.getLevel(), ItemHandlerHelper.copyStackWithSize(transported.stack, 1), recipe, true)
.stream()
.map(stack -> {
TransportedItemStack copy = transported.copy();
Expand Down Expand Up @@ -135,11 +136,23 @@ public static void activate(TransportedItemStack transported, TransportedItemSta
recipe instanceof ItemApplicationRecipe && ((ItemApplicationRecipe) recipe).shouldKeepHeldItem();

if (!keepHeld) {
if (heldItem.isDamageableItem())
if (heldItem.isDamageableItem()) {
heldItem.hurtAndBreak(1, blockEntity.player,
s -> s.broadcastBreakEvent(InteractionHand.MAIN_HAND));
else
} else {
Player player = blockEntity.player;
ItemStack leftover = heldItem.hasCraftingRemainingItem() ? heldItem.getCraftingRemainingItem() : ItemStack.EMPTY;

heldItem.shrink(1);

if (heldItem.isEmpty()) {
player.setItemInHand(InteractionHand.MAIN_HAND, leftover);
} else {
if (!player.getInventory().add(leftover)) {
player.drop(leftover, false);
}
}
}
}

if (resultItem != null && !resultItem.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,23 @@ public static void manualApplicationRecipesApplyInWorld(PlayerInteractEvent.Righ
boolean keepHeld = recipe.shouldKeepHeldItem() || creative;

if (!keepHeld) {
if (heldItem.isDamageableItem())
if (heldItem.isDamageableItem()) {
heldItem.hurtAndBreak(1, event.getEntity(), s -> s.broadcastBreakEvent(InteractionHand.MAIN_HAND));
else
} else {
Player player = event.getEntity();
InteractionHand hand = event.getHand();
ItemStack leftover = heldItem.hasCraftingRemainingItem() ? heldItem.getCraftingRemainingItem() : ItemStack.EMPTY;

heldItem.shrink(1);

if (heldItem.isEmpty()) {
player.setItemInHand(hand, leftover);
} else {
if (!player.getInventory().add(leftover)) {
player.drop(leftover, false);
}
}
}
}

awardAdvancements(event.getEntity(), transformedBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public List<ItemStack> process(ItemStack stack, Level level) {
.getResultItem(registryAccess),
smeltingRecipe.get()
.getResultItem(registryAccess))) {
return RecipeApplier.applyRecipeOn(level, stack, smeltingRecipe.get());
return RecipeApplier.applyRecipeOn(level, stack, smeltingRecipe.get(), false);
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ public List<ItemStack> process(ItemStack stack, Level level) {
HAUNTING_WRAPPER.setItem(0, stack);
Optional<HauntingRecipe> recipe = AllRecipeTypes.HAUNTING.find(HAUNTING_WRAPPER, level);
if (recipe.isPresent())
return RecipeApplier.applyRecipeOn(level, stack, recipe.get());
return RecipeApplier.applyRecipeOn(level, stack, recipe.get(), true);
return null;
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public List<ItemStack> process(ItemStack stack, Level level) {
.filter(AllRecipeTypes.CAN_BE_AUTOMATED);

if (smokingRecipe.isPresent())
return RecipeApplier.applyRecipeOn(level, stack, smokingRecipe.get());
return RecipeApplier.applyRecipeOn(level, stack, smokingRecipe.get(), false);

return null;
}
Expand Down Expand Up @@ -429,7 +429,7 @@ public List<ItemStack> process(ItemStack stack, Level level) {
SPLASHING_WRAPPER.setItem(0, stack);
Optional<SplashingRecipe> recipe = AllRecipeTypes.SPLASHING.find(SPLASHING_WRAPPER, level);
if (recipe.isPresent())
return RecipeApplier.applyRecipeOn(level, stack, recipe.get());
return RecipeApplier.applyRecipeOn(level, stack, recipe.get(), true);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.capabilities.Capability;
Expand Down Expand Up @@ -140,10 +141,14 @@ private void process() {
}

ItemStack stackInSlot = inputInv.getStackInSlot(0);
ItemStack craftingRemainingItem = stackInSlot.getCraftingRemainingItem();
stackInSlot.shrink(1);
inputInv.setStackInSlot(0, stackInSlot);
lastRecipe.rollResults()
.forEach(stack -> ItemHandlerHelper.insertItemStacked(outputInv, stack, false));
if (!craftingRemainingItem.isEmpty()) {
ItemHandlerHelper.insertItemStacked(outputInv, craftingRemainingItem, false);
}
award(AllAdvancements.MILLSTONE);

sendData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public boolean tryProcessInWorld(ItemEntity itemEntity, boolean simulate) {
ItemStack itemCreated = ItemStack.EMPTY;
pressingBehaviour.particleItems.add(item);
if (canProcessInBulk() || item.getCount() == 1) {
RecipeApplier.applyRecipeOn(itemEntity, recipe.get());
RecipeApplier.applyRecipeOn(itemEntity, recipe.get(), true);
itemCreated = itemEntity.getItem()
.copy();
} else {
for (ItemStack result : RecipeApplier.applyRecipeOn(level, ItemHandlerHelper.copyStackWithSize(item, 1),
recipe.get())) {
recipe.get(), true)) {
if (itemCreated.isEmpty())
itemCreated = result.copy();
ItemEntity created =
Expand All @@ -155,7 +155,7 @@ public boolean tryProcessOnBelt(TransportedItemStack input, List<ItemStack> outp
return true;
pressingBehaviour.particleItems.add(input.stack);
List<ItemStack> outputs = RecipeApplier.applyRecipeOn(level,
canProcessInBulk() ? input.stack : ItemHandlerHelper.copyStackWithSize(input.stack, 1), recipe.get());
canProcessInBulk() ? input.stack : ItemHandlerHelper.copyStackWithSize(input.stack, 1), recipe.get(), true);

for (ItemStack created : outputs) {
if (!created.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ else if (recipe instanceof StonecutterRecipe || recipe.getType() == woodcuttingR
for (ItemStack stack : results) {
ItemHelper.addToList(stack, list);
}
if (input.hasCraftingRemainingItem())
ItemHelper.addToList(input.getCraftingRemainingItem(), list);
}

for (int slot = 0; slot < list.size() && slot + 1 < inventory.getSlots(); slot++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;

import net.minecraftforge.items.ItemHandlerHelper;

public class RecipeApplier {
public static void applyRecipeOn(ItemEntity entity, Recipe<?> recipe) {
List<ItemStack> stacks = applyRecipeOn(entity.level(), entity.getItem(), recipe);
public static void applyRecipeOn(ItemEntity entity, Recipe<?> recipe, boolean returnProcessingRemainder) {
List<ItemStack> stacks = applyRecipeOn(entity.level(), entity.getItem(), recipe, returnProcessingRemainder);
if (stacks == null)
return;
if (stacks.isEmpty()) {
Expand All @@ -31,7 +32,7 @@ public static void applyRecipeOn(ItemEntity entity, Recipe<?> recipe) {
}
}

public static List<ItemStack> applyRecipeOn(Level level, ItemStack stackIn, Recipe<?> recipe) {
public static List<ItemStack> applyRecipeOn(Level level, ItemStack stackIn, Recipe<?> recipe, boolean returnProcessingRemainder) {
List<ItemStack> stacks;

if (recipe instanceof ProcessingRecipe<?> pr) {
Expand All @@ -56,6 +57,10 @@ public static List<ItemStack> applyRecipeOn(Level level, ItemStack stackIn, Reci

stacks.add(stack);
}

if (returnProcessingRemainder && stackIn.hasCraftingRemainingItem()) {
ItemHelper.addToList(stackIn.getCraftingRemainingItem(), stacks);
}
}
} else {
ItemStack out = recipe.getResultItem(level.registryAccess())
Expand Down