Lucasdesigner

MinesEvent.java

Oct 16th, 2021
916
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.sf.l2j.gameserver.custom.events.mines;
  2.  
  3. import net.sf.l2j.commons.concurrent.ThreadPool;
  4. import net.sf.l2j.commons.lang.StringUtil;
  5. import net.sf.l2j.gameserver.model.actor.instance.Player;
  6. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  7. import net.sf.l2j.gameserver.util.Broadcast;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.concurrent.Future;
  12.  
  13. public class MinesEvent{
  14.     private final List<Player> _players = new ArrayList<>();
  15.  
  16.     protected Future<?> _startTask = null;
  17.     protected int _countdown = 30;
  18.     private mineState _state;
  19.  
  20.     //add the item to give for reward
  21.     private final static int _rewardItemID = 0;
  22.     //add the quantity of reward items
  23.     private final static int _itemQuantity = 0;
  24.  
  25.     public MinesEvent(){
  26.     }
  27.  
  28.     public enum mineState {
  29.         STARTED,
  30.         STOPPED,
  31.         CLOSED
  32.     }
  33.  
  34.     public static MinesEvent getInstance()
  35.     {
  36.         return MinesEvent.SingletonHolder._instance;
  37.     }
  38.  
  39.     public void start() {
  40.         _state = mineState.STARTED;
  41.         _countdown = 30;
  42.         showToAll();
  43.         _startTask = ThreadPool.scheduleAtFixedRate(new Duration(), 1000, 1000);
  44.     }
  45.  
  46.  
  47.     public void stop() {
  48.         if(_startTask != null) {
  49.             _state = mineState.STOPPED;
  50.             _startTask.cancel(true);
  51.             _startTask = null;
  52.             _players.clear();
  53.         }
  54.     }
  55.  
  56.     public void showToAll(){
  57.         switch(_state) {
  58.             case STARTED:
  59.             for (Player plxr : _players) {
  60.                Mines newMine =  MinesManager.getInstance().getMine(plxr.getObjectId());
  61.                newMine.minesWindow(plxr);
  62.             }
  63.             break;
  64.             case CLOSED:
  65.                 for (Player plxr : _players) {
  66.                     finishWindow(plxr);
  67.                 }
  68.             break;
  69.         }
  70.     }
  71.  
  72.     public void finishWindow(Player player){
  73.         final StringBuilder sb = new StringBuilder();
  74.  
  75.         StringUtil.append(sb, "<br><br>Event is Over.");
  76.  
  77.         NpcHtmlMessage html = new NpcHtmlMessage(0);
  78.         html.setFile("data/html/mods/Events/mines/minesMW.htm");
  79.         html.replace("%mines%", sb.toString());
  80.         player.sendPacket(html);
  81.     }
  82.  
  83.  
  84.     public void addPlayer(Player player) {
  85.         Mines newMine = new Mines();
  86.         MinesManager.getInstance().addNewMine(player,newMine,player.getObjectId());
  87.         player.sendMessage("[Mines]: You successfully registered to the event.");
  88.         _players.add(player);
  89.  
  90.     }
  91.  
  92.     public void removePlayer(Player player) {
  93.         _players.remove(player);
  94.         player.sendMessage("[Mines]: You successfully removed yourself from event.");
  95.     }
  96.  
  97.  
  98.     public void soloRewards(Player player) {
  99.         player.getInventory().addItem("Mines Reward", _rewardItemID, _itemQuantity, player, player);
  100.     }
  101.  
  102.  
  103.     public void reset(){
  104.         Broadcast.announceToOnlinePlayers("[Mines] Finished.", true);
  105.         _state = mineState.CLOSED;
  106.         showToAll();
  107.         _players.clear();
  108.  
  109.     }
  110.  
  111.     public class Duration implements Runnable
  112.     {
  113.         @Override
  114.         public void run()
  115.         {
  116.             if (_countdown < 0)
  117.             {
  118.                 _startTask.cancel(true);
  119.                 _startTask = null;
  120.             }
  121.  
  122.             switch (_countdown)
  123.             {
  124.                 case 1200:
  125.                 case 900:
  126.                 case 600:
  127.                 case 300:
  128.                 case 240:
  129.                 case 180:
  130.                 case 120:
  131.                 case 60:
  132.                     int tempTimer = _countdown / 60;
  133.                     Broadcast.announceToOnlinePlayers("[Mines] Time left ["+tempTimer+"] minute(s)", true);
  134.                     break;
  135.                 case 30:
  136.                 case 15:
  137.                 case 10:
  138.                 case  5:
  139.                 case  1:
  140.                     Broadcast.announceToOnlinePlayers("[Mines] Time left ["+_countdown+"] second(s)", true);
  141.                     break;
  142.                 case  0:
  143.                         reset();
  144.                     break;
  145.             }
  146.  
  147.             _countdown--;
  148.         }
  149.  
  150.     }
  151.  
  152.     public int getTotalPlayers(){
  153.         return _players.size();
  154.     }
  155.  
  156.     private static class SingletonHolder
  157.     {
  158.         protected static final MinesEvent _instance = new MinesEvent();
  159.     }
  160.  
  161. }
  162.  
RAW Paste Data