2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects ;
import engine.Enum ;
import engine.Enum.ItemContainerType ;
import engine.Enum.ItemType ;
import engine.Enum.OwnerType ;
2023-02-24 09:48:15 -05:00
import engine.gameManager.* ;
2023-08-07 12:12:52 -04:00
import engine.loot.ModTableEntry ;
import engine.loot.ModTypeTableEntry ;
2022-04-30 09:41:17 -04:00
import engine.net.ItemProductionManager ;
import engine.net.ItemQueue ;
import engine.net.client.ClientConnection ;
import engine.net.client.msg.ErrorPopupMsg ;
import engine.powers.EffectsBase ;
import engine.server.MBServerStatics ;
import org.joda.time.DateTime ;
import org.pmw.tinylog.Logger ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.ThreadLocalRandom ;
public class ItemFactory {
2024-03-03 15:06:44 -05:00
public static void fillInventory ( PlayerCharacter pc , int templateID , int count ) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( pc = = null )
return ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
int max = 20 ;
2024-03-18 10:01:29 -04:00
CharacterItemManager itemManager = pc . charItemManager ;
2024-03-03 15:06:44 -05:00
2024-03-16 07:57:17 -04:00
ItemTemplate template = ItemTemplate . templates . get ( templateID ) ;
2024-03-03 15:06:44 -05:00
2023-07-15 09:23:48 -04:00
if ( count > max )
count = max ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ClientConnection cc = pc . getClientConnection ( ) ;
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
if ( itemManager = = null | | template = = null | | cc = = null )
2023-07-15 09:23:48 -04:00
return ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
boolean worked ;
2024-03-03 15:06:44 -05:00
2023-07-15 09:23:48 -04:00
for ( int i = 0 ; i < count ; i + + ) {
worked = false ;
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
if ( ! itemManager . hasRoomInventory ( template . item_wt ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
ChatManager . chatSystemInfo ( pc , " You can not carry any more of that item. " ) ;
break ;
}
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
Item item = new Item ( templateID ) ;
2024-03-02 11:07:07 -05:00
item . ownerID = pc . getObjectUUID ( ) ;
item . ownerType = OwnerType . PlayerCharacter ;
item . containerType = ItemContainerType . INVENTORY ;
2023-07-15 09:23:48 -04:00
try {
2024-03-02 09:55:30 -05:00
item = DbManager . ItemQueries . PERSIST ( item ) ;
2023-07-15 09:23:48 -04:00
worked = true ;
} catch ( Exception e ) {
Logger . error ( e ) ;
}
if ( worked ) {
itemManager . addItemToInventory ( item ) ;
}
}
itemManager . updateInventory ( ) ;
}
2024-03-03 15:06:44 -05:00
public static Item fillForge ( NPC npc , PlayerCharacter pc , int itemsToRoll , int templateID , int pToken , int sToken , String customName ) {
2023-07-15 09:23:48 -04:00
String prefixString = " " ;
String suffixString = " " ;
if ( npc = = null )
return null ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
boolean useWarehouse = false ;
2022-04-30 09:41:17 -04:00
2024-03-16 07:57:17 -04:00
ItemTemplate template = ItemTemplate . templates . get ( templateID ) ;
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
if ( template = = null )
2023-07-15 09:23:48 -04:00
return null ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Building forge = npc . getBuilding ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( forge = = null )
return null ;
2022-04-30 09:41:17 -04:00
2024-03-18 10:01:29 -04:00
if ( ! npc . charItemManager . hasRoomInventory ( template . item_wt ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
ErrorPopupMsg . sendErrorPopup ( pc , 21 ) ;
return null ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Zone zone = npc . getBuilding ( ) . getParentZone ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( zone = = null )
return null ;
2022-04-30 09:41:17 -04:00
2023-09-20 15:53:41 -04:00
City city = City . getCity ( zone . playerCityUUID ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( city = = null )
return null ;
2024-03-15 10:01:22 -04:00
2023-07-15 09:23:48 -04:00
MobLoot ml = null ;
city . transactionLock . writeLock ( ) . lock ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
2024-03-17 09:47:00 -04:00
Warehouse cityWarehouse = city . warehouse ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( cityWarehouse ! = null & & forge . assetIsProtected ( ) )
useWarehouse = true ;
// ROLL BANE SCROLL.
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
if ( templateID > 910010 & & templateID < 910019 ) {
2024-03-15 10:01:22 -04:00
ConcurrentHashMap < Enum . ResourceType , Integer > resources = cityWarehouse . resources ;
2022-04-30 09:41:17 -04:00
2024-03-03 13:18:16 -05:00
int buildingWithdraw = BuildingManager . GetWithdrawAmountForRolling ( forge , template . item_value ) ;
int overdraft = BuildingManager . GetOverdraft ( forge , template . item_value ) ;
2024-03-03 15:06:44 -05:00
2023-07-15 09:23:48 -04:00
if ( overdraft > 0 & & ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + " " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2022-04-30 09:41:17 -04:00
2024-03-15 10:01:22 -04:00
if ( overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + " " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( overdraft > resources . get ( ItemBase . GOLD_ITEM_BASE ) ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + " " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( ! forge . transferGold ( - buildingWithdraw , false ) ) {
overdraft + = buildingWithdraw ;
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Building does not have enough gold to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
} else {
if ( overdraft > resources . get ( ItemBase . GOLD_ITEM_BASE ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( overdraft > 0 )
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GOLD , overdraft , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Create Item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2022-04-30 09:41:17 -04:00
2024-03-15 10:01:22 -04:00
ml = new MobLoot ( npc , template , false ) ;
2023-07-15 09:23:48 -04:00
ml . containerType = Enum . ItemContainerType . FORGE ;
ml . setValue ( 0 ) ;
ml . loadEnchantments ( ) ;
float time ;
float rank = npc . getBuilding ( ) . getRank ( ) - 1 ;
float rate = ( float ) ( 2 . 5 * rank ) ;
time = ( 20 - rate ) ;
time * = MBServerStatics . ONE_MINUTE ;
2024-03-15 10:35:41 -04:00
if ( ml . templateID > 910010 & & ml . templateID < 910019 ) {
rank = ml . getTemplateID ( ) - 910010 ;
2023-07-15 09:23:48 -04:00
time = rank * 60 * 60 * 3 * 1000 ;
}
// No job is submitted, as object's upgradetime field
// is used to determin whether or not an object has
// compelted rolling. The game object exists previously
// to this, not when 'compelte' is pressed.
long upgradeTime = System . currentTimeMillis ( ) + ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ;
DateTime dateTime = new DateTime ( ) ;
dateTime = dateTime . withMillis ( upgradeTime ) ;
ml . setDateToUpgrade ( upgradeTime ) ;
npc . addItemToForge ( ml ) ;
int playerID = 0 ;
if ( pc ! = null )
playerID = pc . getObjectUUID ( ) ;
2024-03-15 10:35:41 -04:00
DbManager . NPCQueries . ADD_TO_PRODUCTION_LIST ( ml . getObjectUUID ( ) , npc . getObjectUUID ( ) , ml . getTemplateID ( ) , dateTime , " " , " " , " " , false , playerID ) ;
ProducedItem pi = new ProducedItem ( ml . getObjectUUID ( ) , npc . getObjectUUID ( ) , ml . getTemplateID ( ) , dateTime , false , " " , " " , " " , playerID ) ;
2023-07-15 09:23:48 -04:00
pi . setProducedItemID ( ml . getObjectUUID ( ) ) ;
pi . setAmount ( itemsToRoll ) ;
pi . setRandom ( false ) ;
ItemQueue produced = ItemQueue . borrow ( pi , ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ) ;
ItemProductionManager . send ( produced ) ;
return ml ;
}
int galvorAmount = 0 ;
int wormwoodAmount = 0 ;
int prefixCost = 0 ;
int suffixCost = 0 ;
2024-03-15 10:01:22 -04:00
if ( template . item_type . equals ( ItemType . WEAPON ) & & template . item_skill_required . isEmpty ( ) = = false & &
template . item_skill_required . values ( ) . iterator ( ) . next ( ) = = 110 ) {
2024-03-10 14:10:51 -04:00
switch ( template . item_skill_used ) {
2023-07-15 09:23:48 -04:00
case " Bow " :
case " Crossbow " :
case " Spear " :
case " Pole Arm " :
case " Staff " :
wormwoodAmount = 20 ;
break ;
case " Axe " :
case " Dagger " :
case " Sword " :
case " Hammer " :
case " Unarmed Combat " :
2024-03-10 16:21:23 -04:00
if ( ItemTemplate . isTwoHanded ( template ) )
2023-07-15 09:23:48 -04:00
galvorAmount = 20 ;
else
galvorAmount = 10 ;
break ;
}
}
if ( galvorAmount > 0 | | wormwoodAmount > 0 )
if ( ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " This item requires resources to roll! Please make sure the forge is protected to access the warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( galvorAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GALVOR ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Galvor is locked. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( cityWarehouse . resources . get ( Enum . ResourceType . GALVOR ) < galvorAmount ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Galvor in warehouse to roll this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
if ( wormwoodAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . WORMWOOD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Wormwood is locked. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( cityWarehouse . resources . get ( Enum . ResourceType . WORMWOOD ) < wormwoodAmount ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Wormwood in warehouse to roll this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
2024-03-15 10:01:22 -04:00
ConcurrentHashMap < Enum . ResourceType , Integer > suffixResourceCosts = null ;
ConcurrentHashMap < Enum . ResourceType , Integer > prefixResourceCosts = null ;
2023-07-15 09:23:48 -04:00
EffectsBase prefix = null ;
2024-03-15 10:01:22 -04:00
2023-07-15 09:23:48 -04:00
if ( pToken ! = 0 ) {
if ( ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Forge cannot access warehouse! Check to make sure forge is protected. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
prefix = PowersManager . getEffectByToken ( pToken ) ;
if ( prefix = = null )
return null ;
EffectsBase prefixValue = PowersManager . getEffectByIDString ( prefix . getIDString ( ) + 'A' ) ;
if ( prefixValue = = null )
return null ;
2024-03-03 13:18:16 -05:00
int baseCost = template . item_value ;
2023-07-15 09:23:48 -04:00
int effectCost = ( int ) prefixValue . getValue ( ) ;
int total = baseCost * 10 + effectCost ;
prefixCost = effectCost ;
int overdraft = BuildingManager . GetOverdraft ( forge , total ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( overdraft > cityWarehouse . resources . get ( Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
prefixResourceCosts = prefix . getResourcesForEffect ( ) ;
2024-03-15 10:01:22 -04:00
for ( Enum . ResourceType ibResources : prefixResourceCosts . keySet ( ) ) {
2024-01-24 20:03:27 -06:00
int warehouseAmount = cityWarehouse . resources . get ( ibResources ) ;
2023-07-15 09:23:48 -04:00
int creationAmount = prefixResourceCosts . get ( ibResources ) ;
2024-03-15 10:01:22 -04:00
if ( warehouseAmount < creationAmount )
return null ;
2023-07-15 09:23:48 -04:00
}
}
EffectsBase suffix = null ;
2024-03-15 10:01:22 -04:00
2023-07-15 09:23:48 -04:00
if ( sToken ! = 0 ) {
if ( ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Forge cannot access warehouse! Check to make sure forge is protected. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
suffix = PowersManager . getEffectByToken ( sToken ) ;
if ( suffix = = null )
return null ;
EffectsBase suffixValue = PowersManager . getEffectByIDString ( suffix . getIDString ( ) + 'A' ) ;
if ( suffixValue = = null )
return null ;
suffixResourceCosts = suffix . getResourcesForEffect ( ) ;
2024-03-03 13:18:16 -05:00
int baseCost = template . item_value ;
2023-07-15 09:23:48 -04:00
int effectCost = ( int ) suffixValue . getValue ( ) ;
suffixCost = effectCost ;
int total = baseCost * 10 + effectCost ;
// int buildingWithdraw = Building.GetWithdrawAmountForRolling(forge, total);
int overdraft = BuildingManager . GetOverdraft ( forge , total ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-01-24 20:03:27 -06:00
if ( overdraft > cityWarehouse . resources . get ( ItemBase . GOLD_ITEM_BASE ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
for ( Enum . ResourceType ibResources : suffixResourceCosts . keySet ( ) ) {
2024-01-24 20:03:27 -06:00
int warehouseAmount = cityWarehouse . resources . get ( ibResources ) ;
2023-07-15 09:23:48 -04:00
int creationAmount = suffixResourceCosts . get ( ibResources ) ;
2024-03-15 10:01:22 -04:00
if ( warehouseAmount < creationAmount )
return null ;
2023-07-15 09:23:48 -04:00
}
}
//Check if Total suffix and prefix costs + itemCost can be withdrawn.
2024-03-15 10:01:22 -04:00
2024-03-03 13:18:16 -05:00
int costToCreate = suffixCost + prefixCost + template . item_value ;
2023-07-15 09:23:48 -04:00
int buildingWithdraw = BuildingManager . GetWithdrawAmountForRolling ( forge , costToCreate ) ;
int overdraft = BuildingManager . GetOverdraft ( forge , costToCreate ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( overdraft > 0 & & useWarehouse & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > cityWarehouse . resources . get ( Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
// if (pc != null){
// ChatManager.chatGuildInfo(pc.getGuild(), "Building withdraw = " + buildingWithdraw);
// ChatManager.chatGuildInfo(pc.getGuild(), "Warehouse overdraft withdraw = " + overdraft);
//
// ChatManager.chatGuildInfo(pc.getGuild(), "total withdraw = " + (overdraft + buildingWithdraw));
// }
if ( ! forge . transferGold ( - buildingWithdraw , false ) ) {
overdraft + = buildingWithdraw ;
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Building does not have enough gold to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
} else {
2024-01-24 20:03:27 -06:00
if ( overdraft > cityWarehouse . resources . get ( ItemBase . GOLD_ITEM_BASE ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( overdraft > 0 & & useWarehouse )
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GOLD , overdraft , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Create Item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( prefix ! = null ) {
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Cannot Resource Roll without access to the warehouse! Make sure the forge is currently protected. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
for ( Enum . ResourceType ibResources : prefixResourceCosts . keySet ( ) ) {
2023-07-15 09:23:48 -04:00
int creationAmount = prefixResourceCosts . get ( ibResources ) ;
2024-01-24 20:21:41 -06:00
if ( Warehouse . isResourceLocked ( cityWarehouse , ibResources ) = = true )
2023-07-15 09:23:48 -04:00
return null ;
2024-01-24 20:03:27 -06:00
int oldAmount = cityWarehouse . resources . get ( ibResources ) ;
2023-07-15 09:23:48 -04:00
int amount = creationAmount ;
if ( oldAmount < amount )
amount = oldAmount ;
2024-01-24 20:21:41 -06:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , ibResources , amount , true ) ) {
2023-07-15 09:23:48 -04:00
//ChatManager.chatGuildError(pc, "Failed to create Item");
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Create Item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( suffix ! = null ) {
2024-03-15 10:01:22 -04:00
for ( Enum . ResourceType ibResources : suffixResourceCosts . keySet ( ) ) {
2023-07-15 09:23:48 -04:00
int creationAmount = suffixResourceCosts . get ( ibResources ) ;
2024-01-24 20:21:41 -06:00
if ( Warehouse . isResourceLocked ( cityWarehouse , ibResources ) = = true ) {
2024-02-28 16:42:07 -05:00
ChatManager . chatSystemError ( pc , " Resource is locked! " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-01-24 20:03:27 -06:00
int oldAmount = cityWarehouse . resources . get ( ibResources ) ;
2023-07-15 09:23:48 -04:00
int amount = creationAmount ;
2024-03-15 10:01:22 -04:00
2023-07-15 09:23:48 -04:00
if ( oldAmount < amount )
amount = oldAmount ;
2024-03-15 10:01:22 -04:00
2024-01-24 20:21:41 -06:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , ibResources , amount , true ) ) {
2023-07-15 09:23:48 -04:00
//ChatManager.chatGuildError(pc, "Failed to create Item");
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Create Item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( prefix = = null & & suffix = = null ) {
2024-03-03 13:18:16 -05:00
int baseCost = template . item_value ;
2023-07-15 09:23:48 -04:00
int total = ( int ) ( baseCost + baseCost * ( float ) . 10 ) ;
buildingWithdraw = BuildingManager . GetWithdrawAmountForRolling ( forge , total ) ;
overdraft = BuildingManager . GetOverdraft ( forge , total ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > cityWarehouse . resources . get ( Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
if ( ! forge . transferGold ( - buildingWithdraw , false ) ) {
overdraft + = buildingWithdraw ;
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Building does not have enough gold to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
} else {
2024-03-15 10:01:22 -04:00
if ( overdraft > cityWarehouse . resources . get ( Enum . ResourceType . GOLD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( overdraft > 0 )
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GOLD , overdraft , true ) ) {
2023-07-15 09:23:48 -04:00
//ChatManager.chatGuildError(pc, "Failed to create Item");
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Create Item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
// ChatManager.chatGuildInfo(pc, "Gold Cost = " + total);
if ( galvorAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GALVOR , galvorAmount , true ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Failed to withdraw Galvor from warehouse! " + template . item_base_name ) ;
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
if ( wormwoodAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . WORMWOOD , wormwoodAmount , true ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Failed to withdraw Wormwood from warehouse! " + template . item_base_name ) ;
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
2024-03-15 10:01:22 -04:00
ml = new MobLoot ( npc , template , false ) ;
2023-07-15 09:23:48 -04:00
ml . containerType = Enum . ItemContainerType . FORGE ;
2024-03-16 08:56:22 -04:00
if ( customName . isEmpty ( ) = = false )
ml . name = customName ;
2023-07-15 09:23:48 -04:00
if ( prefix ! = null ) {
ml . addPermanentEnchantment ( prefix . getIDString ( ) , 0 , 0 , true ) ;
ml . setPrefix ( prefix . getIDString ( ) ) ;
prefixString = prefix . getIDString ( ) ;
}
if ( suffix ! = null ) {
ml . addPermanentEnchantment ( suffix . getIDString ( ) , 0 , 0 , false ) ;
ml . setSuffix ( suffix . getIDString ( ) ) ;
suffixString = suffix . getIDString ( ) ;
}
ml . loadEnchantments ( ) ;
//set value to 0 so magicvalue can be recalculated in getValue.
ml . setValue ( 0 ) ;
float time ;
float rank = npc . getBuilding ( ) . getRank ( ) - 1 ;
float rate = ( float ) ( 2 . 5 * rank ) ;
time = ( 20 - rate ) ;
time * = MBServerStatics . ONE_MINUTE ;
if ( ml . getItemBase ( ) . getUUID ( ) > 910010 & & ml . getItemBase ( ) . getUUID ( ) < 910019 ) {
2024-03-15 10:35:41 -04:00
rank = ml . getTemplateID ( ) - 910010 ;
2023-07-15 09:23:48 -04:00
time = rank * 60 * 60 * 3 * 1000 ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// No job is submitted, as object's upgradetime field
// is used to determin whether or not an object has
// compelted rolling. The game object exists previously
// to this, not when 'compelte' is pressed.
long upgradeTime = System . currentTimeMillis ( ) + ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
DateTime dateTime = new DateTime ( ) ;
dateTime = dateTime . withMillis ( upgradeTime ) ;
ml . setDateToUpgrade ( upgradeTime ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
npc . addItemToForge ( ml ) ;
int playerID = 0 ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
playerID = pc . getObjectUUID ( ) ;
2022-04-30 09:41:17 -04:00
2024-03-16 08:56:22 -04:00
DbManager . NPCQueries . ADD_TO_PRODUCTION_LIST ( ml . getObjectUUID ( ) , npc . getObjectUUID ( ) , ml . getTemplateID ( ) , dateTime , prefixString , suffixString , ml . name , false , playerID ) ;
ProducedItem pi = new ProducedItem ( npc . getRolling ( ) . size ( ) , npc . getObjectUUID ( ) , ml . getTemplateID ( ) , dateTime , false , prefixString , suffixString , ml . name , playerID ) ;
2023-07-15 09:23:48 -04:00
pi . setProducedItemID ( ml . getObjectUUID ( ) ) ;
pi . setAmount ( itemsToRoll ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ItemQueue produced = ItemQueue . borrow ( pi , ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ) ;
ItemProductionManager . send ( produced ) ;
} catch ( Exception e ) {
Logger . error ( e ) ;
} finally {
city . transactionLock . writeLock ( ) . unlock ( ) ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// npc.addItemToForge(item);
return ml ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-08-07 15:52:50 -04:00
public static Item randomRoll ( NPC vendor , PlayerCharacter playerCharacter , int itemsToRoll , int itemBaseID ) {
2023-07-15 09:23:48 -04:00
byte itemModTable ;
int prefixMod = 0 ;
int suffixMod = 0 ;
String prefix = " " ;
2023-08-07 12:12:52 -04:00
String suffix = " " ;
ModTableEntry prefixEntry = null ;
ModTableEntry suffixEntry = null ;
2022-04-30 09:41:17 -04:00
2023-08-07 15:52:50 -04:00
ItemBase ib = ItemBase . getItemBase ( itemBaseID ) ;
2024-03-16 07:57:17 -04:00
ItemTemplate template = ItemTemplate . templates . get ( itemBaseID ) ;
2022-04-30 09:41:17 -04:00
2024-03-03 15:06:44 -05:00
if ( template = = null )
2023-07-15 09:23:48 -04:00
return null ;
2022-04-30 09:41:17 -04:00
2024-03-18 10:01:29 -04:00
if ( ! vendor . charItemManager . hasRoomInventory ( template . item_wt ) ) {
2023-08-07 15:52:50 -04:00
if ( playerCharacter ! = null )
ChatManager . chatSystemInfo ( playerCharacter , vendor . getName ( ) + " " + vendor . getContract ( ) . getName ( ) + " Inventory is full. " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
itemModTable = ( byte ) ib . getModTable ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( ! vendor . getItemModTable ( ) . contains ( itemModTable ) ) {
2023-08-07 15:52:50 -04:00
if ( playerCharacter ! = null )
ErrorPopupMsg . sendErrorPopup ( playerCharacter , 59 ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
for ( byte temp : vendor . getItemModTable ( ) ) {
2023-08-07 15:52:50 -04:00
2023-07-15 09:23:48 -04:00
if ( itemModTable ! = temp )
continue ;
2023-08-07 15:52:50 -04:00
2023-07-15 09:23:48 -04:00
prefixMod = vendor . getModTypeTable ( ) . get ( vendor . getItemModTable ( ) . indexOf ( temp ) ) ;
suffixMod = vendor . getModSuffixTable ( ) . get ( vendor . getItemModTable ( ) . indexOf ( temp ) ) ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( prefixMod = = 0 & & suffixMod = = 0 ) {
2024-02-28 16:31:09 -05:00
Logger . info ( " Failed to find modTables for item " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2022-04-30 09:41:17 -04:00
2023-08-07 13:36:16 -04:00
// Roll on the tables for this vendor
ModTypeTableEntry prefixTypeTable = ModTypeTableEntry . rollTable ( prefixMod , ThreadLocalRandom . current ( ) . nextInt ( 1 , 100 + 1 ) ) ;
ModTypeTableEntry suffixTypeTable = ModTypeTableEntry . rollTable ( suffixMod , ThreadLocalRandom . current ( ) . nextInt ( 1 , 100 + 1 ) ) ;
// Sanity check.
if ( prefixTypeTable = = null | | suffixTypeTable = = null )
return null ;
2022-04-30 09:41:17 -04:00
2023-08-07 12:12:52 -04:00
int rollPrefix = ThreadLocalRandom . current ( ) . nextInt ( 1 , 100 + 1 ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( rollPrefix < 80 ) {
2022-04-30 09:41:17 -04:00
2023-08-07 13:55:22 -04:00
int randomPrefix = LootManager . TableRoll ( vendor . getLevel ( ) , false ) ;
2023-08-07 13:36:16 -04:00
prefixEntry = ModTableEntry . rollTable ( prefixTypeTable . modTableID , randomPrefix ) ;
2022-04-30 09:41:17 -04:00
2023-08-07 12:12:52 -04:00
if ( prefixEntry ! = null )
prefix = prefixEntry . action ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-08-07 12:12:52 -04:00
int rollSuffix = ThreadLocalRandom . current ( ) . nextInt ( 1 , 100 + 1 ) ;
2022-04-30 09:41:17 -04:00
2023-08-07 15:52:50 -04:00
// Always have at least one mod on a magic rolled item.
// Suffix will be our backup plan.
2023-08-07 15:00:18 -04:00
if ( rollSuffix < 80 | | prefixEntry = = null ) {
2022-04-30 09:41:17 -04:00
2023-08-07 13:55:22 -04:00
int randomSuffix = LootManager . TableRoll ( vendor . getLevel ( ) , false ) ;
2023-08-07 13:36:16 -04:00
suffixEntry = ModTableEntry . rollTable ( suffixTypeTable . modTableID , randomSuffix ) ;
2022-04-30 09:41:17 -04:00
2023-08-07 12:12:52 -04:00
if ( suffixEntry ! = null )
suffix = suffixEntry . action ;
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-08-07 15:52:50 -04:00
MobLoot toRoll = ItemFactory . produceRandomRoll ( vendor , playerCharacter , prefix , suffix , itemBaseID ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( toRoll = = null )
return null ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
toRoll . setValue ( 0 ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
float time ;
float rank = vendor . getBuilding ( ) . getRank ( ) - 1 ;
float rate = ( float ) ( 2 . 5 * rank ) ;
time = ( 20 - rate ) ;
time * = MBServerStatics . ONE_MINUTE ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( toRoll . getItemBase ( ) . getUUID ( ) > 910010 & & toRoll . getItemBase ( ) . getUUID ( ) < 910019 ) {
2024-03-15 10:35:41 -04:00
rank = toRoll . getTemplateID ( ) - 910010 ;
2023-07-15 09:23:48 -04:00
time = rank * 60 * 60 * 3 * 1000 ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// No job is submitted, as object's upgradetime field
2023-08-07 15:52:50 -04:00
// is used to determine whether an object has
// completed rolling. The game object exists previously
// to this, not when 'complete' is pressed.
2023-07-15 09:23:48 -04:00
long upgradeTime = System . currentTimeMillis ( ) + ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
DateTime dateTime = new DateTime ( ) ;
dateTime = dateTime . withMillis ( upgradeTime ) ;
toRoll . setDateToUpgrade ( upgradeTime ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
int playerID = 0 ;
2022-04-30 09:41:17 -04:00
2023-08-07 15:52:50 -04:00
if ( playerCharacter ! = null )
playerID = playerCharacter . getObjectUUID ( ) ;
2024-03-16 08:56:22 -04:00
DbManager . NPCQueries . ADD_TO_PRODUCTION_LIST ( toRoll . getObjectUUID ( ) , vendor . getObjectUUID ( ) , toRoll . getTemplateID ( ) , dateTime , prefix , suffix , toRoll . name , true , playerID ) ;
2023-08-07 15:52:50 -04:00
2024-03-16 08:56:22 -04:00
ProducedItem pi = new ProducedItem ( toRoll . getObjectUUID ( ) , vendor . getObjectUUID ( ) , toRoll . getTemplateID ( ) , dateTime , true , prefix , suffix , toRoll . name , playerID ) ;
2023-07-15 09:23:48 -04:00
pi . setProducedItemID ( toRoll . getObjectUUID ( ) ) ;
pi . setAmount ( itemsToRoll ) ;
ItemQueue produced = ItemQueue . borrow ( pi , ( long ) ( time * Float . parseFloat ( ConfigManager . MB_PRODUCTION_RATE . getValue ( ) ) ) ) ;
ItemProductionManager . send ( produced ) ;
2023-08-07 15:52:50 -04:00
2023-07-15 09:23:48 -04:00
return toRoll ;
}
2022-04-30 09:41:17 -04:00
2024-03-15 10:01:22 -04:00
public static MobLoot produceRandomRoll ( NPC npc , PlayerCharacter pc , String prefixString , String suffixString , int templateID ) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
boolean useWarehouse = false ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( npc = = null )
return null ;
2022-04-30 09:41:17 -04:00
2024-03-16 07:57:17 -04:00
ItemTemplate template = ItemTemplate . templates . get ( templateID ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Building forge = npc . getBuilding ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( forge = = null )
return null ;
Zone zone = npc . getBuilding ( ) . getParentZone ( ) ;
if ( zone = = null )
return null ;
2023-09-20 15:53:41 -04:00
City city = City . getCity ( zone . playerCityUUID ) ;
2023-07-15 09:23:48 -04:00
if ( city = = null )
return null ;
MobLoot ml = null ;
city . transactionLock . writeLock ( ) . lock ( ) ;
try {
2024-03-17 09:47:00 -04:00
Warehouse cityWarehouse = city . warehouse ;
2023-07-15 09:23:48 -04:00
if ( cityWarehouse ! = null & & forge . assetIsProtected ( ) )
useWarehouse = true ;
2024-03-15 10:01:22 -04:00
ConcurrentHashMap < Enum . ResourceType , Integer > resources = null ;
2023-07-15 09:23:48 -04:00
if ( useWarehouse )
2024-01-24 20:03:27 -06:00
resources = cityWarehouse . resources ;
2023-07-15 09:23:48 -04:00
int galvorAmount = 0 ;
int wormwoodAmount = 0 ;
2022-04-30 09:41:17 -04:00
2024-03-15 10:01:22 -04:00
if ( template . item_type . equals ( ItemType . WEAPON ) & & template . item_skill_required . isEmpty ( ) = = false & &
template . item_skill_required . values ( ) . iterator ( ) . next ( ) = = 110 ) {
2024-03-10 14:10:51 -04:00
switch ( template . item_skill_used ) {
2023-07-15 09:23:48 -04:00
case " Bow " :
case " Crossbow " :
case " Spear " :
case " Pole Arm " :
case " Staff " :
wormwoodAmount = 22 ;
break ;
case " Axe " :
case " Dagger " :
case " Sword " :
case " Hammer " :
case " Unarmed Combat " :
2024-03-10 16:21:23 -04:00
if ( ItemTemplate . isTwoHanded ( template ) )
2023-07-15 09:23:48 -04:00
galvorAmount = 22 ;
else
galvorAmount = 11 ;
break ;
}
}
//Cant roll 110% weapons that require resources if not allowed to use warehouse.
if ( galvorAmount > 0 | | wormwoodAmount > 0 )
if ( ! useWarehouse )
return null ;
if ( galvorAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GALVOR ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Galvor is locked. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( cityWarehouse . resources . get ( Enum . ResourceType . GALVOR ) < galvorAmount ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Galvor in warehouse to roll this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
if ( wormwoodAmount > 0 ) {
2024-03-15 10:01:22 -04:00
if ( Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . WORMWOOD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Galvor is locked. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( cityWarehouse . resources . get ( Enum . ResourceType . WORMWOOD ) < wormwoodAmount ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Galvor in warehouse to roll this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
EffectsBase prefix = null ;
if ( ! prefixString . isEmpty ( ) ) {
prefix = PowersManager . getEffectByIDString ( prefixString ) ;
if ( prefix = = null )
return null ;
}
2024-03-03 13:18:16 -05:00
int baseCost = template . item_value ;
2023-07-15 09:23:48 -04:00
int total = ( int ) ( baseCost + baseCost * . 10 ) ;
EffectsBase suffix = null ;
if ( ! suffixString . isEmpty ( ) ) {
suffix = PowersManager . getEffectByIDString ( suffixString ) ;
if ( suffix = = null )
return null ;
}
//calculate gold costs and remove from the warehouse
if ( prefix ! = null | | suffix ! = null ) {
2024-03-03 13:18:16 -05:00
int costToCreate = ( int ) ( template . item_value + template . item_value * . 10f ) ;
2023-07-15 09:23:48 -04:00
int buildingWithdraw = BuildingManager . GetWithdrawAmountForRolling ( forge , costToCreate ) ;
int overdraft = BuildingManager . GetOverdraft ( forge , costToCreate ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > resources . get ( Enum . ResourceType . GOLD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( ! forge . transferGold ( - buildingWithdraw , false ) ) {
overdraft + = buildingWithdraw ;
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Building does not have enough gold to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
} else {
2024-03-15 10:01:22 -04:00
if ( overdraft > resources . get ( Enum . ResourceType . GOLD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
// there was an overdraft, withdraw the rest from warehouse.
if ( overdraft > 0 ) {
if ( pc ! = null ) {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , pc , Enum . ResourceType . GOLD , overdraft , false , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
} else {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GOLD , overdraft , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
}
if ( prefix = = null & & suffix = = null ) {
int buildingWithdraw = BuildingManager . GetWithdrawAmountForRolling ( forge , total ) ;
int overdraft = BuildingManager . GetOverdraft ( forge , total ) ;
if ( overdraft > 0 & & ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough gold in building strongbox. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > 0 & & Warehouse . isResourceLocked ( cityWarehouse , Enum . ResourceType . GOLD ) ) {
2023-07-15 09:23:48 -04:00
if ( pc ! = null )
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Warehouse gold is barred! Overdraft cannot be withdrawn from warehouse. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
2024-03-15 10:01:22 -04:00
if ( useWarehouse & & overdraft > resources . get ( Enum . ResourceType . GOLD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse for overdraft. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
if ( ! forge . transferGold ( - buildingWithdraw , false ) ) {
overdraft + = buildingWithdraw ;
if ( ! useWarehouse ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Building does not have enough gold to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
} else {
2024-03-15 10:01:22 -04:00
if ( overdraft > resources . get ( Enum . ResourceType . GOLD ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Not enough Gold in Warehouse to produce this item. " + template . item_base_name ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
if ( overdraft > 0 & & useWarehouse ) {
if ( pc ! = null ) {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , pc , Enum . ResourceType . GOLD , overdraft , false , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
} else {
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GOLD , overdraft , true ) ) {
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
}
}
if ( galvorAmount > 0 & & useWarehouse ) {
//ChatManager.chatGuildInfo(pc, "Withdrawing " + galvorAmount + " galvor from warehouse");
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . GALVOR , galvorAmount , true ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Failed to withdraw Galvor from warehouse! " + template . item_base_name ) ;
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
if ( wormwoodAmount > 0 & & useWarehouse ) {
//ChatManager.chatGuildInfo(pc, "Withdrawing " + wormwoodAmount + " wormwood from warehouse");
2024-03-15 10:01:22 -04:00
if ( ! Warehouse . withdraw ( cityWarehouse , npc , Enum . ResourceType . WORMWOOD , wormwoodAmount , true ) ) {
2024-02-28 16:31:09 -05:00
ErrorPopupMsg . sendErrorMsg ( pc , " Failed to withdraw Wormwood from warehouse for " + template . item_base_name ) ;
2024-03-17 09:47:00 -04:00
Logger . error ( " Warehouse for city " + cityWarehouse . city . getName ( ) + " Failed to Withdrawl " ) ;
2023-07-15 09:23:48 -04:00
return null ;
}
}
2024-03-15 10:01:22 -04:00
ml = new MobLoot ( npc , template , false ) ;
2023-07-15 09:23:48 -04:00
ml . containerType = Enum . ItemContainerType . FORGE ;
if ( prefix ! = null ) {
ml . addPermanentEnchantment ( prefix . getIDString ( ) , 0 , 0 , true ) ;
ml . setPrefix ( prefix . getIDString ( ) ) ;
}
if ( suffix ! = null ) {
ml . addPermanentEnchantment ( suffix . getIDString ( ) , 0 , 0 , false ) ;
ml . setSuffix ( suffix . getIDString ( ) ) ;
}
ml . loadEnchantments ( ) ;
ml . setValue ( 0 ) ;
ml . setRandom ( true ) ;
npc . addItemToForge ( ml ) ;
} catch ( Exception e ) {
Logger . error ( e ) ;
} finally {
city . transactionLock . writeLock ( ) . unlock ( ) ;
}
return ml ;
}
2022-04-30 09:41:17 -04:00
}