Castle.java 44 KB

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