/* * Copyright (C) 2004-2015 L2J DataPack * * This file is part of L2J DataPack. * * L2J DataPack is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * L2J DataPack is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package handlers.admincommandhandlers; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.StringTokenizer; import com.l2jserver.Config; import com.l2jserver.gameserver.data.xml.impl.AdminData; import com.l2jserver.gameserver.data.xml.impl.TransformData; import com.l2jserver.gameserver.handler.IAdminCommandHandler; import com.l2jserver.gameserver.model.L2World; import com.l2jserver.gameserver.model.actor.instance.L2PcInstance; import com.l2jserver.gameserver.model.entity.L2Event; import com.l2jserver.gameserver.model.entity.L2Event.EventState; import com.l2jserver.gameserver.network.serverpackets.CharInfo; import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo; import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage; import com.l2jserver.gameserver.network.serverpackets.PlaySound; import com.l2jserver.gameserver.network.serverpackets.UserInfo; import com.l2jserver.gameserver.util.Broadcast; import com.l2jserver.util.Rnd; import com.l2jserver.util.StringUtil; /** * This class handles following admin commands: - admin = shows menu * @version $Revision: 1.3.2.1.2.4 $ $Date: 2005/04/11 10:06:06 $ */ public class AdminEventEngine implements IAdminCommandHandler { private static final String[] ADMIN_COMMANDS = { "admin_event", "admin_event_new", "admin_event_choose", "admin_event_store", "admin_event_set", "admin_event_change_teams_number", "admin_event_announce", "admin_event_panel", "admin_event_control_begin", "admin_event_control_teleport", "admin_add", "admin_event_see", "admin_event_del", "admin_delete_buffer", "admin_event_control_sit", "admin_event_name", "admin_event_control_kill", "admin_event_control_res", "admin_event_control_poly", "admin_event_control_unpoly", "admin_event_control_transform", "admin_event_control_untransform", "admin_event_control_prize", "admin_event_control_chatban", "admin_event_control_kick", "admin_event_control_finish" }; private static String tempBuffer = ""; private static String tempName = ""; private static boolean npcsDeleted = false; @Override public boolean useAdminCommand(String command, L2PcInstance activeChar) { StringTokenizer st = new StringTokenizer(command); String actualCommand = st.nextToken(); try { if (actualCommand.equals("admin_event")) { if (L2Event.eventState != EventState.OFF) { showEventControl(activeChar); } else { showMainPage(activeChar); } } else if (actualCommand.equals("admin_event_new")) { showNewEventPage(activeChar); } else if (actualCommand.startsWith("admin_add")) { // There is an exception here for not using the ST. We use spaces (ST delim) for the event info. tempBuffer += command.substring(10); showNewEventPage(activeChar); } else if (actualCommand.startsWith("admin_event_see")) { // There is an exception here for not using the ST. We use spaces (ST delim) for the event name. String eventName = command.substring(16); try { final NpcHtmlMessage adminReply = new NpcHtmlMessage(); DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(Config.DATAPACK_ROOT + "/data/events/" + eventName))); BufferedReader inbr = new BufferedReader(new InputStreamReader(in)); adminReply.setFile("en", "data/html/mods/EventEngine/Participation.htm"); adminReply.replace("%eventName%", eventName); adminReply.replace("%eventCreator%", inbr.readLine()); adminReply.replace("%eventInfo%", inbr.readLine()); adminReply.replace("npc_%objectId%_event_participate", "admin_event"); // Weird, but nice hack, isnt it? :) adminReply.replace("button value=\"Participate\"", "button value=\"Back\""); activeChar.sendPacket(adminReply); inbr.close(); } catch (Exception e) { e.printStackTrace(); } } else if (actualCommand.startsWith("admin_event_del")) { // There is an exception here for not using the ST. We use spaces (ST delim) for the event name. String eventName = command.substring(16); File file = new File(Config.DATAPACK_ROOT + "/data/events/" + eventName); file.delete(); showMainPage(activeChar); } else if (actualCommand.startsWith("admin_event_name")) { // There is an exception here for not using the ST. We use spaces (ST delim) for the event name. tempName += command.substring(17); showNewEventPage(activeChar); } else if (actualCommand.equalsIgnoreCase("admin_delete_buffer")) { tempBuffer = ""; showNewEventPage(activeChar); } else if (actualCommand.startsWith("admin_event_store")) { try { FileOutputStream file = new FileOutputStream(new File(Config.DATAPACK_ROOT, "data/events/" + tempName)); PrintStream p = new PrintStream(file); p.println(activeChar.getName()); p.println(tempBuffer); file.close(); p.close(); } catch (Exception e) { e.printStackTrace(); } tempBuffer = ""; tempName = ""; showMainPage(activeChar); } else if (actualCommand.startsWith("admin_event_set")) { // There is an exception here for not using the ST. We use spaces (ST delim) for the event name. L2Event._eventName = command.substring(16); showEventParameters(activeChar, 2); } else if (actualCommand.startsWith("admin_event_change_teams_number")) { showEventParameters(activeChar, Integer.parseInt(st.nextToken())); } else if (actualCommand.startsWith("admin_event_panel")) { showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_announce")) { L2Event._npcId = Integer.parseInt(st.nextToken()); L2Event._teamsNumber = Integer.parseInt(st.nextToken()); String temp = " "; String temp2 = ""; while (st.hasMoreElements()) { temp += st.nextToken() + " "; } st = new StringTokenizer(temp, "-"); Integer i = 1; while (st.hasMoreElements()) { temp2 = st.nextToken(); if (!temp2.equals(" ")) { L2Event._teamNames.put(i++, temp2.substring(1, temp2.length() - 1)); } } activeChar.sendMessage(L2Event.startEventParticipation()); Broadcast.toAllOnlinePlayers(activeChar.getName() + " has started an event. You will find a participation NPC somewhere around you."); PlaySound _snd = new PlaySound(1, "B03_F", 0, 0, 0, 0, 0); activeChar.sendPacket(_snd); activeChar.broadcastPacket(_snd); final NpcHtmlMessage adminReply = new NpcHtmlMessage(); final String replyMSG = StringUtil.concat("[ L2J EVENT ENGINE ]
", "
The event ", L2Event._eventName, " has been announced, now you can type //event_panel to see the event panel control

", ""); adminReply.setHtml(replyMSG); activeChar.sendPacket(adminReply); } else if (actualCommand.startsWith("admin_event_control_begin")) { // Starts the event and sends a message of the result activeChar.sendMessage(L2Event.startEvent()); showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_finish")) { // Finishes the event and sends a message of the result activeChar.sendMessage(L2Event.finishEvent()); } else if (actualCommand.startsWith("admin_event_control_teleport")) { while (st.hasMoreElements()) // Every next ST should be a team number { int teamId = Integer.parseInt(st.nextToken()); for (L2PcInstance player : L2Event._teams.get(teamId)) { player.setTitle(L2Event._teamNames.get(teamId)); player.teleToLocation(activeChar.getLocation(), true); player.setInstanceId(activeChar.getInstanceId()); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_sit")) { while (st.hasMoreElements()) // Every next ST should be a team number { // Integer.parseInt(st.nextToken()) == teamId for (L2PcInstance player : L2Event._teams.get(Integer.parseInt(st.nextToken()))) { if (player.getEventStatus() == null) { continue; } player.getEventStatus().setSitForced(!player.getEventStatus().isSitForced()); if (player.getEventStatus().isSitForced()) { player.sitDown(); } else { player.standUp(); } } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_kill")) { while (st.hasMoreElements()) // Every next ST should be a team number { for (L2PcInstance player : L2Event._teams.get(Integer.parseInt(st.nextToken()))) { player.reduceCurrentHp(player.getMaxHp() + player.getMaxCp() + 1, activeChar, null); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_res")) { while (st.hasMoreElements()) // Every next ST should be a team number { for (L2PcInstance player : L2Event._teams.get(Integer.parseInt(st.nextToken()))) { if ((player == null) || !player.isDead()) { continue; } player.restoreExp(100.0); player.doRevive(); player.setCurrentHpMp(player.getMaxHp(), player.getMaxMp()); player.setCurrentCp(player.getMaxCp()); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_poly")) { int teamId = Integer.parseInt(st.nextToken()); String[] polyIds = new String[st.countTokens()]; int i = 0; while (st.hasMoreElements()) // Every next ST should be a polymorph ID { polyIds[i++] = st.nextToken(); } for (L2PcInstance player : L2Event._teams.get(teamId)) { player.getPoly().setPolyInfo("npc", polyIds[Rnd.get(polyIds.length)]); player.teleToLocation(player.getLocation(), true); CharInfo info1 = new CharInfo(player); player.broadcastPacket(info1); UserInfo info2 = new UserInfo(player); player.sendPacket(info2); player.broadcastPacket(new ExBrExtraUserInfo(player)); } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_unpoly")) { while (st.hasMoreElements()) // Every next ST should be a team number { for (L2PcInstance player : L2Event._teams.get(Integer.parseInt(st.nextToken()))) { player.getPoly().setPolyInfo(null, "1"); player.decayMe(); player.spawnMe(player.getX(), player.getY(), player.getZ()); CharInfo info1 = new CharInfo(player); player.broadcastPacket(info1); UserInfo info2 = new UserInfo(player); player.sendPacket(info2); player.broadcastPacket(new ExBrExtraUserInfo(player)); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_transform")) { int teamId = Integer.parseInt(st.nextToken()); int[] transIds = new int[st.countTokens()]; int i = 0; while (st.hasMoreElements()) // Every next ST should be a transform ID { transIds[i++] = Integer.parseInt(st.nextToken()); } for (L2PcInstance player : L2Event._teams.get(teamId)) { int transId = transIds[Rnd.get(transIds.length)]; if (!TransformData.getInstance().transformPlayer(transId, player)) { AdminData.getInstance().broadcastMessageToGMs("EventEngine: Unknow transformation id: " + transId); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_untransform")) { while (st.hasMoreElements()) // Every next ST should be a team number { for (L2PcInstance player : L2Event._teams.get(Integer.parseInt(st.nextToken()))) { player.stopTransformation(true); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_kick")) { if (st.hasMoreElements()) // If has next token, it should be player name. { while (st.hasMoreElements()) { L2PcInstance player = L2World.getInstance().getPlayer(st.nextToken()); if (player != null) { L2Event.removeAndResetPlayer(player); } } } else { if ((activeChar.getTarget() != null) && (activeChar.getTarget() instanceof L2PcInstance)) { L2Event.removeAndResetPlayer((L2PcInstance) activeChar.getTarget()); } } showEventControl(activeChar); } else if (actualCommand.startsWith("admin_event_control_prize")) { int[] teamIds = new int[st.countTokens() - 2]; int i = 0; while ((st.countTokens() - 2) > 0) // The last 2 tokens are used for "n" and "item id" { teamIds[i++] = Integer.parseInt(st.nextToken()); } String[] n = st.nextToken().split("\\*"); int itemId = Integer.parseInt(st.nextToken()); for (int teamId : teamIds) { rewardTeam(activeChar, teamId, Integer.parseInt(n[0]), itemId, n.length == 2 ? n[1] : ""); } showEventControl(activeChar); } } catch (Exception e) { e.printStackTrace(); AdminData.getInstance().broadcastMessageToGMs("EventEngine: Error! Possible blank boxes while executing a command which requires a value in the box?"); } return true; } @Override public String[] getAdminCommandList() { return ADMIN_COMMANDS; } private String showStoredEvents() { final File dir = new File(Config.DATAPACK_ROOT, "/data/events"); if (dir.isFile()) { return "The directory '" + dir.getAbsolutePath() + "' is a file or is corrupted!
"; } String note = ""; if (!dir.exists()) { note = "The directory '" + dir.getAbsolutePath() + "' does not exist!
Trying to create it now...

"; if (dir.mkdirs()) { note += "The directory '" + dir.getAbsolutePath() + "' has been created!
"; } else { note += "The directory '" + dir.getAbsolutePath() + "' hasn't been created!
"; return note; } } final String[] files = dir.list(); final StringBuilder result = new StringBuilder(files.length * 500); result.append(""); for (String fileName : files) { StringUtil.append(result, "", ""); } result.append("
", fileName, "
 
 
"); return note + result.toString(); } public void showMainPage(L2PcInstance activeChar) { final NpcHtmlMessage adminReply = new NpcHtmlMessage(); final String replyMSG = StringUtil.concat("[ L2J EVENT ENGINE ]" + "
"); if (tempBuffer.isEmpty()) { replyMSG.append("You can also use //add text to add text or //delete_buffer to remove the text."); } else { replyMSG.append(tempBuffer); } replyMSG.append("
"); adminReply.setHtml(replyMSG.toString()); activeChar.sendPacket(adminReply); } public void showEventParameters(L2PcInstance activeChar, int teamnumbers) { final NpcHtmlMessage adminReply = new NpcHtmlMessage(); StringBuilder sb = new StringBuilder(); sb.append("[ L2J EVENT ENGINE ]
Current event: "); sb.append(L2Event._eventName); sb.append("

INFO: To start an event, you must first set the number of teams, then type their names in the boxes and finally type the NPC ID that will be the event manager (can be any existing npc) next to the \"Announce Event!\" button.
"); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append("



"); sb.append("Teams' names:
"); for (int i = 1; (i - 1) < teamnumbers; i++) // Team names params { sb.append(""); } sb.append("
Team #"); sb.append(i); sb.append(" name:
"); adminReply.setHtml(sb.toString()); activeChar.sendPacket(adminReply); } private void showEventControl(L2PcInstance activeChar) { final NpcHtmlMessage adminReply = new NpcHtmlMessage(); StringBuilder sb = new StringBuilder(); sb.append("[ L2J EVENT ENGINE ]
Current event: "); sb.append(L2Event._eventName); sb.append("

"); sb.append("
Type the team ID(s) that will be affected by the commands. Commands with '*' work with only 1 team ID in the field, while '!' - none.
 
"); if (!npcsDeleted) { sb.append(""); } sb.append("" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
Destroys all event npcs so no more people can't participate now on
 
Teleports the specified team to your position
 
Sits/Stands up the team
 
Finish with the life of all the players in the selected team
 
Resurrect Team's members
 
Polymorphs the team into the NPC with the ID specified. Multiple IDs result in randomly chosen one for each player.
 
Unpolymorph the team
 
Transforms the team into the transformation with the ID specified. Multiple IDs result in randomly chosen one for each player.
 
Untransforms the team
 
Num
ID
Give the specified item id to every single member of the team, you can put 5*level, 5*kills or 5 in the number field for example
 
Kicks the specified player(s) from the event. Blank field kicks target.
 
Will finish the event teleporting back all the players
 
"); adminReply.setHtml(sb.toString()); activeChar.sendPacket(adminReply); } private void rewardTeam(L2PcInstance activeChar, int team, int n, int id, String type) { int num = n; for (L2PcInstance player : L2Event._teams.get(team)) { if (type.equalsIgnoreCase("level")) { num = n * player.getLevel(); } else if (type.equalsIgnoreCase("kills") && (player.getEventStatus() != null)) { num = n * player.getEventStatus().getKills().size(); } else { num = n; } player.addItem("Event", id, num, activeChar, true); final NpcHtmlMessage adminReply = new NpcHtmlMessage(); adminReply.setHtml(" CONGRATULATIONS! You should have been rewarded. "); player.sendPacket(adminReply); } } }