2014-05-02 21:31:20 +02:00
|
|
|
/*******************************************************************************
|
2014-08-07 08:29:25 +02:00
|
|
|
* Copyright (c) 2011-2014 SirSengir.
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Lesser Public License v3
|
|
|
|
|
* which accompanies this distribution, and is available at
|
2014-08-07 02:56:58 -07:00
|
|
|
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
2015-01-26 22:26:21 -08:00
|
|
|
*
|
2014-08-07 08:29:25 +02:00
|
|
|
* Various Contributors including, but not limited to:
|
|
|
|
|
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
2014-05-02 21:31:20 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
package forestry.mail;
|
|
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraft.world.WorldSavedData;
|
|
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
import forestry.api.mail.EnumPostage;
|
|
|
|
|
import forestry.api.mail.ILetter;
|
2014-12-15 08:15:21 -08:00
|
|
|
import forestry.api.mail.IMailAddress;
|
2014-05-02 21:31:20 +02:00
|
|
|
import forestry.api.mail.IPostOffice;
|
|
|
|
|
import forestry.api.mail.IPostalCarrier;
|
|
|
|
|
import forestry.api.mail.IPostalState;
|
|
|
|
|
import forestry.api.mail.IStamps;
|
|
|
|
|
import forestry.api.mail.ITradeStation;
|
|
|
|
|
import forestry.api.mail.PostManager;
|
|
|
|
|
import forestry.core.config.ForestryItem;
|
|
|
|
|
|
|
|
|
|
public class PostOffice extends WorldSavedData implements IPostOffice {
|
|
|
|
|
|
|
|
|
|
// / CONSTANTS
|
|
|
|
|
public static final String SAVE_NAME = "ForestryMail";
|
2014-07-12 06:43:42 +02:00
|
|
|
private final int[] collectedPostage = new int[EnumPostage.values().length];
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-08-20 23:32:04 +02:00
|
|
|
// CONSTRUCTORS
|
|
|
|
|
public PostOffice() {
|
2014-05-02 21:31:20 +02:00
|
|
|
super(SAVE_NAME);
|
2014-08-20 23:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PostOffice(String s) {
|
|
|
|
|
super(s);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-02 19:15:33 -07:00
|
|
|
public void setWorld(World world) {
|
|
|
|
|
refreshActiveTradeStations(world);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
@Override
|
|
|
|
|
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
for (int i = 0; i < collectedPostage.length; i++) {
|
2015-01-26 22:26:21 -08:00
|
|
|
if (nbttagcompound.hasKey("CPS" + i)) {
|
2014-05-02 21:31:20 +02:00
|
|
|
collectedPostage[i] = nbttagcompound.getInteger("CPS" + i);
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void writeToNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
for (int i = 0; i < collectedPostage.length; i++) {
|
|
|
|
|
nbttagcompound.setInteger("CPS" + i, collectedPostage[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TRADE STATION MANAGMENT */
|
2014-09-10 03:43:16 -07:00
|
|
|
private LinkedHashMap<IMailAddress, ITradeStation> activeTradeStations;
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
@Override
|
2014-09-10 03:43:16 -07:00
|
|
|
public LinkedHashMap<IMailAddress, ITradeStation> getActiveTradeStations(World world) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return this.activeTradeStations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void refreshActiveTradeStations(World world) {
|
2014-09-10 03:43:16 -07:00
|
|
|
activeTradeStations = new LinkedHashMap<IMailAddress, ITradeStation>();
|
2015-01-26 22:26:21 -08:00
|
|
|
if (world == null || world.getSaveHandler() == null) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
File worldSave = world.getSaveHandler().getMapFileFromName("dummy");
|
2015-01-26 22:26:21 -08:00
|
|
|
if (worldSave == null) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
File file = worldSave.getParentFile();
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!file.exists() || !file.isDirectory()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
for (String str : file.list()) {
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!str.startsWith(TradeStation.SAVE_NAME)) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
|
|
|
|
if (!str.endsWith(".dat")) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-08-11 00:31:08 -07:00
|
|
|
MailAddress address = new MailAddress(str.replace(TradeStation.SAVE_NAME, "").replace(".dat", ""));
|
|
|
|
|
ITradeStation trade = PostManager.postRegistry.getTradeStation(world, address);
|
2015-01-26 22:26:21 -08:00
|
|
|
if (trade == null) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
registerTradeStation(trade);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void registerTradeStation(ITradeStation trade) {
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!activeTradeStations.containsKey(trade.getAddress())) {
|
2014-08-11 00:31:08 -07:00
|
|
|
activeTradeStations.put(trade.getAddress(), trade);
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void deregisterTradeStation(ITradeStation trade) {
|
2014-08-11 00:31:08 -07:00
|
|
|
activeTradeStations.remove(trade.getAddress());
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// / STAMP MANAGMENT
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack getAnyStamp(int max) {
|
|
|
|
|
return getAnyStamp(EnumPostage.values(), max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack getAnyStamp(EnumPostage postage, int max) {
|
2015-01-26 22:26:21 -08:00
|
|
|
return getAnyStamp(new EnumPostage[]{postage}, max);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack getAnyStamp(EnumPostage[] postages, int max) {
|
|
|
|
|
|
|
|
|
|
for (EnumPostage postage : postages) {
|
|
|
|
|
|
2014-12-20 03:59:31 -08:00
|
|
|
int collected;
|
2015-01-26 22:26:21 -08:00
|
|
|
if (collectedPostage[postage.ordinal()] <= 0) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
if (max >= collectedPostage[postage.ordinal()]) {
|
|
|
|
|
collected = collectedPostage[postage.ordinal()];
|
|
|
|
|
collectedPostage[postage.ordinal()] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
collected = max;
|
|
|
|
|
collectedPostage[postage.ordinal()] -= max;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
if (collected > 0) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return ForestryItem.stamps.getItemStack(collected, postage.ordinal() - 1);
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// / DELIVERY
|
|
|
|
|
@Override
|
|
|
|
|
public IPostalState lodgeLetter(World world, ItemStack itemstack, boolean doLodge) {
|
|
|
|
|
ILetter letter = PostManager.postRegistry.getLetter(itemstack);
|
|
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
if (letter.isProcessed()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return EnumDeliveryState.ALREADY_MAILED;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!letter.isPostPaid()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return EnumDeliveryState.NOT_POSTPAID;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!letter.isMailable()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return EnumDeliveryState.NOT_MAILABLE;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
IPostalState state = EnumDeliveryState.NOT_MAILABLE;
|
2014-09-10 03:43:16 -07:00
|
|
|
for (IMailAddress address : letter.getRecipients()) {
|
2014-09-12 21:25:46 -07:00
|
|
|
IPostalCarrier carrier = PostManager.postRegistry.getCarrier(address.getType());
|
2015-01-26 22:26:21 -08:00
|
|
|
if (carrier == null) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-08-11 00:31:08 -07:00
|
|
|
state = carrier.deliverLetter(world, this, address, itemstack, doLodge);
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!state.isOk()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
break;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-26 22:26:21 -08:00
|
|
|
if (!state.isOk()) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return state;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
collectPostage(letter.getPostage());
|
|
|
|
|
|
|
|
|
|
markDirty();
|
|
|
|
|
return EnumDeliveryState.OK;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void collectPostage(ItemStack[] stamps) {
|
|
|
|
|
for (ItemStack stamp : stamps) {
|
2015-01-26 22:26:21 -08:00
|
|
|
if (stamp == null) {
|
2014-05-02 21:31:20 +02:00
|
|
|
continue;
|
2015-01-26 22:26:21 -08:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
if (stamp.getItem() instanceof IStamps) {
|
|
|
|
|
EnumPostage postage = ((IStamps) stamp.getItem()).getPostage(stamp);
|
|
|
|
|
collectedPostage[postage.ordinal()] += stamp.stackSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|