Advertisement
Evanth3

Untitled

Aug 30th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2004-2014 L2J DataPack
  3.  *
  4.  * This file is part of L2J DataPack.
  5.  *
  6.  * L2J DataPack is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * L2J DataPack is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19. package instances;
  20.  
  21. import java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.List;
  24.  
  25. import l2r.Config;
  26. import l2r.gameserver.enums.InstanceReenterType;
  27. import l2r.gameserver.instancemanager.InstanceManager;
  28. import l2r.gameserver.model.L2World;
  29. import l2r.gameserver.model.actor.L2Npc;
  30. import l2r.gameserver.model.actor.L2Summon;
  31. import l2r.gameserver.model.actor.instance.L2PcInstance;
  32. import l2r.gameserver.model.effects.L2Effect;
  33. import l2r.gameserver.model.entity.Instance;
  34. import l2r.gameserver.model.holders.InstanceReenterTimeHolder;
  35. import l2r.gameserver.model.instancezone.InstanceWorld;
  36. import l2r.gameserver.network.SystemMessageId;
  37. import l2r.gameserver.network.serverpackets.SystemMessage;
  38.  
  39. import ai.npc.AbstractNpcAI;
  40.  
  41. /**
  42.  * Abstract class for Instances.
  43.  *
  44.  * @author FallenAngel
  45.  */
  46. public abstract class AbstractInstance extends AbstractNpcAI
  47. {
  48.     public AbstractInstance(String name, String desc)
  49.     {
  50.         super(name, desc);
  51.     }
  52.  
  53.     public AbstractInstance(String name)
  54.     {
  55.         super(name, "instances");
  56.     }
  57.  
  58.     protected void enterInstance(L2PcInstance player, InstanceWorld instance, String template, int templateId)
  59.     {
  60.         final InstanceWorld world = InstanceManager.getInstance().getPlayerWorld(player);
  61.         if (world != null)
  62.         {
  63.             if (world.getTemplateId() == templateId)
  64.             {
  65.                 onEnterInstance(player, world, false);
  66.  
  67.                 final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  68.                 if (inst.isRemoveBuffEnabled())
  69.                 {
  70.                     handleRemoveBuffs(player, world);
  71.                 }
  72.                 return;
  73.             }
  74.             player.sendPacket(SystemMessageId.YOU_HAVE_ENTERED_ANOTHER_INSTANT_ZONE_THEREFORE_YOU_CANNOT_ENTER_CORRESPONDING_DUNGEON);
  75.             return;
  76.         }
  77.  
  78.         if (checkConditions(player, templateId))
  79.         {
  80.             instance.setInstanceId(InstanceManager.getInstance().createDynamicInstance(template));
  81.             instance.setTemplateId(templateId);
  82.             instance.setStatus(0);
  83.             InstanceManager.getInstance().addWorld(instance);
  84.             onEnterInstance(player, instance, true);
  85.  
  86.             final Instance inst = InstanceManager.getInstance().getInstance(instance.getInstanceId());
  87.             if (inst.getReenterType() == InstanceReenterType.ON_INSTANCE_ENTER)
  88.             {
  89.                 handleReenterTime(instance);
  90.             }
  91.  
  92.             if (inst.isRemoveBuffEnabled())
  93.             {
  94.                 handleRemoveBuffs(instance);
  95.             }
  96.  
  97.             if (Config.DEBUG_INSTANCES)
  98.             {
  99.                 _log.info("Instance " + inst.getName() + " (" + instance.getTemplateId() + ") has been created by player " + player.getName());
  100.             }
  101.         }
  102.     }
  103.  
  104.     protected void finishInstance(InstanceWorld world)
  105.     {
  106.         finishInstance(world, Config.INSTANCE_FINISH_TIME);
  107.     }
  108.  
  109.     protected void finishInstance(InstanceWorld world, int duration)
  110.     {
  111.         final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  112.  
  113.         if (inst.getReenterType() == InstanceReenterType.ON_INSTANCE_FINISH)
  114.         {
  115.             handleReenterTime(world);
  116.         }
  117.  
  118.         if (duration == 0)
  119.         {
  120.             InstanceManager.getInstance().destroyInstance(inst.getId());
  121.         }
  122.         else if (duration > 0)
  123.         {
  124.             inst.setDuration(duration);
  125.             inst.setEmptyDestroyTime(0);
  126.         }
  127.     }
  128.  
  129.     protected void handleReenterTime(InstanceWorld world)
  130.     {
  131.         final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  132.         final List<InstanceReenterTimeHolder> reenterData = inst.getReenterData();
  133.  
  134.         long time = -1;
  135.  
  136.         for (InstanceReenterTimeHolder data : reenterData)
  137.         {
  138.             if (data.getTime() > 0)
  139.             {
  140.                 time = System.currentTimeMillis() + data.getTime();
  141.                 break;
  142.             }
  143.  
  144.             final Calendar calendar = Calendar.getInstance();
  145.             calendar.set(Calendar.AM_PM, data.getHour() >= 12 ? 1 : 0);
  146.             calendar.set(Calendar.HOUR, data.getHour());
  147.             calendar.set(Calendar.MINUTE, data.getMinute());
  148.             calendar.set(Calendar.SECOND, 0);
  149.  
  150.             if (calendar.getTimeInMillis() <= System.currentTimeMillis())
  151.             {
  152.                 calendar.add(Calendar.DAY_OF_MONTH, 1);
  153.             }
  154.  
  155.             if (data.getDay() != null)
  156.             {
  157.                 while (calendar.get(Calendar.DAY_OF_WEEK) != (Math.min(data.getDay().getValue() + 1, 7)))
  158.                 {
  159.                     calendar.add(Calendar.DAY_OF_MONTH, 1);
  160.                 }
  161.             }
  162.  
  163.             if (time == -1)
  164.             {
  165.                 time = calendar.getTimeInMillis();
  166.             }
  167.             else if (calendar.getTimeInMillis() < time)
  168.             {
  169.                 time = calendar.getTimeInMillis();
  170.             }
  171.         }
  172.  
  173.         if (time > 0)
  174.         {
  175.             setReenterTime(world, time);
  176.         }
  177.     }
  178.  
  179.     protected void handleRemoveBuffs(InstanceWorld world)
  180.     {
  181.         for (int objId : world.getAllowed())
  182.         {
  183.             final L2PcInstance player = L2World.getInstance().getPlayer(objId);
  184.  
  185.             if (player != null)
  186.             {
  187.                 handleRemoveBuffs(player, world);
  188.             }
  189.         }
  190.     }
  191.  
  192.     protected abstract void onEnterInstance(L2PcInstance player, InstanceWorld world, boolean firstEntrance);
  193.  
  194.     protected boolean checkConditions(L2PcInstance player, int templateId)
  195.     {
  196.         return checkConditions(player);
  197.     }
  198.  
  199.     protected boolean checkConditions(L2PcInstance player)
  200.     {
  201.         return true;
  202.     }
  203.  
  204.     /**
  205.      * Spawns group of instance NPC's
  206.      *
  207.      * @param groupName  the name of group from XML definition to spawn
  208.      * @param instanceId the instance ID
  209.      * @return list of spawned NPC's
  210.      */
  211.     protected List<L2Npc> spawnGroup(String groupName, int instanceId)
  212.     {
  213.         return InstanceManager.getInstance().getInstance(instanceId).spawnGroup(groupName);
  214.     }
  215.  
  216.     /**
  217.      * Sets reenter time for every player in the instance.
  218.      *
  219.      * @param world the instance
  220.      * @param time  the time in milliseconds
  221.      */
  222.     protected void setReenterTime(InstanceWorld world, long time)
  223.     {
  224.         for (int objectId : world.getAllowed())
  225.         {
  226.             InstanceManager.getInstance().setInstanceTime(objectId, world.getTemplateId(), time);
  227.             final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  228.             if ((player != null) && player.isOnline())
  229.             {
  230.                 player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INSTANT_ZONE_FROM_HERE_S1_S_ENTRY_HAS_BEEN_RESTRICTED).addString(InstanceManager.getInstance().getInstance(world.getInstanceId()).getName()));
  231.             }
  232.         }
  233.  
  234.         if (Config.DEBUG_INSTANCES)
  235.         {
  236.             _log.info("Time restrictions has been set for player in instance ID: " + world.getInstanceId() + " (" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time) + ")");
  237.         }
  238.     }
  239.  
  240.     private void handleRemoveBuffs(L2PcInstance player, InstanceWorld world)
  241.     {
  242.         final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  243.  
  244.         switch (inst.getRemoveBuffType())
  245.         {
  246.             case ALL:
  247.             {
  248.                 player.stopAllEffectsExceptThoseThatLastThroughDeath();
  249.  
  250.                 final L2Summon summon = player.getSummon();
  251.                 if (summon != null)
  252.                 {
  253.                     summon.stopAllEffectsExceptThoseThatLastThroughDeath();
  254.                 }
  255.                 break;
  256.             }
  257.             case WHITELIST:
  258.             {
  259.                 for (L2Effect info : player.getEffectList().getBuffs())
  260.                 {
  261.                     if (!inst.getBuffExceptionList().contains(info.getSkill().getId()))
  262.                     {
  263.                         info.getEffected().getEffectList().stopSkillEffects(info.getSkill().getId());
  264.                     }
  265.                 }
  266.  
  267.                 final L2Summon summon = player.getSummon();
  268.                 if (summon != null)
  269.                 {
  270.                     for (L2Effect info : summon.getEffectList().getBuffs())
  271.                     {
  272.                         if (!inst.getBuffExceptionList().contains(info.getSkill().getId()))
  273.                         {
  274.                             info.getEffected().getEffectList().stopSkillEffects(info.getSkill().getId());
  275.                         }
  276.                     }
  277.                 }
  278.                 break;
  279.             }
  280.             case BLACKLIST:
  281.             {
  282.                 for (L2Effect info : player.getEffectList().getBuffs())
  283.                 {
  284.                     if (inst.getBuffExceptionList().contains(info.getSkill().getId()))
  285.                     {
  286.                         info.getEffected().getEffectList().stopSkillEffects(info.getSkill().getId());
  287.                     }
  288.                 }
  289.  
  290.                 final L2Summon summon = player.getSummon();
  291.                 if (summon != null)
  292.                 {
  293.                     for (L2Effect info : summon.getEffectList().getBuffs())
  294.                     {
  295.                         if (inst.getBuffExceptionList().contains(info.getSkill().getId()))
  296.                         {
  297.                             info.getEffected().getEffectList().stopSkillEffects(info.getSkill().getId());
  298.                         }
  299.                     }
  300.                 }
  301.                 break;
  302.             }
  303.         }
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement