AdminSiege.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.admincommandhandlers;
  20. import java.util.Calendar;
  21. import java.util.StringTokenizer;
  22. import com.l2jserver.gameserver.SevenSigns;
  23. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  24. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  25. import com.l2jserver.gameserver.instancemanager.AuctionManager;
  26. import com.l2jserver.gameserver.instancemanager.CHSiegeManager;
  27. import com.l2jserver.gameserver.instancemanager.CastleManager;
  28. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  29. import com.l2jserver.gameserver.model.L2Clan;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.entity.Castle;
  32. import com.l2jserver.gameserver.model.entity.ClanHall;
  33. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  34. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  35. import com.l2jserver.gameserver.network.SystemMessageId;
  36. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  37. import com.l2jserver.gameserver.util.Util;
  38. import com.l2jserver.util.StringUtil;
  39. /**
  40. * This class handles all siege commands.
  41. * @author Zoey76 (rework)
  42. */
  43. public class AdminSiege implements IAdminCommandHandler
  44. {
  45. private static final String[] ADMIN_COMMANDS =
  46. {
  47. // Castle commands
  48. "admin_siege",
  49. "admin_add_attacker",
  50. "admin_add_defender",
  51. "admin_add_guard",
  52. "admin_list_siege_clans",
  53. "admin_clear_siege_list",
  54. "admin_move_defenders",
  55. "admin_spawn_doors",
  56. "admin_endsiege",
  57. "admin_startsiege",
  58. "admin_setsiegetime",
  59. "admin_setcastle",
  60. "admin_removecastle",
  61. // Clan hall commands
  62. "admin_clanhall",
  63. "admin_clanhallset",
  64. "admin_clanhalldel",
  65. "admin_clanhallopendoors",
  66. "admin_clanhallclosedoors",
  67. "admin_clanhallteleportself"
  68. };
  69. @Override
  70. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  71. {
  72. StringTokenizer st = new StringTokenizer(command, " ");
  73. command = st.nextToken(); // Get actual command
  74. // Get castle
  75. Castle castle = null;
  76. ClanHall clanhall = null;
  77. if (st.hasMoreTokens())
  78. {
  79. L2PcInstance player = null;
  80. if ((activeChar.getTarget() != null) && activeChar.getTarget().isPlayer())
  81. {
  82. player = activeChar.getTarget().getActingPlayer();
  83. }
  84. String val = st.nextToken();
  85. if (command.startsWith("admin_clanhall"))
  86. {
  87. if (Util.isDigit(val))
  88. {
  89. clanhall = ClanHallManager.getInstance().getClanHallById(Integer.parseInt(val));
  90. L2Clan clan = null;
  91. switch (command)
  92. {
  93. case "admin_clanhallset":
  94. if ((player == null) || (player.getClan() == null))
  95. {
  96. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  97. return false;
  98. }
  99. if (clanhall.getOwnerId() > 0)
  100. {
  101. activeChar.sendMessage("This Clan Hall is not free!");
  102. return false;
  103. }
  104. clan = player.getClan();
  105. if (clan.getHideoutId() > 0)
  106. {
  107. activeChar.sendMessage("You have already a Clan Hall!");
  108. return false;
  109. }
  110. if (!clanhall.isSiegableHall())
  111. {
  112. ClanHallManager.getInstance().setOwner(clanhall.getId(), clan);
  113. if (AuctionManager.getInstance().getAuction(clanhall.getId()) != null)
  114. {
  115. AuctionManager.getInstance().getAuction(clanhall.getId()).deleteAuctionFromDB();
  116. }
  117. }
  118. else
  119. {
  120. clanhall.setOwner(clan);
  121. clan.setHideoutId(clanhall.getId());
  122. }
  123. break;
  124. case "admin_clanhalldel":
  125. if (!clanhall.isSiegableHall())
  126. {
  127. if (!ClanHallManager.getInstance().isFree(clanhall.getId()))
  128. {
  129. ClanHallManager.getInstance().setFree(clanhall.getId());
  130. AuctionManager.getInstance().initNPC(clanhall.getId());
  131. }
  132. else
  133. {
  134. activeChar.sendMessage("This Clan Hall is already free!");
  135. }
  136. }
  137. else
  138. {
  139. final int oldOwner = clanhall.getOwnerId();
  140. if (oldOwner > 0)
  141. {
  142. clanhall.free();
  143. clan = ClanTable.getInstance().getClan(oldOwner);
  144. if (clan != null)
  145. {
  146. clan.setHideoutId(0);
  147. clan.broadcastClanStatus();
  148. }
  149. }
  150. }
  151. break;
  152. case "admin_clanhallopendoors":
  153. clanhall.openCloseDoors(true);
  154. break;
  155. case "admin_clanhallclosedoors":
  156. clanhall.openCloseDoors(false);
  157. break;
  158. case "admin_clanhallteleportself":
  159. final L2ClanHallZone zone = clanhall.getZone();
  160. if (zone != null)
  161. {
  162. activeChar.teleToLocation(zone.getSpawnLoc(), true);
  163. }
  164. break;
  165. default:
  166. if (!clanhall.isSiegableHall())
  167. {
  168. showClanHallPage(activeChar, clanhall);
  169. }
  170. else
  171. {
  172. showSiegableHallPage(activeChar, (SiegableHall) clanhall);
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. else
  179. {
  180. castle = CastleManager.getInstance().getCastle(val);
  181. switch (command)
  182. {
  183. case "admin_add_attacker":
  184. if (player == null)
  185. {
  186. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  187. }
  188. else
  189. {
  190. castle.getSiege().registerAttacker(player, true);
  191. }
  192. break;
  193. case "admin_add_defender":
  194. if (player == null)
  195. {
  196. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  197. }
  198. else
  199. {
  200. castle.getSiege().registerDefender(player, true);
  201. }
  202. break;
  203. case "admin_add_guard":
  204. if (st.hasMoreTokens())
  205. {
  206. val = st.nextToken();
  207. if (Util.isDigit(val))
  208. {
  209. castle.getSiege().getSiegeGuardManager().addSiegeGuard(activeChar, Integer.parseInt(val));
  210. break;
  211. }
  212. }
  213. // If doesn't have more tokens or token is not a number.
  214. activeChar.sendMessage("Usage: //add_guard castle npcId");
  215. break;
  216. case "admin_clear_siege_list":
  217. castle.getSiege().clearSiegeClan();
  218. break;
  219. case "admin_endsiege":
  220. castle.getSiege().endSiege();
  221. break;
  222. case "admin_list_siege_clans":
  223. castle.getSiege().listRegisterClan(activeChar);
  224. break;
  225. case "admin_move_defenders":
  226. activeChar.sendMessage("Not implemented yet.");
  227. break;
  228. case "admin_setcastle":
  229. if ((player == null) || (player.getClan() == null))
  230. {
  231. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  232. }
  233. else
  234. {
  235. castle.setOwner(player.getClan());
  236. }
  237. break;
  238. case "admin_removecastle":
  239. final L2Clan clan = ClanTable.getInstance().getClan(castle.getOwnerId());
  240. if (clan != null)
  241. {
  242. castle.removeOwner(clan);
  243. }
  244. else
  245. {
  246. activeChar.sendMessage("Unable to remove castle.");
  247. }
  248. break;
  249. case "admin_setsiegetime":
  250. if (st.hasMoreTokens())
  251. {
  252. final Calendar cal = Calendar.getInstance();
  253. cal.setTimeInMillis(castle.getSiegeDate().getTimeInMillis());
  254. val = st.nextToken();
  255. if ("month".equals(val))
  256. {
  257. int month = cal.get(Calendar.MONTH) + Integer.parseInt(st.nextToken());
  258. if ((cal.getActualMinimum(Calendar.MONTH) > month) || (cal.getActualMaximum(Calendar.MONTH) < month))
  259. {
  260. activeChar.sendMessage("Unable to change Siege Date - Incorrect month value only " + cal.getActualMinimum(Calendar.MONTH) + "-" + cal.getActualMaximum(Calendar.MONTH) + " is accepted!");
  261. return false;
  262. }
  263. cal.set(Calendar.MONTH, month);
  264. }
  265. else if ("day".equals(val))
  266. {
  267. int day = Integer.parseInt(st.nextToken());
  268. if ((cal.getActualMinimum(Calendar.DAY_OF_MONTH) > day) || (cal.getActualMaximum(Calendar.DAY_OF_MONTH) < day))
  269. {
  270. activeChar.sendMessage("Unable to change Siege Date - Incorrect day value only " + cal.getActualMinimum(Calendar.DAY_OF_MONTH) + "-" + cal.getActualMaximum(Calendar.DAY_OF_MONTH) + " is accepted!");
  271. return false;
  272. }
  273. cal.set(Calendar.DAY_OF_MONTH, day);
  274. }
  275. else if ("hour".equals(val))
  276. {
  277. int hour = Integer.parseInt(st.nextToken());
  278. if ((cal.getActualMinimum(Calendar.HOUR_OF_DAY) > hour) || (cal.getActualMaximum(Calendar.HOUR_OF_DAY) < hour))
  279. {
  280. activeChar.sendMessage("Unable to change Siege Date - Incorrect hour value only " + cal.getActualMinimum(Calendar.HOUR_OF_DAY) + "-" + cal.getActualMaximum(Calendar.HOUR_OF_DAY) + " is accepted!");
  281. return false;
  282. }
  283. cal.set(Calendar.HOUR_OF_DAY, hour);
  284. }
  285. else if ("min".equals(val))
  286. {
  287. int min = Integer.parseInt(st.nextToken());
  288. if ((cal.getActualMinimum(Calendar.MINUTE) > min) || (cal.getActualMaximum(Calendar.MINUTE) < min))
  289. {
  290. activeChar.sendMessage("Unable to change Siege Date - Incorrect minute value only " + cal.getActualMinimum(Calendar.MINUTE) + "-" + cal.getActualMaximum(Calendar.MINUTE) + " is accepted!");
  291. return false;
  292. }
  293. cal.set(Calendar.MINUTE, min);
  294. }
  295. if (cal.getTimeInMillis() < Calendar.getInstance().getTimeInMillis())
  296. {
  297. activeChar.sendMessage("Unable to change Siege Date");
  298. }
  299. else if (cal.getTimeInMillis() != castle.getSiegeDate().getTimeInMillis())
  300. {
  301. castle.getSiegeDate().setTimeInMillis(cal.getTimeInMillis());
  302. castle.getSiege().saveSiegeDate();
  303. activeChar.sendMessage("Castle siege time for castle " + castle.getName() + " has been changed.");
  304. }
  305. }
  306. showSiegeTimePage(activeChar, castle);
  307. break;
  308. case "admin_spawn_doors":
  309. castle.spawnDoor();
  310. break;
  311. case "admin_startsiege":
  312. castle.getSiege().startSiege();
  313. break;
  314. default:
  315. showSiegePage(activeChar, castle.getName());
  316. break;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. showCastleSelectPage(activeChar);
  323. }
  324. return true;
  325. }
  326. /**
  327. * Show castle select page.
  328. * @param activeChar the active char
  329. */
  330. private void showCastleSelectPage(L2PcInstance activeChar)
  331. {
  332. int i = 0;
  333. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  334. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/castles.htm");
  335. final StringBuilder cList = new StringBuilder(500);
  336. for (Castle castle : CastleManager.getInstance().getCastles())
  337. {
  338. if (castle != null)
  339. {
  340. String name = castle.getName();
  341. StringUtil.append(cList, "<td fixwidth=90><a action=\"bypass -h admin_siege ", name, "\">", name, "</a></td>");
  342. i++;
  343. }
  344. if (i > 2)
  345. {
  346. cList.append("</tr><tr>");
  347. i = 0;
  348. }
  349. }
  350. adminReply.replace("%castles%", cList.toString());
  351. cList.setLength(0);
  352. i = 0;
  353. for (SiegableHall hall : CHSiegeManager.getInstance().getConquerableHalls().values())
  354. {
  355. if (hall != null)
  356. {
  357. StringUtil.append(cList, "<td fixwidth=90><a action=\"bypass -h admin_chsiege_siegablehall ", String.valueOf(hall.getId()), "\">", hall.getName(), "</a></td>");
  358. i++;
  359. }
  360. if (i > 1)
  361. {
  362. cList.append("</tr><tr>");
  363. i = 0;
  364. }
  365. }
  366. adminReply.replace("%siegableHalls%", cList.toString());
  367. cList.setLength(0);
  368. i = 0;
  369. for (ClanHall clanhall : ClanHallManager.getInstance().getClanHalls().values())
  370. {
  371. if (clanhall != null)
  372. {
  373. StringUtil.append(cList, "<td fixwidth=134><a action=\"bypass -h admin_clanhall ", String.valueOf(clanhall.getId()), "\">", clanhall.getName(), "</a></td>");
  374. i++;
  375. }
  376. if (i > 1)
  377. {
  378. cList.append("</tr><tr>");
  379. i = 0;
  380. }
  381. }
  382. adminReply.replace("%clanhalls%", cList.toString());
  383. cList.setLength(0);
  384. i = 0;
  385. for (ClanHall clanhall : ClanHallManager.getInstance().getFreeClanHalls().values())
  386. {
  387. if (clanhall != null)
  388. {
  389. StringUtil.append(cList, "<td fixwidth=134><a action=\"bypass -h admin_clanhall ", String.valueOf(clanhall.getId()), "\">", clanhall.getName(), "</a></td>");
  390. i++;
  391. }
  392. if (i > 1)
  393. {
  394. cList.append("</tr><tr>");
  395. i = 0;
  396. }
  397. }
  398. adminReply.replace("%freeclanhalls%", cList.toString());
  399. activeChar.sendPacket(adminReply);
  400. }
  401. /**
  402. * Show the siege page.
  403. * @param activeChar the active char
  404. * @param castleName the castle name
  405. */
  406. private void showSiegePage(L2PcInstance activeChar, String castleName)
  407. {
  408. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  409. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/castle.htm");
  410. adminReply.replace("%castleName%", castleName);
  411. activeChar.sendPacket(adminReply);
  412. }
  413. /**
  414. * Show the siege time page.
  415. * @param activeChar the active char
  416. * @param castle the castle
  417. */
  418. private void showSiegeTimePage(L2PcInstance activeChar, Castle castle)
  419. {
  420. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  421. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/castlesiegetime.htm");
  422. adminReply.replace("%castleName%", castle.getName());
  423. adminReply.replace("%time%", castle.getSiegeDate().getTime().toString());
  424. final Calendar newDay = Calendar.getInstance();
  425. boolean isSunday = false;
  426. if (newDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
  427. {
  428. isSunday = true;
  429. }
  430. else
  431. {
  432. newDay.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  433. }
  434. if (!SevenSigns.getInstance().isDateInSealValidPeriod(newDay))
  435. {
  436. newDay.add(Calendar.DAY_OF_MONTH, 7);
  437. }
  438. if (isSunday)
  439. {
  440. adminReply.replace("%sundaylink%", String.valueOf(newDay.get(Calendar.DAY_OF_YEAR)));
  441. adminReply.replace("%sunday%", String.valueOf(newDay.get(Calendar.MONTH) + "/" + String.valueOf(newDay.get(Calendar.DAY_OF_MONTH))));
  442. newDay.add(Calendar.DAY_OF_MONTH, 13);
  443. adminReply.replace("%saturdaylink%", String.valueOf(newDay.get(Calendar.DAY_OF_YEAR)));
  444. adminReply.replace("%saturday%", String.valueOf(newDay.get(Calendar.MONTH) + "/" + String.valueOf(newDay.get(Calendar.DAY_OF_MONTH))));
  445. }
  446. else
  447. {
  448. adminReply.replace("%saturdaylink%", String.valueOf(newDay.get(Calendar.DAY_OF_YEAR)));
  449. adminReply.replace("%saturday%", String.valueOf(newDay.get(Calendar.MONTH) + "/" + String.valueOf(newDay.get(Calendar.DAY_OF_MONTH))));
  450. newDay.add(Calendar.DAY_OF_MONTH, 1);
  451. adminReply.replace("%sundaylink%", String.valueOf(newDay.get(Calendar.DAY_OF_YEAR)));
  452. adminReply.replace("%sunday%", String.valueOf(newDay.get(Calendar.MONTH) + "/" + String.valueOf(newDay.get(Calendar.DAY_OF_MONTH))));
  453. }
  454. activeChar.sendPacket(adminReply);
  455. }
  456. /**
  457. * Show the clan hall page.
  458. * @param activeChar the active char
  459. * @param clanhall the clan hall
  460. */
  461. private void showClanHallPage(L2PcInstance activeChar, ClanHall clanhall)
  462. {
  463. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  464. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/clanhall.htm");
  465. adminReply.replace("%clanhallName%", clanhall.getName());
  466. adminReply.replace("%clanhallId%", String.valueOf(clanhall.getId()));
  467. final L2Clan owner = ClanTable.getInstance().getClan(clanhall.getOwnerId());
  468. adminReply.replace("%clanhallOwner%", (owner == null) ? "None" : owner.getName());
  469. activeChar.sendPacket(adminReply);
  470. }
  471. /**
  472. * Show the siegable hall page.
  473. * @param activeChar the active char
  474. * @param hall the siegable hall
  475. */
  476. private void showSiegableHallPage(L2PcInstance activeChar, SiegableHall hall)
  477. {
  478. final NpcHtmlMessage msg = new NpcHtmlMessage();
  479. msg.setFile(null, "data/html/admin/siegablehall.htm");
  480. msg.replace("%clanhallId%", String.valueOf(hall.getId()));
  481. msg.replace("%clanhallName%", hall.getName());
  482. if (hall.getOwnerId() > 0)
  483. {
  484. final L2Clan owner = ClanTable.getInstance().getClan(hall.getOwnerId());
  485. msg.replace("%clanhallOwner%", (owner != null) ? owner.getName() : "No Owner");
  486. }
  487. else
  488. {
  489. msg.replace("%clanhallOwner%", "No Owner");
  490. }
  491. activeChar.sendPacket(msg);
  492. }
  493. @Override
  494. public String[] getAdminCommandList()
  495. {
  496. return ADMIN_COMMANDS;
  497. }
  498. }