Advertisement
Nik

L2Event rework core

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