Castle.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 int _siegeDayOfWeek = 7; // Default to saturday
  73. private int _siegeHourOfDay = 20; // Default to 8 pm server time
  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. _siegeDayOfWeek = rs.getInt("siegeDayOfWeek");
  603. if (_siegeDayOfWeek < 1 || _siegeDayOfWeek > 7)
  604. _siegeDayOfWeek = 7;
  605. _siegeHourOfDay = rs.getInt("siegeHourOfDay");
  606. if (_siegeHourOfDay < 0 || _siegeHourOfDay > 23)
  607. _siegeHourOfDay = 20;
  608. _taxPercent = rs.getInt("taxPercent");
  609. _treasury = rs.getInt("treasury");
  610. }
  611. statement.close();
  612. _taxRate = _taxPercent / 100.0;
  613. statement = con.prepareStatement("Select clan_id from clan_data where hasCastle = ?");
  614. statement.setInt(1, getCastleId());
  615. rs = statement.executeQuery();
  616. while (rs.next())
  617. {
  618. _ownerId = rs.getInt("clan_id");
  619. }
  620. if (getOwnerId() > 0)
  621. {
  622. L2Clan clan = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  623. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  624. }
  625. statement.close();
  626. }
  627. catch (Exception e)
  628. {
  629. _log.warning("Exception: loadCastleData(): " + e.getMessage());
  630. e.printStackTrace();
  631. }
  632. finally
  633. {
  634. try
  635. {
  636. con.close();
  637. }
  638. catch (Exception e)
  639. {
  640. }
  641. }
  642. }
  643. /** Load All Functions */
  644. private void loadFunctions()
  645. {
  646. java.sql.Connection con = null;
  647. try
  648. {
  649. PreparedStatement statement;
  650. ResultSet rs;
  651. con = L2DatabaseFactory.getInstance().getConnection();
  652. statement = con.prepareStatement("Select * from castle_functions where castle_id = ?");
  653. statement.setInt(1, getCastleId());
  654. rs = statement.executeQuery();
  655. while (rs.next())
  656. {
  657. _function.put(rs.getInt("type"), new CastleFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  658. }
  659. statement.close();
  660. }
  661. catch (Exception e)
  662. {
  663. _log.log(Level.SEVERE, "Exception: Castle.loadFunctions(): " + e.getMessage(), e);
  664. }
  665. finally
  666. {
  667. try
  668. {
  669. con.close();
  670. }
  671. catch (Exception e)
  672. {
  673. }
  674. }
  675. }
  676. /** Remove function In List and in DB */
  677. public void removeFunction(int functionType)
  678. {
  679. _function.remove(functionType);
  680. java.sql.Connection con = null;
  681. try
  682. {
  683. PreparedStatement statement;
  684. con = L2DatabaseFactory.getInstance().getConnection();
  685. statement = con.prepareStatement("DELETE FROM castle_functions WHERE castle_id=? AND type=?");
  686. statement.setInt(1, getCastleId());
  687. statement.setInt(2, functionType);
  688. statement.execute();
  689. statement.close();
  690. }
  691. catch (Exception e)
  692. {
  693. _log.log(Level.SEVERE, "Exception: Castle.removeFunctions(int functionType): " + e.getMessage(), e);
  694. }
  695. finally
  696. {
  697. try
  698. {
  699. con.close();
  700. }
  701. catch (Exception e)
  702. {
  703. }
  704. }
  705. }
  706. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  707. {
  708. if (player == null)
  709. return false;
  710. if (Config.DEBUG)
  711. _log.warning("Called Castle.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerId());
  712. if (lease > 0)
  713. if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
  714. return false;
  715. if (addNew)
  716. {
  717. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, 0, false));
  718. }
  719. else
  720. {
  721. if (lvl == 0 && lease == 0)
  722. removeFunction(type);
  723. else
  724. {
  725. int diffLease = lease - _function.get(type).getLease();
  726. if (Config.DEBUG)
  727. _log.warning("Called Castle.updateFunctions diffLease : " + diffLease);
  728. if (diffLease > 0)
  729. {
  730. _function.remove(type);
  731. _function.put(type, new CastleFunction(type, lvl, lease, 0, rate, -1, false));
  732. }
  733. else
  734. {
  735. _function.get(type).setLease(lease);
  736. _function.get(type).setLvl(lvl);
  737. _function.get(type).dbSave(false);
  738. }
  739. }
  740. }
  741. return true;
  742. }
  743. // This method loads castle door data from database
  744. private void loadDoor()
  745. {
  746. java.sql.Connection con = null;
  747. try
  748. {
  749. con = L2DatabaseFactory.getInstance().getConnection();
  750. PreparedStatement statement = con.prepareStatement("Select * from castle_door where castleId = ?");
  751. statement.setInt(1, getCastleId());
  752. ResultSet rs = statement.executeQuery();
  753. while (rs.next())
  754. {
  755. // Create list of the door default for use when respawning dead doors
  756. _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") + ";"
  757. + 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"));
  758. L2DoorInstance door = DoorTable.parseList(_doorDefault.get(_doorDefault.size() - 1));
  759. door.spawnMe(door.getX(), door.getY(), door.getZ());
  760. _doors.add(door);
  761. DoorTable.getInstance().putDoor(door);
  762. }
  763. statement.close();
  764. }
  765. catch (Exception e)
  766. {
  767. _log.warning("Exception: loadCastleDoor(): " + e.getMessage());
  768. e.printStackTrace();
  769. }
  770. finally
  771. {
  772. try
  773. {
  774. con.close();
  775. }
  776. catch (Exception e)
  777. {
  778. }
  779. }
  780. }
  781. // This method loads castle door upgrade data from database
  782. private void loadDoorUpgrade()
  783. {
  784. java.sql.Connection con = null;
  785. try
  786. {
  787. con = L2DatabaseFactory.getInstance().getConnection();
  788. PreparedStatement statement = con.prepareStatement("Select * from castle_doorupgrade where doorId in (Select Id from castle_door where castleId = ?)");
  789. statement.setInt(1, getCastleId());
  790. ResultSet rs = statement.executeQuery();
  791. while (rs.next())
  792. {
  793. upgradeDoor(rs.getInt("id"), rs.getInt("hp"), rs.getInt("pDef"), rs.getInt("mDef"));
  794. }
  795. statement.close();
  796. }
  797. catch (Exception e)
  798. {
  799. _log.warning("Exception: loadCastleDoorUpgrade(): " + e.getMessage());
  800. e.printStackTrace();
  801. }
  802. finally
  803. {
  804. try
  805. {
  806. con.close();
  807. }
  808. catch (Exception e)
  809. {
  810. }
  811. }
  812. }
  813. private void removeDoorUpgrade()
  814. {
  815. java.sql.Connection con = null;
  816. try
  817. {
  818. con = L2DatabaseFactory.getInstance().getConnection();
  819. PreparedStatement statement = con.prepareStatement("delete from castle_doorupgrade where doorId in (select id from castle_door where castleId=?)");
  820. statement.setInt(1, getCastleId());
  821. statement.execute();
  822. statement.close();
  823. }
  824. catch (Exception e)
  825. {
  826. _log.warning("Exception: removeDoorUpgrade(): " + e.getMessage());
  827. e.printStackTrace();
  828. }
  829. finally
  830. {
  831. try
  832. {
  833. con.close();
  834. }
  835. catch (Exception e)
  836. {
  837. }
  838. }
  839. }
  840. private void saveDoorUpgrade(int doorId, int hp, int pDef, int mDef)
  841. {
  842. java.sql.Connection con = null;
  843. try
  844. {
  845. con = L2DatabaseFactory.getInstance().getConnection();
  846. PreparedStatement statement = con.prepareStatement("INSERT INTO castle_doorupgrade (doorId, hp, pDef, mDef) values (?,?,?,?)");
  847. statement.setInt(1, doorId);
  848. statement.setInt(2, hp);
  849. statement.setInt(3, pDef);
  850. statement.setInt(4, mDef);
  851. statement.execute();
  852. statement.close();
  853. }
  854. catch (Exception e)
  855. {
  856. _log.warning("Exception: saveDoorUpgrade(int doorId, int hp, int pDef, int mDef): " + e.getMessage());
  857. e.printStackTrace();
  858. }
  859. finally
  860. {
  861. try
  862. {
  863. con.close();
  864. }
  865. catch (Exception e)
  866. {
  867. }
  868. }
  869. }
  870. private void updateOwnerInDB(L2Clan clan)
  871. {
  872. if (clan != null)
  873. _ownerId = clan.getClanId(); // Update owner id property
  874. else
  875. _ownerId = 0; // Remove owner
  876. java.sql.Connection con = null;
  877. try
  878. {
  879. con = L2DatabaseFactory.getInstance().getConnection();
  880. PreparedStatement statement;
  881. // ============================================================================
  882. // NEED TO REMOVE HAS CASTLE FLAG FROM CLAN_DATA
  883. // SHOULD BE CHECKED FROM CASTLE TABLE
  884. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=0 WHERE hasCastle=?");
  885. statement.setInt(1, getCastleId());
  886. statement.execute();
  887. statement.close();
  888. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=? WHERE clan_id=?");
  889. statement.setInt(1, getCastleId());
  890. statement.setInt(2, getOwnerId());
  891. statement.execute();
  892. statement.close();
  893. // ============================================================================
  894. // Announce to clan memebers
  895. if (clan != null)
  896. {
  897. clan.setHasCastle(getCastleId()); // Set has castle flag for new owner
  898. Announcements.getInstance().announceToAll(clan.getName() + " has taken " + getName() + " castle!");
  899. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  900. clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
  901. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  902. }
  903. }
  904. catch (Exception e)
  905. {
  906. _log.warning("Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage());
  907. e.printStackTrace();
  908. }
  909. finally
  910. {
  911. try
  912. {
  913. con.close();
  914. }
  915. catch (Exception e)
  916. {
  917. }
  918. }
  919. }
  920. // =========================================================
  921. // Property
  922. public final int getCastleId()
  923. {
  924. return _castleId;
  925. }
  926. public final L2DoorInstance getDoor(int doorId)
  927. {
  928. if (doorId <= 0)
  929. return null;
  930. for (int i = 0; i < getDoors().size(); i++)
  931. {
  932. L2DoorInstance door = getDoors().get(i);
  933. if (door.getDoorId() == doorId)
  934. return door;
  935. }
  936. return null;
  937. }
  938. public final List<L2DoorInstance> getDoors()
  939. {
  940. return _doors;
  941. }
  942. public final String getName()
  943. {
  944. return _name;
  945. }
  946. public final int getOwnerId()
  947. {
  948. return _ownerId;
  949. }
  950. public final Siege getSiege()
  951. {
  952. if (_siege == null)
  953. _siege = new Siege(new Castle[]
  954. {
  955. this
  956. });
  957. return _siege;
  958. }
  959. public final Calendar getSiegeDate()
  960. {
  961. return _siegeDate;
  962. }
  963. public final int getSiegeDayOfWeek()
  964. {
  965. return _siegeDayOfWeek;
  966. }
  967. public final int getSiegeHourOfDay()
  968. {
  969. return _siegeHourOfDay;
  970. }
  971. public final int getTaxPercent()
  972. {
  973. return _taxPercent;
  974. }
  975. public final double getTaxRate()
  976. {
  977. return _taxRate;
  978. }
  979. public final int getTreasury()
  980. {
  981. return _treasury;
  982. }
  983. public FastList<SeedProduction> getSeedProduction(int period)
  984. {
  985. return (period == CastleManorManager.PERIOD_CURRENT ? _production : _productionNext);
  986. }
  987. public FastList<CropProcure> getCropProcure(int period)
  988. {
  989. return (period == CastleManorManager.PERIOD_CURRENT ? _procure : _procureNext);
  990. }
  991. public void setSeedProduction(FastList<SeedProduction> seed, int period)
  992. {
  993. if (period == CastleManorManager.PERIOD_CURRENT)
  994. _production = seed;
  995. else
  996. _productionNext = seed;
  997. }
  998. public void setCropProcure(FastList<CropProcure> crop, int period)
  999. {
  1000. if (period == CastleManorManager.PERIOD_CURRENT)
  1001. _procure = crop;
  1002. else
  1003. _procureNext = crop;
  1004. }
  1005. public synchronized SeedProduction getSeed(int seedId, int period)
  1006. {
  1007. for (SeedProduction seed : getSeedProduction(period))
  1008. {
  1009. if (seed.getId() == seedId)
  1010. {
  1011. return seed;
  1012. }
  1013. }
  1014. return null;
  1015. }
  1016. public synchronized CropProcure getCrop(int cropId, int period)
  1017. {
  1018. for (CropProcure crop : getCropProcure(period))
  1019. {
  1020. if (crop.getId() == cropId)
  1021. {
  1022. return crop;
  1023. }
  1024. }
  1025. return null;
  1026. }
  1027. public int getManorCost(int period)
  1028. {
  1029. FastList<CropProcure> procure;
  1030. FastList<SeedProduction> production;
  1031. if (period == CastleManorManager.PERIOD_CURRENT)
  1032. {
  1033. procure = _procure;
  1034. production = _production;
  1035. }
  1036. else
  1037. {
  1038. procure = _procureNext;
  1039. production = _productionNext;
  1040. }
  1041. int total = 0;
  1042. if (production != null)
  1043. {
  1044. for (SeedProduction seed : production)
  1045. {
  1046. total += L2Manor.getInstance().getSeedBuyPrice(seed.getId()) * seed.getStartProduce();
  1047. }
  1048. }
  1049. if (procure != null)
  1050. {
  1051. for (CropProcure crop : procure)
  1052. {
  1053. total += crop.getPrice() * crop.getStartAmount();
  1054. }
  1055. }
  1056. return total;
  1057. }
  1058. //save manor production data
  1059. public void saveSeedData()
  1060. {
  1061. java.sql.Connection con = null;
  1062. PreparedStatement statement;
  1063. try
  1064. {
  1065. con = L2DatabaseFactory.getInstance().getConnection();
  1066. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION);
  1067. statement.setInt(1, getCastleId());
  1068. statement.execute();
  1069. statement.close();
  1070. if (_production != null)
  1071. {
  1072. int count = 0;
  1073. String query = "INSERT INTO castle_manor_production VALUES ";
  1074. String values[] = new String[_production.size()];
  1075. for (SeedProduction s : _production)
  1076. {
  1077. values[count] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1078. count++;
  1079. }
  1080. if (values.length > 0)
  1081. {
  1082. query += values[0];
  1083. for (int i = 1; i < values.length; i++)
  1084. {
  1085. query += "," + values[i];
  1086. }
  1087. statement = con.prepareStatement(query);
  1088. statement.execute();
  1089. statement.close();
  1090. }
  1091. }
  1092. if (_productionNext != null)
  1093. {
  1094. int count = 0;
  1095. String query = "INSERT INTO castle_manor_production VALUES ";
  1096. String values[] = new String[_productionNext.size()];
  1097. for (SeedProduction s : _productionNext)
  1098. {
  1099. values[count] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1100. count++;
  1101. }
  1102. if (values.length > 0)
  1103. {
  1104. query += values[0];
  1105. for (int i = 1; i < values.length; i++)
  1106. {
  1107. query += "," + values[i];
  1108. }
  1109. statement = con.prepareStatement(query);
  1110. statement.execute();
  1111. statement.close();
  1112. }
  1113. }
  1114. }
  1115. catch (Exception e)
  1116. {
  1117. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1118. }
  1119. finally
  1120. {
  1121. try
  1122. {
  1123. con.close();
  1124. }
  1125. catch (Exception e)
  1126. {
  1127. }
  1128. }
  1129. }
  1130. //save manor production data for specified period
  1131. public void saveSeedData(int period)
  1132. {
  1133. java.sql.Connection con = null;
  1134. PreparedStatement statement;
  1135. try
  1136. {
  1137. con = L2DatabaseFactory.getInstance().getConnection();
  1138. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION_PERIOD);
  1139. statement.setInt(1, getCastleId());
  1140. statement.setInt(2, period);
  1141. statement.execute();
  1142. statement.close();
  1143. FastList<SeedProduction> prod = null;
  1144. prod = getSeedProduction(period);
  1145. if (prod != null)
  1146. {
  1147. int count = 0;
  1148. String query = "INSERT INTO castle_manor_production VALUES ";
  1149. String values[] = new String[prod.size()];
  1150. for (SeedProduction s : prod)
  1151. {
  1152. values[count] = "(" + getCastleId() + "," + s.getId() + "," + s.getCanProduce() + "," + s.getStartProduce() + "," + s.getPrice() + "," + period + ")";
  1153. count++;
  1154. }
  1155. if (values.length > 0)
  1156. {
  1157. query += values[0];
  1158. for (int i = 1; i < values.length; i++)
  1159. {
  1160. query += "," + values[i];
  1161. }
  1162. statement = con.prepareStatement(query);
  1163. statement.execute();
  1164. statement.close();
  1165. }
  1166. }
  1167. }
  1168. catch (Exception e)
  1169. {
  1170. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1171. }
  1172. finally
  1173. {
  1174. try
  1175. {
  1176. con.close();
  1177. }
  1178. catch (Exception e)
  1179. {
  1180. }
  1181. }
  1182. }
  1183. //save crop procure data
  1184. public void saveCropData()
  1185. {
  1186. java.sql.Connection con = null;
  1187. PreparedStatement statement;
  1188. try
  1189. {
  1190. con = L2DatabaseFactory.getInstance().getConnection();
  1191. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE);
  1192. statement.setInt(1, getCastleId());
  1193. statement.execute();
  1194. statement.close();
  1195. if (_procure != null)
  1196. {
  1197. int count = 0;
  1198. String query = "INSERT INTO castle_manor_procure VALUES ";
  1199. String values[] = new String[_procure.size()];
  1200. for (CropProcure cp : _procure)
  1201. {
  1202. values[count] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_CURRENT + ")";
  1203. count++;
  1204. }
  1205. if (values.length > 0)
  1206. {
  1207. query += values[0];
  1208. for (int i = 1; i < values.length; i++)
  1209. {
  1210. query += "," + values[i];
  1211. }
  1212. statement = con.prepareStatement(query);
  1213. statement.execute();
  1214. statement.close();
  1215. }
  1216. }
  1217. if (_procureNext != null)
  1218. {
  1219. int count = 0;
  1220. String query = "INSERT INTO castle_manor_procure VALUES ";
  1221. String values[] = new String[_procureNext.size()];
  1222. for (CropProcure cp : _procureNext)
  1223. {
  1224. values[count] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + CastleManorManager.PERIOD_NEXT + ")";
  1225. count++;
  1226. }
  1227. if (values.length > 0)
  1228. {
  1229. query += values[0];
  1230. for (int i = 1; i < values.length; i++)
  1231. {
  1232. query += "," + values[i];
  1233. }
  1234. statement = con.prepareStatement(query);
  1235. statement.execute();
  1236. statement.close();
  1237. }
  1238. }
  1239. }
  1240. catch (Exception e)
  1241. {
  1242. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1243. }
  1244. finally
  1245. {
  1246. try
  1247. {
  1248. con.close();
  1249. }
  1250. catch (Exception e)
  1251. {
  1252. }
  1253. }
  1254. }
  1255. // save crop procure data for specified period
  1256. public void saveCropData(int period)
  1257. {
  1258. java.sql.Connection con = null;
  1259. PreparedStatement statement;
  1260. try
  1261. {
  1262. con = L2DatabaseFactory.getInstance().getConnection();
  1263. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE_PERIOD);
  1264. statement.setInt(1, getCastleId());
  1265. statement.setInt(2, period);
  1266. statement.execute();
  1267. statement.close();
  1268. FastList<CropProcure> proc = null;
  1269. proc = getCropProcure(period);
  1270. if (proc != null)
  1271. {
  1272. int count = 0;
  1273. String query = "INSERT INTO castle_manor_procure VALUES ";
  1274. String values[] = new String[proc.size()];
  1275. for (CropProcure cp : proc)
  1276. {
  1277. values[count] = "(" + getCastleId() + "," + cp.getId() + "," + cp.getAmount() + "," + cp.getStartAmount() + "," + cp.getPrice() + "," + cp.getReward() + "," + period + ")";
  1278. count++;
  1279. }
  1280. if (values.length > 0)
  1281. {
  1282. query += values[0];
  1283. for (int i = 1; i < values.length; i++)
  1284. {
  1285. query += "," + values[i];
  1286. }
  1287. statement = con.prepareStatement(query);
  1288. statement.execute();
  1289. statement.close();
  1290. }
  1291. }
  1292. }
  1293. catch (Exception e)
  1294. {
  1295. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1296. }
  1297. finally
  1298. {
  1299. try
  1300. {
  1301. con.close();
  1302. }
  1303. catch (Exception e)
  1304. {
  1305. }
  1306. }
  1307. }
  1308. public void updateCrop(int cropId, int amount, int period)
  1309. {
  1310. java.sql.Connection con = null;
  1311. PreparedStatement statement;
  1312. try
  1313. {
  1314. con = L2DatabaseFactory.getInstance().getConnection();
  1315. statement = con.prepareStatement(CASTLE_UPDATE_CROP);
  1316. statement.setInt(1, amount);
  1317. statement.setInt(2, cropId);
  1318. statement.setInt(3, getCastleId());
  1319. statement.setInt(4, period);
  1320. statement.execute();
  1321. statement.close();
  1322. }
  1323. catch (Exception e)
  1324. {
  1325. _log.info("Error adding crop data for castle " + getName() + ": " + e.getMessage());
  1326. }
  1327. finally
  1328. {
  1329. try
  1330. {
  1331. con.close();
  1332. }
  1333. catch (Exception e)
  1334. {
  1335. }
  1336. }
  1337. }
  1338. public void updateSeed(int seedId, int amount, int period)
  1339. {
  1340. java.sql.Connection con = null;
  1341. PreparedStatement statement;
  1342. try
  1343. {
  1344. con = L2DatabaseFactory.getInstance().getConnection();
  1345. statement = con.prepareStatement(CASTLE_UPDATE_SEED);
  1346. statement.setInt(1, amount);
  1347. statement.setInt(2, seedId);
  1348. statement.setInt(3, getCastleId());
  1349. statement.setInt(4, period);
  1350. statement.execute();
  1351. statement.close();
  1352. }
  1353. catch (Exception e)
  1354. {
  1355. _log.info("Error adding seed production data for castle " + getName() + ": " + e.getMessage());
  1356. }
  1357. finally
  1358. {
  1359. try
  1360. {
  1361. con.close();
  1362. }
  1363. catch (Exception e)
  1364. {
  1365. }
  1366. }
  1367. }
  1368. public boolean isNextPeriodApproved()
  1369. {
  1370. return _isNextPeriodApproved;
  1371. }
  1372. public void setNextPeriodApproved(boolean val)
  1373. {
  1374. _isNextPeriodApproved = val;
  1375. }
  1376. public void updateClansReputation()
  1377. {
  1378. if (_formerOwner != null)
  1379. {
  1380. if (_formerOwner != ClanTable.getInstance().getClan(getOwnerId()))
  1381. {
  1382. int maxreward = Math.max(0, _formerOwner.getReputationScore());
  1383. _formerOwner.setReputationScore(_formerOwner.getReputationScore() - 1000, true);
  1384. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1385. if (owner != null)
  1386. {
  1387. owner.setReputationScore(owner.getReputationScore() + Math.min(1000, maxreward), true);
  1388. owner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(owner));
  1389. }
  1390. }
  1391. else
  1392. _formerOwner.setReputationScore(_formerOwner.getReputationScore() + 500, true);
  1393. _formerOwner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(_formerOwner));
  1394. }
  1395. else
  1396. {
  1397. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  1398. if (owner != null)
  1399. {
  1400. owner.setReputationScore(owner.getReputationScore() + 1000, true);
  1401. owner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(owner));
  1402. }
  1403. }
  1404. }
  1405. }