1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 01:10:30 +00:00

Several cleanup to turtle crafting upgrade

- 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.
This commit is contained in:
Jonathan Coates 2024-11-15 07:31:49 +00:00
parent f39e86bb10
commit 0c8e757314
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 5 additions and 71 deletions

View File

@ -33,8 +33,6 @@ import net.minecraft.world.item.CreativeModeTab;
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;
@ -216,26 +214,6 @@ public interface PlatformHelper {
*/
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.
*

View File

@ -26,6 +26,7 @@ public class TurtleCraftCommand implements TurtleCommand {
// 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();

View File

@ -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;
@ -82,18 +80,15 @@ public final class TurtleInventoryCrafting {
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;
@ -110,10 +105,9 @@ public final class TurtleInventoryCrafting {
// 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);
}

View File

@ -34,8 +34,6 @@ import net.minecraft.world.inventory.MenuType;
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;
@ -160,16 +158,6 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
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";

View File

@ -55,9 +55,7 @@ import net.minecraft.world.inventory.AbstractContainerMenu;
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;
@ -197,15 +195,6 @@ public class PlatformHelperImpl implements PlatformHelper {
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;

View File

@ -15,7 +15,6 @@ import dan200.computercraft.api.peripheral.IPeripheral;
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;
@ -42,9 +41,7 @@ import net.minecraft.world.item.CreativeModeTab;
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;
@ -192,19 +189,6 @@ public class PlatformHelperImpl implements PlatformHelper {
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();