Castle.java 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.entity;
  16. import gnu.trove.TIntIntHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.CastleUpdater;
  29. import com.l2jserver.gameserver.SevenSigns;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.datatables.ClanTable;
  32. import com.l2jserver.gameserver.datatables.DoorTable;
  33. import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
  34. import com.l2jserver.gameserver.instancemanager.CastleManager;
  35. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  36. import com.l2jserver.gameserver.instancemanager.FortManager;
  37. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  38. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  39. import com.l2jserver.gameserver.instancemanager.CastleManorManager.CropProcure;
  40. import com.l2jserver.gameserver.instancemanager.CastleManorManager.SeedProduction;
  41. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager.Territory;
  42. import com.l2jserver.gameserver.model.L2Clan;
  43. import com.l2jserver.gameserver.model.L2Manor;
  44. import com.l2jserver.gameserver.model.L2Object;
  45. import com.l2jserver.gameserver.model.L2Skill;
  46. import com.l2jserver.gameserver.model.actor.instance.L2ArtefactInstance;
  47. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  48. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  49. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  50. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  51. import com.l2jserver.gameserver.model.zone.type.L2CastleTeleportZone;
  52. import com.l2jserver.gameserver.model.zone.type.L2CastleZone;
  53. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  54. import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  55. import javolution.util.FastList;
  56. import javolution.util.FastMap;
  57. public class Castle
  58. {
  59. protected static final Logger _log = Logger.getLogger(Castle.class.getName());
  60. // =========================================================
  61. // Data Field
  62. private List<CropProcure> _procure = new ArrayList<CropProcure>();
  63. private List<SeedProduction> _production = new ArrayList<SeedProduction>();
  64. private List<CropProcure> _procureNext = new ArrayList<CropProcure>();
  65. private List<SeedProduction> _productionNext = new ArrayList<SeedProduction>();
  66. private boolean _isNextPeriodApproved = false;
  67. private static final String CASTLE_MANOR_DELETE_PRODUCTION = "DELETE FROM castle_manor_production WHERE castle_id=?;";
  68. private static final String CASTLE_MANOR_DELETE_PRODUCTION_PERIOD = "DELETE FROM castle_manor_production WHERE castle_id=? AND period=?;";
  69. private static final String CASTLE_MANOR_DELETE_PROCURE = "DELETE FROM castle_manor_procure WHERE castle_id=?;";
  70. private static final String CASTLE_MANOR_DELETE_PROCURE_PERIOD = "DELETE FROM castle_manor_procure WHERE castle_id=? AND period=?;";
  71. private static final String CASTLE_UPDATE_CROP = "UPDATE castle_manor_procure SET can_buy=? WHERE crop_id=? AND castle_id=? AND period=?";
  72. private static final String CASTLE_UPDATE_SEED = "UPDATE castle_manor_production SET can_produce=? WHERE seed_id=? AND castle_id=? AND period=?";
  73. // =========================================================
  74. // Data Field
  75. private int _castleId = 0;
  76. private List<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
  77. private List<String> _doorDefault = new FastList<String>();
  78. private String _name = "";
  79. private int _ownerId = 0;
  80. private Siege _siege = null;
  81. private Calendar _siegeDate;
  82. private boolean _isTimeRegistrationOver = true; // true if Castle Lords set the time, or 24h is elapsed after the siege
  83. private Calendar _siegeTimeRegistrationEndDate; // last siege end date + 1 day
  84. private int _taxPercent = 0;
  85. private double _taxRate = 0;
  86. private long _treasury = 0;
  87. private boolean _showNpcCrest = false;
  88. private L2CastleZone _zone = null;
  89. private L2CastleTeleportZone _teleZone;
  90. private L2Clan _formerOwner = null;
  91. private List<L2ArtefactInstance> _artefacts = new ArrayList<L2ArtefactInstance>(1);
  92. private TIntIntHashMap _engrave = new TIntIntHashMap(1);
  93. private Map<Integer, CastleFunction> _function;
  94. private FastList<L2Skill> _residentialSkills = new FastList<L2Skill>();
  95. /** Castle Functions */
  96. public static final int FUNC_TELEPORT = 1;
  97. public static final int FUNC_RESTORE_HP = 2;
  98. public static final int FUNC_RESTORE_MP = 3;
  99. public static final int FUNC_RESTORE_EXP = 4;
  100. public static final int FUNC_SUPPORT = 5;
  101. public class CastleFunction
  102. {
  103. private int _type;
  104. private int _lvl;
  105. protected int _fee;
  106. protected int _tempFee;
  107. private long _rate;
  108. private long _endDate;
  109. protected boolean _inDebt;
  110. public boolean _cwh;
  111. public CastleFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
  112. {
  113. _type = type;
  114. _lvl = lvl;
  115. _fee = lease;
  116. _tempFee = tempLease;
  117. _rate = rate;
  118. _endDate = time;
  119. initializeTask(cwh);
  120. }
  121. public int getType()
  122. {
  123. return _type;
  124. }
  125. public int getLvl()
  126. {
  127. return _lvl;
  128. }
  129. public int getLease()
  130. {
  131. return _fee;
  132. }
  133. public long getRate()
  134. {
  135. return _rate;
  136. }
  137. public long getEndTime()
  138. {
  139. return _endDate;
  140. }
  141. public void setLvl(int lvl)
  142. {
  143. _lvl = lvl;
  144. }
  145. public void setLease(int lease)
  146. {
  147. _fee = lease;
  148. }
  149. public void setEndTime(long time)
  150. {
  151. _endDate = time;
  152. }
  153. private void initializeTask(boolean cwh)
  154. {
  155. if (getOwnerId() <= 0)
  156. return;
  157. long currentTime = System.currentTimeMillis();
  158. if (_endDate > currentTime)
  159. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
  160. else
  161. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
  162. }
  163. private class FunctionTask implements Runnable
  164. {
  165. public FunctionTask(boolean cwh)
  166. {
  167. _cwh = cwh;
  168. }
  169. public void run()
  170. {
  171. try
  172. {
  173. if (getOwnerId() <= 0)
  174. return;
  175. if (ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= _fee || !_cwh)
  176. {
  177. int fee = _fee;
  178. if (getEndTime() == -1)
  179. fee = _tempFee;
  180. setEndTime(System.currentTimeMillis() + getRate());
  181. dbSave();
  182. if (_cwh)
  183. {
  184. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CS_function_fee", 57, fee, null, null);
  185. if (Config.DEBUG)
  186. _log.warning("deducted " + fee + " adena from " + getName() + " owner's cwh for function id : " + getType());
  187. }
  188. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
  189. }
  190. else
  191. removeFunction(getType());
  192. }
  193. catch (Exception e)
  194. {
  195. _log.log(Level.SEVERE, "", e);
  196. }
  197. }
  198. }
  199. public void dbSave()
  200. {
  201. Connection con = null;
  202. try
  203. {
  204. PreparedStatement statement;
  205. con = L2DatabaseFactory.getInstance().getConnection();
  206. statement = con.prepareStatement("REPLACE INTO castle_functions (castle_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)");
  207. statement.setInt(1, getCastleId());
  208. statement.setInt(2, getType());
  209. statement.setInt(3, getLvl());
  210. statement.setInt(4, getLease());
  211. statement.setLong(5, getRate());
  212. statement.setLong(6, getEndTime());
  213. statement.execute();
  214. statement.close();
  215. }
  216. catch (Exception e)
  217. {
  218. _log.log(Level.SEVERE, "Exception: Castle.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): "
  219. + e.getMessage(), e);
  220. }
  221. finally
  222. {
  223. L2DatabaseFactory.close(con);
  224. }
  225. }
  226. }
  227. // =========================================================
  228. // Constructor
  229. public Castle(int castleId)
  230. {
  231. _castleId = castleId;
  232. /*if (_castleId == 7 || castleId == 9) // Goddard and Schuttgart
  233. _nbArtifact = 2;*/
  234. load();
  235. loadDoor();
  236. _function = new FastMap<Integer, CastleFunction>();
  237. _residentialSkills = ResidentialSkillTable.getInstance().getSkills(castleId);
  238. if (getOwnerId() != 0)
  239. {
  240. loadFunctions();
  241. }
  242. }
  243. // =========================================================
  244. // Method - Public
  245. /** Return function with id */
  246. public CastleFunction getFunction(int type)
  247. {
  248. if (_function.get(type) != null)
  249. return _function.get(type);
  250. return null;
  251. }
  252. public synchronized void engrave(L2Clan clan, L2Object target)
  253. {
  254. if (!_artefacts.contains(target))
  255. return;
  256. _engrave.put(target.getObjectId(), clan.getClanId());
  257. if (_engrave.size() == _artefacts.size())
  258. {
  259. for (L2ArtefactInstance art : _artefacts)
  260. {
  261. if (_engrave.get(art.getObjectId()) != clan.getClanId())
  262. return;
  263. }
  264. _engrave.clear();
  265. setOwner(clan);
  266. }
  267. }
  268. // This method add to the treasury
  269. /** Add amount to castle instance's treasury (warehouse). */
  270. public void addToTreasury(long amount)
  271. {
  272. // check if owned
  273. if (getOwnerId() <= 0)
  274. {
  275. return;
  276. }
  277. if (_name.equalsIgnoreCase("Schuttgart") || _name.equalsIgnoreCase("Goddard"))
  278. {
  279. Castle rune = CastleManager.getInstance().getCastle("rune");
  280. if (rune != null)
  281. {
  282. long runeTax = (long) (amount * rune.getTaxRate());
  283. if (rune.getOwnerId() > 0)
  284. rune.addToTreasury(runeTax);
  285. amount -= runeTax;
  286. }
  287. }
  288. if (!_name.equalsIgnoreCase("aden") && !_name.equalsIgnoreCase("Rune") && !_name.equalsIgnoreCase("Schuttgart") && !_name.equalsIgnoreCase("Goddard")) // If current castle instance is not Aden, Rune, Goddard or Schuttgart.
  289. {
  290. Castle aden = CastleManager.getInstance().getCastle("aden");
  291. if (aden != null)
  292. {
  293. long adenTax = (long) (amount * aden.getTaxRate()); // Find out what Aden gets from the current castle instance's income
  294. if (aden.getOwnerId() > 0)
  295. aden.addToTreasury(adenTax); // Only bother to really add the tax to the treasury if not npc owned
  296. amount -= adenTax; // Subtract Aden's income from current castle instance's income
  297. }
  298. }
  299. addToTreasuryNoTax(amount);
  300. }
  301. /** Add amount to castle instance's treasury (warehouse), no tax paying. */
  302. public boolean addToTreasuryNoTax(long amount)
  303. {
  304. if (getOwnerId() <= 0)
  305. return false;
  306. if (amount < 0)
  307. {
  308. amount *= -1;
  309. if (_treasury < amount)
  310. return false;
  311. _treasury -= amount;
  312. }
  313. else
  314. {
  315. if (_treasury + amount > PcInventory.MAX_ADENA) // TODO is this valid after gracia final?
  316. _treasury = PcInventory.MAX_ADENA;
  317. else
  318. _treasury += amount;
  319. }
  320. Connection con = null;
  321. try
  322. {
  323. con = L2DatabaseFactory.getInstance().getConnection();
  324. PreparedStatement statement = con.prepareStatement("Update castle set treasury = ? where id = ?");
  325. statement.setLong(1, getTreasury());
  326. statement.setInt(2, getCastleId());
  327. statement.execute();
  328. statement.close();
  329. }
  330. catch (Exception e)
  331. {
  332. }
  333. finally
  334. {
  335. L2DatabaseFactory.close(con);
  336. }
  337. return true;
  338. }
  339. /**
  340. * Move non clan members off castle area and to nearest town.<BR><BR>
  341. */
  342. public void banishForeigners()
  343. {
  344. getZone().banishForeigners(getOwnerId());
  345. }
  346. /**
  347. * Return true if object is inside the zone
  348. */
  349. public boolean checkIfInZone(int x, int y, int z)
  350. {
  351. return getZone().isInsideZone(x, y, z);
  352. }
  353. public L2CastleZone getZone()
  354. {
  355. if (_zone == null)
  356. {
  357. for (L2ZoneType zone : ZoneManager.getInstance().getAllZones())
  358. {
  359. if (zone instanceof L2CastleZone && ((L2CastleZone) zone).getCastleId() == getCastleId())
  360. {
  361. _zone = (L2CastleZone) zone;
  362. break;
  363. }
  364. }
  365. }
  366. return _zone;
  367. }
  368. public L2CastleTeleportZone getTeleZone()
  369. {
  370. if (_teleZone == null)
  371. {
  372. for (L2ZoneType zone : ZoneManager.getInstance().getAllZones())
  373. {
  374. if (zone instanceof L2CastleTeleportZone && ((L2CastleTeleportZone) zone).getCastleId() == getCastleId())
  375. {
  376. _teleZone = (L2CastleTeleportZone) zone;
  377. break;
  378. }
  379. }
  380. }
  381. return _teleZone;
  382. }
  383. public void oustAllPlayers()
  384. {
  385. getTeleZone().oustAllPlayers();
  386. }
  387. /**
  388. * Get the objects distance to this castle
  389. * @param obj
  390. * @return
  391. */
  392. public double getDistance(L2Object obj)
  393. {
  394. return getZone().getDistanceToZone(obj);
  395. }
  396. public void closeDoor(L2PcInstance activeChar, int doorId)
  397. {
  398. openCloseDoor(activeChar, doorId, false);
  399. }
  400. public void openDoor(L2PcInstance activeChar, int doorId)
  401. {
  402. openCloseDoor(activeChar, doorId, true);
  403. }
  404. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  405. {
  406. if (activeChar.getClanId() != getOwnerId())
  407. return;
  408. L2DoorInstance door = getDoor(doorId);
  409. if (door != null)
  410. {
  411. if (open)
  412. door.openMe();
  413. else
  414. door.closeMe();
  415. }
  416. }
  417. // This method is used to begin removing all castle upgrades
  418. public void removeUpgrade()
  419. {
  420. removeDoorUpgrade();
  421. for (Map.Entry<Integer, CastleFunction> fc : _function.entrySet())
  422. removeFunction(fc.getKey());
  423. _function.clear();
  424. }
  425. // This method updates the castle tax rate
  426. public void setOwner(L2Clan clan)
  427. {
  428. // Remove old owner
  429. if (getOwnerId() > 0 && (clan == null || clan.getClanId() != getOwnerId()))
  430. {
  431. L2Clan oldOwner = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  432. if (oldOwner != null)
  433. {
  434. if (_formerOwner == null)
  435. {
  436. _formerOwner = oldOwner;
  437. if (Config.REMOVE_CASTLE_CIRCLETS)
  438. {
  439. CastleManager.getInstance().removeCirclet(_formerOwner, getCastleId());
  440. }
  441. }
  442. try
  443. {
  444. L2PcInstance oldleader = oldOwner.getLeader().getPlayerInstance();
  445. if (oldleader != null)
  446. {
  447. if (oldleader.getMountType() == 2)
  448. oldleader.dismount();
  449. }
  450. }
  451. catch (Exception e)
  452. {
  453. _log.log(Level.WARNING, "Exception in setOwner: " + e.getMessage(), e);
  454. }
  455. oldOwner.setHasCastle(0); // Unset has castle flag for old owner
  456. for (L2PcInstance member : oldOwner.getOnlineMembers(0))
  457. {
  458. removeResidentialSkills(member);
  459. member.sendSkillList();
  460. }
  461. }
  462. }
  463. updateOwnerInDB(clan); // Update in database
  464. setShowNpcCrest(false);
  465. // if clan have fortress, remove it
  466. if (clan.getHasFort() > 0)
  467. FortManager.getInstance().getFortByOwner(clan).removeOwner(true);
  468. if (getSiege().getIsInProgress()) // If siege in progress
  469. getSiege().midVictory(); // Mid victory phase of siege
  470. TerritoryWarManager.getInstance().getTerritory(_castleId).setOwnerClan(clan);
  471. for (L2PcInstance member : clan.getOnlineMembers(0))
  472. {
  473. giveResidentialSkills(member);
  474. member.sendSkillList();
  475. }
  476. }
  477. public void removeOwner(L2Clan clan)
  478. {
  479. if (clan != null)
  480. {
  481. _formerOwner = clan;
  482. if (Config.REMOVE_CASTLE_CIRCLETS)
  483. {
  484. CastleManager.getInstance().removeCirclet(_formerOwner, getCastleId());
  485. }
  486. for (L2PcInstance member : clan.getOnlineMembers(0))
  487. {
  488. removeResidentialSkills(member);
  489. member.sendSkillList();
  490. }
  491. clan.setHasCastle(0);
  492. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  493. }
  494. updateOwnerInDB(null);
  495. if (getSiege().getIsInProgress())
  496. getSiege().midVictory();
  497. for (Map.Entry<Integer, CastleFunction> fc : _function.entrySet())
  498. removeFunction(fc.getKey());
  499. _function.clear();
  500. }
  501. // This method updates the castle tax rate
  502. public void setTaxPercent(L2PcInstance activeChar, int taxPercent)
  503. {
  504. int maxTax;
  505. switch (SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE))
  506. {
  507. case SevenSigns.CABAL_DAWN:
  508. maxTax = 25;
  509. break;
  510. case SevenSigns.CABAL_DUSK:
  511. maxTax = 5;
  512. break;
  513. default: // no owner
  514. maxTax = 15;
  515. }
  516. if (taxPercent < 0 || taxPercent > maxTax)
  517. {
  518. activeChar.sendMessage("Tax value must be between 0 and " + maxTax + ".");
  519. return;
  520. }
  521. setTaxPercent(taxPercent);
  522. activeChar.sendMessage(getName() + " castle tax changed to " + taxPercent + "%.");
  523. }
  524. public void setTaxPercent(int taxPercent)
  525. {
  526. _taxPercent = taxPercent;
  527. _taxRate = _taxPercent / 100.0;
  528. Connection con = null;
  529. try
  530. {
  531. con = L2DatabaseFactory.getInstance().getConnection();
  532. PreparedStatement statement = con.prepareStatement("Update castle set taxPercent = ? where id = ?");
  533. statement.setInt(1, taxPercent);
  534. statement.setInt(2, getCastleId());
  535. statement.execute();
  536. statement.close();
  537. }
  538. catch (Exception e)
  539. {
  540. }
  541. finally
  542. {
  543. L2DatabaseFactory.close(con);
  544. }
  545. }
  546. /**
  547. * Respawn all doors on castle grounds<BR><BR>
  548. */
  549. public void spawnDoor()
  550. {
  551. spawnDoor(false);
  552. }
  553. /**
  554. * Respawn all doors on castle grounds<BR><BR>
  555. */
  556. public void spawnDoor(boolean isDoorWeak)
  557. {
  558. for (int i = 0; i < getDoors().size(); i++)
  559. {
  560. L2DoorInstance door = getDoors().get(i);
  561. if (door.getCurrentHp() <= 0)
  562. {
  563. door.decayMe(); // Kill current if not killed already
  564. door = DoorTable.parseList(_doorDefault.get(i), false);
  565. DoorTable.getInstance().putDoor(door); //Readd the new door to the DoorTable By Erb
  566. if (isDoorWeak)
  567. door.setCurrentHp(door.getMaxHp() / 2);
  568. door.spawnMe(door.getX(), door.getY(), door.getZ());
  569. getDoors().set(i, door);
  570. }
  571. else if (door.getOpen())
  572. door.closeMe();
  573. }
  574. loadDoorUpgrade(); // Check for any upgrade the doors may have
  575. }
  576. // This method upgrade door
  577. public void upgradeDoor(int doorId, int hp, int pDef, int mDef)
  578. {
  579. L2DoorInstance door = getDoor(doorId);
  580. if (door == null)
  581. return;
  582. door.setCurrentHp(door.getMaxHp() + hp);
  583. saveDoorUpgrade(doorId, hp, pDef, mDef);
  584. }
  585. // =========================================================
  586. // Method - Private
  587. // This method loads castle
  588. private void load()
  589. {
  590. Connection con = null;
  591. try
  592. {
  593. PreparedStatement statement;
  594. ResultSet rs;
  595. con = L2DatabaseFactory.getInstance().getConnection();
  596. statement = con.prepareStatement("Select * from castle where id = ?");
  597. statement.setInt(1, getCastleId());
  598. rs = statement.executeQuery();
  599. while (rs.next())
  600. {
  601. _name = rs.getString("name");
  602. //_OwnerId = rs.getInt("ownerId");
  603. _siegeDate = Calendar.getInstance();
  604. _siegeDate.setTimeInMillis(rs.getLong("siegeDate"));
  605. _siegeTimeRegistrationEndDate = Calendar.getInstance();
  606. _siegeTimeRegistrationEndDate.setTimeInMillis(rs.getLong("regTimeEnd"));
  607. _isTimeRegistrationOver = rs.getBoolean("regTimeOver");
  608. _taxPercent = rs.getInt("taxPercent");
  609. _treasury = rs.getLong("treasury");
  610. _showNpcCrest = rs.getBoolean("showNpcCrest");
  611. }
  612. rs.close();
  613. statement.close();
  614. _taxRate = _taxPercent / 100.0;
  615. statement = con.prepareStatement("Select clan_id from clan_data where hasCastle = ?");
  616. statement.setInt(1, getCastleId());
  617. rs = statement.executeQuery();
  618. while (rs.next())
  619. {
  620. _ownerId = rs.getInt("clan_id");
  621. }
  622. if (getOwnerId() > 0)
  623. {
  624. L2Clan clan = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  625. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  626. }
  627. rs.close();
  628. statement.close();
  629. }
  630. catch (Exception e)
  631. {
  632. _log.log(Level.WARNING, "Exception: loadCastleData(): " + e.getMessage(), e);
  633. }
  634. finally
  635. {
  636. L2DatabaseFactory.close(con);
  637. }
  638. }
  639. /** Load All Functions */
  640. private void loadFunctions()
  641. {
  642. Connection con = null;
  643. try
  644. {
  645. PreparedStatement statement;
  646. ResultSet rs;
  647. con = L2DatabaseFactory.getInstance().getConnection();
  648. statement = con.prepareStatement("Select * from castle_functions where castle_id = ?");
  649. statement.setInt(1, getCastleId());
  650. rs = statement.executeQuery();
  651. while (rs.next())
  652. {
  653. _function.put(rs.getInt("type"), new CastleFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  654. }
  655. rs.close();
  656. statement.close();
  657. }
  658. catch (Exception e)
  659. {
  660. _log.log(Level.SEVERE, "Exception: Castle.loadFunctions(): " + e.getMessage(), e);
  661. }
  662. finally
  663. {
  664. L2DatabaseFactory.close(con);
  665. }
  666. }
  667. /** Remove function In List and in DB */
  668. public void removeFunction(int functionType)
  669. {
  670. _function.remove(functionType);
  671. Connection con = null;
  672. try
  673. {
  674. PreparedStatement statement;
  675. con = L2DatabaseFactory.getInstance().getConnection();
  676. statement = con.prepareStatement("DELETE FROM castle_functions WHERE castle_id=? AND type=?");
  677. statement.setInt(1, getCastleId());
  678. statement.setInt(2, functionType);
  679. statement.execute();
  680. statement.close();
  681. }
  682. catch (Exception e)
  683. {
  684. _log.log(Level.SEVERE, "Exception: Castle.removeFunctions(int functionType): " + e.getMessage(), e);
  685. }
  686. finally
  687. {
  688. L2DatabaseFactory.close(con);
  689. }
  690. }
  691. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  692. {
  693. if (player == null)
  694. return false;
  695. if (Config.DEBUG)
  696. _log.warning("Called Castle.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerId());
  697. if (lease > 0)
  698. if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
  699. return false;
  700. if (addNew)
  701. {
  702. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, 0, false));
  703. }
  704. else
  705. {
  706. if (lvl == 0 && lease == 0)
  707. removeFunction(type);
  708. else
  709. {
  710. int diffLease = lease - _function.get(type).getLease();
  711. if (Config.DEBUG)
  712. _log.warning("Called Castle.updateFunctions diffLease : " + diffLease);
  713. if (diffLease > 0)
  714. {
  715. _function.remove(type);
  716. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, -1, false));
  717. }
  718. else
  719. {
  720. _function.get(type).setLease(lease);
  721. _function.get(type).setLvl(lvl);
  722. _function.get(type).dbSave();
  723. }
  724. }
  725. }
  726. return true;
  727. }
  728. public void activateInstance()
  729. {
  730. for (final L2DoorInstance door : _doors)
  731. {
  732. door.spawnMe(door.getX(), door.getY(), door.getZ());
  733. }
  734. }
  735. // This method loads castle door data from database
  736. private void loadDoor()
  737. {
  738. Connection con = null;
  739. try
  740. {
  741. con = L2DatabaseFactory.getInstance().getConnection();
  742. PreparedStatement statement = con.prepareStatement("Select * from castle_door where castleId = ?");
  743. statement.setInt(1, getCastleId());
  744. ResultSet rs = statement.executeQuery();
  745. while (rs.next())
  746. {
  747. // Create list of the door default for use when respawning dead doors
  748. _doorDefault.add(rs.getString("name") + ";" + rs.getInt("id") + ";" + rs.getInt("x") + ";" + rs.getInt("y") + ";"
  749. + rs.getInt("z") + ";" + rs.getInt("range_xmin") + ";" + rs.getInt("range_ymin") + ";" + rs.getInt("range_zmin")
  750. + ";" + rs.getInt("range_xmax") + ";" + rs.getInt("range_ymax") + ";" + rs.getInt("range_zmax") + ";"
  751. + rs.getInt("hp") + ";" + rs.getInt("pDef") + ";" + rs.getInt("mDef"));
  752. L2DoorInstance door = DoorTable.parseList(_doorDefault.get(_doorDefault.size() - 1), false);
  753. _doors.add(door);
  754. DoorTable.getInstance().putDoor(door);
  755. }
  756. rs.close();
  757. statement.close();
  758. }
  759. catch (Exception e)
  760. {
  761. _log.log(Level.WARNING, "Exception: loadCastleDoor(): " + e.getMessage(),e );
  762. }
  763. finally
  764. {
  765. L2DatabaseFactory.close(con);
  766. }
  767. }
  768. // This method loads castle door upgrade data from database
  769. private void loadDoorUpgrade()
  770. {
  771. Connection con = null;
  772. try
  773. {
  774. con = L2DatabaseFactory.getInstance().getConnection();
  775. PreparedStatement statement = con.prepareStatement("Select * from castle_doorupgrade where doorId in (Select Id from castle_door where castleId = ?)");
  776. statement.setInt(1, getCastleId());
  777. ResultSet rs = statement.executeQuery();
  778. while (rs.next())
  779. {
  780. upgradeDoor(rs.getInt("id"), rs.getInt("hp"), rs.getInt("pDef"), rs.getInt("mDef"));
  781. }
  782. rs.close();
  783. statement.close();
  784. }
  785. catch (Exception e)
  786. {
  787. _log.log(Level.WARNING, "Exception: loadCastleDoorUpgrade(): " + e.getMessage(), e);
  788. }
  789. finally
  790. {
  791. L2DatabaseFactory.close(con);
  792. }
  793. }
  794. private void removeDoorUpgrade()
  795. {
  796. Connection con = null;
  797. try
  798. {
  799. con = L2DatabaseFactory.getInstance().getConnection();
  800. PreparedStatement statement = con.prepareStatement("delete from castle_doorupgrade where doorId in (select id from castle_door where castleId=?)");
  801. statement.setInt(1, getCastleId());
  802. statement.execute();
  803. statement.close();
  804. }
  805. catch (Exception e)
  806. {
  807. _log.log(Level.WARNING, "Exception: removeDoorUpgrade(): " + e.getMessage(), e);
  808. }
  809. finally
  810. {
  811. L2DatabaseFactory.close(con);
  812. }
  813. }
  814. private void saveDoorUpgrade(int doorId, int hp, int pDef, int mDef)
  815. {
  816. Connection con = null;
  817. try
  818. {
  819. con = L2DatabaseFactory.getInstance().getConnection();
  820. PreparedStatement statement = con.prepareStatement("INSERT INTO castle_doorupgrade (doorId, hp, pDef, mDef) values (?,?,?,?)");
  821. statement.setInt(1, doorId);
  822. statement.setInt(2, hp);
  823. statement.setInt(3, pDef);
  824. statement.setInt(4, mDef);
  825. statement.execute();
  826. statement.close();
  827. }
  828. catch (Exception e)
  829. {
  830. _log.log(Level.WARNING, "Exception: saveDoorUpgrade(int doorId, int hp, int pDef, int mDef): " + e.getMessage(), e);
  831. }
  832. finally
  833. {
  834. L2DatabaseFactory.close(con);
  835. }
  836. }
  837. private void updateOwnerInDB(L2Clan clan)
  838. {
  839. if (clan != null)
  840. _ownerId = clan.getClanId(); // Update owner id property
  841. else
  842. {
  843. _ownerId = 0; // Remove owner
  844. resetManor();
  845. }
  846. Connection con = null;
  847. try
  848. {
  849. con = L2DatabaseFactory.getInstance().getConnection();
  850. PreparedStatement statement;
  851. // ============================================================================
  852. // NEED TO REMOVE HAS CASTLE FLAG FROM CLAN_DATA
  853. // SHOULD BE CHECKED FROM CASTLE TABLE
  854. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=0 WHERE hasCastle=?");
  855. statement.setInt(1, getCastleId());
  856. statement.execute();
  857. statement.close();
  858. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=? WHERE clan_id=?");
  859. statement.setInt(1, getCastleId());
  860. statement.setInt(2, getOwnerId());
  861. statement.execute();
  862. statement.close();
  863. // ============================================================================
  864. // Announce to clan memebers
  865. if (clan != null)
  866. {
  867. clan.setHasCastle(getCastleId()); // Set has castle flag for new owner
  868. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  869. clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
  870. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  871. }
  872. }
  873. catch (Exception e)
  874. {
  875. _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
  876. }
  877. finally
  878. {
  879. L2DatabaseFactory.close(con);
  880. }
  881. }
  882. // =========================================================
  883. // Property
  884. public final int getCastleId()
  885. {
  886. return _castleId;
  887. }
  888. public final L2DoorInstance getDoor(int doorId)
  889. {
  890. if (doorId <= 0)
  891. return null;
  892. for (L2DoorInstance door : getDoors())
  893. {
  894. if (door.getDoorId() == doorId)
  895. return door;
  896. }
  897. return null;
  898. }
  899. public final List<L2DoorInstance> getDoors()
  900. {
  901. return _doors;
  902. }
  903. public final String getName()
  904. {
  905. return _name;
  906. }
  907. public final int getOwnerId()
  908. {
  909. return _ownerId;
  910. }
  911. public final Siege getSiege()
  912. {
  913. if (_siege == null)
  914. _siege = new Siege(new Castle[] { this });
  915. return _siege;
  916. }
  917. public final Calendar getSiegeDate()
  918. {
  919. return _siegeDate;
  920. }
  921. public boolean getIsTimeRegistrationOver()
  922. {
  923. return _isTimeRegistrationOver;
  924. }
  925. public void setIsTimeRegistrationOver(boolean val)
  926. {
  927. _isTimeRegistrationOver = val;
  928. }
  929. public Calendar getTimeRegistrationOverDate()
  930. {
  931. if (_siegeTimeRegistrationEndDate == null)
  932. _siegeTimeRegistrationEndDate = Calendar.getInstance();
  933. return _siegeTimeRegistrationEndDate;
  934. }
  935. public final int getTaxPercent()
  936. {
  937. return _taxPercent;
  938. }
  939. public final double getTaxRate()
  940. {
  941. return _taxRate;
  942. }
  943. public final long getTreasury()
  944. {
  945. return _treasury;
  946. }
  947. public final boolean getShowNpcCrest()
  948. {
  949. return _showNpcCrest;
  950. }
  951. public final void setShowNpcCrest(boolean showNpcCrest)
  952. {
  953. if (_showNpcCrest != showNpcCrest)
  954. {
  955. _showNpcCrest = showNpcCrest;
  956. updateShowNpcCrest();
  957. }
  958. }
  959. public List<SeedProduction> getSeedProduction(int period)
  960. {
  961. return (period == CastleManorManager.PERIOD_CURRENT ? _production : _productionNext);
  962. }
  963. public List<CropProcure> getCropProcure(int period)
  964. {
  965. return (period == CastleManorManager.PERIOD_CURRENT ? _procure : _procureNext);
  966. }
  967. public void setSeedProduction(List<SeedProduction> seed, int period)
  968. {
  969. if (period == CastleManorManager.PERIOD_CURRENT)
  970. _production = seed;
  971. else
  972. _productionNext = seed;
  973. }
  974. public void setCropProcure(List<CropProcure> crop, int period)
  975. {
  976. if (period == CastleManorManager.PERIOD_CURRENT)
  977. _procure = crop;
  978. else
  979. _procureNext = crop;
  980. }
  981. public synchronized SeedProduction getSeed(int seedId, int period)
  982. {
  983. for (SeedProduction seed : getSeedProduction(period))
  984. {
  985. if (seed.getId() == seedId)
  986. {
  987. return seed;
  988. }
  989. }
  990. return null;
  991. }
  992. public synchronized CropProcure getCrop(int cropId, int period)
  993. {
  994. for (CropProcure crop : getCropProcure(period))
  995. {
  996. if (crop.getId() == cropId)
  997. {
  998. return crop;
  999. }
  1000. }
  1001. return null;
  1002. }
  1003. public long getManorCost(int period)
  1004. {
  1005. List<CropProcure> procure;
  1006. List<SeedProduction> production;
  1007. if (period == CastleManorManager.PERIOD_CURRENT)
  1008. {
  1009. procure = _procure;
  1010. production = _production;
  1011. }
  1012. else
  1013. {
  1014. procure = _procureNext;
  1015. production = _productionNext;
  1016. }
  1017. long total = 0;
  1018. if (production != null)
  1019. {
  1020. for (SeedProduction seed : production)
  1021. {
  1022. total += L2Manor.getInstance().getSeedBuyPrice(seed.getId()) * seed.getStartProduce();
  1023. }
  1024. }
  1025. if (procure != null)
  1026. {
  1027. for (CropProcure crop : procure)
  1028. {
  1029. total += crop.getPrice() * crop.getStartAmount();
  1030. }
  1031. }
  1032. return total;
  1033. }
  1034. //save manor production data
  1035. public void saveSeedData()
  1036. {
  1037. Connection con = null;
  1038. PreparedStatement statement;
  1039. try
  1040. {
  1041. con = L2DatabaseFactory.getInstance().getConnection();
  1042. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION);
  1043. statement.setInt(1, getCastleId());
  1044. statement.execute();
  1045. statement.close();
  1046. if (_production != null)
  1047. {
  1048. int count = 0;
  1049. String query = "INSERT INTO castle_manor_production VALUES ";
  1050. String values[] = new String[_production.size()];
  1051. for (SeedProduction s : _production)
  1052. {
  1053. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + ","
  1054. + s.getPrice() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1055. }
  1056. if (values.length > 0)
  1057. {
  1058. query += values[0];
  1059. for (int i = 1; i < values.length; i++)
  1060. {
  1061. query += "," + values[i];
  1062. }
  1063. statement = con.prepareStatement(query);
  1064. statement.execute();
  1065. statement.close();
  1066. }
  1067. }
  1068. if (_productionNext != null)
  1069. {
  1070. int count = 0;
  1071. String query = "INSERT INTO castle_manor_production VALUES ";
  1072. String values[] = new String[_productionNext.size()];
  1073. for (SeedProduction s : _productionNext)
  1074. {
  1075. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + ","
  1076. + s.getPrice() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1077. }
  1078. if (values.length > 0)
  1079. {
  1080. query += values[0];
  1081. for (int i = 1; i < values.length; i++)
  1082. {
  1083. query += "," + values[i];
  1084. }
  1085. statement = con.prepareStatement(query);
  1086. statement.execute();
  1087. statement.close();
  1088. }
  1089. }
  1090. }
  1091. catch (Exception e)
  1092. {
  1093. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1094. }
  1095. finally
  1096. {
  1097. L2DatabaseFactory.close(con);
  1098. }
  1099. }
  1100. //save manor production data for specified period
  1101. public void saveSeedData(int period)
  1102. {
  1103. Connection con = null;
  1104. PreparedStatement statement;
  1105. try
  1106. {
  1107. con = L2DatabaseFactory.getInstance().getConnection();
  1108. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION_PERIOD);
  1109. statement.setInt(1, getCastleId());
  1110. statement.setInt(2, period);
  1111. statement.execute();
  1112. statement.close();
  1113. List<SeedProduction> prod = null;
  1114. prod = getSeedProduction(period);
  1115. if (prod != null)
  1116. {
  1117. int count = 0;
  1118. String query = "INSERT INTO castle_manor_production VALUES ";
  1119. String values[] = new String[prod.size()];
  1120. for (SeedProduction s : prod)
  1121. {
  1122. values[count++] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + ","
  1123. + s.getPrice() + "," + period + ")";
  1124. }
  1125. if (values.length > 0)
  1126. {
  1127. query += values[0];
  1128. for (int i = 1; i < values.length; i++)
  1129. {
  1130. query += "," + values[i];
  1131. }
  1132. statement = con.prepareStatement(query);
  1133. statement.execute();
  1134. statement.close();
  1135. }
  1136. }
  1137. }
  1138. catch (Exception e)
  1139. {
  1140. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1141. }
  1142. finally
  1143. {
  1144. L2DatabaseFactory.close(con);
  1145. }
  1146. }
  1147. //save crop procure data
  1148. public void saveCropData()
  1149. {
  1150. Connection con = null;
  1151. PreparedStatement statement;
  1152. try
  1153. {
  1154. con = L2DatabaseFactory.getInstance().getConnection();
  1155. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE);
  1156. statement.setInt(1, getCastleId());
  1157. statement.execute();
  1158. statement.close();
  1159. if (_procure != null && _procure.size() > 0)
  1160. {
  1161. int count = 0;
  1162. String query = "INSERT INTO castle_manor_procure VALUES ";
  1163. String values[] = new String[_procure.size()];
  1164. for (CropProcure cp : _procure)
  1165. {
  1166. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + ","
  1167. + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1168. }
  1169. if (values.length > 0)
  1170. {
  1171. query += values[0];
  1172. for (int i = 1; i < values.length; i++)
  1173. {
  1174. query += "," + values[i];
  1175. }
  1176. statement = con.prepareStatement(query);
  1177. statement.execute();
  1178. statement.close();
  1179. }
  1180. }
  1181. if (_procureNext != null && _procureNext.size() > 0)
  1182. {
  1183. int count = 0;
  1184. String query = "INSERT INTO castle_manor_procure VALUES ";
  1185. String values[] = new String[_procureNext.size()];
  1186. for (CropProcure cp : _procureNext)
  1187. {
  1188. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + ","
  1189. + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1190. }
  1191. if (values.length > 0)
  1192. {
  1193. query += values[0];
  1194. for (int i = 1; i < values.length; i++)
  1195. {
  1196. query += "," + values[i];
  1197. }
  1198. statement = con.prepareStatement(query);
  1199. statement.execute();
  1200. statement.close();
  1201. }
  1202. }
  1203. }
  1204. catch (Exception e)
  1205. {
  1206. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1207. }
  1208. finally
  1209. {
  1210. L2DatabaseFactory.close(con);
  1211. }
  1212. }
  1213. // save crop procure data for specified period
  1214. public void saveCropData(int period)
  1215. {
  1216. Connection con = null;
  1217. PreparedStatement statement;
  1218. try
  1219. {
  1220. con = L2DatabaseFactory.getInstance().getConnection();
  1221. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE_PERIOD);
  1222. statement.setInt(1, getCastleId());
  1223. statement.setInt(2, period);
  1224. statement.execute();
  1225. statement.close();
  1226. List<CropProcure> proc = null;
  1227. proc = getCropProcure(period);
  1228. if (proc != null && proc.size() > 0)
  1229. {
  1230. int count = 0;
  1231. String query = "INSERT INTO castle_manor_procure VALUES ";
  1232. String values[] = new String[proc.size()];
  1233. for (CropProcure cp : proc)
  1234. {
  1235. values[count++] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + ","
  1236. + cp.getPrice() + "," + cp.getReward() + "," + period + ")";
  1237. }
  1238. if (values.length > 0)
  1239. {
  1240. query += values[0];
  1241. for (int i = 1; i < values.length; i++)
  1242. {
  1243. query += "," + values[i];
  1244. }
  1245. statement = con.prepareStatement(query);
  1246. statement.execute();
  1247. statement.close();
  1248. }
  1249. }
  1250. }
  1251. catch (Exception e)
  1252. {
  1253. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1254. }
  1255. finally
  1256. {
  1257. L2DatabaseFactory.close(con);
  1258. }
  1259. }
  1260. public void updateCrop(int cropId, long amount, int period)
  1261. {
  1262. Connection con = null;
  1263. PreparedStatement statement;
  1264. try
  1265. {
  1266. con = L2DatabaseFactory.getInstance().getConnection();
  1267. statement = con.prepareStatement(CASTLE_UPDATE_CROP);
  1268. statement.setLong(1, amount);
  1269. statement.setInt(2, cropId);
  1270. statement.setInt(3, getCastleId());
  1271. statement.setInt(4, period);
  1272. statement.execute();
  1273. statement.close();
  1274. }
  1275. catch (Exception e)
  1276. {
  1277. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1278. }
  1279. finally
  1280. {
  1281. L2DatabaseFactory.close(con);
  1282. }
  1283. }
  1284. public void updateSeed(int seedId, long amount, int period)
  1285. {
  1286. Connection con = null;
  1287. PreparedStatement statement;
  1288. try
  1289. {
  1290. con = L2DatabaseFactory.getInstance().getConnection();
  1291. statement = con.prepareStatement(CASTLE_UPDATE_SEED);
  1292. statement.setLong(1, amount);
  1293. statement.setInt(2, seedId);
  1294. statement.setInt(3, getCastleId());
  1295. statement.setInt(4, period);
  1296. statement.execute();
  1297. statement.close();
  1298. }
  1299. catch (Exception e)
  1300. {
  1301. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1302. }
  1303. finally
  1304. {
  1305. L2DatabaseFactory.close(con);
  1306. }
  1307. }
  1308. public boolean isNextPeriodApproved()
  1309. {
  1310. return _isNextPeriodApproved;
  1311. }
  1312. public void setNextPeriodApproved(boolean val)
  1313. {
  1314. _isNextPeriodApproved = val;
  1315. }
  1316. public void updateClansReputation()
  1317. {
  1318. if (_formerOwner != null)
  1319. {
  1320. if (_formerOwner != ClanTable.getInstance().getClan(getOwnerId()))
  1321. {
  1322. int maxreward = Math.max(0, _formerOwner.getReputationScore());
  1323. _formerOwner.takeReputationScore(Config.LOOSE_CASTLE_POINTS, true);
  1324. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1325. if (owner != null)
  1326. owner.addReputationScore(Math.min(Config.TAKE_CASTLE_POINTS, maxreward), true);
  1327. }
  1328. else
  1329. _formerOwner.addReputationScore(Config.CASTLE_DEFENDED_POINTS, true);
  1330. }
  1331. else
  1332. {
  1333. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1334. if (owner != null)
  1335. {
  1336. owner.addReputationScore(Config.TAKE_CASTLE_POINTS, true);
  1337. }
  1338. }
  1339. }
  1340. public void updateShowNpcCrest()
  1341. {
  1342. Connection con = null;
  1343. PreparedStatement statement;
  1344. try
  1345. {
  1346. con = L2DatabaseFactory.getInstance().getConnection();
  1347. statement = con.prepareStatement("UPDATE castle SET showNpcCrest = ? WHERE id = ?");
  1348. statement.setString(1, String.valueOf(getShowNpcCrest()));
  1349. statement.setInt(2, getCastleId());
  1350. statement.execute();
  1351. statement.close();
  1352. }
  1353. catch (Exception e)
  1354. {
  1355. _log.info("Error saving showNpcCrest for castle " + getName() + ": " + e.getMessage());
  1356. }
  1357. finally
  1358. {
  1359. L2DatabaseFactory.close(con);
  1360. }
  1361. }
  1362. public FastList<L2Skill> getResidentialSkills()
  1363. {
  1364. return _residentialSkills;
  1365. }
  1366. public void giveResidentialSkills(L2PcInstance player)
  1367. {
  1368. if (_residentialSkills != null && !_residentialSkills.isEmpty())
  1369. {
  1370. for (L2Skill sk : _residentialSkills)
  1371. player.addSkill(sk, false);
  1372. }
  1373. Territory territory = TerritoryWarManager.getInstance().getTerritory(getCastleId());
  1374. if (territory != null && territory.getOwnedWardIds().contains(getCastleId() + 80))
  1375. for(int wardId : territory.getOwnedWardIds())
  1376. if (ResidentialSkillTable.getInstance().getSkills(wardId) != null)
  1377. for (L2Skill sk : ResidentialSkillTable.getInstance().getSkills(wardId))
  1378. player.addSkill(sk, false);
  1379. }
  1380. public void removeResidentialSkills(L2PcInstance player)
  1381. {
  1382. if (_residentialSkills != null && !_residentialSkills.isEmpty())
  1383. {
  1384. for (L2Skill sk : _residentialSkills)
  1385. player.removeSkill(sk, false);
  1386. }
  1387. if (TerritoryWarManager.getInstance().getTerritory(getCastleId()) != null)
  1388. for(int wardId : TerritoryWarManager.getInstance().getTerritory(getCastleId()).getOwnedWardIds())
  1389. if (ResidentialSkillTable.getInstance().getSkills(wardId) != null)
  1390. for (L2Skill sk : ResidentialSkillTable.getInstance().getSkills(wardId))
  1391. player.removeSkill(sk, false);
  1392. }
  1393. /**
  1394. * Register Artefact to castle
  1395. * @param artefact
  1396. */
  1397. public void registerArtefact(L2ArtefactInstance artefact)
  1398. {
  1399. if (Config.DEBUG)
  1400. _log.info("ArtefactId: "+ artefact.getObjectId() + " is registered to "+getName()+" castle.");
  1401. _artefacts.add(artefact);
  1402. }
  1403. public List<L2ArtefactInstance> getArtefacts()
  1404. {
  1405. return _artefacts;
  1406. }
  1407. public void resetManor()
  1408. {
  1409. setCropProcure(new FastList<CropProcure>(), CastleManorManager.PERIOD_CURRENT);
  1410. setCropProcure(new FastList<CropProcure>(), CastleManorManager.PERIOD_NEXT);
  1411. setSeedProduction(new FastList<SeedProduction>(), CastleManorManager.PERIOD_CURRENT);
  1412. setSeedProduction(new FastList<SeedProduction>(), CastleManorManager.PERIOD_NEXT);
  1413. if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
  1414. {
  1415. saveCropData();
  1416. saveSeedData();
  1417. }
  1418. }
  1419. }