@Override
public IPostalState handleLetter(World world, IMailAddress recipient, ItemStack letterstack, boolean doLodge) {
boolean sendOwnerNotice = doLodge && owner != null;
ILetter letter = PostManager.postRegistry.getLetter(letterstack);
if (!isVirtual() && !hasPaper(sendOwnerNotice ? 2 : 1))
return EnumStationState.INSUFFICIENT_PAPER;
int ordersToFill = StackUtils.containsSets(inventory.getStacks(SLOT_EXCHANGE_1, SLOT_EXCHANGE_COUNT), letter.getAttachments());
// Not a single match.
if (ordersToFill <= 0)
return EnumStationState.INSUFFICIENT_OFFER;
if (!isVirtual()) {
int fillable = countFillableOrders(ordersToFill, inventory.getStackInSlot(SLOT_TRADEGOOD));
// Nothing can be filled.
if (fillable <= 0)
return EnumStationState.INSUFFICIENT_TRADE_GOOD;
if (fillable < ordersToFill)
ordersToFill = fillable;
// Check for sufficient output buffer
int storable = countStorablePayment(ordersToFill, inventory.getStacks(SLOT_EXCHANGE_1, SLOT_EXCHANGE_COUNT));
if (storable <= 0)
return EnumStationState.INSUFFICIENT_BUFFER;
if (storable < ordersToFill)
ordersToFill = storable;
}
// Prepare the letter
ILetter mail = new Letter(this.address, letter.getSender());
mail.setText("Please find your order attached.");
for (int i = 0; i < ordersToFill; i++) {
mail.addAttachment(inventory.getStackInSlot(SLOT_TRADEGOOD).copy());
}
mail.addAttachments(getSurplusAttachments(ordersToFill, letter.getAttachments()));
// Check for necessary postage
int requiredPostage = mail.requiredPostage();
if (!isVirtual())
if (!canPayPostage(requiredPostage + (sendOwnerNotice ? 1 : 0)))
return EnumStationState.INSUFFICIENT_STAMPS;
// Attach necessary postage
int[] stampCount = getPostage(requiredPostage, isVirtual());
for (int i = 0; i < stampCount.length; i++) {
if (stampCount[i] > 0)
mail.addStamps(ForestryItem.stamps.getItemStack(stampCount[i], EnumPostage.values()[i].ordinal() - 1));
}
// Send the letter
NBTTagCompound nbttagcompound = new NBTTagCompound();
mail.writeToNBT(nbttagcompound);
ItemStack mailstack = ForestryItem.letters.getItemStack(1, ItemLetter.encodeMeta(1, ItemLetter.getType(mail)));
mailstack.setTagCompound(nbttagcompound);
IPostalState responseState = PostManager.postRegistry.getPostOffice(world).lodgeLetter(world, mailstack, doLodge);
if (!responseState.isOk()) {
return EnumDeliveryState.RESPONSE_NOT_MAILABLE;
}
// Store received items
for (int i = 0; i < ordersToFill; i++) {
for (ItemStack stack : inventory.getStacks(SLOT_EXCHANGE_1, SLOT_EXCHANGE_COUNT)) {
if (stack == null)
continue;
inventory.tryAddStack(stack.copy(), SLOT_RECEIVE_BUFFER, SLOT_RECEIVE_BUFFER_COUNT, false);
}
}
// Remove resources
removePaper();
removeStamps(stampCount);
removeTradegood(ordersToFill);
// Send confirmation message to seller
if (sendOwnerNotice) {
nbttagcompound = new NBTTagCompound();
ILetter confirm = new Letter(this.address, new MailAddress(this.owner));
confirm.setText(ordersToFill + " order(s) from " + letter.getSender().getName() + " were filled.");
confirm.addStamps(ForestryItem.stamps.getItemStack(1, EnumPostage.P_1.ordinal() - 1));
confirm.writeToNBT(nbttagcompound);
ItemStack confirmstack = ForestryItem.letters.getItemStack(1, ItemLetter.encodeMeta(1, ItemLetter.getType(confirm)));
confirmstack.setTagCompound(nbttagcompound);
PostManager.postRegistry.getPostOffice(world).lodgeLetter(world, confirmstack, doLodge);