1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-08 13:02:54 +00:00

Draft implemention of upgrade data passing from and to respectful items

This commit is contained in:
SirEdvin 2023-06-03 23:20:59 +03:00
parent f0abb83f6e
commit b3f5505e1d
3 changed files with 29 additions and 8 deletions

View File

@ -8,9 +8,11 @@ import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.impl.PlatformHelper; import dan200.computercraft.impl.PlatformHelper;
import net.minecraft.Util; import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import javax.annotation.Nonnull;
import java.util.Objects; import java.util.Objects;
/** /**
@ -50,6 +52,14 @@ public interface UpgradeBase {
*/ */
ItemStack getCraftingItem(); ItemStack getCraftingItem();
default @Nonnull ItemStack produceCraftingItem(@Nonnull CompoundTag upgradeData) {
return getCraftingItem();
}
default @Nonnull CompoundTag produceUpgradeData(@Nonnull ItemStack stack) {
return new CompoundTag();
}
/** /**
* Determine if an item is suitable for being used for this upgrade. * Determine if an item is suitable for being used for this upgrade.
* <p> * <p>

View File

@ -61,17 +61,21 @@ public class PocketAPI implements ILuaAPI {
// Attempt to find the upgrade, starting in the main segment, and then looking in the opposite // Attempt to find the upgrade, starting in the main segment, and then looking in the opposite
// one. We start from the position the item is currently in and loop round to the start. // one. We start from the position the item is currently in and loop round to the start.
var newUpgrade = findUpgrade(inventory.items, inventory.selected, previousUpgrade); var newUpgradeItem = findUpgrade(inventory.items, inventory.selected, previousUpgrade);
if (newUpgrade == null) { if (newUpgradeItem == null) {
newUpgrade = findUpgrade(inventory.offhand, 0, previousUpgrade); newUpgradeItem = findUpgrade(inventory.offhand, 0, previousUpgrade);
} }
if (newUpgradeItem == null) return new Object[]{ false, "Cannot find a valid upgrade" };
// Unpack new upgrade here and ensure some mystical error is not happened
var newUpgrade = PocketUpgrades.instance().get(newUpgradeItem);
if (newUpgrade == null) return new Object[]{ false, "Cannot find a valid upgrade" }; if (newUpgrade == null) return new Object[]{ false, "Cannot find a valid upgrade" };
// Remove the current upgrade // Remove the current upgrade
if (previousUpgrade != null) storeItem(player, previousUpgrade.getCraftingItem().copy()); if (previousUpgrade != null) storeItem(player, previousUpgrade.produceCraftingItem(computer.getUpgradeNBTData()).copy());
// Set the new upgrade // Set the new upgrade
computer.setUpgrade(newUpgrade); computer.setUpgrade(newUpgrade);
computer.getUpgradeNBTData().merge(newUpgrade.produceUpgradeData(newUpgradeItem));
return new Object[]{ true }; return new Object[]{ true };
} }
@ -105,7 +109,7 @@ public class PocketAPI implements ILuaAPI {
} }
} }
private static @Nullable IPocketUpgrade findUpgrade(NonNullList<ItemStack> inv, int start, @Nullable IPocketUpgrade previous) { private static @Nullable ItemStack findUpgrade(NonNullList<ItemStack> inv, int start, @Nullable IPocketUpgrade previous) {
for (var i = 0; i < inv.size(); i++) { for (var i = 0; i < inv.size(); i++) {
var invStack = inv.get((i + start) % inv.size()); var invStack = inv.get((i + start) % inv.size());
if (!invStack.isEmpty()) { if (!invStack.isEmpty()) {
@ -117,7 +121,7 @@ public class PocketAPI implements ILuaAPI {
invStack.shrink(1); invStack.shrink(1);
inv.set((i + start) % inv.size(), invStack.isEmpty() ? ItemStack.EMPTY : invStack); inv.set((i + start) % inv.size(), invStack.isEmpty() ? ItemStack.EMPTY : invStack);
return newUpgrade; return invStack.copyWithCount(1);
} }
} }
} }

View File

@ -7,6 +7,7 @@ package dan200.computercraft.shared.turtle.core;
import dan200.computercraft.api.turtle.*; import dan200.computercraft.api.turtle.*;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.turtle.TurtleUtil; import dan200.computercraft.shared.turtle.TurtleUtil;
import net.minecraft.nbt.CompoundTag;
public class TurtleEquipCommand implements TurtleCommand { public class TurtleEquipCommand implements TurtleCommand {
private final TurtleSide side; private final TurtleSide side;
@ -30,10 +31,16 @@ public class TurtleEquipCommand implements TurtleCommand {
newUpgrade = null; newUpgrade = null;
} }
CompoundTag upgradeData = null;
// Do the swapping: // Do the swapping:
if (newUpgrade != null) turtle.getInventory().removeItem(turtle.getSelectedSlot(), 1); if (newUpgrade != null) {
if (oldUpgrade != null) TurtleUtil.storeItemOrDrop(turtle, oldUpgrade.getCraftingItem().copy()); var upgradeItem = turtle.getInventory().removeItem(turtle.getSelectedSlot(), 1);
upgradeData = newUpgrade.produceUpgradeData(upgradeItem);
}
if (oldUpgrade != null) TurtleUtil.storeItemOrDrop(turtle, oldUpgrade.produceCraftingItem(turtle.getUpgradeNBTData(side)).copy());
turtle.setUpgrade(side, newUpgrade); turtle.setUpgrade(side, newUpgrade);
if (upgradeData != null) turtle.getUpgradeNBTData(side).merge(upgradeData);
// Animate // Animate
if (newUpgrade != null || oldUpgrade != null) { if (newUpgrade != null || oldUpgrade != null) {