From dcc74e15c7ab88e5ec7bb4c83ca00d49c074b615 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 27 Oct 2024 09:43:47 +0000 Subject: [PATCH] Fix pocket upgrades not applying after crafting The most annoying thing about pocket computers is handling computer state (label, upgrades, etc...). Unlike other computers, which are tied to a specific block entity, pocket computers float untethered. We can't hold a reference to a specific item stack (as the computer might be moved between inventories, crafted, etc...), so instead we explicitly sync data between the computer and *current* stack, whenever the holding player/entity is ticked. In ed0b156e05ff64784f2f80346c82ab5142d954dc I rewrote this syncing code to always treat the computer as the source of truth. Upgrades would be copied to the computer, but never the other way round. However, this meant that upgrades obtained by crafting would never be detected, requiring the computer to be destroyed and recreated. A more long-term fix here is probably to rewrite IPocketAccess to only allow updating upgrade data on the main thread, and when we have a valid PocketHolder. This is a breaking API change though, and so will have to wait for 1.21.3. For now, we just add a hook that refreshes the upgrade after crafting. Fixes #1957 --- .../shared/pocket/core/PocketBrain.java | 5 ++++- .../shared/pocket/items/PocketComputerItem.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java index 33b5e8231..6d8286e5b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketBrain.java @@ -24,6 +24,7 @@ import net.minecraft.world.phys.Vec3; import javax.annotation.Nullable; import java.util.Collections; import java.util.Map; +import java.util.Objects; /** * Holds additional state for a pocket computer. This includes pocket computer upgrade, @@ -177,7 +178,9 @@ public final class PocketBrain implements IPocketAccess { */ @Override public void setUpgrade(@Nullable UpgradeData upgrade) { - this.upgrade = upgrade; + if (Objects.equals(this.upgrade, upgrade)) return; + + this.upgrade = UpgradeData.copyOf(upgrade); dirty = true; invalidatePeripheral(); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java index 348c96dec..f08c871d1 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java @@ -267,6 +267,21 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I return getServerComputer(ServerContext.get(server).registry(), stack); } + @Override + public void onCraftedBy(ItemStack stack, Level level, Player player) { + var tag = stack.getTag(); + if (tag == null) return; + + // Normally we treat the computer instance as the source of truth, and copy the computer's state back to the + // item. However, if we've just crafted the computer with an upgrade, we should sync the other way, and update + // the computer. + var server = level.getServer(); + if (server != null) { + var computer = getServerComputer(server, stack); + if (computer != null) computer.getBrain().setUpgrade(getUpgradeWithData(stack)); + } + } + // IComputerItem implementation private static void setComputerID(ItemStack stack, int computerID) {