Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.sf.l2j.gameserver.taskmanager;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import net.sf.l2j.commons.pool.ThreadPool;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.data.manager.ZoneManager;
- import net.sf.l2j.gameserver.data.xml.AdminData;
- import net.sf.l2j.gameserver.enums.ZoneId;
- import net.sf.l2j.gameserver.model.World;
- import net.sf.l2j.gameserver.model.actor.Creature;
- import net.sf.l2j.gameserver.model.actor.Npc;
- import net.sf.l2j.gameserver.model.actor.Player;
- import net.sf.l2j.gameserver.network.clientpackets.Say2;
- import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
- public final class RandomZoneTaskManager
- {
- private int currentZoneId;
- private int timer;
- private List<Npc> zoneMonsters = new ArrayList<>();
- public RandomZoneTaskManager()
- {
- if (getZonesCount() > 1)
- scheduleEvent();
- }
- private void scheduleEvent()
- {
- Calendar currentTime = Calendar.getInstance();
- Calendar nextStartTime = null;
- for (String time : Config.RANDOM_ZONE_SCHEDULE_TIME)
- {
- final String[] splitTime = time.split(":");
- nextStartTime = Calendar.getInstance();
- nextStartTime.setLenient(true);
- nextStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTime[0]));
- nextStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTime[1]));
- if (nextStartTime.getTimeInMillis() > System.currentTimeMillis())
- {
- final long delay = nextStartTime.getTimeInMillis() - System.currentTimeMillis();
- World.announceToOnlinePlayers("Next Random zone will start on: " + new SimpleDateFormat("dd-MM-yyyy HH:mm").format(nextStartTime.getTimeInMillis()) + ".", true);
- ThreadPool.scheduleAtFixedRate(new EventTask(), delay, 1000);
- break;
- }
- }
- }
- private class EventTask implements Runnable
- {
- public EventTask()
- {
- selectZone();
- }
- @Override
- public void run()
- {
- switch (timer)
- {
- case 0:
- spawnMonsters();
- scheduleEvent();
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 10:
- case 20:
- case 30:
- World.announceToOnlinePlayers("Random zone will finish in " + timer + " second(s).", true);
- break;
- case 60:
- case 120:
- case 180:
- case 240:
- case 300:
- case 600:
- case 1200:
- case 1800:
- World.announceToOnlinePlayers("Random zone will finish in " + (timer / 60) + " minute(s).", true);
- break;
- case 3600:
- case 7200:
- World.announceToOnlinePlayers("Random zone will finish in " + (timer / 60 / 60) + " hour(s).", true);
- break;
- }
- timer--;
- }
- }
- private void selectZone()
- {
- currentZoneId = currentZoneId == getZonesCount() ? 1 : ++currentZoneId;
- final RandomZone zone = ZoneManager.getInstance().getZoneById(currentZoneId, RandomZone.class);
- if (zone == null)
- {
- AdminData.getInstance().broadcastToGMs(new CreatureSay(0, Say2.PARTYROOM_COMMANDER, "RandomZone", "No zone with id: " + currentZoneId));
- return;
- }
- timer = zone.getTime() * 60;
- World.announceToOnlinePlayers("New Random Zone: " + zone.getName() + ". Duration: " + zone.getTime() + " min(s).", true);
- World.getInstance().getPlayers().stream().filter(p -> p.isInsideZone(ZoneId.RANDOM) && zone.isActive()).forEach(this::teleport);
- despawnMonsters(currentZoneId);
- }
- private final void despawnMonsters(int zoneId)
- {
- final RandomZone zone = ZoneManager.getInstance().getZoneById(zoneId, RandomZone.class);
- if (zone.despawnNpcs())
- {
- for (Creature creature : zone.getCharacters())
- {
- if (creature instanceof Npc)
- {
- final Npc npc = (Npc) creature;
- npc.decayMe();
- zoneMonsters.add(npc);
- }
- }
- }
- }
- private final void spawnMonsters()
- {
- if (zoneMonsters.isEmpty())
- return;
- zoneMonsters.forEach(Npc::spawnMe);
- zoneMonsters.clear();
- }
- private final void teleport(Player player)
- {
- if (player.isDead())
- player.doRevive();
- player.teleToLocation(getCurrentZone().getSpawnLoc(), 0);
- }
- public void onEnter(Player player)
- {
- if (currentZoneId > 0)
- player.sendPacket(new CreatureSay(0, Say2.HERO_VOICE, "RandomZone", "Current zone: " + getCurrentZone().getName()));
- }
- public final int getCurrentZoneId()
- {
- return currentZoneId;
- }
- private static final int getZonesCount()
- {
- return ZoneManager.getInstance().getAllZones(RandomZone.class).size();
- }
- public final RandomZone getCurrentZone()
- {
- return ZoneManager.getInstance().getAllZones(RandomZone.class).stream().filter(t -> t.getId() == currentZoneId).findFirst().orElse(null);
- }
- public static final RandomZoneTaskManager getInstance()
- {
- return SingletonHolder.INSTANCE;
- }
- private static class SingletonHolder
- {
- protected static final RandomZoneTaskManager INSTANCE = new RandomZoneTaskManager();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement