AdminCHSiege.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 com.l2jserver.Config;
  22. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  23. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  24. import com.l2jserver.gameserver.instancemanager.CHSiegeManager;
  25. import com.l2jserver.gameserver.model.L2Clan;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.entity.clanhall.ClanHallSiegeEngine;
  29. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  30. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  31. import com.l2jserver.gameserver.network.serverpackets.SiegeInfo;
  32. /**
  33. * @author BiggBoss
  34. */
  35. public final class AdminCHSiege implements IAdminCommandHandler
  36. {
  37. private static final String[] COMMANDS =
  38. {
  39. "admin_chsiege_siegablehall",
  40. "admin_chsiege_startSiege",
  41. "admin_chsiege_endsSiege",
  42. "admin_chsiege_setSiegeDate",
  43. "admin_chsiege_addAttacker",
  44. "admin_chsiege_removeAttacker",
  45. "admin_chsiege_clearAttackers",
  46. "admin_chsiege_listAttackers",
  47. "admin_chsiege_forwardSiege"
  48. };
  49. @Override
  50. public String[] getAdminCommandList()
  51. {
  52. return COMMANDS;
  53. }
  54. @Override
  55. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  56. {
  57. final String[] split = command.split(" ");
  58. SiegableHall hall = null;
  59. if (Config.ALT_DEV_NO_QUESTS)
  60. {
  61. activeChar.sendMessage("AltDevNoQuests = true; Clan Hall Sieges are disabled!");
  62. return false;
  63. }
  64. if (split.length < 2)
  65. {
  66. activeChar.sendMessage("You have to specify the hall id at least");
  67. return false;
  68. }
  69. if ((hall = getHall(split[1], activeChar)) == null)
  70. {
  71. activeChar.sendMessage("Couldnt find he desired siegable hall (" + split[1] + ")");
  72. return false;
  73. }
  74. if (hall.getSiege() == null)
  75. {
  76. activeChar.sendMessage("The given hall dont have any attached siege!");
  77. return false;
  78. }
  79. if (split[0].equals(COMMANDS[1]))
  80. {
  81. if (hall.isInSiege())
  82. {
  83. activeChar.sendMessage("The requested clan hall is alredy in siege!");
  84. }
  85. else
  86. {
  87. L2Clan owner = ClanTable.getInstance().getClan(hall.getOwnerId());
  88. if (owner != null)
  89. {
  90. hall.free();
  91. owner.setHideoutId(0);
  92. hall.addAttacker(owner);
  93. }
  94. hall.getSiege().startSiege();
  95. }
  96. }
  97. else if (split[0].equals(COMMANDS[2]))
  98. {
  99. if (!hall.isInSiege())
  100. {
  101. activeChar.sendMessage("The requested clan hall isnt in siege!");
  102. }
  103. else
  104. {
  105. hall.getSiege().endSiege();
  106. }
  107. }
  108. else if (split[0].equals(COMMANDS[3]))
  109. {
  110. if (!hall.isRegistering())
  111. {
  112. activeChar.sendMessage("Cannot change siege date while hall is in siege");
  113. }
  114. else if (split.length < 3)
  115. {
  116. activeChar.sendMessage("The date format is incorrect. Try again.");
  117. }
  118. else
  119. {
  120. String[] rawDate = split[2].split(";");
  121. if (rawDate.length < 2)
  122. {
  123. activeChar.sendMessage("You have to specify this format DD-MM-YYYY;HH:MM");
  124. }
  125. else
  126. {
  127. String[] day = rawDate[0].split("-");
  128. String[] hour = rawDate[1].split(":");
  129. if ((day.length < 3) || (hour.length < 2))
  130. {
  131. activeChar.sendMessage("Incomplete day, hour or both!");
  132. }
  133. else
  134. {
  135. int d = parseInt(day[0]);
  136. int month = parseInt(day[1]) - 1;
  137. int year = parseInt(day[2]);
  138. int h = parseInt(hour[0]);
  139. int min = parseInt(hour[1]);
  140. if (((month == 2) && (d > 28)) || (d > 31) || (d <= 0) || (month <= 0) || (month > 12) || (year < Calendar.getInstance().get(Calendar.YEAR)))
  141. {
  142. activeChar.sendMessage("Wrong day/month/year gave!");
  143. }
  144. else if ((h <= 0) || (h > 24) || (min < 0) || (min >= 60))
  145. {
  146. activeChar.sendMessage("Wrong hour/minutes gave!");
  147. }
  148. else
  149. {
  150. Calendar c = Calendar.getInstance();
  151. c.set(Calendar.YEAR, year);
  152. c.set(Calendar.MONTH, month);
  153. c.set(Calendar.DAY_OF_MONTH, d);
  154. c.set(Calendar.HOUR_OF_DAY, h);
  155. c.set(Calendar.MINUTE, min);
  156. c.set(Calendar.SECOND, 0);
  157. if (c.getTimeInMillis() > System.currentTimeMillis())
  158. {
  159. activeChar.sendMessage(hall.getName() + " siege: " + c.getTime().toString());
  160. hall.setNextSiegeDate(c.getTimeInMillis());
  161. hall.getSiege().updateSiege();
  162. hall.updateDb();
  163. }
  164. else
  165. {
  166. activeChar.sendMessage("The given time is in the past!");
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. else if (split[0].equals(COMMANDS[4]))
  174. {
  175. if (hall.isInSiege())
  176. {
  177. activeChar.sendMessage("The clan hall is in siege, cannot add attackers now.");
  178. return false;
  179. }
  180. L2Clan attacker = null;
  181. if (split.length < 3)
  182. {
  183. L2Object rawTarget = activeChar.getTarget();
  184. L2PcInstance target = null;
  185. if (rawTarget == null)
  186. {
  187. activeChar.sendMessage("You must target a clan member of the attacker!");
  188. }
  189. else if (!(rawTarget instanceof L2PcInstance))
  190. {
  191. activeChar.sendMessage("You must target a player with clan!");
  192. }
  193. else if ((target = (L2PcInstance) rawTarget).getClan() == null)
  194. {
  195. activeChar.sendMessage("Your target does not have any clan!");
  196. }
  197. else if (hall.getSiege().checkIsAttacker(target.getClan()))
  198. {
  199. activeChar.sendMessage("Your target's clan is alredy participating!");
  200. }
  201. else
  202. {
  203. attacker = target.getClan();
  204. }
  205. }
  206. else
  207. {
  208. L2Clan rawClan = ClanTable.getInstance().getClanByName(split[2]);
  209. if (rawClan == null)
  210. {
  211. activeChar.sendMessage("The given clan does not exist!");
  212. }
  213. else if (hall.getSiege().checkIsAttacker(rawClan))
  214. {
  215. activeChar.sendMessage("The given clan is alredy participating!");
  216. }
  217. else
  218. {
  219. attacker = rawClan;
  220. }
  221. }
  222. if (attacker != null)
  223. {
  224. hall.addAttacker(attacker);
  225. }
  226. }
  227. else if (split[0].equals(COMMANDS[5]))
  228. {
  229. if (hall.isInSiege())
  230. {
  231. activeChar.sendMessage("The clan hall is in siege, cannot remove attackers now.");
  232. return false;
  233. }
  234. if (split.length < 3)
  235. {
  236. L2Object rawTarget = activeChar.getTarget();
  237. L2PcInstance target = null;
  238. if (rawTarget == null)
  239. {
  240. activeChar.sendMessage("You must target a clan member of the attacker!");
  241. }
  242. else if (!(rawTarget instanceof L2PcInstance))
  243. {
  244. activeChar.sendMessage("You must target a player with clan!");
  245. }
  246. else if ((target = (L2PcInstance) rawTarget).getClan() == null)
  247. {
  248. activeChar.sendMessage("Your target does not have any clan!");
  249. }
  250. else if (!hall.getSiege().checkIsAttacker(target.getClan()))
  251. {
  252. activeChar.sendMessage("Your target's clan is not participating!");
  253. }
  254. else
  255. {
  256. hall.removeAttacker(target.getClan());
  257. }
  258. }
  259. else
  260. {
  261. L2Clan rawClan = ClanTable.getInstance().getClanByName(split[2]);
  262. if (rawClan == null)
  263. {
  264. activeChar.sendMessage("The given clan does not exist!");
  265. }
  266. else if (!hall.getSiege().checkIsAttacker(rawClan))
  267. {
  268. activeChar.sendMessage("The given clan is not participating!");
  269. }
  270. else
  271. {
  272. hall.removeAttacker(rawClan);
  273. }
  274. }
  275. }
  276. else if (split[0].equals(COMMANDS[6]))
  277. {
  278. if (hall.isInSiege())
  279. {
  280. activeChar.sendMessage("The requested hall is in siege right now, cannot clear attacker list!");
  281. }
  282. else
  283. {
  284. hall.getSiege().getAttackers().clear();
  285. }
  286. }
  287. else if (split[0].equals(COMMANDS[7]))
  288. {
  289. activeChar.sendPacket(new SiegeInfo(hall));
  290. }
  291. else if (split[0].equals(COMMANDS[8]))
  292. {
  293. ClanHallSiegeEngine siegable = hall.getSiege();
  294. siegable.cancelSiegeTask();
  295. switch (hall.getSiegeStatus())
  296. {
  297. case REGISTERING:
  298. siegable.prepareOwner();
  299. break;
  300. case WAITING_BATTLE:
  301. siegable.startSiege();
  302. break;
  303. case RUNNING:
  304. siegable.endSiege();
  305. break;
  306. }
  307. }
  308. sendSiegableHallPage(activeChar, split[1], hall);
  309. return false;
  310. }
  311. private SiegableHall getHall(String id, L2PcInstance gm)
  312. {
  313. int ch = parseInt(id);
  314. if (ch == 0)
  315. {
  316. gm.sendMessage("Wrong clan hall id, unparseable id!");
  317. return null;
  318. }
  319. SiegableHall hall = CHSiegeManager.getInstance().getSiegableHall(ch);
  320. if (hall == null)
  321. {
  322. gm.sendMessage("Couldnt find the clan hall.");
  323. }
  324. return hall;
  325. }
  326. private int parseInt(String st)
  327. {
  328. int val = 0;
  329. try
  330. {
  331. val = Integer.parseInt(st);
  332. }
  333. catch (NumberFormatException e)
  334. {
  335. e.printStackTrace();
  336. }
  337. return val;
  338. }
  339. private void sendSiegableHallPage(L2PcInstance activeChar, String hallId, SiegableHall hall)
  340. {
  341. final NpcHtmlMessage msg = new NpcHtmlMessage();
  342. msg.setFile(null, "data/html/admin/siegablehall.htm");
  343. msg.replace("%clanhallId%", hallId);
  344. msg.replace("%clanhallName%", hall.getName());
  345. if (hall.getOwnerId() > 0)
  346. {
  347. L2Clan owner = ClanTable.getInstance().getClan(hall.getOwnerId());
  348. if (owner != null)
  349. {
  350. msg.replace("%clanhallOwner%", owner.getName());
  351. }
  352. else
  353. {
  354. msg.replace("%clanhallOwner%", "No Owner");
  355. }
  356. }
  357. else
  358. {
  359. msg.replace("%clanhallOwner%", "No Owner");
  360. }
  361. activeChar.sendPacket(msg);
  362. }
  363. }