Compare commits

...
8 changed files with 152 additions and 10 deletions

No files matched your search

+1
View File
@@ -9,5 +9,6 @@ dependencies {
compileOnly('com.github.GTNewHorizons:BuildCraft:7.1.57:api') compileOnly('com.github.GTNewHorizons:BuildCraft:7.1.57:api')
compileOnly('net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev') compileOnly('net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev')
compileOnly('curse.maven:craftguide-75557:2459320') compileOnly('curse.maven:craftguide-75557:2459320')
compileOnly('com.github.GTNewHorizons:Postea:1.4.0-mu:dev')
devOnlyNonPublishable('net.xonich.mc:NoHotbarNeeded:0.0.3:dev') devOnlyNonPublishable('net.xonich.mc:NoHotbarNeeded:0.0.3:dev')
} }
+2 -1
View File
@@ -63,7 +63,8 @@ import forestry.plugins.PluginManager;
+ "after:BiomesOPlenty;" + "after:BiomesOPlenty;"
+ "after:IC2@[2.0.140,);" + "after:IC2@[2.0.140,);"
+ "after:Natura@[2.2.0,);" + "after:Natura@[2.2.0,);"
+ "after:HardcoreEnderExpansion;") + "after:HardcoreEnderExpansion;"
+ "after:postea@[1.4.0,);")
public class Forestry { public class Forestry {
@Mod.Instance(Constants.MOD) @Mod.Instance(Constants.MOD)
@@ -0,0 +1,51 @@
package forestry.core.utils;
import net.minecraft.nbt.NBTTagCompound;
import com.gtnewhorizons.postea.api.CustomDataReplacementManager;
import cpw.mods.fml.common.Loader;
/// Used to provide custom save data (like mail boxes) to Postea's versioned transformers.
///
/// These transformers allow other mods to change saved data based on their own version stamps. This compat flags,
/// to those transformers, the times they should fire. [#transform] is used before consuming data, so the transformer
/// can run, and [#stamp] is used for fresh data to ensure the transformer can add a stamp of its current version.
public final class PosteaCompat {
private static final boolean POSTEA = Loader.isModLoaded("postea");
private PosteaCompat() {}
/// Transforms a root read from disk in place; call before consuming it.
public static void transform(String storageId, NBTTagCompound root) {
if (POSTEA) Impl.transform(storageId, root);
}
/// A copy of the stamps on a root read from disk, or null when it carries none or Postea is absent. Call after
/// [#transform].
public static NBTTagCompound savedStamps(NBTTagCompound root) {
return POSTEA ? Impl.savedStamps(root) : null;
}
/// Stamps a freshly built root about to be written to disk, keeping the stamps in `carried` (from [#savedStamps])
/// for transformers not registered now. `carried` may be null.
public static void stamp(NBTTagCompound root, NBTTagCompound carried) {
if (POSTEA) Impl.stamp(root, carried);
}
private static final class Impl {
static void transform(String storageId, NBTTagCompound root) {
CustomDataReplacementManager.transform(storageId, root);
}
static NBTTagCompound savedStamps(NBTTagCompound root) {
return CustomDataReplacementManager.savedStamps(root);
}
static void stamp(NBTTagCompound root, NBTTagCompound carried) {
CustomDataReplacementManager.stamp(root, carried);
}
}
}
@@ -0,0 +1,53 @@
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. A storage whose migration failed is never
/// saved. [PostRegistry] rethrows the failure instead of serving the storage.
public abstract class MailSavedData extends WorldSavedData {
private RuntimeException loadFailure;
private NBTTagCompound savedStamps;
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;
}
savedStamps = PosteaCompat.savedStamps(root);
readData(root);
}
@Override
public final void writeToNBT(NBTTagCompound root) {
writeData(root);
PosteaCompat.stamp(root, savedStamps);
}
@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);
}
+3 -4
View File
@@ -12,7 +12,6 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.WorldSavedData;
import forestry.api.mail.ILetter; import forestry.api.mail.ILetter;
import forestry.api.mail.IMailAddress; import forestry.api.mail.IMailAddress;
@@ -20,7 +19,7 @@ import forestry.api.mail.PostManager;
import forestry.core.inventory.InventoryAdapter; import forestry.core.inventory.InventoryAdapter;
import forestry.core.utils.InventoryUtil; import forestry.core.utils.InventoryUtil;
public class POBox extends WorldSavedData implements IInventory { public class POBox extends MailSavedData implements IInventory {
public static final String SAVE_NAME = "POBox_"; public static final String SAVE_NAME = "POBox_";
public static final short SLOT_SIZE = 84; public static final short SLOT_SIZE = 84;
@@ -41,7 +40,7 @@ public class POBox extends WorldSavedData implements IInventory {
} }
@Override @Override
public void readFromNBT(NBTTagCompound nbttagcompound) { protected void readData(NBTTagCompound nbttagcompound) {
if (nbttagcompound.hasKey("address")) { if (nbttagcompound.hasKey("address")) {
this.address = MailAddress.loadFromNBT(nbttagcompound.getCompoundTag("address")); this.address = MailAddress.loadFromNBT(nbttagcompound.getCompoundTag("address"));
} }
@@ -49,7 +48,7 @@ public class POBox extends WorldSavedData implements IInventory {
} }
@Override @Override
public void writeToNBT(NBTTagCompound nbttagcompound) { protected void writeData(NBTTagCompound nbttagcompound) {
if (this.address != null) { if (this.address != null) {
NBTTagCompound nbt = new NBTTagCompound(); NBTTagCompound nbt = new NBTTagCompound();
this.address.writeToNBT(nbt); this.address.writeToNBT(nbt);
@@ -158,6 +158,8 @@ public class PostOffice extends WorldSavedData implements IPostOffice {
return EnumDeliveryState.NOT_MAILABLE; return EnumDeliveryState.NOT_MAILABLE;
} }
loadDeliveryTargets(world, letter, doLodge);
IPostalState state = EnumDeliveryState.NOT_MAILABLE; IPostalState state = EnumDeliveryState.NOT_MAILABLE;
for (IMailAddress address : letter.getRecipients()) { for (IMailAddress address : letter.getRecipients()) {
IPostalCarrier carrier = PostManager.postRegistry.getCarrier(address.getType()); IPostalCarrier carrier = PostManager.postRegistry.getCarrier(address.getType());
@@ -180,6 +182,16 @@ public class PostOffice extends WorldSavedData implements IPostOffice {
return EnumDeliveryState.OK; return EnumDeliveryState.OK;
} }
/// Loads every storage a delivery of `letter` writes to, so a storage whose migration failed throws before the
/// delivery changes anything.
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 @Override
public void collectPostage(ItemStack[] stamps) { public void collectPostage(ItemStack[] stamps) {
for (ItemStack stamp : stamps) { for (ItemStack stamp : stamps) {
@@ -57,11 +57,20 @@ public class PostRegistry implements IPostRegistry {
POBox pobox = (POBox) world.loadItemData(POBox.class, POBox.SAVE_NAME + address); POBox pobox = (POBox) world.loadItemData(POBox.class, POBox.SAVE_NAME + address);
if (pobox != null) { if (pobox != null) {
pobox.throwIfLoadFailed();
cachedPOBoxes.put(address, pobox); cachedPOBoxes.put(address, pobox);
} }
return pobox; return pobox;
} }
/// The post box or trade station at `address`, or null when none exists or the trade station is invalid.
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) { public static POBox getOrCreatePOBox(World world, IMailAddress address) {
POBox pobox = getPOBox(world, address); POBox pobox = getPOBox(world, address);
@@ -111,6 +120,9 @@ public class PostRegistry implements IPostRegistry {
} }
TradeStation trade = (TradeStation) world.loadItemData(TradeStation.class, TradeStation.SAVE_NAME + address); TradeStation trade = (TradeStation) world.loadItemData(TradeStation.class, TradeStation.SAVE_NAME + address);
if (trade != null) {
trade.throwIfLoadFailed();
}
// Only existing and valid mail orders are returned // Only existing and valid mail orders are returned
if (trade != null && trade.isValid()) { if (trade != null && trade.isValid()) {
+18 -5
View File
@@ -16,7 +16,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil; import net.minecraft.nbt.NBTUtil;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
@@ -37,7 +36,7 @@ import forestry.mail.inventory.InventoryTradeStation;
import forestry.mail.items.EnumStampDefinition; import forestry.mail.items.EnumStampDefinition;
import forestry.plugins.PluginMail; import forestry.plugins.PluginMail;
public class TradeStation extends WorldSavedData implements ITradeStation, IInventoryAdapter { public class TradeStation extends MailSavedData implements ITradeStation, IInventoryAdapter {
public static final String SAVE_NAME = "TradePO_"; public static final String SAVE_NAME = "TradePO_";
public static final short SLOT_TRADEGOOD = 0; public static final short SLOT_TRADEGOOD = 0;
@@ -85,7 +84,7 @@ public class TradeStation extends WorldSavedData implements ITradeStation, IInve
// / SAVING & LOADING // / SAVING & LOADING
@Override @Override
public void readFromNBT(NBTTagCompound nbttagcompound) { protected void readData(NBTTagCompound nbttagcompound) {
if (nbttagcompound.hasKey("owner")) { if (nbttagcompound.hasKey("owner")) {
owner = NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("owner")); owner = NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("owner"));
} }
@@ -100,7 +99,7 @@ public class TradeStation extends WorldSavedData implements ITradeStation, IInve
} }
@Override @Override
public void writeToNBT(NBTTagCompound nbttagcompound) { protected void writeData(NBTTagCompound nbttagcompound) {
if (owner != null) { if (owner != null) {
NBTTagCompound nbt = new NBTTagCompound(); NBTTagCompound nbt = new NBTTagCompound();
NBTUtil.func_152460_a(nbt, owner); NBTUtil.func_152460_a(nbt, owner);
@@ -172,9 +171,10 @@ public class TradeStation extends WorldSavedData implements ITradeStation, IInve
@Override @Override
public IPostalState handleLetter(World world, IMailAddress recipient, ItemStack letterstack, boolean doLodge) { 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); ILetter letter = PostManager.postRegistry.getLetter(letterstack);
loadTradeTargets(world, letter, doLodge);
if (!isVirtual() && !hasPaper(sendOwnerNotice ? 2 : 1)) { if (!isVirtual() && !hasPaper(sendOwnerNotice ? 2 : 1)) {
return EnumStationState.INSUFFICIENT_PAPER; return EnumStationState.INSUFFICIENT_PAPER;
@@ -310,6 +310,19 @@ public class TradeStation extends WorldSavedData implements ITradeStation, IInve
return EnumDeliveryState.OK; 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 */ /* TRADE LOGIC */
private int countFillableOrders(int max, ItemStack tradegood) { private int countFillableOrders(int max, ItemStack tradegood) {