Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program 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.
- *
- * This program 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 <http://www.gnu.org/licenses/>.
- */
- package custom.events;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Logger;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.cache.HtmCache;
- import net.sf.l2j.gameserver.datatables.DoorTable;
- import net.sf.l2j.gameserver.datatables.ItemTable;
- import net.sf.l2j.gameserver.datatables.SkillTable;
- import net.sf.l2j.gameserver.model.L2Skill;
- import net.sf.l2j.gameserver.model.actor.L2Character;
- import net.sf.l2j.gameserver.model.actor.L2Summon;
- import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
- import net.sf.l2j.gameserver.model.itemcontainer.PcInventory;
- import net.sf.l2j.gameserver.model.olympiad.OlympiadManager;
- import net.sf.l2j.gameserver.network.SystemMessageId;
- import net.sf.l2j.gameserver.network.clientpackets.Say2;
- import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
- import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
- import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
- import net.sf.l2j.util.Rnd;
- import net.sf.l2j.util.StringUtil;
- /**
- * @author FBIagent
- */
- public class TvTEvent
- {
- enum EventState
- {
- INACTIVE,
- INACTIVATING,
- PARTICIPATING,
- STARTING,
- STARTED,
- REWARDING
- }
- protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
- /** html path **/
- private static final String htmlPath = "data/html/mods/TvTEvent/";
- /** The teams of the TvTEvent<br> */
- private static TvTEventTeam[] _teams = new TvTEventTeam[2];
- /** The state of the TvTEvent<br> */
- private static EventState _state = EventState.INACTIVE;
- /** the npc instance of the participation npc<br> */
- private static L2NpcInstance _lastNpcSpawn = null;
- /** Instance id<br> */
- private static int _TvTEventInstance = 0;
- /** Listeds Ips<br> */
- public static ArrayList<String> _listedIps;
- /**
- * No instance of this class!<br>
- */
- private TvTEvent()
- {
- }
- /**
- * Teams initializing<br>
- */
- @SuppressWarnings("unused")
- public static void init()
- {
- _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
- _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
- if (Config.TVT_RESTORE_PLAYER_POS)
- TvTEventTeleporter.initializeRestoreMap();
- if (Config.TVT_DISALLOW_DUALBOX)
- _listedIps = new ArrayList<String>(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS * 2);
- }
- /**
- * Starts the participation of the TvTEvent<br>
- * 1. Get L2NpcTemplate by Config.TVT_EVENT_PARTICIPATION_NPC_ID<br>
- * 2. Try to spawn a new npc of it<br><br>
- *
- * @return boolean: true if success, otherwise false<br>
- */
- public static boolean startParticipation()
- {
- setState(EventState.PARTICIPATING);
- return true;
- }
- private static int highestLevelPcInstanceOf(Map< Integer, L2PcInstance > players)
- {
- int maxLevel = Integer.MIN_VALUE, maxLevelId = -1;
- for (L2PcInstance player : players.values())
- {
- if (player.getLevel() >= maxLevel)
- {
- maxLevel = player.getLevel();
- maxLevelId = player.getObjectId();
- }
- }
- return maxLevelId;
- }
- /**
- * Starts the TvTEvent fight<br>
- * 1. Set state EventState.STARTING<br>
- * 2. Close doors specified in configs<br>
- * 3. Abort if not enought participants(return false)<br>
- * 4. Set state EventState.STARTED<br>
- * 5. Teleport all participants to team spot<br><br>
- *
- * @return boolean: true if success, otherwise false<br>
- */
- public static boolean startFight()
- {
- // Set state to STARTING
- setState(EventState.STARTING);
- // Randomize and balance team distribution
- @SuppressWarnings("unused")
- Map< Integer, L2PcInstance > allParticipants = new HashMap< Integer, L2PcInstance >();
- allParticipants.putAll(_teams[0].getParticipatedPlayers());
- allParticipants.putAll(_teams[1].getParticipatedPlayers());
- _teams[0].cleanMe();
- _teams[1].cleanMe();
- int balance[] = { 0, 0 }, priority = 0, highestLevelPlayerId;
- L2PcInstance highestLevelPlayer;
- // XXX: allParticipants should be sorted by level instead of using highestLevelPcInstanceOf for every fetch
- while (!allParticipants.isEmpty())
- {
- // Priority team gets one player
- highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
- highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
- allParticipants.remove(highestLevelPlayerId);
- _teams[priority].addPlayer(highestLevelPlayer);
- balance[priority] += highestLevelPlayer.getLevel();
- // Exiting if no more players
- if (allParticipants.isEmpty()) break;
- // The other team gets one player
- // XXX: Code not dry
- priority = 1-priority;
- highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
- highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
- allParticipants.remove(highestLevelPlayerId);
- _teams[priority].addPlayer(highestLevelPlayer);
- balance[priority] += highestLevelPlayer.getLevel();
- // Recalculating priority
- priority = balance[0] > balance[1] ? 1 : 0;
- }
- // Check for enought participants
- if (_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS || _teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS)
- {
- // Set state INACTIVE
- setState(EventState.INACTIVE);
- // Cleanup of teams
- _teams[0].cleanMe();
- _teams[1].cleanMe();
- return false;
- }
- // Opens all doors specified in configs for tvt
- openDoors(Config.TVT_DOORS_IDS_TO_OPEN);
- // Closes all doors specified in configs for tvt
- closeDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
- // Set state STARTED
- setState(EventState.STARTED);
- // Iterate over all teams
- for (TvTEventTeam team : _teams)
- {
- // Iterate over all participated player instances in this team
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- @SuppressWarnings("unused")
- boolean _canreward = true;
- if (playerInstance != null)
- {
- // Teleporter implements Runnable and starts itself
- new TvTEventTeleporter(playerInstance, team.getCoordinates(), false);
- }
- }
- }
- return true;
- }
- /**
- * Calculates the TvTEvent reward<br>
- * 1. If both teams are at a tie(points equals), send it as system message to all participants, if one of the teams have 0 participants left online abort rewarding<br>
- * 2. Wait till teams are not at a tie anymore<br>
- * 3. Set state EvcentState.REWARDING<br>
- * 4. Reward team with more points<br>
- * 5. Show win html to wining team participants<br><br>
- *
- * @return String: winning team name<br>
- */
- public static String calculateRewards()
- {
- if (_teams[0].getPoints() == _teams[1].getPoints())
- {
- // Check if one of the teams have no more players left
- if (_teams[0].getParticipatedPlayerCount() == 0 || _teams[1].getParticipatedPlayerCount() == 0)
- {
- // set state to rewarding
- setState(EventState.REWARDING);
- // return here, the fight can't be completed
- return "Team vs Team Event: Event has ended. No team won due to inactivity!";
- }
- // Both teams have equals points
- sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
- if (Config.TVT_REWARD_TEAM_TIE)
- {
- rewardTeam(_teams[0]);
- rewardTeam(_teams[1]);
- return "Team vs Team Event: Event has ended with both teams tying.";
- }
- else
- return "Team vs Team Event: Event has ended with both teams tying.";
- }
- // Set state REWARDING so nobody can point anymore
- setState(EventState.REWARDING);
- // Get team which has more points
- TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
- rewardTeam(team);
- return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
- }
- private static void rewardTeam(TvTEventTeam team)
- {
- // Iterate over all participated player instances of the winning team
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- boolean _canreward = true;
- // Check for nullpointer
- if (playerInstance == null)
- {
- continue;
- }
- if (Config.TVT_REWARD_ONLY_KILLERS)
- {
- if (playerInstance.getTvTKills() <= 0)
- _canreward = false;
- }
- SystemMessage systemMessage = null;
- if (_canreward)
- {
- // Iterate over all tvt event rewards
- for (int[] reward : Config.TVT_EVENT_REWARDS)
- {
- PcInventory inv = playerInstance.getInventory();
- // Check for stackable item, non stackabe items need to be added one by one
- if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
- {
- inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
- if (reward[1] > 1)
- {
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
- systemMessage.addItemName(reward[0]);
- systemMessage.addNumber(reward[1]);
- }
- else
- {
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
- systemMessage.addItemName(reward[0]);
- }
- playerInstance.sendPacket(systemMessage);
- }
- else
- {
- for (int i = 0; i < reward[1]; ++i)
- {
- inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
- systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
- systemMessage.addItemName(reward[0]);
- playerInstance.sendPacket(systemMessage);
- }
- }
- }
- StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
- statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+ "Reward.htm"));
- playerInstance.sendPacket(statusUpdate);
- playerInstance.sendPacket(npcHtmlMessage);
- }
- else
- {
- StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
- statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Reward0.htm"));
- playerInstance.sendPacket(statusUpdate);
- playerInstance.sendPacket(npcHtmlMessage);
- }
- }
- }
- /**
- * Stops the TvTEvent fight<br>
- * 1. Set state EventState.INACTIVATING<br>
- * 2. Remove tvt npc from world<br>
- * 3. Open doors specified in configs<br>
- * 4. Teleport all participants back to participation npc location<br>
- * 5. Teams cleaning<br>
- * 6. Set state EventState.INACTIVE<br>
- */
- public static void stopFight()
- {
- // Set state INACTIVATING
- setState(EventState.INACTIVATING);
- // Opens all doors specified in configs for tvt
- openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
- // Closes all doors specified in Configs for tvt
- closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
- // Iterate over all teams
- for (TvTEventTeam team : _teams)
- {
- for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
- {
- @SuppressWarnings("unused")
- boolean _canreward = true;
- // Check for nullpointer
- if (playerInstance != null)
- {
- new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false);
- }
- }
- }
- if (Config.TVT_RESTORE_PLAYER_POS)
- TvTEventTeleporter.clearRestoreMap();
- if (Config.TVT_DISALLOW_DUALBOX)
- _listedIps.clear();
- // Cleanup of teams
- _teams[0].cleanMe();
- _teams[1].cleanMe();
- // Set state INACTIVE
- setState(EventState.INACTIVE);
- }
- /**
- * Adds a player to a TvTEvent team<br>
- * 1. Calculate the id of the team in which the player should be added<br>
- * 2. Add the player to the calculated team<br><br>
- *
- * @param playerInstance as L2PcInstance<br>
- * @return boolean: true if success, otherwise false<br>
- */
- public static synchronized boolean addParticipant(L2PcInstance playerInstance)
- {
- // Check for nullpoitner
- if (playerInstance == null)
- {
- return false;
- }
- byte teamId = 0;
- // Check to which team the player should be added
- if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
- {
- teamId = (byte) (Rnd.get(2));
- }
- else
- {
- teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
- }
- if (Config.TVT_DISALLOW_DUALBOX)
- {
- String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
- if (host != null && !_listedIps.contains(host))
- _listedIps.add(host);
- else
- return false;
- }
- return _teams[teamId].addPlayer(playerInstance);
- }
- /**
- * Removes a TvTEvent player from it's team<br>
- * 1. Get team id of the player<br>
- * 2. Remove player from it's team<br><br>
- * @param playerObjectId
- * @return boolean: true if success, otherwise false<br>
- */
- public static boolean removeParticipant(int playerObjectId)
- {
- // Get the teamId of the player
- byte teamId = getParticipantTeamId(playerObjectId);
- // Check if the player is participant
- if (teamId != -1)
- {
- // Remove the player from team
- _teams[teamId].removePlayer(playerObjectId);
- return true;
- }
- return false;
- }
- public static boolean payParticipationFee(L2PcInstance playerInstance)
- {
- int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
- int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
- if (itemId == 0 || itemNum == 0)
- return true;
- if (playerInstance.getInventory().getInventoryItemCount(itemId, -1) < itemNum)
- return false;
- return playerInstance.destroyItemByItemId("TvT Participation Fee", itemId, itemNum, _lastNpcSpawn, true);
- }
- public static String getParticipationFee()
- {
- int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
- int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
- if (itemId == 0 || itemNum == 0)
- return "-";
- return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
- }
- /**
- * Send a SystemMessage to all participated players<br>
- * 1. Send the message to all players of team number one<br>
- * 2. Send the message to all players of team number two<br><br>
- *
- * @param message as String<br>
- */
- public static void sysMsgToAllParticipants(String message)
- {
- for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendMessage(message);
- }
- }
- for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendMessage(message);
- }
- }
- }
- /**
- * Close doors specified in configs
- * @param doors
- */
- private static void closeDoors(List<Integer> doors)
- {
- for (int doorId : doors)
- {
- L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
- if (doorInstance != null)
- {
- doorInstance.closeMe();
- }
- }
- }
- /**
- * Open doors specified in configs
- * @param doors
- */
- private static void openDoors(List<Integer> doors)
- {
- for (int doorId : doors)
- {
- L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
- if (doorInstance != null)
- {
- doorInstance.openMe();
- }
- }
- }
- /**
- * Called when a player logs in<br><br>
- *
- * @param playerInstance as L2PcInstance<br>
- */
- public static void onLogin(L2PcInstance playerInstance)
- {
- if (playerInstance == null || (!isStarting() && !isStarted()))
- {
- return;
- }
- byte teamId = getParticipantTeamId(playerInstance.getObjectId());
- if (teamId == -1)
- {
- return;
- }
- _teams[teamId].addPlayer(playerInstance);
- new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true);
- }
- /**
- * Called when a player logs out<br><br>
- *
- * @param playerInstance as L2PcInstance<br>
- */
- public static void onLogout(L2PcInstance playerInstance)
- {
- if (playerInstance != null && (isStarting() || isStarted() || isParticipating()))
- {
- if (removeParticipant(playerInstance.getObjectId()))
- playerInstance.setXYZInvisible(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)-50,
- Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)-50,
- Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
- }
- }
- /**
- * Called on every bypass by npc of type L2TvTEventNpc<br>
- * Needs synchronization cause of the max player check<br><br>
- *
- * @param command as String<br>
- * @param playerInstance as L2PcInstance<br>
- */
- public static synchronized void onBypass(String command, L2PcInstance playerInstance)
- {
- if (playerInstance == null || !isParticipating())
- return;
- final String htmContent;
- if (command.equals("tvt_event_participation"))
- {
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
- int playerLevel = playerInstance.getLevel();
- if (Config.TVT_DISALLOW_DUALBOX && isDualBoxing(playerInstance))
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"DualBox.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%pName%", playerInstance.getName());
- String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
- npcHtmlMessage.replace("%ip%", host);
- }
- }
- else if (playerInstance.isCursedWeaponEquipped())
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"CursedWeaponEquipped.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- else if (OlympiadManager.getInstance().isRegistered(playerInstance))
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"Olympiad.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- /*else if(playerInstance.isAio())
- {
- playerInstance.sendMessage("AIO cannot register for TvT Event.");
- return;
- }*/
- else if (playerInstance.getKarma() > 0)
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"Karma.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- }
- }
- else if (playerLevel < Config.TVT_EVENT_MIN_LVL || playerLevel > Config.TVT_EVENT_MAX_LVL)
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"Level.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
- npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
- }
- }
- else if (_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS && _teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS)
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"TeamsFull.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
- }
- }
- else if (!payParticipationFee(playerInstance))
- {
- htmContent = HtmCache.getInstance().getHtm(htmlPath+"ParticipationFee.htm");
- if (htmContent != null)
- {
- npcHtmlMessage.setHtml(htmContent);
- npcHtmlMessage.replace("%fee%", getParticipationFee());
- }
- }
- else if (addParticipant(playerInstance))
- {
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Registered.htm"));
- }
- else
- {
- return;
- }
- playerInstance.sendPacket(npcHtmlMessage);
- }
- else if (command.equals("tvt_event_remove_participation"))
- {
- removeParticipant(playerInstance.getObjectId());
- if (Config.TVT_DISALLOW_DUALBOX)
- {
- String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
- if (_listedIps.contains(host))
- {
- _listedIps.remove(host);
- }
- }
- NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
- npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Unregistered.htm"));
- playerInstance.sendPacket(npcHtmlMessage);
- }
- }
- /**
- * Called on every onAction in L2PcIstance<br><br>
- * @param playerInstance
- * @param targetedPlayerObjectId
- *
- * @return boolean: true if player is allowed to target, otherwise false<br>
- */
- public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
- {
- if (playerInstance == null || !isStarted())
- {
- return true;
- }
- if (playerInstance.isGM())
- {
- return true;
- }
- byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
- byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
- if ((playerTeamId != -1 && targetedPlayerTeamId == -1) || (playerTeamId == -1 && targetedPlayerTeamId != -1))
- {
- return false;
- }
- if (playerTeamId != -1 && targetedPlayerTeamId != -1 && playerTeamId == targetedPlayerTeamId && playerInstance.getObjectId() != targetedPlayerObjectId && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
- {
- return false;
- }
- return true;
- }
- /**
- * Called on every scroll use<br><br>
- * @param playerObjectId
- *
- * @return boolean: true if player is allowed to use scroll, otherwise false<br>
- */
- public static boolean onScrollUse(int playerObjectId)
- {
- if (!isStarted())
- return true;
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
- return false;
- return true;
- }
- /**
- * Called on every potion use<br><br>
- * @param playerObjectId
- *
- * @return boolean: true if player is allowed to use potions, otherwise false<br>
- */
- public static boolean onPotionUse(int playerObjectId)
- {
- if (!isStarted())
- return true;
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
- return false;
- return true;
- }
- /**
- * Called on every escape use(thanks to nbd)<br><br>
- * @param playerObjectId
- *
- * @return boolean: true if player is not in tvt event, otherwise false<br>
- */
- public static boolean onEscapeUse(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
- if (isPlayerParticipant(playerObjectId))
- {
- return false;
- }
- return true;
- }
- /**
- * Called on every summon item use<br><br>
- * @param playerObjectId
- *
- * @return boolean: true if player is allowed to summon by item, otherwise false<br>
- */
- public static boolean onItemSummon(int playerObjectId)
- {
- if (!isStarted())
- {
- return true;
- }
- if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
- {
- return false;
- }
- return true;
- }
- /**
- * Is called when a player is killed<br><br>
- *
- * @param killerCharacter as L2Character<br>
- * @param killedPlayerInstance as L2PcInstance<br>
- */
- public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
- {
- if (killedPlayerInstance == null || !isStarted())
- {
- return;
- }
- byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
- if (killedTeamId == -1)
- {
- return;
- }
- new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false);
- if (killerCharacter == null)
- {
- return;
- }
- L2PcInstance killerPlayerInstance = null;
- if (killerCharacter instanceof L2PetInstance || killerCharacter instanceof L2SummonInstance)
- {
- killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
- if (killerPlayerInstance == null)
- {
- return;
- }
- }
- else if (killerCharacter instanceof L2PcInstance)
- {
- killerPlayerInstance = (L2PcInstance) killerCharacter;
- }
- else
- {
- return;
- }
- byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
- if (killerTeamId != -1 && killedTeamId != -1 && killerTeamId != killedTeamId)
- {
- TvTEventTeam killerTeam = _teams[killerTeamId];
- killerTeam.increasePoints();
- if (Config.TVT_REWARD_ONLY_KILLERS)
- killerPlayerInstance.increaseTvTKills();
- CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
- killerPlayerInstance._originalKarmaTvT = killerPlayerInstance.getKarma();
- killerPlayerInstance._originalTitle = killerPlayerInstance.getTitle();
- killerPlayerInstance.setTitle("Kills: "+killerPlayerInstance.getTvTKills());
- killerPlayerInstance.setKarma(0);
- killerPlayerInstance.broadcastStatusUpdate();
- killerPlayerInstance.broadcastUserInfo();
- for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
- {
- if (playerInstance != null)
- {
- playerInstance.sendPacket(cs);
- }
- }
- }
- }
- /**
- * Called on Appearing packet received (player finished teleporting)<br><br>
- * @param playerInstance
- */
- public static void onTeleported(L2PcInstance playerInstance)
- {
- if (!isStarted() || playerInstance == null || !isPlayerParticipant(playerInstance.getObjectId()))
- return;
- if (playerInstance.isMageClass())
- {
- if (Config.TVT_EVENT_MAGE_BUFFS != null && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
- {
- for (int i : Config.TVT_EVENT_MAGE_BUFFS.keySet())
- {
- L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_MAGE_BUFFS.get(i));
- if (skill != null)
- skill.getEffects(playerInstance, playerInstance);
- }
- }
- }
- else
- {
- if (Config.TVT_EVENT_FIGHTER_BUFFS != null && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
- {
- for (int i : Config.TVT_EVENT_FIGHTER_BUFFS.keySet())
- {
- L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_FIGHTER_BUFFS.get(i));
- if (skill != null)
- skill.getEffects(playerInstance, playerInstance);
- }
- }
- }
- if (Config.TVT_EVENT_RESTORE_CPHPMP)
- {
- playerInstance.setCurrentCp(playerInstance.getMaxCp());
- playerInstance.setCurrentHpMp(playerInstance.getMaxHp(), playerInstance.getMaxMp());
- }
- }
- /*
- * Return true if player valid for skill
- */
- public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
- {
- if (!isStarted())
- return true;
- // TvT is started
- final int sourcePlayerId = source.getObjectId();
- final int targetPlayerId = target.getObjectId();
- final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
- final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
- // both players not participating
- if (!isSourceParticipant && !isTargetParticipant)
- return true;
- // one player not participating
- if (!(isSourceParticipant && isTargetParticipant))
- return false;
- // players in the different teams ?
- if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
- {
- if (!skill.isOffensive())
- return false;
- }
- return true;
- }
- /**
- * Sets the TvTEvent state<br><br>
- *
- * @param state as EventState<br>
- */
- private static void setState(EventState state)
- {
- synchronized (_state)
- {
- _state = state;
- }
- }
- /**
- * Is TvTEvent inactive?<br><br>
- *
- * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
- */
- public static boolean isInactive()
- {
- boolean isInactive;
- synchronized (_state)
- {
- isInactive = _state == EventState.INACTIVE;
- }
- return isInactive;
- }
- /**
- * Is TvTEvent in inactivating?<br><br>
- *
- * @return boolean: true if event is in inactivating progress, otherwise false<br>
- */
- public static boolean isInactivating()
- {
- boolean isInactivating;
- synchronized (_state)
- {
- isInactivating = _state == EventState.INACTIVATING;
- }
- return isInactivating;
- }
- /**
- * Is TvTEvent in participation?<br><br>
- *
- * @return boolean: true if event is in participation progress, otherwise false<br>
- */
- public static boolean isParticipating()
- {
- boolean isParticipating = false;
- synchronized (_state)
- {
- isParticipating = _state == EventState.PARTICIPATING;
- }
- return isParticipating;
- }
- /**
- * Is TvTEvent starting?<br><br>
- *
- * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
- */
- public static boolean isStarting()
- {
- boolean isStarting;
- synchronized (_state)
- {
- isStarting = _state == EventState.STARTING;
- }
- return isStarting;
- }
- /**
- * Is TvTEvent started?<br><br>
- *
- * @return boolean: true if event is started, otherwise false<br>
- */
- public static boolean isStarted()
- {
- boolean isStarted;
- synchronized (_state)
- {
- isStarted = _state == EventState.STARTED;
- }
- return isStarted;
- }
- /**
- * Is TvTEvent rewadrding?<br><br>
- *
- * @return boolean: true if event is currently rewarding, otherwise false<br>
- */
- public static boolean isRewarding()
- {
- boolean isRewarding;
- synchronized (_state)
- {
- isRewarding = _state == EventState.REWARDING;
- }
- return isRewarding;
- }
- /**
- * Returns the team id of a player, if player is not participant it returns -1<br><br>
- * @param playerObjectId
- *
- * @return byte: team name of the given playerName, if not in event -1<br>
- */
- public static byte getParticipantTeamId(int playerObjectId)
- {
- return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
- }
- /**
- * Returns the team of a player, if player is not participant it returns null <br><br>
- * @param playerObjectId
- *
- * @return TvTEventTeam: team of the given playerObjectId, if not in event null <br>
- */
- public static TvTEventTeam getParticipantTeam(int playerObjectId)
- {
- return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
- }
- /**
- * Returns the enemy team of a player, if player is not participant it returns null <br><br>
- * @param playerObjectId
- *
- * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null <br>
- */
- public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
- {
- return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
- }
- /**
- * Returns the team coordinates in which the player is in, if player is not in a team return null<br><br>
- * @param playerObjectId
- *
- * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static int[] getParticipantTeamCoordinates(int playerObjectId)
- {
- return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
- }
- /**
- * Is given player participant of the event?<br><br>
- * @param playerObjectId
- *
- * @return boolean: true if player is participant, ohterwise false<br>
- */
- public static boolean isPlayerParticipant(int playerObjectId)
- {
- if (!isParticipating() && !isStarting() && !isStarted())
- {
- return false;
- }
- return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
- }
- /**
- * Returns participated player count<br><br>
- *
- * @return int: amount of players registered in the event<br>
- */
- public static int getParticipatedPlayersCount()
- {
- if (!isParticipating() && !isStarting() && !isStarted())
- {
- return 0;
- }
- return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
- }
- /**
- * Returns teams names<br><br>
- *
- * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static String[] getTeamNames()
- {
- return new String[]
- {
- _teams[0].getName(), _teams[1].getName()
- };
- }
- /**
- * Returns player count of both teams<br><br>
- *
- * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static int[] getTeamsPlayerCounts()
- {
- return new int[]
- {
- _teams[0].getParticipatedPlayerCount(), _teams[1].getParticipatedPlayerCount()
- };
- }
- /**
- * Returns points count of both teams
- *
- * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
- */
- public static int[] getTeamsPoints()
- {
- return new int[]
- {
- _teams[0].getPoints(), _teams[1].getPoints()
- };
- }
- public static int getTvTEventInstance()
- {
- return _TvTEventInstance;
- }
- public static boolean isDualBoxing(L2PcInstance player)
- {
- if (player == null) return true;
- String host = player.getClient().getConnection().getInetAddress().getHostAddress();
- if (_listedIps.contains(host))
- return true;
- return false;
- }
- }
- /*
- * This program 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.
- *
- * This program 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 <http://www.gnu.org/licenses/>.
- */
- package custom.events;
- import java.util.HashMap;
- import java.util.Map;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- /**
- * @author FBIagent
- */
- public class TvTEventTeam
- {
- /** The name of the team<br> */
- private String _name;
- /** The team spot coordinated<br> */
- private int[] _coordinates = new int[3];
- /** The points of the team<br> */
- private short _points;
- /** Name and instance of all participated players in HashMap<br> */
- @SuppressWarnings("unused")
- private Map<Integer, L2PcInstance> _participatedPlayers = new HashMap<Integer, L2PcInstance>();
- /**
- * C'tor initialize the team<br><br>
- *
- * @param name as String<br>
- * @param coordinates as int[]<br>
- */
- public TvTEventTeam(String name, int[] coordinates)
- {
- _name = name;
- _coordinates = coordinates;
- _points = 0;
- }
- /**
- * Adds a player to the team<br><br>
- *
- * @param playerInstance as L2PcInstance<br>
- * @return boolean: true if success, otherwise false<br>
- */
- public boolean addPlayer(L2PcInstance playerInstance)
- {
- if (playerInstance == null)
- {
- return false;
- }
- synchronized (_participatedPlayers)
- {
- _participatedPlayers.put(playerInstance.getObjectId(), playerInstance);
- }
- return true;
- }
- /**
- * Removes a player from the team<br><br>
- * @param playerObjectId
- *
- */
- public void removePlayer(int playerObjectId)
- {
- synchronized (_participatedPlayers)
- {
- _participatedPlayers.remove(playerObjectId);
- }
- }
- /**
- * Increases the points of the team<br>
- */
- public void increasePoints()
- {
- ++_points;
- }
- /**
- * Cleanup the team and make it ready for adding players again<br>
- */
- @SuppressWarnings("unused")
- public void cleanMe()
- {
- _participatedPlayers.clear();
- _participatedPlayers = new HashMap<Integer, L2PcInstance>();
- _points = 0;
- }
- /**
- * Is given player in this team?<br><br>
- * @param playerObjectId
- * @return boolean: true if player is in this team, otherwise false<br>
- */
- public boolean containsPlayer(int playerObjectId)
- {
- boolean containsPlayer;
- synchronized (_participatedPlayers)
- {
- containsPlayer = _participatedPlayers.containsKey(playerObjectId);
- }
- return containsPlayer;
- }
- /**
- * Returns the name of the team<br><br>
- *
- * @return String: name of the team<br>
- */
- public String getName()
- {
- return _name;
- }
- /**
- * Returns the coordinates of the team spot<br><br>
- *
- * @return int[]: team coordinates<br>
- */
- public int[] getCoordinates()
- {
- return _coordinates;
- }
- /**
- * Returns the points of the team<br><br>
- *
- * @return short: team points<br>
- */
- public short getPoints()
- {
- return _points;
- }
- /**
- * Returns name and instance of all participated players in HashMap<br><br>
- *
- * @return Map<String, L2PcInstance>: map of players in this team<br>
- */
- public Map<Integer, L2PcInstance> getParticipatedPlayers()
- {
- Map<Integer, L2PcInstance> participatedPlayers = null;
- synchronized (_participatedPlayers)
- {
- participatedPlayers = _participatedPlayers;
- }
- return participatedPlayers;
- }
- /**
- * Returns player count of this team<br><br>
- *
- * @return int: number of players in team<br>
- */
- public int getParticipatedPlayerCount()
- {
- int participatedPlayerCount;
- synchronized (_participatedPlayers)
- {
- participatedPlayerCount = _participatedPlayers.size();
- }
- return participatedPlayerCount;
- }
- }
- /*
- * This program 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.
- *
- * This program 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 <http://www.gnu.org/licenses/>.
- */
- package custom.events;
- import java.util.HashMap;
- import java.util.logging.Logger;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.model.actor.L2Summon;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.entity.Duel;
- import net.sf.l2j.gameserver.model.entity.Duel.DuelState;
- import net.sf.l2j.util.Rnd;
- public class TvTEventTeleporter implements Runnable
- {
- /** The instance of the player to teleport */
- private L2PcInstance _playerInstance = null;
- /** Coordinates of the spot to teleport to */
- private int[] _coordinates = new int[3];
- /** Admin removed this player from event */
- //private boolean _adminRemove = false;
- /** Old Player Coordinates */
- private static HashMap<Integer, Integer[]> _oldPlayerPos;
- public static final Logger _log = Logger.getLogger(TvTEventTeleporter.class.getName());
- /**
- * Initialize the teleporter and start the delayed task<br><br>
- *
- * @param playerInstance as L2PcInstance<br>
- * @param coordinates as int[]<br>
- * @param fastSchedule
- */
- public TvTEventTeleporter(L2PcInstance playerInstance, int[] coordinates, boolean fastSchedule)
- {
- _playerInstance = playerInstance;
- _coordinates = coordinates;
- //_adminRemove = adminRemove;
- long delay = (TvTEvent.isStarted() ? Config.TVT_EVENT_RESPAWN_TELEPORT_DELAY : Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY) * 1000;
- ThreadPoolManager.getInstance().scheduleGeneral(this, fastSchedule ? 0 : delay);
- }
- /**
- * The task method to teleport the player<br>
- * 1. Unsummon pet if there is one<br>
- * 2. Remove all effects<br>
- * 3. Revive and full heal the player<br>
- * 4. Teleport the player<br>
- * 5. Broadcast status and user info<br><br>
- */
- @Override
- public void run()
- {
- if (_playerInstance == null)
- return;
- L2Summon summon = _playerInstance.getPet();
- if (summon != null)
- summon.unSummon(_playerInstance);
- if (Config.TVT_EVENT_EFFECTS_REMOVAL == 0
- || (Config.TVT_EVENT_EFFECTS_REMOVAL == 1 || (_playerInstance.isInDuel() && _playerInstance.getDuelState() != DuelState.INTERRUPTED)))
- _playerInstance.stopAllEffects();
- if (_playerInstance.isInDuel())
- _playerInstance.setDuelState(DuelState.INTERRUPTED);
- _playerInstance.doRevive();
- int objId = _playerInstance.getObjectId();
- if (Config.TVT_RESTORE_PLAYER_POS && TvTEvent.isStarted())
- {
- final Integer[] oldCoords =
- {
- _playerInstance.getX(),
- _playerInstance.getY(),
- _playerInstance.getZ()
- };
- _oldPlayerPos.put(objId, oldCoords);
- }
- if (Config.TVT_RESTORE_PLAYER_POS && !TvTEvent.isStarted())
- {
- Integer[] coor = _oldPlayerPos.get(objId);
- if (coor != null)
- {
- _playerInstance.teleToLocation(coor[0], coor[1], coor[2], 0);
- }
- else
- {
- _playerInstance.teleToLocation( _coordinates[ 0 ] + Rnd.get(101)-50, _coordinates[ 1 ] + Rnd.get(101)-50, _coordinates[ 2 ], 0 );
- }
- }
- else
- _playerInstance.teleToLocation( _coordinates[ 0 ] + Rnd.get(101)-50, _coordinates[ 1 ] + Rnd.get(101)-50, _coordinates[ 2 ], 0 );
- if (TvTEvent.isStarted())
- {
- _playerInstance.getAppearance().getNameColor();
- }
- else
- {
- _playerInstance.getAppearance().getNameColor();
- }
- if (_oldPlayerPos.containsKey(Integer.valueOf(objId)))
- {
- _oldPlayerPos.remove(Integer.valueOf(objId));
- }
- _playerInstance.setCurrentCp(_playerInstance.getMaxCp());
- _playerInstance.setCurrentHp(_playerInstance.getMaxHp());
- _playerInstance.setCurrentMp(_playerInstance.getMaxMp());
- _playerInstance.broadcastStatusUpdate();
- _playerInstance.broadcastTitleInfo();
- _playerInstance.broadcastUserInfo();
- }
- /**
- * Initializes the map where the player position
- * will be stored
- */
- @SuppressWarnings("unused")
- public static void initializeRestoreMap()
- {
- if (_oldPlayerPos == null)
- _oldPlayerPos = new HashMap<Integer, Integer[]>();
- }
- /**
- * Clear all containing data for a new event
- */
- public static void clearRestoreMap()
- {
- _oldPlayerPos.clear();
- }
- }
- /*
- * Copyright (C) 2004-2013 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server 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 Server 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 <http://www.gnu.org/licenses/>.
- */
- package custom.events;
- import java.util.Calendar;
- import java.util.concurrent.ScheduledFuture;
- import java.util.logging.Logger;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.Announcements;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- /**
- * @author FBIagent
- */
- public class TvTManager
- {
- protected static final Logger _log = Logger.getLogger(TvTManager.class.getName());
- /** Task for event cycles<br> */
- private TvTStartTask _task;
- /**
- * New instance only by getInstance()<br>
- */
- protected TvTManager()
- {
- if (Config.TVT_EVENT_ENABLED)
- {
- TvTEvent.init();
- scheduleEventStart();
- _log.info("TvTEventEngine[TvTManager.TvTManager()]: Started.");
- }
- else
- {
- _log.info("TvTEventEngine[TvTManager.TvTManager()]: Engine is disabled.");
- }
- }
- /**
- * Initialize new/Returns the one and only instance<br>
- * <br>
- * @return TvTManager<br>
- */
- public static TvTManager getInstance()
- {
- return SingletonHolder._instance;
- }
- /**
- * Starts TvTStartTask
- */
- public void scheduleEventStart()
- {
- try
- {
- Calendar currentTime = Calendar.getInstance();
- Calendar nextStartTime = null;
- Calendar testStartTime = null;
- for (String timeOfDay : Config.TVT_EVENT_INTERVAL)
- {
- // Creating a Calendar object from the specified interval value
- testStartTime = Calendar.getInstance();
- testStartTime.setLenient(true);
- String[] splitTimeOfDay = timeOfDay.split(":");
- testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
- testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
- // If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
- if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
- {
- testStartTime.add(Calendar.DAY_OF_MONTH, 1);
- }
- // Check for the test date to be the minimum (smallest in the specified list)
- if ((nextStartTime == null) || (testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis()))
- {
- nextStartTime = testStartTime;
- }
- }
- if (nextStartTime != null)
- {
- _task = new TvTStartTask(nextStartTime.getTimeInMillis());
- ThreadPoolManager.getInstance().executeTask(_task);
- }
- }
- catch (Exception e)
- {
- _log.warning("TvTEventEngine[TvTManager.scheduleEventStart()]: Error figuring out a start time. Check TvTEventInterval in config file.");
- }
- }
- /**
- * Method to start participation
- */
- public void startReg()
- {
- if (!TvTEvent.startParticipation())
- {
- Announcements.EventAnnounceToAll("TvT Event:","Team vs Team Event: Event was cancelled.");
- _log.warning("TvTEventEngine[TvTManager.run()]: Error spawning event npc for participation.");
- scheduleEventStart();
- }
- else
- {
- Announcements.EventAnnounceToAll("TvT Event: ","Team vs Team Event: Registration opened for " + Config.TVT_EVENT_PARTICIPATION_TIME + " minute(s).");
- // schedule registration end
- _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_PARTICIPATION_TIME));
- ThreadPoolManager.getInstance().executeTask(_task);
- Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: Type .events to enter panel!");
- }
- }
- /**
- * Method to start the fight
- */
- public void startEvent()
- {
- if (!TvTEvent.startFight())
- {
- Announcements.EventAnnounceToAll("TvT Event","Team vs Team Event: Event cancelled due to lack of Participation.");
- _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
- scheduleEventStart();
- }
- else
- {
- _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_RUNNING_TIME));
- ThreadPoolManager.getInstance().executeTask(_task);
- }
- }
- /**
- * Method to end the event and reward
- */
- public void endEvent()
- {
- Announcements.EventAnnounceToAll("",TvTEvent.calculateRewards());
- TvTEvent.sysMsgToAllParticipants("Team vs Team Event: Teleporting back to the MainTown " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
- TvTEvent.stopFight();
- scheduleEventStart();
- }
- public void skipDelay()
- {
- if (_task.nextRun.cancel(false))
- {
- _task.setStartTime(System.currentTimeMillis());
- ThreadPoolManager.getInstance().executeTask(_task);
- }
- }
- /**
- * Class for TvT cycles
- */
- class TvTStartTask implements Runnable
- {
- private long _startTime;
- public ScheduledFuture<?> nextRun;
- public TvTStartTask(long startTime)
- {
- _startTime = startTime;
- }
- public void setStartTime(long startTime)
- {
- _startTime = startTime;
- }
- @Override
- public void run()
- {
- int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
- if (delay > 0)
- {
- announce(delay);
- }
- int nextMsg = 0;
- if (delay > 3600)
- {
- nextMsg = delay - 3600;
- }
- else if (delay > 1800)
- {
- nextMsg = delay - 1800;
- }
- else if (delay > 900)
- {
- nextMsg = delay - 900;
- }
- else if (delay > 600)
- {
- nextMsg = delay - 600;
- }
- else if (delay > 300)
- {
- nextMsg = delay - 300;
- }
- else if (delay > 60)
- {
- nextMsg = delay - 60;
- }
- else if (delay > 5)
- {
- nextMsg = delay - 5;
- }
- else if (delay > 0)
- {
- nextMsg = delay;
- }
- else
- {
- // start
- if (TvTEvent.isInactive())
- {
- startReg();
- }
- else if(TvTEvent.isParticipating())
- {
- startEvent();
- }
- else
- {
- endEvent();
- }
- }
- if (delay > 0)
- {
- nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
- }
- }
- private void announce(long time)
- {
- if ((time >= 3600) && ((time % 3600) == 0))
- {
- if (TvTEvent.isParticipating())
- {
- Announcements.EventAnnounceToAll("TvT Event","Team vs Team Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
- }
- else if (TvTEvent.isStarted())
- {
- TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
- }
- }
- else if (time >= 60)
- {
- if (TvTEvent.isParticipating())
- {
- Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: " + (time / 60) + " minute(s) until registration is closed!");
- }
- else if (TvTEvent.isStarted())
- {
- TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + (time / 60) + " minute(s) until the event is finished!");
- }
- }
- else
- {
- if (TvTEvent.isParticipating())
- {
- Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: " + time + " second(s) until registration is closed!");
- }
- else if (TvTEvent.isStarted())
- {
- TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + time + " second(s) until the event is finished!");
- }
- }
- }
- }
- private static class SingletonHolder
- {
- protected static final TvTManager _instance = new TvTManager();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment