Castle.java 40 KB

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