Castle.java 43 KB

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