AdminMobGroup.java 14 KB

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