Advertisement
Guest User

Untitled

a guest
Aug 31st, 2011
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. /* This program is free software; you can redistribute it and/or modify
  2.  * it under the terms of the GNU General Public License as published by
  3.  * the Free Software Foundation; either version 2, or (at your option)
  4.  * any later version.
  5.  *
  6.  * This program is distributed in the hope that it will be useful,
  7.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.  * GNU General Public License for more details.
  10.  *
  11.  * You should have received a copy of the GNU General Public License
  12.  * along with this program; if not, write to the Free Software
  13.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14.  * 02111-1307, USA.
  15.  *
  16.  * http://www.gnu.org/copyleft/gpl.html
  17.  */
  18. package net.sf.l2j.gameserver.model.entity;
  19.  
  20. import java.util.Collection;
  21. import java.util.Vector;
  22.  
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.clientpackets.Say2;
  25. import net.sf.l2j.gameserver.model.L2World;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.serverpackets.CreatureSay;
  28. import net.sf.l2j.util.Rnd;
  29.  
  30. /**
  31.  *
  32.  * @author  Ventic
  33.  */
  34. public class LastHero
  35. {
  36.     public static Vector<L2PcInstance> _players = new Vector<L2PcInstance>();
  37.     public L2PcInstance winner = null,loser = null, player1 = null, player2 = null;
  38.     public static State state = State.INACTIVE;
  39.     public static enum State  { INACTIVE, REGISTERING, TELEPORTING, REWARDING, FIGHTING }
  40.    
  41.     public class Task implements Runnable
  42.     {
  43.         public void run()
  44.         {
  45.             if (state != State.INACTIVE)
  46.             {
  47.                 return;
  48.             }
  49.             handle();
  50.         }
  51.     }
  52.    
  53.     public void handle()
  54.     {
  55.         GlobalMessage("Registrations opened for 5 minutes");
  56.         state = State.REGISTERING;
  57.         wait(5);
  58.         getPlayers();
  59.         GlobalMessage("You will be moved to coliseum in 2 minutes");
  60.         wait(2);
  61.         state = State.TELEPORTING;
  62.         teleport(false);
  63.         GlobalMessage("You have 45 seconds to get prepared");
  64.         waitSecs(45);
  65.         prepare();
  66.         GlobalMessage("Match started");
  67.         start();
  68.         if (!(state == State.INACTIVE))
  69.         {
  70.             while (state == State.REWARDING)
  71.             {
  72.                 endAndReward();
  73.                 teleport(true);
  74.             }
  75.         }
  76.         clear();
  77.     }
  78.    
  79.     public void getPlayers()
  80.     {
  81.         if (_players.size() >= 2)
  82.         {
  83.             player1 = _players.elementAt(Rnd.get(_players.size()-1));
  84.             _players.remove(player1);
  85.             player2 = _players.elementAt(Rnd.get(_players.size()-1));
  86.             _players.clear();
  87.             _players.add(player1);
  88.             _players.add(player2);
  89.             player1.sendMessage("You have been chosen for the next LastHero match. Your enemy: "+player2.getName()+".");
  90.             player1.sendMessage("You will be teleported to Coliseum in 2 minutes.");
  91.             player2.sendMessage("You have been chosen for the next LastHero match. Your enemy: "+player1.getName()+".");
  92.             player2.sendMessage("You will be teleported to Coliseum in 2 minutes.");
  93.         }
  94.         else
  95.         {
  96.             if (_players.size() >= 0)
  97.             {
  98.                 for (L2PcInstance p : _players)
  99.                 {
  100.                     p.sendMessage("The match was cancelled due to lack of participation.");
  101.                     p.inLHE = false;
  102.                 }
  103.             }
  104.             state = State.INACTIVE;
  105.             _players.clear();
  106.         }
  107.     }
  108.    
  109.     public void teleport(boolean back)
  110.     {
  111.         if (!back)
  112.         {
  113.             for (L2PcInstance p : _players)
  114.             {
  115.                 p.teleToLocation(0, 0, 0);
  116.                 p.sendMessage("You have been moved to coliseum");
  117.             }
  118.         }
  119.         else
  120.         {
  121.             for (L2PcInstance p : _players)
  122.             {
  123.                 p.teleToLocation(0, 0, 0);
  124.                 p.sendMessage("You have been moved to giran");
  125.             }
  126.         }
  127.     }
  128.    
  129.     public void register(L2PcInstance p)
  130.     {
  131.         state = State.REGISTERING;
  132.        
  133.         if (!p.isHero())
  134.         {
  135.             p.sendMessage("Only heroes can register");
  136.             return;
  137.         }
  138.         if (_players.contains(p))
  139.         {
  140.             p.sendMessage("You have already register");
  141.             return;
  142.         }
  143.         if (state != State.REGISTERING)
  144.         {
  145.             p.sendMessage("Can't register now");
  146.             return;
  147.         }
  148.         if (p.isInOlympiadMode())
  149.         {
  150.             p.sendMessage("You can't register while you have register for olympiad match");
  151.             return;
  152.         }
  153.         if (_players.size() == 2)
  154.         {
  155.             p.sendMessage("Event is full");
  156.             return;
  157.         }
  158.        
  159.         _players.add(p);
  160.         p.sendMessage("Succesfully registered");
  161.         p.inLHE = true;
  162.     }
  163.    
  164.     public void unregister(L2PcInstance p)
  165.     {
  166.         if (!_players.contains(p))
  167.         {
  168.             p.sendMessage("You have already unregister");
  169.             return;
  170.         }
  171.         if (state != State.REGISTERING)
  172.         {
  173.             p.sendMessage("You can't unregister while match is about to begin");
  174.             return;
  175.         }
  176.        
  177.         _players.remove(p);
  178.         p.sendMessage("Succesfully unregistered");
  179.         p.inLHE = false;
  180.     }
  181.    
  182.     public void prepare()
  183.     {
  184.         for (L2PcInstance p : _players)
  185.         {
  186.             p.setTitle("LHE");
  187.             p.stopAllEffects();
  188.             p.setIsRooted(true);
  189.             p.sendMessage("You have 45 seconds to get prepared");
  190.         }
  191.     }
  192.    
  193.     public void start()
  194.     {
  195.         state = State.FIGHTING;
  196.        
  197.         for (L2PcInstance p : _players)
  198.         {
  199.             p.stopRooting(null);
  200.             p.sendMessage("Go go go,start fighting");
  201.         }
  202.     }
  203.    
  204.     public void endAndReward()
  205.     {
  206.         state = State.REWARDING;
  207.        
  208.         if (player1.isDead())
  209.         {
  210.             winner = player2;
  211.             loser = player1;
  212.             if (winner != null)
  213.             {
  214.                 winner.getInventory().addItem("Event", 3470, 1, winner, null);
  215.             }
  216.         }
  217.         if (player2.isDead())
  218.         {
  219.             winner = player1;
  220.             loser = player2;
  221.             if (winner != null)
  222.             {
  223.                 winner.getInventory().addItem("Event", 3470, 1, winner, null);
  224.             }
  225.         }
  226.     }
  227.    
  228.     public void clear()
  229.     {
  230.         _players.clear();
  231.         player1 = null;
  232.         player2 = null;
  233.         winner = null;
  234.         loser = null;
  235.         state = State.INACTIVE;
  236.     }
  237.    
  238.     public void GlobalMessage(String msg)
  239.     {
  240.         Collection<L2PcInstance> plrs = L2World.getInstance().getAllPlayers().values();
  241.         CreatureSay cs = new CreatureSay(1, Say2.ANNOUNCEMENT, "LastHero Event:", msg);
  242.        
  243.         for (L2PcInstance p : plrs)
  244.         {
  245.             p.sendPacket(cs);
  246.         }
  247.     }
  248.    
  249.     private LastHero()
  250.     {
  251.         ThreadPoolManager.getInstance().scheduleGeneral(new Task(), 7200000);
  252.     }
  253.    
  254.     public void waitSecs(int i)
  255.     {
  256.         try
  257.         {
  258.             Thread.sleep(i * 1000);
  259.         }
  260.         catch (InterruptedException ie)
  261.         {
  262.             ie.printStackTrace();
  263.         }
  264.     }
  265.    
  266.     public void wait(int i)
  267.     {
  268.         try
  269.         {
  270.             Thread.sleep(i * 60000);
  271.         }
  272.         catch (InterruptedException ie)
  273.         {
  274.             ie.printStackTrace();
  275.         }
  276.     }
  277.    
  278.     public static LastHero getInstance()
  279.     {
  280.         return SingletonHolder._instance;
  281.     }
  282.    
  283.     private static class SingletonHolder
  284.     {
  285.         @SuppressWarnings("synthetic-access")
  286.         protected static final LastHero _instance = new LastHero();
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement