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.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
import forestry.api.mail.ILetter;
|
|
|
|
|
import forestry.api.mail.IPostOffice;
|
|
|
|
|
import forestry.api.mail.IPostRegistry;
|
|
|
|
|
import forestry.api.mail.IPostalCarrier;
|
|
|
|
|
import forestry.api.mail.ITradeStation;
|
|
|
|
|
import forestry.api.mail.MailAddress;
|
|
|
|
|
import forestry.api.mail.PostManager;
|
|
|
|
|
import forestry.core.config.ForestryItem;
|
|
|
|
|
import forestry.mail.items.ItemLetter;
|
|
|
|
|
import forestry.plugins.PluginMail;
|
|
|
|
|
|
|
|
|
|
public class PostRegistry implements IPostRegistry {
|
|
|
|
|
|
|
|
|
|
public static PostOffice cachedPostOffice;
|
2014-07-12 06:43:42 +02:00
|
|
|
public static HashMap<GameProfile, POBox> cachedPOBoxes = new HashMap<GameProfile, POBox>();
|
|
|
|
|
public static HashMap<GameProfile, ITradeStation> cachedTradeStations = new HashMap<GameProfile, ITradeStation>();
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param world
|
|
|
|
|
* @param username
|
|
|
|
|
* @return true if the passed username is valid for poboxes.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public boolean isValidPOBox(World world, GameProfile username) {
|
|
|
|
|
if (!username.getName().matches("^[a-zA-Z0-9]+$"))
|
2014-05-02 21:31:20 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
public static POBox getPOBox(World world, GameProfile username) {
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
if (cachedPOBoxes.containsKey(username))
|
|
|
|
|
return cachedPOBoxes.get(username);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
POBox pobox = (POBox) world.loadItemData(POBox.class, POBox.SAVE_NAME + username.getId());
|
2014-05-02 21:31:20 +02:00
|
|
|
if (pobox != null)
|
2014-07-12 06:43:42 +02:00
|
|
|
cachedPOBoxes.put(username, pobox);
|
2014-05-02 21:31:20 +02:00
|
|
|
return pobox;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
public static POBox getOrCreatePOBox(World world, GameProfile username) {
|
2014-05-02 21:31:20 +02:00
|
|
|
POBox pobox = getPOBox(world, username);
|
|
|
|
|
|
|
|
|
|
if (pobox == null) {
|
2014-07-12 06:43:42 +02:00
|
|
|
pobox = new POBox(username, true);
|
|
|
|
|
world.setItemData(POBox.SAVE_NAME + username.getId(), pobox);
|
2014-05-02 21:31:20 +02:00
|
|
|
pobox.markDirty();
|
2014-07-12 06:43:42 +02:00
|
|
|
cachedPOBoxes.put(username, pobox);
|
2014-05-02 21:31:20 +02:00
|
|
|
PluginMail.proxy.setPOBoxInfo(world, username, pobox.getPOBoxInfo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pobox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param world
|
|
|
|
|
* @param moniker
|
|
|
|
|
* @return true if the passed moniker can be a moniker for a trade station
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public boolean isValidTradeMoniker(World world, GameProfile moniker) {
|
|
|
|
|
if (!moniker.getName().matches("^[a-zA-Z0-9]+$"))
|
2014-05-02 21:31:20 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param world
|
|
|
|
|
* @param moniker
|
|
|
|
|
* @return true if the trade moniker has not yet been used before.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public boolean isAvailableTradeMoniker(World world, GameProfile moniker) {
|
2014-05-02 21:31:20 +02:00
|
|
|
return getTradeStation(world, moniker) == null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public TradeStation getTradeStation(World world, GameProfile moniker) {
|
2014-05-02 21:31:20 +02:00
|
|
|
if (cachedTradeStations.containsKey(moniker))
|
|
|
|
|
return (TradeStation)cachedTradeStations.get(moniker);
|
|
|
|
|
|
2014-07-12 06:43:42 +02:00
|
|
|
TradeStation trade = (TradeStation) world.loadItemData(TradeStation.class, TradeStation.SAVE_NAME + moniker.getId()+"_"+moniker.getName());
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
// Only existing and valid mail orders are returned
|
|
|
|
|
if (trade != null && trade.isValid()) {
|
|
|
|
|
cachedTradeStations.put(moniker, trade);
|
|
|
|
|
getPostOffice(world).registerTradeStation(trade);
|
|
|
|
|
return trade;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public TradeStation getOrCreateTradeStation(World world, GameProfile owner, GameProfile moniker) {
|
2014-05-02 21:31:20 +02:00
|
|
|
TradeStation trade = getTradeStation(world, moniker);
|
|
|
|
|
|
|
|
|
|
if (trade == null) {
|
|
|
|
|
trade = new TradeStation(owner, moniker, true);
|
2014-07-12 06:43:42 +02:00
|
|
|
world.setItemData(TradeStation.SAVE_NAME + moniker.getId()+"_"+moniker.getName(), trade);
|
2014-05-02 21:31:20 +02:00
|
|
|
trade.markDirty();
|
|
|
|
|
cachedTradeStations.put(moniker, trade);
|
|
|
|
|
getPostOffice(world).registerTradeStation(trade);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return trade;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-07-12 06:43:42 +02:00
|
|
|
public void deleteTradeStation(World world, GameProfile moniker) {
|
|
|
|
|
TradeStation trade = getTradeStation(world, moniker);
|
2014-05-02 21:31:20 +02:00
|
|
|
if (trade == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Need to be marked as invalid since WorldSavedData seems to do some caching of its own.
|
|
|
|
|
trade.invalidate();
|
|
|
|
|
cachedTradeStations.remove(moniker);
|
|
|
|
|
getPostOffice(world).deregisterTradeStation(trade);
|
|
|
|
|
File file = world.getSaveHandler().getMapFileFromName(trade.mapName);
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IPostOffice getPostOffice(World world) {
|
|
|
|
|
if (cachedPostOffice != null)
|
|
|
|
|
return cachedPostOffice;
|
|
|
|
|
|
|
|
|
|
PostOffice office = (PostOffice) world.loadItemData(PostOffice.class, PostOffice.SAVE_NAME);
|
|
|
|
|
|
|
|
|
|
// Create office if there is none yet
|
|
|
|
|
if (office == null) {
|
|
|
|
|
office = new PostOffice();
|
|
|
|
|
world.setItemData(PostOffice.SAVE_NAME, office);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cachedPostOffice = office;
|
|
|
|
|
return office;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* CARRIER */
|
2014-07-12 06:43:42 +02:00
|
|
|
private final HashMap<String, IPostalCarrier> carriers = new HashMap<String, IPostalCarrier>();
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String, IPostalCarrier> getRegisteredCarriers() {
|
|
|
|
|
return carriers;
|
|
|
|
|
}
|
2014-07-12 06:43:42 +02:00
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
@Override
|
|
|
|
|
public void registerCarrier(IPostalCarrier carrier) {
|
|
|
|
|
carriers.put(carrier.getUID(), carrier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IPostalCarrier getCarrier(String uid) {
|
|
|
|
|
return carriers.get(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* LETTERS */
|
|
|
|
|
@Override
|
|
|
|
|
public ILetter createLetter(MailAddress sender, MailAddress recipient) {
|
|
|
|
|
return new Letter(sender, recipient);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack createLetterStack(ILetter letter) {
|
|
|
|
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
|
|
|
|
letter.writeToNBT(nbttagcompound);
|
|
|
|
|
|
|
|
|
|
ItemStack mailstack = ForestryItem.letters.getItemStack(1, ItemLetter.encodeMeta(1, ItemLetter.getType(letter)));
|
|
|
|
|
mailstack.setTagCompound(nbttagcompound);
|
2014-07-12 06:43:42 +02:00
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
return mailstack;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ILetter getLetter(ItemStack itemstack) {
|
|
|
|
|
if (itemstack == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!PostManager.postRegistry.isLetter(itemstack))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (itemstack.getTagCompound() == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return new Letter(itemstack.getTagCompound());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isLetter(ItemStack itemstack) {
|
|
|
|
|
return ForestryItem.letters.isItemEqual(itemstack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|