1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 20:20:30 +00:00

Copy pocket upgrades items before dropping them

We were modifying the stack, meaning the crafting item would end up
empty!
This commit is contained in:
Jonathan Coates 2023-01-26 22:40:03 +00:00
parent b21e2f4e63
commit 1e3a930543
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 8 additions and 20 deletions

View File

@ -69,7 +69,7 @@ public class PocketAPI implements ILuaAPI {
if (newUpgrade == null) return new Object[]{ false, "Cannot find a valid upgrade" };
// Remove the current upgrade
if (previousUpgrade != null) storeItem(player, previousUpgrade.getCraftingItem());
if (previousUpgrade != null) storeItem(player, previousUpgrade.getCraftingItem().copy());
// Set the new upgrade
computer.setUpgrade(newUpgrade);
@ -88,14 +88,13 @@ public class PocketAPI implements ILuaAPI {
public final Object[] unequipBack() {
var entity = computer.getEntity();
if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" };
var inventory = player.getInventory();
var previousUpgrade = computer.getUpgrade();
if (previousUpgrade == null) return new Object[]{ false, "Nothing to unequip" };
computer.setUpgrade(null);
storeItem(player, previousUpgrade.getCraftingItem());
storeItem(player, previousUpgrade.getCraftingItem().copy());
return new Object[]{ true };
}

View File

@ -8,7 +8,6 @@ package dan200.computercraft.shared.turtle.core;
import dan200.computercraft.api.turtle.*;
import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.turtle.TurtleUtil;
import net.minecraft.world.item.ItemStack;
public class TurtleEquipCommand implements TurtleCommand {
private final TurtleSide side;
@ -19,32 +18,22 @@ public class TurtleEquipCommand implements TurtleCommand {
@Override
public TurtleCommandResult execute(ITurtleAccess turtle) {
// Determine the upgrade to replace
var oldUpgrade = turtle.getUpgrade(side);
// Determine the upgrade to equipLeft
ITurtleUpgrade newUpgrade;
ItemStack newUpgradeStack;
var selectedStack = turtle.getInventory().getItem(turtle.getSelectedSlot());
if (!selectedStack.isEmpty()) {
newUpgradeStack = selectedStack.copy();
newUpgrade = TurtleUpgrades.instance().get(newUpgradeStack);
newUpgrade = TurtleUpgrades.instance().get(selectedStack);
if (newUpgrade == null) return TurtleCommandResult.failure("Not a valid upgrade");
} else {
newUpgradeStack = null;
newUpgrade = null;
}
// Determine the upgrade to replace
ItemStack oldUpgradeStack;
var oldUpgrade = turtle.getUpgrade(side);
if (oldUpgrade != null) {
var craftingItem = oldUpgrade.getCraftingItem();
oldUpgradeStack = !craftingItem.isEmpty() ? craftingItem.copy() : null;
} else {
oldUpgradeStack = null;
}
// Do the swapping:
if (newUpgradeStack != null) turtle.getInventory().removeItem(turtle.getSelectedSlot(), 1);
if (oldUpgradeStack != null) TurtleUtil.storeItemOrDrop(turtle, oldUpgradeStack);
if (newUpgrade != null) turtle.getInventory().removeItem(turtle.getSelectedSlot(), 1);
if (oldUpgrade != null) TurtleUtil.storeItemOrDrop(turtle, oldUpgrade.getCraftingItem().copy());
turtle.setUpgrade(side, newUpgrade);
// Animate