AdminMobGroup.java 15 KB

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