Skip to content

Commit 73c7343

Browse files
committed
fix: persist consumed research costs to NBT for restart-safe refunds
consumedCosts and consumedFluidCost were in-memory-only snapshots; a server restart during active research would null them out, making cancel give no refund. Both fields are now serialized in saveAdditional and restored in loadAdditional so refunds survive chunk reloads and server restarts.
1 parent 5e7babc commit 73c7343

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

src/main/java/com/researchcube/block/ResearchTableBlockEntity.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import software.bernie.geckolib.animation.*;
4848
import software.bernie.geckolib.util.GeckoLibUtil;
4949

50+
import java.util.ArrayList;
5051
import java.util.List;
5152
import java.util.Set;
5253

@@ -627,6 +628,23 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
627628
if (researchKey != null) {
628629
tag.putString("ResearchKey", researchKey);
629630
}
631+
// Persist consumed costs so refunds survive server restarts
632+
if (consumedCosts != null) {
633+
ListTag costList = new ListTag();
634+
for (ItemCost cost : consumedCosts) {
635+
CompoundTag costTag = new CompoundTag();
636+
costTag.putString("Item", cost.itemId().toString());
637+
costTag.putInt("Count", cost.count());
638+
costList.add(costTag);
639+
}
640+
tag.put("ConsumedCosts", costList);
641+
}
642+
if (consumedFluidCost != null) {
643+
CompoundTag fluidCostTag = new CompoundTag();
644+
fluidCostTag.putString("Fluid", consumedFluidCost.fluidId().toString());
645+
fluidCostTag.putInt("Amount", consumedFluidCost.amount());
646+
tag.put("ConsumedFluidCost", fluidCostTag);
647+
}
630648
}
631649
}
632650

@@ -653,9 +671,30 @@ protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries)
653671
startTime = -1;
654672
researchKey = null;
655673
}
656-
// consumedCosts/consumedFluidCost are not persisted — on reload, cancel will not refund
657-
consumedCosts = null;
658-
consumedFluidCost = null;
674+
// Restore consumed costs for refund support across restarts
675+
if (tag.contains("ConsumedCosts", Tag.TAG_LIST)) {
676+
ListTag costList = tag.getList("ConsumedCosts", Tag.TAG_COMPOUND);
677+
List<ItemCost> costs = new ArrayList<>();
678+
for (int i = 0; i < costList.size(); i++) {
679+
CompoundTag costTag = costList.getCompound(i);
680+
costs.add(new ItemCost(
681+
ResourceLocation.parse(costTag.getString("Item")),
682+
costTag.getInt("Count")
683+
));
684+
}
685+
consumedCosts = costs;
686+
} else {
687+
consumedCosts = null;
688+
}
689+
if (tag.contains("ConsumedFluidCost", Tag.TAG_COMPOUND)) {
690+
CompoundTag fluidCostTag = tag.getCompound("ConsumedFluidCost");
691+
consumedFluidCost = new FluidCost(
692+
ResourceLocation.parse(fluidCostTag.getString("Fluid")),
693+
fluidCostTag.getInt("Amount")
694+
);
695+
} else {
696+
consumedFluidCost = null;
697+
}
659698
}
660699

661700
// ── Client Sync ──

0 commit comments

Comments
 (0)