Skip to content

Commit

Permalink
Several cleanup to turtle crafting upgrade
Browse files Browse the repository at this point in the history
 - Don't construct a fake player when crafting: vanilla now has its own
   automated crafting, so no longer requires the presence of a player.

 - Fix remainder stack not being set in some situations. Closes #2007.
  • Loading branch information
SquidDev committed Nov 15, 2024
1 parent f39e86b commit 0c8e757
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -216,26 +214,6 @@ default void invalidateComponent(BlockEntity owner) {
*/
ItemStack getCraftingRemainingItem(ItemStack stack);

/**
* A more general version of {@link #getCraftingRemainingItem(ItemStack)} which gets all remaining items for a
* recipe.
*
* @param player The player performing the crafting.
* @param recipe The recipe currently doing the crafting.
* @param container The crafting container.
* @return A list of items to return to the player after crafting.
*/
List<ItemStack> getRecipeRemainingItems(ServerPlayer player, Recipe<CraftingInput> recipe, CraftingInput container);

/**
* Fire an event after crafting has occurred.
*
* @param player The player performing the crafting.
* @param container The current crafting container.
* @param stack The resulting stack from crafting.
*/
void onItemCrafted(ServerPlayer player, CraftingInput container, ItemStack stack);

/**
* Check whether we should notify neighbours in a particular direction.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public TurtleCommandResult execute(ITurtleAccess turtle) {

// Store or drop any remainders
for (var stack : results) TurtleUtil.storeItemOrDrop(turtle, stack);
turtle.getInventory().setChanged();

if (!results.isEmpty()) turtle.playAnimation(TurtleAnimation.WAIT);
return TurtleCommandResult.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package dan200.computercraft.shared.turtle.upgrades;

import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.shared.platform.PlatformHelper;
import dan200.computercraft.shared.turtle.blocks.TurtleBlockEntity;
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -82,18 +80,15 @@ public static List<ItemStack> craft(ITurtleAccess turtle, int maxCount) {
var xStart = candidate.xStart();
var yStart = candidate.yStart();

var player = TurtlePlayer.get(turtle).player();

var results = new ArrayList<ItemStack>();
for (var i = 0; i < maxCount && recipe.matches(input, level); i++) {
var result = recipe.assemble(input, level.registryAccess());
if (result.isEmpty()) break;
results.add(result);

result.onCraftedBy(level, player, result.getCount());
PlatformHelper.get().onItemCrafted(player, input, result);
result.onCraftedBySystem(level);

var remainders = PlatformHelper.get().getRecipeRemainingItems(player, recipe, input);
var remainders = recipe.getRemainingItems(input);
for (var y = 0; y < input.height(); y++) {
for (var x = 0; x < input.width(); x++) {
var slot = xStart + x + (y + yStart) * TurtleBlockEntity.INVENTORY_WIDTH;
Expand All @@ -110,10 +105,9 @@ public static List<ItemStack> craft(ITurtleAccess turtle, int maxCount) {
// Either update the current stack or add it to the remainder list (to be inserted into the inventory
// afterwards).
if (existing.isEmpty()) {
inventory.setItem(slot, existing);
} else if (ItemStack.isSameItemSameComponents(existing, remainder)) {
remainder.grow(existing.getCount());
inventory.setItem(slot, remainder);
} else if (ItemStack.isSameItemSameComponents(existing, remainder)) {
existing.grow(remainder.getCount());
} else {
results.add(remainder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -160,16 +158,6 @@ public ContainerTransfer getContainer(ServerLevel level, BlockPos pos, Direction
throw new UnsupportedOperationException("Cannot interact with the world inside tests");
}

@Override
public List<ItemStack> getRecipeRemainingItems(ServerPlayer player, Recipe<CraftingInput> recipe, CraftingInput container) {
throw new UnsupportedOperationException("Cannot query recipes inside tests");
}

@Override
public void onItemCrafted(ServerPlayer player, CraftingInput container, ItemStack stack) {
throw new UnsupportedOperationException("Cannot interact with the world inside tests");
}

@Override
public String getInstalledVersion() {
return "1.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -197,15 +195,6 @@ public ItemStack getCraftingRemainingItem(ItemStack stack) {
return stack.getRecipeRemainder();
}

@Override
public List<ItemStack> getRecipeRemainingItems(ServerPlayer player, Recipe<CraftingInput> recipe, CraftingInput container) {
return recipe.getRemainingItems(container);
}

@Override
public void onItemCrafted(ServerPlayer player, CraftingInput container, ItemStack stack) {
}

@Override
public boolean onNotifyNeighbour(Level level, BlockPos pos, BlockState block, Direction direction) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import dan200.computercraft.api.peripheral.PeripheralCapability;
import dan200.computercraft.impl.Peripherals;
import dan200.computercraft.shared.config.ConfigFile;
import dan200.computercraft.shared.container.ListContainer;
import dan200.computercraft.shared.network.container.ContainerData;
import dan200.computercraft.shared.util.InventoryUtil;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
Expand All @@ -42,9 +41,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -192,19 +189,6 @@ public ItemStack getCraftingRemainingItem(ItemStack stack) {
return stack.getCraftingRemainingItem();
}

@Override
public List<ItemStack> getRecipeRemainingItems(ServerPlayer player, Recipe<CraftingInput> recipe, CraftingInput container) {
CommonHooks.setCraftingPlayer(player);
var result = recipe.getRemainingItems(container);
CommonHooks.setCraftingPlayer(null);
return result;
}

@Override
public void onItemCrafted(ServerPlayer player, CraftingInput container, ItemStack stack) {
EventHooks.firePlayerCraftingEvent(player, stack, new ListContainer(container.items()));
}

@Override
public boolean onNotifyNeighbour(Level level, BlockPos pos, BlockState block, Direction direction) {
return !EventHooks.onNeighborNotify(level, pos, block, EnumSet.of(direction), false).isCanceled();
Expand Down

0 comments on commit 0c8e757

Please sign in to comment.