Load every mail storage a delivery writes to before changing any

This commit is contained in:
Ruling-0 committed 2026-11-24 15:57:30 -06:00
1 parent 990e8158df
commit 376d83abc1
4 files changed
+35 -2

No files matched your search

@@ -9,7 +9,8 @@ import forestry.core.utils.PosteaCompat;
/// `forestry:<mapName>`.
///
/// `MapStorage` swallows load exceptions and keeps the half-read storage. A storage whose migration failed is never
/// saved. [PostRegistry] rethrows the failure instead of serving the storage.
/// saved. [PostRegistry] rethrows the failure instead of serving the storage. A delivery loads every storage it writes
/// to before changing any of them. A failed storage then stops the whole delivery.
public abstract class MailSavedData extends WorldSavedData {
private RuntimeException loadFailure;
@@ -158,6 +158,8 @@ public class PostOffice extends WorldSavedData implements IPostOffice {
return EnumDeliveryState.NOT_MAILABLE;
}
loadDeliveryTargets(world, letter, doLodge);
IPostalState state = EnumDeliveryState.NOT_MAILABLE;
for (IMailAddress address : letter.getRecipients()) {
IPostalCarrier carrier = PostManager.postRegistry.getCarrier(address.getType());
@@ -180,6 +182,14 @@ public class PostOffice extends WorldSavedData implements IPostOffice {
return EnumDeliveryState.OK;
}
private static void loadDeliveryTargets(World world, ILetter letter, boolean doLodge) {
for (IMailAddress recipient : letter.getRecipients()) {
if (PostRegistry.loadMailStorage(world, recipient) instanceof TradeStation trade) {
trade.loadTradeTargets(world, letter, doLodge);
}
}
}
@Override
public void collectPostage(ItemStack[] stamps) {
for (ItemStack stamp : stamps) {
@@ -63,6 +63,14 @@ public class PostRegistry implements IPostRegistry {
return pobox;
}
/// The post box or trade station at `address`, or null when none exists.
static MailSavedData loadMailStorage(World world, IMailAddress address) {
if (address == null) return null;
if (address.isPlayer()) return getPOBox(world, address);
if (address.isTrader()) return (TradeStation) PostManager.postRegistry.getTradeStation(world, address);
return null;
}
public static POBox getOrCreatePOBox(World world, IMailAddress address) {
POBox pobox = getPOBox(world, address);
+15 -1
View File
@@ -171,9 +171,10 @@ public class TradeStation extends MailSavedData implements ITradeStation, IInven
@Override
public IPostalState handleLetter(World world, IMailAddress recipient, ItemStack letterstack, boolean doLodge) {
boolean sendOwnerNotice = doLodge && owner != null;
boolean sendOwnerNotice = sendsOwnerNotice(doLodge);
ILetter letter = PostManager.postRegistry.getLetter(letterstack);
loadTradeTargets(world, letter, doLodge);
if (!isVirtual() && !hasPaper(sendOwnerNotice ? 2 : 1)) {
return EnumStationState.INSUFFICIENT_PAPER;
@@ -309,6 +310,19 @@ public class TradeStation extends MailSavedData implements ITradeStation, IInven
return EnumDeliveryState.OK;
}
/// Loads the storages a trade with `letter` delivers to: the sender's, and the owner's post box when the trade
/// sends an owner notice.
void loadTradeTargets(World world, ILetter letter, boolean doLodge) {
PostRegistry.loadMailStorage(world, letter.getSender());
if (sendsOwnerNotice(doLodge)) {
PostRegistry.getPOBox(world, new MailAddress(owner));
}
}
private boolean sendsOwnerNotice(boolean doLodge) {
return doLodge && owner != null;
}
/* TRADE LOGIC */
private int countFillableOrders(int max, ItemStack tradegood) {