mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-25 22:34:33 +00:00
Refuse post boxes whose Postea migration failed
This commit is contained in:
3 files changed
+55
-7
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package forestry.mail;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.WorldSavedData;
|
||||
|
||||
import forestry.core.utils.PosteaCompat;
|
||||
|
||||
/// A mail storage in the world's `MapStorage`, with saved item stacks migrated through Postea under the storage id
|
||||
/// `forestry:<mapName>`.
|
||||
///
|
||||
/// `MapStorage` swallows load exceptions and keeps the half-read storage, so a storage whose migration failed is never
|
||||
/// saved and [PostRegistry] rethrows the failure instead of serving it.
|
||||
public abstract class MailSavedData extends WorldSavedData {
|
||||
|
||||
private RuntimeException loadFailure;
|
||||
|
||||
protected MailSavedData(String mapName) {
|
||||
super(mapName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void readFromNBT(NBTTagCompound root) {
|
||||
try {
|
||||
PosteaCompat.transform("forestry:" + mapName, root);
|
||||
} catch (RuntimeException e) {
|
||||
loadFailure = e;
|
||||
throw e;
|
||||
}
|
||||
readData(root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeToNBT(NBTTagCompound root) {
|
||||
writeData(root);
|
||||
PosteaCompat.stamp(root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty() {
|
||||
return loadFailure == null && super.isDirty();
|
||||
}
|
||||
|
||||
/// Throws the exception that failed this storage's migration, if one did.
|
||||
void throwIfLoadFailed() {
|
||||
if (loadFailure != null) throw loadFailure;
|
||||
}
|
||||
|
||||
protected abstract void readData(NBTTagCompound root);
|
||||
|
||||
protected abstract void writeData(NBTTagCompound root);
|
||||
}
|
||||
@@ -12,16 +12,14 @@ import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.WorldSavedData;
|
||||
|
||||
import forestry.api.mail.ILetter;
|
||||
import forestry.api.mail.IMailAddress;
|
||||
import forestry.api.mail.PostManager;
|
||||
import forestry.core.inventory.InventoryAdapter;
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
import forestry.core.utils.PosteaCompat;
|
||||
|
||||
public class POBox extends WorldSavedData implements IInventory {
|
||||
public class POBox extends MailSavedData implements IInventory {
|
||||
|
||||
public static final String SAVE_NAME = "POBox_";
|
||||
public static final short SLOT_SIZE = 84;
|
||||
@@ -42,8 +40,7 @@ public class POBox extends WorldSavedData implements IInventory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
||||
PosteaCompat.transform("forestry:" + mapName, nbttagcompound);
|
||||
protected void readData(NBTTagCompound nbttagcompound) {
|
||||
if (nbttagcompound.hasKey("address")) {
|
||||
this.address = MailAddress.loadFromNBT(nbttagcompound.getCompoundTag("address"));
|
||||
}
|
||||
@@ -51,14 +48,13 @@ public class POBox extends WorldSavedData implements IInventory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbttagcompound) {
|
||||
protected void writeData(NBTTagCompound nbttagcompound) {
|
||||
if (this.address != null) {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
this.address.writeToNBT(nbt);
|
||||
nbttagcompound.setTag("address", nbt);
|
||||
}
|
||||
letters.writeToNBT(nbttagcompound);
|
||||
PosteaCompat.stamp(nbttagcompound);
|
||||
}
|
||||
|
||||
public boolean storeLetter(ItemStack letterstack) {
|
||||
|
||||
@@ -57,6 +57,7 @@ public class PostRegistry implements IPostRegistry {
|
||||
|
||||
POBox pobox = (POBox) world.loadItemData(POBox.class, POBox.SAVE_NAME + address);
|
||||
if (pobox != null) {
|
||||
pobox.throwIfLoadFailed();
|
||||
cachedPOBoxes.put(address, pobox);
|
||||
}
|
||||
return pobox;
|
||||
|
||||
Reference in new issue
Block a user