Castle.java 43 KB

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