Files
ForestryMC/src/main/java/forestry/mail/PostOffice.java
T

202 lines
5.6 KiB
Java
Raw Normal View History

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
2014-05-02 21:31:20 +02: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;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.UUID;
2014-05-02 21:31:20 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import com.mojang.authlib.GameProfile;
2014-05-02 21:31:20 +02:00
import forestry.api.mail.EnumPostage;
import forestry.api.mail.ILetter;
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.MailAddress;
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";
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
}
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++) {
if (nbttagcompound.hasKey("CPS" + i))
collectedPostage[i] = nbttagcompound.getInteger("CPS" + i);
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
for (int i = 0; i < collectedPostage.length; i++) {
nbttagcompound.setInteger("CPS" + i, collectedPostage[i]);
}
}
/* TRADE STATION MANAGMENT */
2014-08-11 00:31:08 -07:00
private LinkedHashMap<MailAddress, ITradeStation> activeTradeStations;
2014-05-02 21:31:20 +02:00
@Override
2014-08-11 00:31:08 -07:00
public LinkedHashMap<MailAddress, ITradeStation> getActiveTradeStations(World world) {
2014-05-02 21:31:20 +02:00
return this.activeTradeStations;
}
private void refreshActiveTradeStations(World world) {
2014-08-11 00:31:08 -07:00
activeTradeStations = new LinkedHashMap<MailAddress, ITradeStation>();
2014-05-02 21:31:20 +02:00
if (world == null || world.getSaveHandler() == null)
return;
File worldSave = world.getSaveHandler().getMapFileFromName("dummy");
if (worldSave == null)
return;
File file = worldSave.getParentFile();
if (!file.exists() || !file.isDirectory())
return;
for (String str : file.list()) {
if (!str.startsWith(TradeStation.SAVE_NAME))
continue;
if (!str.endsWith(".dat"))
continue;
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);
2014-05-02 21:31:20 +02:00
if (trade == null)
continue;
registerTradeStation(trade);
}
}
@Override
public void registerTradeStation(ITradeStation trade) {
2014-08-11 00:31:08 -07:00
if (!activeTradeStations.containsKey(trade.getAddress()))
activeTradeStations.put(trade.getAddress(), trade);
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) {
return getAnyStamp(new EnumPostage[] { postage }, max);
}
@Override
public ItemStack getAnyStamp(EnumPostage[] postages, int max) {
for (EnumPostage postage : postages) {
int collected = 0;
if (collectedPostage[postage.ordinal()] <= 0)
continue;
if (max >= collectedPostage[postage.ordinal()]) {
collected = collectedPostage[postage.ordinal()];
collectedPostage[postage.ordinal()] = 0;
} else {
collected = max;
collectedPostage[postage.ordinal()] -= max;
}
if (collected > 0)
return ForestryItem.stamps.getItemStack(collected, postage.ordinal() - 1);
}
return null;
}
// / DELIVERY
@Override
public IPostalState lodgeLetter(World world, ItemStack itemstack, boolean doLodge) {
ILetter letter = PostManager.postRegistry.getLetter(itemstack);
if (letter.isProcessed())
return EnumDeliveryState.ALREADY_MAILED;
if (!letter.isPostPaid())
return EnumDeliveryState.NOT_POSTPAID;
if (!letter.isMailable())
return EnumDeliveryState.NOT_MAILABLE;
IPostalState state = EnumDeliveryState.NOT_MAILABLE;
for (MailAddress address : letter.getRecipients()) {
IPostalCarrier carrier = PostManager.postRegistry.getCarrier(address.getType());
if (carrier == null)
continue;
2014-08-11 00:31:08 -07:00
state = carrier.deliverLetter(world, this, address, itemstack, doLodge);
2014-05-02 21:31:20 +02:00
if (!state.isOk())
break;
}
/*
for (MailAddress address : letter.getRecipients())
if (address.isPlayer())
state = storeInPOBox(world, address, itemstack, doLodge);
else if (address.getType().equals(EnumAddressee.TRADER.toString()))
state = handleTradeLetter(world, address, itemstack, doLodge);
*/
if (!state.isOk())
return state;
collectPostage(letter.getPostage());
markDirty();
return EnumDeliveryState.OK;
}
@Override
public void collectPostage(ItemStack[] stamps) {
for (ItemStack stamp : stamps) {
if (stamp == null)
continue;
if (stamp.getItem() instanceof IStamps) {
EnumPostage postage = ((IStamps) stamp.getItem()).getPostage(stamp);
collectedPostage[postage.ordinal()] += stamp.stackSize;
}
}
}
}