Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

822 lines
31 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects;
import ch.claude_martin.enumbitset.EnumBitSet;
import engine.Enum;
import engine.gameManager.BuildingManager;
10 months ago
import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.ClientConnection;
import engine.net.client.msg.*;
import engine.server.MBServerStatics;
import org.joda.time.DateTime;
import org.pmw.tinylog.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.concurrent.ConcurrentHashMap;
public class Warehouse extends AbstractWorldObject {
10 months ago
public static ConcurrentHashMap<Integer, Warehouse> warehouseByBuildingUUID = new ConcurrentHashMap<>();
public EnumBitSet<Enum.ResourceType> lockedResourceTypes;
10 months ago
public int UID;
public int buildingUID;
public ArrayList<Transaction> transactions = new ArrayList<>();
public ConcurrentHashMap<Enum.ResourceType, Integer> resources = new ConcurrentHashMap<>();
/**
* ResultSet Constructor
*/
public Warehouse(ResultSet rs) throws SQLException {
super(rs);
this.UID = rs.getInt("UID");
this.resources.put(Enum.ResourceType.STONE, rs.getInt("warehouse_stone"));
this.resources.put(Enum.ResourceType.TRUESTEEL, rs.getInt("warehouse_truesteel"));
this.resources.put(Enum.ResourceType.IRON, rs.getInt("warehouse_iron"));
this.resources.put(Enum.ResourceType.ADAMANT, rs.getInt("warehouse_adamant"));
this.resources.put(Enum.ResourceType.LUMBER, rs.getInt("warehouse_lumber"));
this.resources.put(Enum.ResourceType.OAK, rs.getInt("warehouse_oak"));
this.resources.put(Enum.ResourceType.BRONZEWOOD, rs.getInt("warehouse_bronzewood"));
this.resources.put(Enum.ResourceType.MANDRAKE, rs.getInt("warehouse_mandrake"));
this.resources.put(Enum.ResourceType.COAL, rs.getInt("warehouse_coal"));
this.resources.put(Enum.ResourceType.AGATE, rs.getInt("warehouse_agate"));
this.resources.put(Enum.ResourceType.DIAMOND, rs.getInt("warehouse_diamond"));
this.resources.put(Enum.ResourceType.ONYX, rs.getInt("warehouse_onyx"));
this.resources.put(Enum.ResourceType.AZOTH, rs.getInt("warehouse_azoth"));
this.resources.put(Enum.ResourceType.ORICHALK, rs.getInt("warehouse_orichalk"));
this.resources.put(Enum.ResourceType.ANTIMONY, rs.getInt("warehouse_antimony"));
this.resources.put(Enum.ResourceType.SULFUR, rs.getInt("warehouse_sulfur"));
this.resources.put(Enum.ResourceType.QUICKSILVER, rs.getInt("warehouse_quicksilver"));
this.resources.put(Enum.ResourceType.GALVOR, rs.getInt("warehouse_galvor"));
this.resources.put(Enum.ResourceType.WORMWOOD, rs.getInt("warehouse_wormwood"));
this.resources.put(Enum.ResourceType.OBSIDIAN, rs.getInt("warehouse_obsidian"));
this.resources.put(Enum.ResourceType.BLOODSTONE, rs.getInt("warehouse_bloodstone"));
this.resources.put(Enum.ResourceType.MITHRIL, rs.getInt("warehouse_mithril"));
this.resources.put(Enum.ResourceType.GOLD, rs.getInt("warehouse_gold"));
this.lockedResourceTypes = EnumBitSet.asEnumBitSet(rs.getLong("warehouse_locks"), Enum.ResourceType.class);
this.buildingUID = rs.getInt("parent");
10 months ago
warehouseByBuildingUUID.put(this.buildingUID, this);
}
public static void warehouseDeposit(MerchantMsg msg, PlayerCharacter player, NPC npc) {
Building warehouseBuilding;
Warehouse warehouse;
int depositAmount;
Dispatch dispatch;
Item resource = Item.getFromCache(msg.getItemID());
if (resource == null)
return;
depositAmount = msg.getAmount();
CharacterItemManager itemMan = player.getCharItemManager();
if (!itemMan.doesCharOwnThisItem(resource.getObjectUUID()))
return;
warehouseBuilding = npc.getBuilding();
if (warehouseBuilding == null)
return;
warehouse = warehouseByBuildingUUID.get(warehouseBuilding.getObjectUUID());
if (warehouse == null)
return;
if (!deposit(player, resource, depositAmount, true, true,warehouse)) {
return;
}
ViewResourcesMessage vrm = new ViewResourcesMessage(player);
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
public static void warehouseWithdraw(MerchantMsg msg, PlayerCharacter player, NPC npc) {
int withdrawAmount;
Building warehouseBuilding;
Warehouse warehouse;
Dispatch dispatch;
withdrawAmount = msg.getAmount();
warehouseBuilding = npc.getBuilding();
if (warehouseBuilding == null)
return;
if (player.getGuild() != warehouseBuilding.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus()))
return;
warehouse = warehouseByBuildingUUID.get(warehouseBuilding.getObjectUUID());
if (warehouse == null)
return;
Enum.ResourceType resourceType = Enum.ResourceType.hashLookup.get(msg.getHashID());
10 months ago
if (isResourceLocked(warehouse, resourceType)) {
10 months ago
ChatManager.chatSystemInfo(player, "You cannot withdrawl a locked resource.");
return;
}
if (!withdraw(warehouse, player, resourceType, withdrawAmount, true, true)) {
ChatManager.chatGuildError(player, "Failed to withdrawl " + resourceType.name() + '.');
Logger.debug(player.getName() + " Failed to withdrawl =" + resourceType.name() + " from Warehouse With ID = " + warehouseBuilding.getObjectUUID());
10 months ago
return;
}
ViewResourcesMessage vrm = new ViewResourcesMessage(player);
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
public static void warehouseLock(MerchantMsg msg, PlayerCharacter player, NPC npc) {
Building warehouse;
int hashID;
Dispatch dispatch;
hashID = msg.getHashID();
warehouse = npc.getBuilding();
if (warehouse == null)
return;
if (player.getGuild() != warehouse.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus()))
return;
Warehouse wh = warehouseByBuildingUUID.get(warehouse.getObjectUUID());
if (wh == null)
return;
Enum.ResourceType resourceType = Enum.ResourceType.hashLookup.get(hashID);
10 months ago
if (isResourceLocked(wh, resourceType)) {
10 months ago
boolean worked;
EnumBitSet<Enum.ResourceType> bitSet = EnumBitSet.asEnumBitSet(wh.lockedResourceTypes.toLong(), Enum.ResourceType.class);
bitSet.remove(Enum.ResourceType.resourceLookup.get(ItemBase.getItemBase(resourceType.templateID)));
10 months ago
worked = DbManager.WarehouseQueries.updateLocks(wh, bitSet.toLong());
if (worked) {
wh.lockedResourceTypes.remove(Enum.ResourceType.resourceLookup.get(ItemBase.getItemBase(resourceType.templateID)));
10 months ago
ViewResourcesMessage vrm = new ViewResourcesMessage(player);
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouse);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
return;
}
EnumBitSet<Enum.ResourceType> bitSet = EnumBitSet.asEnumBitSet(wh.lockedResourceTypes.toLong(), Enum.ResourceType.class);
bitSet.add(Enum.ResourceType.resourceLookup.get(ItemBase.getItemBase(resourceType.templateID)));
10 months ago
if (!DbManager.WarehouseQueries.updateLocks(wh, bitSet.toLong()))
return;
wh.lockedResourceTypes.add(Enum.ResourceType.resourceLookup.get(ItemBase.getItemBase(resourceType.templateID)));
10 months ago
ViewResourcesMessage vrm = new ViewResourcesMessage(player);
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouse);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
public static synchronized boolean deposit(PlayerCharacter pc, Item resource, int amount, boolean removeFromInventory, boolean transaction, Warehouse warehouse) {
ClientConnection origin = pc.getClientConnection();
10 months ago
if (origin == null)
return false;
if (amount < 0) {
Logger.info(pc.getFirstName() + " Attempting to Dupe!!!!!!");
return false;
}
Enum.ResourceType resourceType = Enum.ResourceType.resourceLookup.get(resource.templateID);
10 months ago
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
CharacterItemManager itemMan = pc.getCharItemManager();
if (itemMan == null)
return false;
if (itemMan.getGoldTrading() > 0) {
ErrorPopupMsg.sendErrorPopup(pc, 195);
return false;
}
if (!itemMan.doesCharOwnThisItem(resource.getObjectUUID()))
return false;
if (!resource.validForInventory(origin, pc, itemMan))
return false;
if (resource.getNumOfItems() < amount)
return false;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > Enum.ResourceType.resourceLookup.get(resource.templateID).deposit_limit) {
10 months ago
//ChatManager.chatSystemInfo(pc, "The Warehouse is at it's maximum for this type of resource.");
return false;
}
if (removeFromInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
if (itemMan.getGoldInventory().getNumOfItems() - amount < 0)
return false;
if (itemMan.getGoldInventory().getNumOfItems() - amount > MBServerStatics.PLAYER_GOLD_LIMIT)
return false;
if (!itemMan.modifyInventoryGold(-amount)) {
//ChatManager.chatSystemError(pc, "You do not have this Gold.");
return false;
}
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
itemMan.delete(resource);
itemMan.updateInventory();
}
}
itemMan.updateInventory();
if (!DepositApproved(resourceType, amount, warehouse))
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (resource.template.item_type.equals(Enum.ItemType.GOLD))
resourceType = Enum.ResourceType.GOLD;
10 months ago
else
resourceType = Enum.ResourceType.valueOf(ItemTemplate.itemTemplates.get(resource.getTemplateID()).item_base_name.toUpperCase());
10 months ago
if (transaction)
AddTransactionToWarehouse(warehouse, pc.getObjectType(), pc.getObjectUUID(), Enum.TransactionType.DEPOSIT, resourceType, amount);
return true;
}
public static synchronized boolean depositFromMine(Mine mine, Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > resourceType.deposit_limit)
10 months ago
return false;
if (!DepositApproved(resourceType, amount, warehouse))
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (mine != null)
AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, mine.getBuildingID(), Enum.TransactionType.MINE, resourceType, amount);
return true;
}
public static boolean DepositApproved(Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > resourceType.deposit_limit)
10 months ago
return false;
boolean worked = false;
switch (resourceType) {
case GOLD:
10 months ago
worked = DbManager.WarehouseQueries.updateGold(warehouse, newAmount);
break;
case STONE:
10 months ago
worked = DbManager.WarehouseQueries.updateStone(warehouse, newAmount);
break;
case TRUESTEEL:
10 months ago
worked = DbManager.WarehouseQueries.updateTruesteel(warehouse, newAmount);
break;
case IRON:
10 months ago
worked = DbManager.WarehouseQueries.updateIron(warehouse, newAmount);
break;
case ADAMANT:
10 months ago
worked = DbManager.WarehouseQueries.updateAdamant(warehouse, newAmount);
break;
case LUMBER:
10 months ago
worked = DbManager.WarehouseQueries.updateLumber(warehouse, newAmount);
break;
case OAK:
10 months ago
worked = DbManager.WarehouseQueries.updateOak(warehouse, newAmount);
break;
case BRONZEWOOD:
10 months ago
worked = DbManager.WarehouseQueries.updateBronzewood(warehouse, newAmount);
break;
case MANDRAKE:
10 months ago
worked = DbManager.WarehouseQueries.updateMandrake(warehouse, newAmount);
break;
case COAL:
10 months ago
worked = DbManager.WarehouseQueries.updateCoal(warehouse, newAmount);
break;
case AGATE:
10 months ago
worked = DbManager.WarehouseQueries.updateAgate(warehouse, newAmount);
break;
case DIAMOND:
10 months ago
worked = DbManager.WarehouseQueries.updateDiamond(warehouse, newAmount);
break;
case ONYX:
10 months ago
worked = DbManager.WarehouseQueries.updateOnyx(warehouse, newAmount);
break;
case AZOTH:
10 months ago
worked = DbManager.WarehouseQueries.updateAzoth(warehouse, newAmount);
break;
case ORICHALK:
10 months ago
worked = DbManager.WarehouseQueries.updateOrichalk(warehouse, newAmount);
break;
case ANTIMONY:
10 months ago
worked = DbManager.WarehouseQueries.updateAntimony(warehouse, newAmount);
break;
case SULFUR:
10 months ago
worked = DbManager.WarehouseQueries.updateSulfur(warehouse, newAmount);
break;
case QUICKSILVER:
10 months ago
worked = DbManager.WarehouseQueries.updateQuicksilver(warehouse, newAmount);
break;
case GALVOR:
10 months ago
worked = DbManager.WarehouseQueries.updateGalvor(warehouse, newAmount);
break;
case WORMWOOD:
10 months ago
worked = DbManager.WarehouseQueries.updateWormwood(warehouse, newAmount);
break;
case OBSIDIAN:
10 months ago
worked = DbManager.WarehouseQueries.updateObsidian(warehouse, newAmount);
break;
case BLOODSTONE:
10 months ago
worked = DbManager.WarehouseQueries.updateBloodstone(warehouse, newAmount);
break;
case MITHRIL:
10 months ago
worked = DbManager.WarehouseQueries.updateMithril(warehouse, newAmount);
break;
}
return worked;
}
public static synchronized void depositRealmTaxes(PlayerCharacter taxer, Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
if (!DepositApproved(resourceType, amount, warehouse))
10 months ago
return;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
warehouse.resources.put(resourceType, newAmount);
10 months ago
AddTransactionToWarehouse(warehouse, taxer.getObjectType(), taxer.getObjectUUID(), Enum.TransactionType.TAXRESOURCEDEPOSIT, resourceType, amount);
}
public static synchronized void depositProfitTax(Enum.ResourceType resourceType, int amount, Building building, Warehouse warehouse) {
if (warehouse.resources.get(resourceType) == null)
10 months ago
return;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > resourceType.deposit_limit)
10 months ago
return;
if (!DepositApproved(resourceType, amount, warehouse))
10 months ago
return;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (building != null)
AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.DEPOSIT, resourceType, amount);
}
public static boolean WithdrawApproved(Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
return false;
int newAmount = oldAmount - amount;
boolean worked = false;
switch (resourceType) {
case GOLD:
10 months ago
worked = DbManager.WarehouseQueries.updateGold(warehouse, newAmount);
break;
case STONE:
10 months ago
worked = DbManager.WarehouseQueries.updateStone(warehouse, newAmount);
break;
case TRUESTEEL:
10 months ago
worked = DbManager.WarehouseQueries.updateTruesteel(warehouse, newAmount);
break;
case IRON:
10 months ago
worked = DbManager.WarehouseQueries.updateIron(warehouse, newAmount);
break;
case ADAMANT:
10 months ago
worked = DbManager.WarehouseQueries.updateAdamant(warehouse, newAmount);
break;
case LUMBER:
10 months ago
worked = DbManager.WarehouseQueries.updateLumber(warehouse, newAmount);
break;
case OAK:
10 months ago
worked = DbManager.WarehouseQueries.updateOak(warehouse, newAmount);
break;
case BRONZEWOOD:
10 months ago
worked = DbManager.WarehouseQueries.updateBronzewood(warehouse, newAmount);
break;
case MANDRAKE:
10 months ago
worked = DbManager.WarehouseQueries.updateMandrake(warehouse, newAmount);
break;
case COAL:
10 months ago
worked = DbManager.WarehouseQueries.updateCoal(warehouse, newAmount);
break;
case AGATE:
10 months ago
worked = DbManager.WarehouseQueries.updateAgate(warehouse, newAmount);
break;
case DIAMOND:
10 months ago
worked = DbManager.WarehouseQueries.updateDiamond(warehouse, newAmount);
break;
case ONYX:
10 months ago
worked = DbManager.WarehouseQueries.updateOnyx(warehouse, newAmount);
break;
case AZOTH:
10 months ago
worked = DbManager.WarehouseQueries.updateAzoth(warehouse, newAmount);
break;
case ORICHALK:
10 months ago
worked = DbManager.WarehouseQueries.updateOrichalk(warehouse, newAmount);
break;
case ANTIMONY:
10 months ago
worked = DbManager.WarehouseQueries.updateAntimony(warehouse, newAmount);
break;
case SULFUR:
10 months ago
worked = DbManager.WarehouseQueries.updateSulfur(warehouse, newAmount);
break;
case QUICKSILVER:
10 months ago
worked = DbManager.WarehouseQueries.updateQuicksilver(warehouse, newAmount);
break;
case GALVOR:
10 months ago
worked = DbManager.WarehouseQueries.updateGalvor(warehouse, newAmount);
break;
case WORMWOOD:
10 months ago
worked = DbManager.WarehouseQueries.updateWormwood(warehouse, newAmount);
break;
case OBSIDIAN:
10 months ago
worked = DbManager.WarehouseQueries.updateObsidian(warehouse, newAmount);
break;
case BLOODSTONE:
10 months ago
worked = DbManager.WarehouseQueries.updateBloodstone(warehouse, newAmount);
break;
case MITHRIL:
10 months ago
worked = DbManager.WarehouseQueries.updateMithril(warehouse, newAmount);
break;
}
return worked;
}
public static synchronized boolean withdraw(Warehouse warehouse, NPC npc, Enum.ResourceType resourceType, int amount, boolean transaction) {
10 months ago
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount - amount;
if (!WithdrawApproved(resourceType, amount, warehouse))
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (transaction)
AddTransactionToWarehouse(warehouse, npc.getObjectType(), npc.getObjectUUID(), Enum.TransactionType.WITHDRAWL, resourceType, amount);
return true;
}
public static synchronized void transferResources(Warehouse warehouse, PlayerCharacter taxer, TaxResourcesMsg msg, ArrayList<Enum.ResourceType> realmResources, float taxPercent) {
10 months ago
for (Enum.ResourceType resourceType : realmResources) {
if (warehouse.resources.get(resourceType) == null)
10 months ago
return;
int amount = (int) (warehouse.resources.get(resourceType) * taxPercent);
10 months ago
if (amount <= 0) {
msg.getResources().put(resourceType.hash, 0);
10 months ago
continue;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
amount = oldAmount;
int newAmount = oldAmount - amount;
if (newAmount < amount)
continue;
if (!WithdrawApproved(resourceType, amount, warehouse)) {
msg.getResources().put(resourceType.hash, 0);
10 months ago
continue;
}
msg.getResources().put(resourceType.hash, amount);
10 months ago
warehouse.resources.put(resourceType, newAmount);
depositRealmTaxes(taxer, resourceType, amount, warehouse);
Enum.ResourceType resource;
10 months ago
AddTransactionToWarehouse(warehouse, taxer.getObjectType(), taxer.getObjectUUID(), Enum.TransactionType.TAXRESOURCE, resourceType, amount);
10 months ago
}
}
public static synchronized boolean withdraw(Warehouse warehouse, PlayerCharacter pc, Enum.ResourceType resourceType, int amount, boolean addToInventory, boolean transaction) {
10 months ago
if (pc == null)
return false;
ItemTemplate template = ItemTemplate.itemTemplates.get(resourceType.templateID);
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
CharacterItemManager itemMan = pc.getCharItemManager();
if (itemMan == null)
return false;
if (addToInventory)
8 months ago
if (!itemMan.hasRoomInventory(template.item_wt * amount)) {
10 months ago
ChatManager.chatSystemInfo(pc, "You can not carry any more of that item.");
return false;
}
if (addToInventory && resourceType.equals(Enum.ResourceType.GOLD)) {
if (pc.getCharItemManager().getGoldInventory().getNumOfItems() + amount > MBServerStatics.PLAYER_GOLD_LIMIT)
10 months ago
return false;
if (pc.getCharItemManager().getGoldInventory().getNumOfItems() + amount < 0)
return false;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
return false;
int newAmount = oldAmount - amount;
if (!WithdrawApproved(resourceType, amount, warehouse))
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (addToInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
itemMan.addGoldToInventory(amount, false);
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
boolean itemWorked = false;
Item item = new Item(resourceType.templateID);
item.ownerID = pc.getObjectUUID();
item.ownerType = Enum.OwnerType.PlayerCharacter;
10 months ago
item.containerType = Enum.ItemContainerType.INVENTORY;
item.numberOfItems = amount;
10 months ago
try {
item = DbManager.ItemQueries.PERSIST(item);
10 months ago
itemWorked = true;
} catch (Exception e) {
Logger.error(e);
}
if (itemWorked) {
itemMan.addItemToInventory(item);
itemMan.updateInventory();
}
}
}
if (transaction)
AddTransactionToWarehouse(warehouse, pc.getObjectType(), pc.getObjectUUID(), Enum.TransactionType.WITHDRAWL, resourceType, amount);
return true;
}
public static synchronized boolean loot(Warehouse warehouse, PlayerCharacter pc, Enum.ResourceType resourceType, int amount, boolean addToInventory) {
10 months ago
if (pc == null)
return false;
ItemTemplate template = ItemTemplate.itemTemplates.get(resourceType);
if (template == null)
10 months ago
return false;
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
CharacterItemManager itemMan = pc.getCharItemManager();
if (itemMan == null)
return false;
if (!itemMan.hasRoomInventory(template.item_wt)) {
10 months ago
ChatManager.chatSystemInfo(pc, "You can not carry any more of that item.");
return false;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
return false;
int newAmount = oldAmount - amount;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (addToInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
itemMan.addGoldToInventory(amount, false);
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
boolean itemWorked = false;
Item item = new Item(resourceType.templateID);
item.ownerID = pc.getObjectUUID();
item.ownerType = Enum.OwnerType.PlayerCharacter;
10 months ago
item.containerType = Enum.ItemContainerType.INVENTORY;
item.numberOfItems = amount;
10 months ago
try {
item = DbManager.ItemQueries.PERSIST(item);
10 months ago
itemWorked = true;
} catch (Exception e) {
Logger.error(e);
}
if (itemWorked) {
itemMan.addItemToInventory(item);
itemMan.updateInventory();
}
}
}
return true;
}
public static boolean isEmpty(Warehouse warehouse) {
int amount = 0;
for (Enum.ResourceType resourceType : EnumSet.allOf(Enum.ResourceType.class)) {
amount += warehouse.resources.get(resourceType);
10 months ago
if (amount > 0)
return false;
}
return true;
}
public static void loadAllTransactions(Warehouse warehouse) {
warehouse.transactions = DbManager.WarehouseQueries.GET_TRANSACTIONS_FOR_WAREHOUSE(warehouse.buildingUID);
}
public static void AddTransactionToWarehouse(Warehouse warehouse, Enum.GameObjectType targetType, int targetUUID, Enum.TransactionType transactionType, Enum.ResourceType resource, int amount) {
10 months ago
if (!DbManager.WarehouseQueries.CREATE_TRANSACTION(warehouse.buildingUID, targetType, targetUUID, transactionType, resource, amount, DateTime.now()))
return;
Transaction transaction = new Transaction(warehouse.buildingUID, targetType, targetUUID, transactionType, resource, amount, DateTime.now());
warehouse.transactions.add(transaction);
}
public static boolean isAboveCap(Warehouse warehouse, Enum.ResourceType resourceType, int deposit) {
int newAmount = warehouse.resources.get(resourceType) + deposit;
return newAmount > resourceType.deposit_limit;
10 months ago
}
public static boolean isResourceLocked(Warehouse warehouse, Enum.ResourceType resourceType) {
10 months ago
return resourceType.elementOf(warehouse.lockedResourceTypes);
}
@Override
public void updateDatabase() {
// TODO Auto-generated method stub
}
@Override
public void runAfterLoad() {
try {
Building warehouseBuilding = BuildingManager.getBuilding(this.buildingUID);
Logger.info("configuring warehouse " + UID + " for city " + warehouseBuilding.getCity().getCityName() + " structure UUID " + this.buildingUID);
//Building is gone, but Warehouse still in DB?? Should never happen, sanity check anyway.
if (warehouseBuilding == null) {
Logger.error("Failed to load Building for Warehouse");
return;
}
Zone cityZone = warehouseBuilding.getParentZone();
if (cityZone == null) {
Logger.error("Failed to load Zone for Warehouse with UUID " + this.getObjectUUID());
return;
}
City city = City.getCity(cityZone.playerCityUUID);
if (city == null) {
Logger.error("Failed to load City for Warehouse with UUID " + this.getObjectUUID());
return;
}
10 months ago
warehouseByBuildingUUID.put(this.buildingUID, this);
city.setWarehouseBuildingID(this.buildingUID);
} catch (Exception E) {
Logger.info(this.getObjectUUID() + " failed");
}
}
10 months ago
}