Castle.java 42 KB

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