AdminMobGroup.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 handlers.admincommandhandlers;
  16. import com.l2jserver.gameserver.datatables.NpcTable;
  17. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  18. import com.l2jserver.gameserver.model.L2World;
  19. import com.l2jserver.gameserver.model.MobGroup;
  20. import com.l2jserver.gameserver.model.MobGroupTable;
  21. import com.l2jserver.gameserver.model.actor.L2Character;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  24. import com.l2jserver.gameserver.network.SystemMessageId;
  25. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  26. import com.l2jserver.gameserver.network.serverpackets.SetupGauge;
  27. import com.l2jserver.gameserver.util.Broadcast;
  28. /**
  29. * @author littlecrow
  30. * Admin commands handler for controllable mobs
  31. */
  32. public class AdminMobGroup implements IAdminCommandHandler
  33. {
  34. private static final String[] ADMIN_COMMANDS =
  35. {
  36. "admin_mobmenu",
  37. "admin_mobgroup_list",
  38. "admin_mobgroup_create",
  39. "admin_mobgroup_remove",
  40. "admin_mobgroup_delete",
  41. "admin_mobgroup_spawn",
  42. "admin_mobgroup_unspawn",
  43. "admin_mobgroup_kill",
  44. "admin_mobgroup_idle",
  45. "admin_mobgroup_attack",
  46. "admin_mobgroup_rnd",
  47. "admin_mobgroup_return",
  48. "admin_mobgroup_follow",
  49. "admin_mobgroup_casting",
  50. "admin_mobgroup_nomove",
  51. "admin_mobgroup_attackgrp",
  52. "admin_mobgroup_invul"
  53. };
  54. @Override
  55. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  56. {
  57. if (command.equals("admin_mobmenu"))
  58. {
  59. showMainPage(activeChar, command);
  60. return true;
  61. }
  62. else if (command.equals("admin_mobgroup_list"))
  63. showGroupList(activeChar);
  64. else if (command.startsWith("admin_mobgroup_create"))
  65. createGroup(command, activeChar);
  66. else if (command.startsWith("admin_mobgroup_delete") || command.startsWith("admin_mobgroup_remove"))
  67. removeGroup(command, activeChar);
  68. else if (command.startsWith("admin_mobgroup_spawn"))
  69. spawnGroup(command, activeChar);
  70. else if (command.startsWith("admin_mobgroup_unspawn"))
  71. unspawnGroup(command, activeChar);
  72. else if (command.startsWith("admin_mobgroup_kill"))
  73. killGroup(command, activeChar);
  74. else if (command.startsWith("admin_mobgroup_attackgrp"))
  75. attackGrp(command, activeChar);
  76. else if (command.startsWith("admin_mobgroup_attack"))
  77. {
  78. if (activeChar.getTarget() instanceof L2Character)
  79. {
  80. L2Character target = (L2Character) activeChar.getTarget();
  81. attack(command, activeChar, target);
  82. }
  83. }
  84. else if (command.startsWith("admin_mobgroup_rnd"))
  85. setNormal(command, activeChar);
  86. else if (command.startsWith("admin_mobgroup_idle"))
  87. idle(command, activeChar);
  88. else if (command.startsWith("admin_mobgroup_return"))
  89. returnToChar(command, activeChar);
  90. else if (command.startsWith("admin_mobgroup_follow"))
  91. follow(command, activeChar, activeChar);
  92. else if (command.startsWith("admin_mobgroup_casting"))
  93. setCasting(command, activeChar);
  94. else if (command.startsWith("admin_mobgroup_nomove"))
  95. noMove(command, activeChar);
  96. else if (command.startsWith("admin_mobgroup_invul"))
  97. invul(command, activeChar);
  98. else if (command.startsWith("admin_mobgroup_teleport"))
  99. teleportGroup(command, activeChar);
  100. showMainPage(activeChar, command);
  101. return true;
  102. }
  103. /**
  104. * @param activeChar
  105. * @param command
  106. */
  107. private void showMainPage(L2PcInstance activeChar, String command)
  108. {
  109. String filename = "mobgroup.htm";
  110. AdminHelpPage.showHelpPage(activeChar, filename);
  111. }
  112. private void returnToChar(String command, L2PcInstance activeChar)
  113. {
  114. int groupId;
  115. try
  116. {
  117. groupId = Integer.parseInt(command.split(" ")[1]);
  118. }
  119. catch (Exception e)
  120. {
  121. activeChar.sendMessage("Incorrect command arguments.");
  122. return;
  123. }
  124. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  125. if (group == null)
  126. {
  127. activeChar.sendMessage("Invalid group specified.");
  128. return;
  129. }
  130. group.returnGroup(activeChar);
  131. }
  132. private void idle(String command, L2PcInstance activeChar)
  133. {
  134. int groupId;
  135. try
  136. {
  137. groupId = Integer.parseInt(command.split(" ")[1]);
  138. }
  139. catch (Exception e)
  140. {
  141. activeChar.sendMessage("Incorrect command arguments.");
  142. return;
  143. }
  144. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  145. if (group == null)
  146. {
  147. activeChar.sendMessage("Invalid group specified.");
  148. return;
  149. }
  150. group.setIdleMode();
  151. }
  152. private void setNormal(String command, L2PcInstance activeChar)
  153. {
  154. int groupId;
  155. try
  156. {
  157. groupId = Integer.parseInt(command.split(" ")[1]);
  158. }
  159. catch (Exception e)
  160. {
  161. activeChar.sendMessage("Incorrect command arguments.");
  162. return;
  163. }
  164. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  165. if (group == null)
  166. {
  167. activeChar.sendMessage("Invalid group specified.");
  168. return;
  169. }
  170. group.setAttackRandom();
  171. }
  172. private void attack(String command, L2PcInstance activeChar, L2Character target)
  173. {
  174. int groupId;
  175. try
  176. {
  177. groupId = Integer.parseInt(command.split(" ")[1]);
  178. }
  179. catch (Exception e)
  180. {
  181. activeChar.sendMessage("Incorrect command arguments.");
  182. return;
  183. }
  184. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  185. if (group == null)
  186. {
  187. activeChar.sendMessage("Invalid group specified.");
  188. return;
  189. }
  190. group.setAttackTarget(target);
  191. }
  192. private void follow(String command, L2PcInstance activeChar, L2Character target)
  193. {
  194. int groupId;
  195. try
  196. {
  197. groupId = Integer.parseInt(command.split(" ")[1]);
  198. }
  199. catch (Exception e)
  200. {
  201. activeChar.sendMessage("Incorrect command arguments.");
  202. return;
  203. }
  204. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  205. if (group == null)
  206. {
  207. activeChar.sendMessage("Invalid group specified.");
  208. return;
  209. }
  210. group.setFollowMode(target);
  211. }
  212. private void createGroup(String command, L2PcInstance activeChar)
  213. {
  214. int groupId;
  215. int templateId;
  216. int mobCount;
  217. try
  218. {
  219. String[] cmdParams = command.split(" ");
  220. groupId = Integer.parseInt(cmdParams[1]);
  221. templateId = Integer.parseInt(cmdParams[2]);
  222. mobCount = Integer.parseInt(cmdParams[3]);
  223. }
  224. catch (Exception e)
  225. {
  226. activeChar.sendMessage("Usage: //mobgroup_create <group> <npcid> <count>");
  227. return;
  228. }
  229. if (MobGroupTable.getInstance().getGroup(groupId) != null)
  230. {
  231. activeChar.sendMessage("Mob group " + groupId + " already exists.");
  232. return;
  233. }
  234. L2NpcTemplate template = NpcTable.getInstance().getTemplate(templateId);
  235. if (template == null)
  236. {
  237. activeChar.sendMessage("Invalid NPC ID specified.");
  238. return;
  239. }
  240. MobGroup group = new MobGroup(groupId, template, mobCount);
  241. MobGroupTable.getInstance().addGroup(groupId, group);
  242. activeChar.sendMessage("Mob group " + groupId + " created.");
  243. }
  244. private void removeGroup(String command, L2PcInstance activeChar)
  245. {
  246. int groupId;
  247. try
  248. {
  249. groupId = Integer.parseInt(command.split(" ")[1]);
  250. }
  251. catch (Exception e)
  252. {
  253. activeChar.sendMessage("Usage: //mobgroup_remove <groupId>");
  254. return;
  255. }
  256. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  257. if (group == null)
  258. {
  259. activeChar.sendMessage("Invalid group specified.");
  260. return;
  261. }
  262. doAnimation(activeChar);
  263. group.unspawnGroup();
  264. if (MobGroupTable.getInstance().removeGroup(groupId))
  265. activeChar.sendMessage("Mob group " + groupId + " unspawned and removed.");
  266. }
  267. private void spawnGroup(String command, L2PcInstance activeChar)
  268. {
  269. int groupId;
  270. boolean topos = false;
  271. int posx = 0;
  272. int posy = 0;
  273. int posz = 0;
  274. try
  275. {
  276. String[] cmdParams = command.split(" ");
  277. groupId = Integer.parseInt(cmdParams[1]);
  278. try
  279. { // we try to get a position
  280. posx = Integer.parseInt(cmdParams[2]);
  281. posy = Integer.parseInt(cmdParams[3]);
  282. posz = Integer.parseInt(cmdParams[4]);
  283. topos = true;
  284. }
  285. catch (Exception e)
  286. {
  287. // no position given
  288. }
  289. }
  290. catch (Exception e)
  291. {
  292. activeChar.sendMessage("Usage: //mobgroup_spawn <group> [ x y z ]");
  293. return;
  294. }
  295. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  296. if (group == null)
  297. {
  298. activeChar.sendMessage("Invalid group specified.");
  299. return;
  300. }
  301. doAnimation(activeChar);
  302. if (topos)
  303. group.spawnGroup(posx, posy, posz);
  304. else
  305. group.spawnGroup(activeChar);
  306. activeChar.sendMessage("Mob group " + groupId + " spawned.");
  307. }
  308. private void unspawnGroup(String command, L2PcInstance activeChar)
  309. {
  310. int groupId;
  311. try
  312. {
  313. groupId = Integer.parseInt(command.split(" ")[1]);
  314. }
  315. catch (Exception e)
  316. {
  317. activeChar.sendMessage("Usage: //mobgroup_unspawn <groupId>");
  318. return;
  319. }
  320. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  321. if (group == null)
  322. {
  323. activeChar.sendMessage("Invalid group specified.");
  324. return;
  325. }
  326. doAnimation(activeChar);
  327. group.unspawnGroup();
  328. activeChar.sendMessage("Mob group " + groupId + " unspawned.");
  329. }
  330. private void killGroup(String command, L2PcInstance activeChar)
  331. {
  332. int groupId;
  333. try
  334. {
  335. groupId = Integer.parseInt(command.split(" ")[1]);
  336. }
  337. catch (Exception e)
  338. {
  339. activeChar.sendMessage("Usage: //mobgroup_kill <groupId>");
  340. return;
  341. }
  342. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  343. if (group == null)
  344. {
  345. activeChar.sendMessage("Invalid group specified.");
  346. return;
  347. }
  348. doAnimation(activeChar);
  349. group.killGroup(activeChar);
  350. }
  351. private void setCasting(String command, L2PcInstance activeChar)
  352. {
  353. int groupId;
  354. try
  355. {
  356. groupId = Integer.parseInt(command.split(" ")[1]);
  357. }
  358. catch (Exception e)
  359. {
  360. activeChar.sendMessage("Usage: //mobgroup_casting <groupId>");
  361. return;
  362. }
  363. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  364. if (group == null)
  365. {
  366. activeChar.sendMessage("Invalid group specified.");
  367. return;
  368. }
  369. group.setCastMode();
  370. }
  371. private void noMove(String command, L2PcInstance activeChar)
  372. {
  373. int groupId;
  374. String enabled;
  375. try
  376. {
  377. groupId = Integer.parseInt(command.split(" ")[1]);
  378. enabled = command.split(" ")[2];
  379. }
  380. catch (Exception e)
  381. {
  382. activeChar.sendMessage("Usage: //mobgroup_nomove <groupId> <on|off>");
  383. return;
  384. }
  385. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  386. if (group == null)
  387. {
  388. activeChar.sendMessage("Invalid group specified.");
  389. return;
  390. }
  391. if (enabled.equalsIgnoreCase("on") || enabled.equalsIgnoreCase("true"))
  392. group.setNoMoveMode(true);
  393. else if (enabled.equalsIgnoreCase("off") || enabled.equalsIgnoreCase("false"))
  394. group.setNoMoveMode(false);
  395. else
  396. activeChar.sendMessage("Incorrect command arguments.");
  397. }
  398. private void doAnimation(L2PcInstance activeChar)
  399. {
  400. Broadcast.toSelfAndKnownPlayersInRadius(activeChar, new MagicSkillUse(activeChar, 1008, 1, 4000, 0), 2250000/*1500*/);
  401. activeChar.sendPacket(new SetupGauge(0, 4000));
  402. }
  403. private void attackGrp(String command, L2PcInstance activeChar)
  404. {
  405. int groupId;
  406. int othGroupId;
  407. try
  408. {
  409. groupId = Integer.parseInt(command.split(" ")[1]);
  410. othGroupId = Integer.parseInt(command.split(" ")[2]);
  411. }
  412. catch (Exception e)
  413. {
  414. activeChar.sendMessage("Usage: //mobgroup_attackgrp <groupId> <TargetGroupId>");
  415. return;
  416. }
  417. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  418. if (group == null)
  419. {
  420. activeChar.sendMessage("Invalid group specified.");
  421. return;
  422. }
  423. MobGroup othGroup = MobGroupTable.getInstance().getGroup(othGroupId);
  424. if (othGroup == null)
  425. {
  426. activeChar.sendMessage("Incorrect target group.");
  427. return;
  428. }
  429. group.setAttackGroup(othGroup);
  430. }
  431. private void invul(String command, L2PcInstance activeChar)
  432. {
  433. int groupId;
  434. String enabled;
  435. try
  436. {
  437. groupId = Integer.parseInt(command.split(" ")[1]);
  438. enabled = command.split(" ")[2];
  439. }
  440. catch (Exception e)
  441. {
  442. activeChar.sendMessage("Usage: //mobgroup_invul <groupId> <on|off>");
  443. return;
  444. }
  445. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  446. if (group == null)
  447. {
  448. activeChar.sendMessage("Invalid group specified.");
  449. return;
  450. }
  451. if (enabled.equalsIgnoreCase("on") || enabled.equalsIgnoreCase("true"))
  452. group.setInvul(true);
  453. else if (enabled.equalsIgnoreCase("off") || enabled.equalsIgnoreCase("false"))
  454. group.setInvul(false);
  455. else
  456. activeChar.sendMessage("Incorrect command arguments.");
  457. }
  458. private void teleportGroup(String command, L2PcInstance activeChar)
  459. {
  460. int groupId;
  461. String targetPlayerStr = null;
  462. L2PcInstance targetPlayer = null;
  463. try
  464. {
  465. groupId = Integer.parseInt(command.split(" ")[1]);
  466. targetPlayerStr = command.split(" ")[2];
  467. if (targetPlayerStr != null)
  468. targetPlayer = L2World.getInstance().getPlayer(targetPlayerStr);
  469. if (targetPlayer == null)
  470. targetPlayer = activeChar;
  471. }
  472. catch (Exception e)
  473. {
  474. activeChar.sendMessage("Usage: //mobgroup_teleport <groupId> [playerName]");
  475. return;
  476. }
  477. MobGroup group = MobGroupTable.getInstance().getGroup(groupId);
  478. if (group == null)
  479. {
  480. activeChar.sendMessage("Invalid group specified.");
  481. return;
  482. }
  483. group.teleportGroup(activeChar);
  484. }
  485. private void showGroupList(L2PcInstance activeChar)
  486. {
  487. MobGroup[] mobGroupList = MobGroupTable.getInstance().getGroups();
  488. activeChar.sendMessage("======= <Mob Groups> =======");
  489. for (MobGroup mobGroup : mobGroupList)
  490. activeChar.sendMessage(mobGroup.getGroupId() + ": " + mobGroup.getActiveMobCount() + " alive out of " + mobGroup.getMaxMobCount() + " of NPC ID " + mobGroup.getTemplate().getNpcId() + " (" + mobGroup.getStatus() + ")");
  491. activeChar.sendPacket(SystemMessageId.FRIEND_LIST_FOOTER);
  492. }
  493. @Override
  494. public String[] getAdminCommandList()
  495. {
  496. return ADMIN_COMMANDS;
  497. }
  498. }