Castle.java 41 KB

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