Advertisement
Nik

L2Event rework core

Nik
May 17th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 33.11 KB | None | 0 0
  1. Index: java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java
  2. ===================================================================
  3. --- java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java    (revision 4623)
  4. +++ java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java    (working copy)
  5. @@ -21,7 +21,6 @@
  6.  import java.util.Arrays;
  7.  import java.util.Calendar;
  8.  import java.util.Collection;
  9. -import java.util.LinkedList;
  10.  import java.util.List;
  11.  import java.util.Map;
  12.  import java.util.Set;
  13. @@ -265,6 +264,7 @@
  14.  import com.l2jserver.gameserver.templates.skills.L2SkillType;
  15.  import com.l2jserver.gameserver.util.FloodProtectors;
  16.  import com.l2jserver.gameserver.util.Util;
  17. +import com.l2jserver.util.PlayerEventStatus;
  18.  import com.l2jserver.util.Point3D;
  19.  import com.l2jserver.util.Rnd;
  20.  
  21. @@ -558,7 +558,7 @@
  22.     private boolean _recoTwoHoursGiven = false;
  23.    
  24.     private final PcInventory _inventory = new PcInventory(this);
  25. -   private PcFreight _freight = new PcFreight(this);
  26. +   private final PcFreight _freight = new PcFreight(this);
  27.     private PcWarehouse _warehouse;
  28.     private PcRefund _refund;
  29.    
  30. @@ -729,16 +729,7 @@
  31.     public final ReentrantLock soulShotLock = new ReentrantLock();
  32.    
  33.     /** Event parameters */
  34. -   public int eventX;
  35. -   public int eventY;
  36. -   public int eventZ;
  37. -   public int eventkarma;
  38. -   public int eventpvpkills;
  39. -   public int eventpkkills;
  40. -   public String eventTitle;
  41. -   public LinkedList<String> kills = new LinkedList<String>();
  42. -   public boolean eventSitForced = false;
  43. -   public boolean atEvent = false;
  44. +   private PlayerEventStatus eventStatus = null;
  45.    
  46.     private byte _handysBlockCheckerEventArena = -1;
  47.    
  48. @@ -3142,9 +3133,9 @@
  49.      */
  50.     public void standUp()
  51.     {
  52. -       if (L2Event.active && eventSitForced)
  53. +       if (L2Event.isParticipant(this) && getEventStatus().eventSitForced)
  54.         {
  55. -           sendMessage("A dark force beyond your mortal understanding makes your knees to shake when you try to stand up ...");
  56. +           sendMessage("A dark force beyond your mortal understanding makes your knees to shake when you try to stand up...");
  57.         }
  58.         else if (_waitTypeSitting && !isInStoreMode() && !isAlikeDead())
  59.         {
  60. @@ -5369,10 +5360,8 @@
  61.            
  62.             TvTEvent.onKill(killer, this);
  63.            
  64. -           if (atEvent && pk != null)
  65. -           {
  66. -               pk.kills.add(getName());
  67. -           }
  68. +           if (L2Event.isParticipant(pk) && pk != null)
  69. +               pk.getEventStatus().kills.add(this);
  70.            
  71.             //announce pvp/pk
  72.             if (Config.ANNOUNCE_PK_PVP && pk != null && !pk.isGM())
  73. @@ -5525,7 +5514,7 @@
  74.    
  75.     private void onDieDropItem(L2Character killer)
  76.     {
  77. -       if (atEvent || killer == null)
  78. +       if (L2Event.isParticipant(this) || killer == null)
  79.             return;
  80.        
  81.         L2PcInstance pk = killer.getActingPlayer();
  82. @@ -5920,7 +5909,7 @@
  83.        
  84.         // Calculate the Experience loss
  85.         long lostExp = 0;
  86. -       if (!atEvent)
  87. +       if (!L2Event.isParticipant(this))
  88.             if (lvl < Experience.MAX_LEVEL)
  89.                 lostExp = Math.round((getStat().getExpForLevel(lvl+1) - getStat().getExpForLevel(lvl)) * percentLost /100);
  90.             else
  91. @@ -15156,4 +15145,19 @@
  92.     {
  93.         return _contactList;
  94.     }
  95. +  
  96. +   public void setEventStatus()
  97. +   {
  98. +       eventStatus = new PlayerEventStatus(this);
  99. +   }
  100. +  
  101. +   public void setEventStatus(PlayerEventStatus pes)
  102. +   {
  103. +       eventStatus = pes;
  104. +   }
  105. +  
  106. +   public PlayerEventStatus getEventStatus()
  107. +   {
  108. +       return eventStatus;
  109. +   }
  110.  }
  111. Index: java/com/l2jserver/gameserver/model/entity/L2Event.java
  112. ===================================================================
  113. --- java/com/l2jserver/gameserver/model/entity/L2Event.java (revision 4623)
  114. +++ java/com/l2jserver/gameserver/model/entity/L2Event.java (working copy)
  115. @@ -19,170 +19,154 @@
  116.  import java.io.DataInputStream;
  117.  import java.io.FileInputStream;
  118.  import java.io.InputStreamReader;
  119. -import java.util.HashMap;
  120. -import java.util.Iterator;
  121. -import java.util.LinkedList;
  122. +import java.util.List;
  123. +import java.util.Map;
  124. +import java.util.Map.Entry;
  125.  import java.util.logging.Level;
  126.  import java.util.logging.Logger;
  127.  
  128. +import javolution.util.FastList;
  129. +import javolution.util.FastMap;
  130. +
  131.  import com.l2jserver.gameserver.datatables.NpcTable;
  132.  import com.l2jserver.gameserver.datatables.SpawnTable;
  133.  import com.l2jserver.gameserver.model.L2Spawn;
  134.  import com.l2jserver.gameserver.model.L2World;
  135. +import com.l2jserver.gameserver.model.actor.L2Npc;
  136.  import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  137. +import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  138. +import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  139.  import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  140.  import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  141. +import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  142.  import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  143. -import com.l2jserver.gameserver.util.Broadcast;
  144. -import com.l2jserver.util.EventData;
  145. +import com.l2jserver.util.PlayerEventStatus;
  146.  import com.l2jserver.util.StringUtil;
  147. -
  148. +import com.l2jserver.util.ValueSortMap;
  149.  
  150.  /**
  151. - * This class ...
  152. - *
  153. - * @version $Revision: 1.3.4.1 $ $Date: 2005/03/27 15:29:32 $
  154. + * @since $Revision: 1.3.4.1 $ $Date: 2005/03/27 15:29:32 $
  155. + * This ancient thingie got reworked by Nik at $Date: 2011/05/17 21:51:39 $
  156. + * Yeah, for 6 years no one bothered reworking this buggy event engine.
  157.   */
  158.  
  159.  public class L2Event
  160.  {
  161.     protected static final Logger _log = Logger.getLogger(L2Event.class.getName());
  162. -   public static String eventName = "";
  163. -   public static int teamsNumber = 0;
  164. -   public static final HashMap<Integer, String> names = new HashMap<Integer, String>();
  165. -   public static final LinkedList<String> participatingPlayers = new LinkedList<String>(); //TODO store by objid
  166. -   public static final HashMap<Integer, LinkedList<String>> players = new HashMap<Integer, LinkedList<String>>();
  167. -   public static int id = 12760;
  168. -   public static final LinkedList<String> npcs = new LinkedList<String>();
  169. -   public static boolean active = false;
  170. -   public static final HashMap<String, EventData> connectionLossData = new HashMap<String, EventData>();
  171. +   public static EventState eventState = EventState.OFF;
  172. +   public static String _eventName = "";
  173. +   public static int _teamsNumber = 0;
  174. +   public static final Map<Integer, String> _teamNames = new FastMap<Integer, String>();
  175. +   public static final List<L2PcInstance> _registeredPlayers = new FastList<L2PcInstance>();
  176. +   public static final Map<Integer, FastList<L2PcInstance>> _teams = new FastMap<Integer, FastList<L2PcInstance>>();
  177. +   public static int _npcId = 12760;
  178. +   public static final List<L2Npc> _npcs = new FastList<L2Npc>();
  179. +   private static final Map<L2PcInstance, PlayerEventStatus> _connectionLossData = new FastMap<L2PcInstance, PlayerEventStatus>();
  180.    
  181. -   public static int getTeamOfPlayer(String name)
  182. +   enum EventState
  183.     {
  184. -       for (int i = 1; i <= players.size(); i++)
  185. +       OFF, // Not running
  186. +       STANDBY, // Waiting for participants to register
  187. +       ON // Registration is over and the event has started.
  188. +   }
  189. +  
  190. +   /**
  191. +    *
  192. +    * @param player
  193. +    * @return The team ID where the player is in, or -1 if player is null or team not found.
  194. +    */
  195. +   public static int getPlayerTeamId(L2PcInstance player)
  196. +   {
  197. +       if (player == null)
  198. +           return -1;
  199. +      
  200. +       for (Entry<Integer, FastList<L2PcInstance>> team : _teams.entrySet())
  201.         {
  202. -           LinkedList<String> temp = players.get(i);
  203. -           Iterator<String> it = temp.iterator();
  204. -           while (it.hasNext())
  205. -           {
  206. -               if (it.next().equals(name))
  207. -                   return i;
  208. -           }
  209. +           if (team.getValue().contains(player))
  210. +               return team.getKey();
  211.         }
  212. -       return 0;
  213. +      
  214. +       return -1;
  215.     }
  216.    
  217. -   public static String[] getTopNKillers(int N)
  218. -   { //this will return top N players sorted by kills, first element in the array will be the one with more kills
  219. -       String[] killers = new String[N];
  220. -       String playerTemp = "";
  221. -       int kills = 0;
  222. -       LinkedList<String> killersTemp = new LinkedList<String>();
  223. +   public static List<L2PcInstance> getTopNKillers(int n)
  224. +   {
  225. +       Map<L2PcInstance, Integer> tmp = new FastMap<L2PcInstance, Integer>();
  226.        
  227. -       for (int k = 0; k < N; k++)
  228. +       for (FastList<L2PcInstance> teamList : _teams.values())
  229.         {
  230. -           kills = 0;
  231. -           for (int i = 1; i <= teamsNumber; i++)
  232. +           for (L2PcInstance player : teamList)
  233.             {
  234. -               LinkedList<String> temp = players.get(i);
  235. -               Iterator<String> it = temp.iterator();
  236. -               while (it.hasNext())
  237. -               {
  238. -                   try
  239. -                   {
  240. -                       L2PcInstance player = L2World.getInstance().getPlayer(it.next());
  241. -                       if (!killersTemp.contains(player.getName()))
  242. -                       {
  243. -                           if (player.kills.size() > kills)
  244. -                           {
  245. -                               kills = player.kills.size();
  246. -                               playerTemp = player.getName();
  247. -                           }
  248. -                       }
  249. -                   }
  250. -                   catch (Exception e)
  251. -                   {
  252. -                   }
  253. -               }
  254. +               if (player.getEventStatus() == null)
  255. +                   continue;
  256. +              
  257. +               tmp.put(player, player.getEventStatus().kills.size());
  258.             }
  259. -           killersTemp.add(playerTemp);
  260.         }
  261.        
  262. -       for (int i = 0; i < N; i++)
  263. +       ValueSortMap.sortMapByValue(tmp, false);
  264. +      
  265. +       // If the map size is less than "n", n will be as much as the map size
  266. +       if (tmp.size() <= n)
  267. +       {
  268. +           List<L2PcInstance> toReturn = new FastList<L2PcInstance>();
  269. +           toReturn.addAll(tmp.keySet());
  270. +           return toReturn;
  271. +       }
  272. +       else
  273. +       {
  274. +           List<L2PcInstance> toReturn = new FastList<L2PcInstance>();
  275. +           toReturn.addAll(tmp.keySet());
  276. +           return toReturn.subList(1, n);
  277. +       }
  278. +   }
  279. +
  280. +   public static void showEventHtml(L2PcInstance player, String objectid)
  281. +   {//TODO: work on this
  282. +      
  283. +       if (eventState != EventState.OFF)
  284.         {
  285. -           kills = 0;
  286. -           Iterator<String> it = killersTemp.iterator();
  287. -           while (it.hasNext())
  288. +           try
  289.             {
  290. -               try
  291. +               NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  292. +              
  293. +               DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data/events/" + _eventName)));
  294. +               BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
  295. +              
  296. +               final StringBuilder replyMSG = new StringBuilder();
  297. +               StringUtil.append(replyMSG, "<html><body>" + "<center><font color=\"LEVEL\">", _eventName, "</font><font color=\"FF0000\"> by ", inbr.readLine(), "</font></center><br>" + "<br>", inbr.readLine());
  298. +              
  299. +               if (_registeredPlayers.contains(player))
  300.                 {
  301. -                   L2PcInstance player = L2World.getInstance().getPlayer(it.next());
  302. -                   if (player.kills.size() > kills)
  303. -                   {
  304. -                       kills = player.kills.size();
  305. -                       playerTemp = player.getName();
  306. -                   }
  307. +                   replyMSG.append("<br><center>You are already in the event players list !!</center></body></html>");
  308.                 }
  309. -               catch (Exception e)
  310. +               else
  311.                 {
  312. +                   StringUtil.append(replyMSG, "<br><center><button value=\"Participate !! \" action=\"bypass -h npc_", String.valueOf(objectid), "_event_participate\" width=90 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></center></body></html>");
  313.                 }
  314. +              
  315. +               adminReply.setHtml(replyMSG.toString());
  316. +               player.sendPacket(adminReply);
  317.             }
  318. -           killers[i] = playerTemp;
  319. -           killersTemp.remove(playerTemp);
  320. -       }
  321. -       return killers;
  322. -   }
  323. -  
  324. -   public static void showEventHtml(L2PcInstance player, String objectid)
  325. -   {
  326. -       try
  327. -       {
  328. -           NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  329. -          
  330. -           DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data/events/" + eventName)));
  331. -           BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
  332. -          
  333. -           final StringBuilder replyMSG = new StringBuilder();
  334. -           StringUtil.append(replyMSG,
  335. -                   "<html><body>" +
  336. -                   "<center><font color=\"LEVEL\">",
  337. -                   eventName,
  338. -                   "</font><font color=\"FF0000\"> bY ",
  339. -                   inbr.readLine(),
  340. -                   "</font></center><br>" +
  341. -                   "<br>",
  342. -                   inbr.readLine()
  343. -           );
  344. -          
  345. -           if (L2Event.participatingPlayers.contains(player.getName())) {
  346. -               replyMSG.append("<br><center>You are already in the event players list !!</center></body></html>");
  347. -           } else {
  348. -               StringUtil.append(replyMSG,
  349. -                       "<br><center><button value=\"Participate !! \" action=\"bypass -h npc_",
  350. -                       String.valueOf(objectid),
  351. -                       "_event_participate\" width=90 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></center></body></html>"
  352. -               );
  353. +           catch (Exception e)
  354. +           {
  355. +               _log.log(Level.WARNING, "Exception on showEventHtml(): " + e.getMessage(), e);
  356.             }
  357. -          
  358. -           adminReply.setHtml(replyMSG.toString());
  359. -           player.sendPacket(adminReply);
  360. -       }
  361. -       catch (Exception e)
  362. -       {
  363. -           _log.log(Level.WARNING, "Exception on showEventHtml(): " + e.getMessage(), e);
  364.         }
  365.     }
  366.    
  367. -   public static void spawn(L2PcInstance target, int npcid)
  368. +   /**
  369. +    * Spawns an event participation NPC near the player.
  370. +    * The npc id used to spawning is L2Event._npcId
  371. +    */
  372. +   public static void spawnEventNpc(L2PcInstance target)
  373.     {
  374.        
  375. -       L2NpcTemplate template1 = NpcTable.getInstance().getTemplate(npcid);
  376. +       L2NpcTemplate template = NpcTable.getInstance().getTemplate(_npcId);
  377.        
  378.         try
  379.         {
  380. -           //L2MonsterInstance mob = new L2MonsterInstance(template1);
  381. -          
  382. -           L2Spawn spawn = new L2Spawn(template1);
  383. +           L2Spawn spawn = new L2Spawn(template);
  384.            
  385.             spawn.setLocx(target.getX() + 50);
  386.             spawn.setLocy(target.getY() + 50);
  387. @@ -195,16 +179,14 @@
  388.            
  389.             spawn.init();
  390.             spawn.getLastSpawn().setCurrentHp(999999999);
  391. -           spawn.getLastSpawn().setName("event inscriptor");
  392. -           spawn.getLastSpawn().setTitle(L2Event.eventName);
  393. +           spawn.getLastSpawn().setTitle(_eventName);
  394.             spawn.getLastSpawn().isEventMob = true;
  395. -           spawn.getLastSpawn().isAggressive();
  396.             spawn.getLastSpawn().decayMe();
  397.             spawn.getLastSpawn().spawnMe(spawn.getLastSpawn().getX(), spawn.getLastSpawn().getY(), spawn.getLastSpawn().getZ());
  398.            
  399.             spawn.getLastSpawn().broadcastPacket(new MagicSkillUse(spawn.getLastSpawn(), spawn.getLastSpawn(), 1034, 1, 1, 1));
  400.            
  401. -           npcs.add(String.valueOf(spawn.getLastSpawn().getObjectId()));
  402. +           _npcs.add(spawn.getLastSpawn());
  403.            
  404.         }
  405.         catch (Exception e)
  406. @@ -214,88 +196,286 @@
  407.        
  408.     }
  409.    
  410. -   public static void announceAllPlayers(String text)
  411. +   public static void unspawnEventNpcs()
  412.     {
  413. -       Broadcast.announceToOnlinePlayers(text);
  414. +       for (L2Npc npc : _npcs)
  415. +           npc.deleteMe();
  416.     }
  417.    
  418. -   public static boolean isOnEvent(L2PcInstance player)
  419. +   /**
  420. +    *
  421. +    * @return False: If player is null, his event status is null or the event state is off.
  422. +    * True: if the player is inside the _registeredPlayers list while the event state is STANDBY.
  423. +    * If the event state is ON, it will check if the player is inside in one of the teams.
  424. +    */
  425. +   public static boolean isParticipant(L2PcInstance player)
  426.     {
  427. +       if (player == null || player.getEventStatus() == null)
  428. +           return false;
  429.        
  430. -       for (int k = 0; k < L2Event.teamsNumber; k++)
  431. +       switch (eventState)
  432.         {
  433. -           Iterator<String> it = L2Event.players.get(k + 1).iterator();
  434. -           boolean temp = false;
  435. -           while (it.hasNext())
  436. -           {
  437. -               temp = player.getName().equalsIgnoreCase(it.next());
  438. -               if (temp)
  439. -                   return true;
  440. -           }
  441. +           case OFF:
  442. +               return false;
  443. +           case STANDBY:
  444. +               return _registeredPlayers.contains(player);
  445. +           case ON:
  446. +               for (FastList<L2PcInstance> teamList : _teams.values())
  447. +               {
  448. +                   if (teamList.contains(player))
  449. +                       return true;
  450. +               }
  451.         }
  452.         return false;
  453.        
  454.     }
  455.    
  456. -   public static void inscribePlayer(L2PcInstance player)
  457. +   /**
  458. +    *
  459. +    * Adds the player to the list of participants.
  460. +    * If the event state is NOT STANDBY, the player wont be registered.
  461. +    */
  462. +   public static void registerPlayer(L2PcInstance player)
  463.     {
  464. +       if (eventState != EventState.STANDBY)
  465. +       {
  466. +           player.sendMessage("The registration period for this event is over.");
  467. +           return;
  468. +       }
  469. +      
  470. +       _registeredPlayers.add(player);
  471. +   }
  472. +  
  473. +   /**
  474. +    *
  475. +    * Removes the player from the participating players and the teams and restores
  476. +    * his init stats before he registered at the event (loc, pvp, pk, title etc)
  477. +    */
  478. +   public static void removeAndResetPlayer(L2PcInstance player)
  479. +   {
  480.        
  481.         try
  482. -       {
  483. -           L2Event.participatingPlayers.add(player.getName());
  484. -           player.eventkarma = player.getKarma();
  485. -           player.eventX = player.getX();
  486. -           player.eventY = player.getY();
  487. -           player.eventZ = player.getZ();
  488. -           player.eventpkkills = player.getPkKills();
  489. -           player.eventpvpkills = player.getPvpKills();
  490. -           player.eventTitle = player.getTitle();
  491. -           player.kills.clear();
  492. -           player.atEvent = true;
  493. +       {          
  494. +           if (isParticipant(player))
  495. +           {
  496. +               if (player.isDead())
  497. +               {
  498. +                   player.restoreExp(100.0);
  499. +                   player.doRevive();
  500. +                   player.setCurrentHpMp(player.getMaxHp(), player.getMaxMp());
  501. +                   player.setCurrentCp(player.getMaxCp());
  502. +               }
  503. +              
  504. +               player.getPoly().setPolyInfo(null, "1");
  505. +               player.decayMe();
  506. +               player.spawnMe(player.getX(), player.getY(), player.getZ());
  507. +               CharInfo info1 = new CharInfo(player);
  508. +               player.broadcastPacket(info1);
  509. +               UserInfo info2 = new UserInfo(player);
  510. +               player.sendPacket(info2);
  511. +               player.broadcastPacket(new ExBrExtraUserInfo(player));
  512. +           }
  513. +          
  514. +           if (player.getEventStatus() != null)
  515. +               player.getEventStatus().restoreInits();
  516. +          
  517. +           player.setEventStatus(null);
  518. +          
  519. +           _registeredPlayers.remove(player);
  520. +           int teamId = getPlayerTeamId(player);
  521. +           if (_teams.containsKey(teamId))
  522. +               _teams.get(teamId).remove(player);
  523.         }
  524.         catch (Exception e)
  525.         {
  526. -           _log.log(Level.WARNING, "Error when signing in the event:" + e.getMessage(), e);
  527. +           _log.log(Level.WARNING, "Error at unregisterAndResetPlayer in the event:" + e.getMessage(), e);
  528. +       }
  529. +   }
  530. +  
  531. +   /**
  532. +    *
  533. +    * The player's event status will be saved at _connectionLossData
  534. +    */
  535. +   public static void savePlayerEventStatus(L2PcInstance player)
  536. +   {
  537. +           _connectionLossData.put(player, player.getEventStatus());
  538. +   }
  539. +  
  540. +   /**
  541. +    *
  542. +    * If _connectionLossData contains the player, it will restore the player's event status.
  543. +    * Also it will remove the player from the _connectionLossData.
  544. +    */
  545. +   public static void restorePlayerEventStatus(L2PcInstance player)
  546. +   {
  547. +       if (_connectionLossData.containsKey(player))
  548. +       {
  549. +           player.setEventStatus(_connectionLossData.get(player));
  550. +           _connectionLossData.remove(player);
  551.         }
  552.     }
  553.    
  554. -   public static void restoreChar(L2PcInstance player)
  555. +   /**
  556. +    * If the event is ON or STANDBY, it will not start.
  557. +    * Sets the event state to STANDBY and spawns registration NPCs
  558. +    * @return a string with information if the event participation has been successfully started or not.
  559. +    */
  560. +   public static String startEventParticipation()
  561.     {
  562.         try
  563.         {
  564. -           player.eventX = connectionLossData.get(player.getName()).eventX;
  565. -           player.eventY = connectionLossData.get(player.getName()).eventY;
  566. -           player.eventZ = connectionLossData.get(player.getName()).eventZ;
  567. -           player.eventkarma = connectionLossData.get(player.getName()).eventKarma;
  568. -           player.eventpvpkills = connectionLossData.get(player.getName()).eventPvpKills;
  569. -           player.eventpkkills = connectionLossData.get(player.getName()).eventPkKills;
  570. -           player.eventTitle = connectionLossData.get(player.getName()).eventTitle;
  571. -           player.kills = connectionLossData.get(player.getName()).kills;
  572. -           player.eventSitForced = connectionLossData.get(player.getName()).eventSitForced;
  573. -           player.atEvent = true;
  574. +           switch (eventState)
  575. +           {
  576. +               case ON:
  577. +                   return "Cannot start event, it is already on.";
  578. +               case STANDBY:
  579. +                   return "Cannot start event, it is on standby mode.";
  580. +               case OFF: // Event is off, so no problem turning it on.
  581. +                   eventState = EventState.STANDBY;
  582. +                   break;
  583. +           }
  584. +          
  585. +           // Just in case
  586. +           unspawnEventNpcs();
  587. +           _registeredPlayers.clear();
  588. +           _npcs.clear();
  589. +          
  590. +           List<L2PcInstance> temp = new FastList<L2PcInstance>();
  591. +           for (L2PcInstance player : L2World.getInstance().getAllPlayers().values())
  592. +           {
  593. +               if (!temp.contains(player))
  594. +               {
  595. +                   spawnEventNpc(player);
  596. +                   temp.add(player);
  597. +               }
  598. +               for (L2PcInstance playertemp : player.getKnownList().getKnownPlayers().values())
  599. +               {
  600. +                   if ((Math.abs(playertemp.getX() - player.getX()) < 1000) && (Math.abs(playertemp.getY() - player.getY()) < 1000) && (Math.abs(playertemp.getZ() - player.getZ()) < 1000))
  601. +                       temp.add(playertemp);
  602. +               }
  603. +           }  
  604.         }
  605.         catch (Exception e)
  606.         {
  607. +           e.printStackTrace();
  608. +           return "Cannot start event participation, an error has occured.";
  609.         }
  610. +      
  611. +       return "The event participation has been successfully started.";
  612.     }
  613.    
  614. -   public static void restoreAndTeleChar(L2PcInstance target)
  615. +   /**
  616. +    * If the event is ON or OFF, it will not start.
  617. +    * Sets the event state to ON, creates the teams,
  618. +    * adds the registered players ordered by level at the teams
  619. +    * and adds a new event status to the players.
  620. +    * @return a string with information if the event has been successfully started or not.
  621. +    */
  622. +   public static String startEvent()
  623.     {
  624. -      
  625.         try
  626.         {
  627. -           restoreChar(target);
  628. -           target.setTitle(target.eventTitle);
  629. -           target.setKarma(target.eventkarma);
  630. -           target.setPvpKills(target.eventpvpkills);
  631. -           target.setPkKills(target.eventpkkills);
  632. -           target.teleToLocation(target.eventX, target.eventY, target.eventZ, true);
  633. -           target.kills.clear();
  634. -           target.eventSitForced = false;
  635. -           target.atEvent = false;
  636. +           switch (eventState)
  637. +           {
  638. +               case ON:
  639. +                   return "Cannot start event, it is already on.";
  640. +               case STANDBY:
  641. +                   eventState = EventState.ON;
  642. +                   break;
  643. +               case OFF: // Event is off, so no problem turning it on.
  644. +                   return "Cannot start event, it is off. Participation start is required.";
  645. +           }
  646. +          
  647. +           // Clean the things we will use, just in case.
  648. +           unspawnEventNpcs();
  649. +           _teams.clear();
  650. +           _connectionLossData.clear();
  651. +          
  652. +           // Insert empty lists at _teams.
  653. +           for (int i = 0; i < _teamsNumber; i++)
  654. +               _teams.put(i + 1, new FastList<L2PcInstance>());
  655. +          
  656. +           int i = 0;
  657. +           while (!_registeredPlayers.isEmpty())
  658. +           {
  659. +               //Get the player with the biggest level
  660. +               int max = 0;
  661. +               L2PcInstance biggestLvlPlayer = null;
  662. +               for (L2PcInstance player : _registeredPlayers)
  663. +               {
  664. +                       if (player == null)
  665. +                           continue;
  666. +                      
  667. +                       if (max < player.getLevel())
  668. +                       {
  669. +                           max = player.getLevel();
  670. +                           biggestLvlPlayer = player;
  671. +                       }
  672. +               }
  673. +              
  674. +               if (biggestLvlPlayer == null)
  675. +                   continue;
  676. +              
  677. +               _registeredPlayers.remove(biggestLvlPlayer);
  678. +               _teams.get(i + 1).add(biggestLvlPlayer);
  679. +               biggestLvlPlayer.setEventStatus();
  680. +               i = (i + 1) % _teamsNumber;
  681. +           }
  682. +          
  683.         }
  684.         catch (Exception e)
  685.         {
  686. +           e.printStackTrace();
  687. +           return "Cannot start event, an error has occured.";
  688.         }
  689. +      
  690. +       return "The event has been successfully started.";
  691. +   }
  692. +  
  693. +   /**
  694. +    * If the event state is OFF, it will not finish.
  695. +    * Sets the event state to OFF, unregisters and resets the players,
  696. +    * unspawns and clers the event NPCs, clears the teams, registered players,
  697. +    * connection loss data, sets the teams number to 0, sets the event name to empty.
  698. +    * @return a string with information if the event has been successfully stopped or not.
  699. +    */
  700. +   public static String finishEvent()
  701. +   {
  702. +           switch (eventState)
  703. +           {
  704. +               case OFF:
  705. +                   return "Cannot finish event, it is already off.";
  706. +               case STANDBY:
  707. +                   for (L2PcInstance player : _registeredPlayers)
  708. +                       removeAndResetPlayer(player);
  709. +                  
  710. +                   unspawnEventNpcs();
  711. +                   _npcs.clear();
  712. +                   _registeredPlayers.clear();
  713. +                   _teams.clear();
  714. +                   _connectionLossData.clear();
  715. +                   _teamsNumber = 0;
  716. +                   _eventName = "";
  717. +                   eventState = EventState.OFF;
  718. +                   return "The event has been stopped at STANDBY mode, all players unregistered and all event npcs unspawned.";
  719. +               case ON:
  720. +                   for (FastList<L2PcInstance> teamList : _teams.values())
  721. +                   {
  722. +                       for (L2PcInstance player : teamList)
  723. +                           removeAndResetPlayer(player);
  724. +                   }
  725. +                  
  726. +                   unspawnEventNpcs(); // Just in case
  727. +                   _npcs.clear();
  728. +                   _registeredPlayers.clear();
  729. +                   _teams.clear();
  730. +                   _connectionLossData.clear();
  731. +                   _teamsNumber = 0;
  732. +                   _eventName = "";
  733. +                   eventState = EventState.OFF;
  734. +                   return "The event has been stopped, all players unregistered and all event npcs unspawned.";
  735. +           }
  736. +      
  737. +       return "The event has been successfully finished.";
  738.     }
  739.  }
  740. Index: java/com/l2jserver/gameserver/network/L2GameClient.java
  741. ===================================================================
  742. --- java/com/l2jserver/gameserver/network/L2GameClient.java (revision 4623)
  743. +++ java/com/l2jserver/gameserver/network/L2GameClient.java (working copy)
  744. @@ -52,7 +52,6 @@
  745.  import com.l2jserver.gameserver.network.serverpackets.ServerClose;
  746.  import com.l2jserver.gameserver.util.FloodProtectors;
  747.  import com.l2jserver.gameserver.util.Util;
  748. -import com.l2jserver.util.EventData;
  749.  
  750.  /**
  751.   * Represents a client connected on Game Server
  752. @@ -78,10 +77,10 @@
  753.     private String _accountName;
  754.     private SessionKey _sessionId;
  755.     private L2PcInstance _activeChar;
  756. -   private ReentrantLock _activeCharLock = new ReentrantLock();
  757. +   private final ReentrantLock _activeCharLock = new ReentrantLock();
  758.    
  759.     private boolean _isAuthedGG;
  760. -   private long _connectionStartTime;
  761. +   private final long _connectionStartTime;
  762.     private CharSelectInfoPackage[] _charSlotMapping = null;
  763.    
  764.     // floodprotectors
  765. @@ -94,16 +93,16 @@
  766.     private L2GameServerPacket _aditionalClosePacket;
  767.    
  768.     // Crypt
  769. -   private GameCrypt _crypt;
  770. +   private final GameCrypt _crypt;
  771.    
  772. -   private ClientStats _stats;
  773. +   private final ClientStats _stats;
  774.    
  775.     private boolean _isDetached = false;
  776.    
  777.     private boolean _protocol;
  778.    
  779.     private final ArrayBlockingQueue<ReceivablePacket<L2GameClient>> _packetQueue;
  780. -   private ReentrantLock _queueLock = new ReentrantLock();
  781. +   private final ReentrantLock _queueLock = new ReentrantLock();
  782.    
  783.     private int[][] trace;
  784.    
  785. @@ -796,11 +795,9 @@
  786.                     }
  787.                    
  788.                     // we store all data from players who are disconnected while in an event in order to restore it in the next login
  789. -                   if (player.atEvent)
  790. +                   if (L2Event.isParticipant(player))
  791.                     {
  792. -                       EventData data = new EventData(player.eventX, player.eventY, player.eventZ, player.eventkarma, player.eventpvpkills, player.eventpkkills, player.eventTitle, player.kills,
  793. -                               player.eventSitForced);
  794. -                       L2Event.connectionLossData.put(player.getName(), data);
  795. +                       L2Event.savePlayerEventStatus(player);
  796.                     }
  797.  
  798.                     // prevent closing again
  799. Index: java/com/l2jserver/gameserver/network/clientpackets/EnterWorld.java
  800. ===================================================================
  801. --- java/com/l2jserver/gameserver/network/clientpackets/EnterWorld.java (revision 4623)
  802. +++ java/com/l2jserver/gameserver/network/clientpackets/EnterWorld.java (working copy)
  803. @@ -102,7 +102,7 @@
  804.    
  805.     private static Logger _log = Logger.getLogger(EnterWorld.class.getName());
  806.    
  807. -   private int[][] tracert = new int[5][4];
  808. +   private final int[][] tracert = new int[5][4];
  809.    
  810.     public TaskPriority getPriority()
  811.     {
  812. @@ -336,10 +336,8 @@
  813.        
  814.         activeChar.spawnMe(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  815.        
  816. -       if (L2Event.active && L2Event.connectionLossData.containsKey(activeChar.getName()) && L2Event.isOnEvent(activeChar))
  817. -           L2Event.restoreChar(activeChar);
  818. -       else if (L2Event.connectionLossData.containsKey(activeChar.getName()))
  819. -           L2Event.restoreAndTeleChar(activeChar);
  820. +       if (L2Event.isParticipant(activeChar))
  821. +           L2Event.restorePlayerEventStatus(activeChar);
  822.        
  823.         // Wedding Checks
  824.         if (Config.L2JMOD_ALLOW_WEDDING)
  825. Index: java/com/l2jserver/gameserver/network/clientpackets/Logout.java
  826. ===================================================================
  827. --- java/com/l2jserver/gameserver/network/clientpackets/Logout.java (revision 4623)
  828. +++ java/com/l2jserver/gameserver/network/clientpackets/Logout.java (working copy)
  829. @@ -22,6 +22,7 @@
  830.  import com.l2jserver.gameserver.SevenSignsFestival;
  831.  import com.l2jserver.gameserver.model.L2Party;
  832.  import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  833. +import com.l2jserver.gameserver.model.entity.L2Event;
  834.  import com.l2jserver.gameserver.network.SystemMessageId;
  835.  import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  836.  import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  837. @@ -76,7 +77,7 @@
  838.             return;
  839.         }
  840.        
  841. -       if(player.atEvent)
  842. +       if(L2Event.isParticipant(player))
  843.         {
  844.             player.sendMessage("A superior power doesn't allow you to leave the event");
  845.             player.sendPacket(ActionFailed.STATIC_PACKET);
  846. Index: java/com/l2jserver/gameserver/network/clientpackets/RequestBypassToServer.java
  847. ===================================================================
  848. --- java/com/l2jserver/gameserver/network/clientpackets/RequestBypassToServer.java  (revision 4623)
  849. +++ java/com/l2jserver/gameserver/network/clientpackets/RequestBypassToServer.java  (working copy)
  850. @@ -139,7 +139,7 @@
  851.                     L2Object object = L2World.getInstance().findObject(Integer.parseInt(id));
  852.                    
  853.                     if (_command.substring(endOfId+1).startsWith("event_participate"))
  854. -                       L2Event.inscribePlayer(activeChar);
  855. +                       L2Event.registerPlayer(activeChar);
  856.                     else if (object instanceof L2Npc && endOfId > 0 && activeChar.isInsideRadius(object, L2Npc.INTERACTION_DISTANCE, false, false))
  857.                         ((L2Npc)object).onBypassFeedback(activeChar, _command.substring(endOfId+1));
  858.                    
  859. Index: java/com/l2jserver/util/EventData.java
  860. ===================================================================
  861. --- java/com/l2jserver/util/EventData.java  (revision 4623)
  862. +++ java/com/l2jserver/util/EventData.java  (working copy)
  863. @@ -1,52 +0,0 @@
  864. -/*
  865. - * $Header: EventData.java
  866. - *
  867. - * $Author: SarEvoK
  868. - * Added copyright notice
  869. - *
  870. - *
  871. - * This program is free software: you can redistribute it and/or modify it under
  872. - * the terms of the GNU General Public License as published by the Free Software
  873. - * Foundation, either version 3 of the License, or (at your option) any later
  874. - * version.
  875. - *
  876. - * This program is distributed in the hope that it will be useful, but WITHOUT
  877. - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  878. - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  879. - * details.
  880. - *
  881. - * You should have received a copy of the GNU General Public License along with
  882. - * this program. If not, see <http://www.gnu.org/licenses/>.
  883. - */
  884. -
  885. -package com.l2jserver.util;
  886. -
  887. -import java.util.LinkedList;
  888. -
  889. -public class EventData
  890. -{
  891. -   public int eventX;
  892. -   public int eventY;
  893. -   public int eventZ;
  894. -   public int eventKarma;
  895. -   public int eventPvpKills;
  896. -   public int eventPkKills;
  897. -   public String eventTitle;
  898. -   public LinkedList<String> kills = new LinkedList<String>();
  899. -   public boolean eventSitForced = false;
  900. -  
  901. -   public EventData(int pEventX, int pEventY, int pEventZ, int pEventkarma, int pEventpvpkills,
  902. -           int pEventpkkills, String pEventTitle, LinkedList<String> pKills,
  903. -           boolean pEventSitForced)
  904. -   {
  905. -       eventX = pEventX;
  906. -       eventY = pEventY;
  907. -       eventZ = pEventZ;
  908. -       eventKarma = pEventkarma;
  909. -       eventPvpKills = pEventpvpkills;
  910. -       eventPkKills = pEventpkkills;
  911. -       eventTitle = pEventTitle;
  912. -       kills = pKills;
  913. -       eventSitForced = pEventSitForced;
  914. -   }
  915. -}
  916. Index: java/com/l2jserver/util/PlayerEventStatus.java
  917. ===================================================================
  918. --- java/com/l2jserver/util/PlayerEventStatus.java  (revision 0)
  919. +++ java/com/l2jserver/util/PlayerEventStatus.java  (revision 0)
  920. @@ -0,0 +1,58 @@
  921. +/*
  922. + * This program is free software: you can redistribute it and/or modify it under
  923. + * the terms of the GNU General Public License as published by the Free Software
  924. + * Foundation, either version 3 of the License, or (at your option) any later
  925. + * version.
  926. + *
  927. + * This program is distributed in the hope that it will be useful, but WITHOUT
  928. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  929. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  930. + * details.
  931. + *
  932. + * You should have received a copy of the GNU General Public License along with
  933. + * this program. If not, see <http://www.gnu.org/licenses/>.
  934. + */
  935. +package com.l2jserver.util;
  936. +
  937. +import java.util.List;
  938. +
  939. +import javolution.util.FastList;
  940. +
  941. +import com.l2jserver.gameserver.model.Location;
  942. +import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  943. +
  944. +/**
  945. + * @author Nik
  946. + */
  947. +
  948. +public class PlayerEventStatus
  949. +{
  950. +   public L2PcInstance player = null;
  951. +   public Location initLoc = new Location(0,0,0);
  952. +   public int initKarma = 0;
  953. +   public int initPvpKills = 0;
  954. +   public int initPkKills = 0;
  955. +   public String initTitle = "";
  956. +   public List<L2PcInstance> kills = new FastList<L2PcInstance>();
  957. +   public boolean eventSitForced = false;
  958. +  
  959. +   public PlayerEventStatus(L2PcInstance player)
  960. +   {
  961. +       this.player = player;
  962. +       initLoc = new Location(player.getX(), player.getY(), player.getZ(), player.getHeading());
  963. +       initKarma = player.getKarma();
  964. +       initPvpKills = player.getPvpKills();
  965. +       initPkKills = player.getPkKills();
  966. +       initTitle = player.getTitle();
  967. +      
  968. +   }
  969. +  
  970. +   public void restoreInits()
  971. +   {
  972. +       player.teleToLocation(initLoc, true);
  973. +       player.setKarma(initKarma);
  974. +       player.setPvpKills(initPvpKills);
  975. +       player.setPkKills(initPkKills);
  976. +       player.setTitle(initTitle);
  977. +   }
  978. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement