Advertisement
xSweeTs

RandomZone schedule

Sep 23rd, 2020 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. package net.sf.l2j.gameserver.taskmanager;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.List;
  7.  
  8. import net.sf.l2j.commons.pool.ThreadPool;
  9.  
  10. import net.sf.l2j.Config;
  11. import net.sf.l2j.gameserver.data.manager.ZoneManager;
  12. import net.sf.l2j.gameserver.data.xml.AdminData;
  13. import net.sf.l2j.gameserver.enums.ZoneId;
  14. import net.sf.l2j.gameserver.model.World;
  15. import net.sf.l2j.gameserver.model.actor.Creature;
  16. import net.sf.l2j.gameserver.model.actor.Npc;
  17. import net.sf.l2j.gameserver.model.actor.Player;
  18. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  19. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  20.  
  21. public final class RandomZoneTaskManager
  22. {
  23.     private int currentZoneId;
  24.     private int timer;
  25.    
  26.     private List<Npc> zoneMonsters = new ArrayList<>();
  27.    
  28.     public RandomZoneTaskManager()
  29.     {
  30.         if (getZonesCount() > 1)
  31.             scheduleEvent();
  32.     }
  33.    
  34.     private void scheduleEvent()
  35.     {
  36.         Calendar currentTime = Calendar.getInstance();
  37.         Calendar nextStartTime = null;
  38.        
  39.         for (String time : Config.RANDOM_ZONE_SCHEDULE_TIME)
  40.         {
  41.             final String[] splitTime = time.split(":");
  42.            
  43.             nextStartTime = Calendar.getInstance();
  44.             nextStartTime.setLenient(true);
  45.            
  46.             nextStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTime[0]));
  47.             nextStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTime[1]));
  48.            
  49.             if (nextStartTime.getTimeInMillis() > System.currentTimeMillis())
  50.             {
  51.                 final long delay = nextStartTime.getTimeInMillis() - System.currentTimeMillis();
  52.                 World.announceToOnlinePlayers("Next Random zone will start on: " + new SimpleDateFormat("dd-MM-yyyy HH:mm").format(nextStartTime.getTimeInMillis()) + ".", true);
  53.                 ThreadPool.scheduleAtFixedRate(new EventTask(), delay, 1000);
  54.                 break;
  55.             }
  56.         }
  57.     }
  58.    
  59.     private class EventTask implements Runnable
  60.     {
  61.         public EventTask()
  62.         {
  63.             selectZone();
  64.         }
  65.        
  66.         @Override
  67.         public void run()
  68.         {
  69.             switch (timer)
  70.             {
  71.                 case 0:
  72.                     spawnMonsters();
  73.                     scheduleEvent();
  74.                     break;
  75.                
  76.                 case 1:
  77.                 case 2:
  78.                 case 3:
  79.                 case 4:
  80.                 case 5:
  81.                 case 10:
  82.                 case 20:
  83.                 case 30:
  84.                     World.announceToOnlinePlayers("Random zone will finish in " + timer + " second(s).", true);
  85.                     break;
  86.                
  87.                 case 60:
  88.                 case 120:
  89.                 case 180:
  90.                 case 240:
  91.                 case 300:
  92.                 case 600:
  93.                 case 1200:
  94.                 case 1800:
  95.                     World.announceToOnlinePlayers("Random zone will finish in " + (timer / 60) + " minute(s).", true);
  96.                     break;
  97.                
  98.                 case 3600:
  99.                 case 7200:
  100.                     World.announceToOnlinePlayers("Random zone will finish in " + (timer / 60 / 60) + " hour(s).", true);
  101.                     break;
  102.             }
  103.             timer--;
  104.         }
  105.     }
  106.    
  107.     private void selectZone()
  108.     {
  109.         currentZoneId = currentZoneId == getZonesCount() ? 1 : ++currentZoneId;
  110.        
  111.         final RandomZone zone = ZoneManager.getInstance().getZoneById(currentZoneId, RandomZone.class);
  112.         if (zone == null)
  113.         {
  114.             AdminData.getInstance().broadcastToGMs(new CreatureSay(0, Say2.PARTYROOM_COMMANDER, "RandomZone", "No zone with id: " + currentZoneId));
  115.             return;
  116.         }
  117.        
  118.         timer = zone.getTime() * 60;
  119.        
  120.         World.announceToOnlinePlayers("New Random Zone: " + zone.getName() + ". Duration: " + zone.getTime() + " min(s).", true);
  121.         World.getInstance().getPlayers().stream().filter(p -> p.isInsideZone(ZoneId.RANDOM) && zone.isActive()).forEach(this::teleport);
  122.         despawnMonsters(currentZoneId);
  123.     }
  124.    
  125.     private final void despawnMonsters(int zoneId)
  126.     {
  127.         final RandomZone zone = ZoneManager.getInstance().getZoneById(zoneId, RandomZone.class);
  128.         if (zone.despawnNpcs())
  129.         {
  130.             for (Creature creature : zone.getCharacters())
  131.             {
  132.                 if (creature instanceof Npc)
  133.                 {
  134.                     final Npc npc = (Npc) creature;
  135.                     npc.decayMe();
  136.                    
  137.                     zoneMonsters.add(npc);
  138.                 }
  139.             }
  140.         }
  141.     }
  142.    
  143.     private final void spawnMonsters()
  144.     {
  145.         if (zoneMonsters.isEmpty())
  146.             return;
  147.        
  148.         zoneMonsters.forEach(Npc::spawnMe);
  149.         zoneMonsters.clear();
  150.     }
  151.    
  152.     private final void teleport(Player player)
  153.     {
  154.         if (player.isDead())
  155.             player.doRevive();
  156.        
  157.         player.teleToLocation(getCurrentZone().getSpawnLoc(), 0);
  158.     }
  159.    
  160.     public void onEnter(Player player)
  161.     {
  162.         if (currentZoneId > 0)
  163.             player.sendPacket(new CreatureSay(0, Say2.HERO_VOICE, "RandomZone", "Current zone: " + getCurrentZone().getName()));
  164.     }
  165.    
  166.     public final int getCurrentZoneId()
  167.     {
  168.         return currentZoneId;
  169.     }
  170.    
  171.     private static final int getZonesCount()
  172.     {
  173.         return ZoneManager.getInstance().getAllZones(RandomZone.class).size();
  174.     }
  175.    
  176.     public final RandomZone getCurrentZone()
  177.     {
  178.         return ZoneManager.getInstance().getAllZones(RandomZone.class).stream().filter(t -> t.getId() == currentZoneId).findFirst().orElse(null);
  179.     }
  180.    
  181.     public static final RandomZoneTaskManager getInstance()
  182.     {
  183.         return SingletonHolder.INSTANCE;
  184.     }
  185.    
  186.     private static class SingletonHolder
  187.     {
  188.         protected static final RandomZoneTaskManager INSTANCE = new RandomZoneTaskManager();
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement