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

225 lines
6.2 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.HashMap;
import java.util.Map;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
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-08-11 00:31:08 -07:00
public static HashMap<MailAddress, POBox> cachedPOBoxes = new HashMap<>();
public static HashMap<MailAddress, ITradeStation> cachedTradeStations = new HashMap<>();
2014-05-02 21:31:20 +02:00
/**
* @param world
2014-08-11 00:31:08 -07:00
* @param address
* @return true if the passed address is valid for poboxes.
2014-05-02 21:31:20 +02:00
*/
@Override
2014-08-11 00:31:08 -07:00
public boolean isValidPOBox(World world, MailAddress address) {
if (!address.isPlayer())
return false;
GameProfile userProfile = ((GameProfile)address.getIdentifier());
if (!userProfile.getName().matches("^[a-zA-Z0-9]+$"))
2014-05-02 21:31:20 +02:00
return false;
return true;
}
2014-08-11 00:31:08 -07:00
public static POBox getPOBox(World world, MailAddress address) {
2014-05-02 21:31:20 +02:00
2014-08-11 00:31:08 -07:00
if (cachedPOBoxes.containsKey(address))
return cachedPOBoxes.get(address);
2014-05-02 21:31:20 +02:00
2014-08-11 00:31:08 -07:00
POBox pobox = (POBox) world.loadItemData(POBox.class, POBox.SAVE_NAME + address);
2014-05-02 21:31:20 +02:00
if (pobox != null)
2014-08-11 00:31:08 -07:00
cachedPOBoxes.put(address, pobox);
2014-05-02 21:31:20 +02:00
return pobox;
}
2014-08-11 00:31:08 -07:00
public static POBox getOrCreatePOBox(World world, MailAddress address) {
POBox pobox = getPOBox(world, address);
2014-05-02 21:31:20 +02:00
if (pobox == null) {
2014-08-11 00:31:08 -07:00
pobox = new POBox(address, true);
world.setItemData(POBox.SAVE_NAME + address, pobox);
2014-05-02 21:31:20 +02:00
pobox.markDirty();
2014-08-11 00:31:08 -07:00
cachedPOBoxes.put(address, pobox);
PluginMail.proxy.setPOBoxInfo(world, address, pobox.getPOBoxInfo());
2014-05-02 21:31:20 +02:00
}
return pobox;
}
/**
* @param world
2014-08-11 00:31:08 -07:00
* @param address
* @return true if the passed address can be an address for a trade station
2014-05-02 21:31:20 +02:00
*/
@Override
2014-08-11 00:31:08 -07:00
public boolean isValidTradeAddress(World world, MailAddress address) {
if (address.isPlayer())
return false;
String moniker = (String)address.getIdentifier();
if (!moniker.matches("^[a-zA-Z0-9]+$"))
2014-05-02 21:31:20 +02:00
return false;
return true;
}
/**
* @param world
2014-08-11 00:31:08 -07:00
* @param address
2014-05-02 21:31:20 +02:00
* @return true if the trade moniker has not yet been used before.
*/
@Override
2014-08-11 00:31:08 -07:00
public boolean isAvailableTradeAddress(World world, MailAddress address) {
return getTradeStation(world, address) == null;
2014-05-02 21:31:20 +02:00
}
@Override
2014-08-11 00:31:08 -07:00
public TradeStation getTradeStation(World world, MailAddress address) {
if (cachedTradeStations.containsKey(address))
return (TradeStation)cachedTradeStations.get(address);
2014-05-02 21:31:20 +02:00
2014-08-11 00:31:08 -07:00
TradeStation trade = (TradeStation) world.loadItemData(TradeStation.class, TradeStation.SAVE_NAME + address);
2014-05-02 21:31:20 +02:00
// Only existing and valid mail orders are returned
if (trade != null && trade.isValid()) {
2014-08-11 00:31:08 -07:00
cachedTradeStations.put(address, trade);
2014-05-02 21:31:20 +02:00
getPostOffice(world).registerTradeStation(trade);
return trade;
}
return null;
}
@Override
2014-08-11 00:31:08 -07:00
public TradeStation getOrCreateTradeStation(World world, GameProfile owner, MailAddress address) {
TradeStation trade = getTradeStation(world, address);
2014-05-02 21:31:20 +02:00
if (trade == null) {
2014-08-11 00:31:08 -07:00
trade = new TradeStation(owner, address, true);
world.setItemData(TradeStation.SAVE_NAME + address, trade);
2014-05-02 21:31:20 +02:00
trade.markDirty();
2014-08-11 00:31:08 -07:00
cachedTradeStations.put(address, trade);
2014-05-02 21:31:20 +02:00
getPostOffice(world).registerTradeStation(trade);
}
return trade;
}
@Override
2014-08-11 00:31:08 -07:00
public void deleteTradeStation(World world, MailAddress address) {
TradeStation trade = getTradeStation(world, address);
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();
2014-08-11 00:31:08 -07:00
cachedTradeStations.remove(address);
2014-05-02 21:31:20 +02:00
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 */
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-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-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);
}
}