mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-03-18 09:28:12 +00:00
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
This commit is contained in:
parent
c271ed7c7f
commit
dcc74e15c7
@ -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<IPocketUpgrade> upgrade) {
|
||||
this.upgrade = upgrade;
|
||||
if (Objects.equals(this.upgrade, upgrade)) return;
|
||||
|
||||
this.upgrade = UpgradeData.copyOf(upgrade);
|
||||
dirty = true;
|
||||
invalidatePeripheral();
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user