AbstractInstance.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2004-2015 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. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.List;
  23. import ai.npc.AbstractNpcAI;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.enums.InstanceReenterType;
  26. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.L2Summon;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.entity.Instance;
  32. import com.l2jserver.gameserver.model.holders.InstanceReenterTimeHolder;
  33. import com.l2jserver.gameserver.model.instancezone.InstanceWorld;
  34. import com.l2jserver.gameserver.model.skills.BuffInfo;
  35. import com.l2jserver.gameserver.network.SystemMessageId;
  36. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  37. /**
  38. * Abstract class for Instances.
  39. * @author FallenAngel
  40. */
  41. public abstract class AbstractInstance extends AbstractNpcAI
  42. {
  43. public AbstractInstance(String name, String desc)
  44. {
  45. super(name, desc);
  46. }
  47. public AbstractInstance(String name)
  48. {
  49. super(name, "instances");
  50. }
  51. protected void enterInstance(L2PcInstance player, InstanceWorld instance, String template, int templateId)
  52. {
  53. final InstanceWorld world = InstanceManager.getInstance().getPlayerWorld(player);
  54. if (world != null)
  55. {
  56. if (world.getTemplateId() == templateId)
  57. {
  58. onEnterInstance(player, world, false);
  59. final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  60. if (inst.isRemoveBuffEnabled())
  61. {
  62. handleRemoveBuffs(player, world);
  63. }
  64. return;
  65. }
  66. player.sendPacket(SystemMessageId.YOU_HAVE_ENTERED_ANOTHER_INSTANT_ZONE_THEREFORE_YOU_CANNOT_ENTER_CORRESPONDING_DUNGEON);
  67. return;
  68. }
  69. if (checkConditions(player, templateId))
  70. {
  71. instance.setInstanceId(InstanceManager.getInstance().createDynamicInstance(template));
  72. instance.setTemplateId(templateId);
  73. instance.setStatus(0);
  74. InstanceManager.getInstance().addWorld(instance);
  75. onEnterInstance(player, instance, true);
  76. final Instance inst = InstanceManager.getInstance().getInstance(instance.getInstanceId());
  77. if (inst.getReenterType() == InstanceReenterType.ON_INSTANCE_ENTER)
  78. {
  79. handleReenterTime(instance);
  80. }
  81. if (inst.isRemoveBuffEnabled())
  82. {
  83. handleRemoveBuffs(instance);
  84. }
  85. if (Config.DEBUG_INSTANCES)
  86. {
  87. _log.info("Instance " + inst.getName() + " (" + instance.getTemplateId() + ") has been created by player " + player.getName());
  88. }
  89. }
  90. }
  91. protected void finishInstance(InstanceWorld world)
  92. {
  93. finishInstance(world, Config.INSTANCE_FINISH_TIME);
  94. }
  95. protected void finishInstance(InstanceWorld world, int duration)
  96. {
  97. final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  98. if (inst.getReenterType() == InstanceReenterType.ON_INSTANCE_FINISH)
  99. {
  100. handleReenterTime(world);
  101. }
  102. if (duration == 0)
  103. {
  104. InstanceManager.getInstance().destroyInstance(inst.getId());
  105. }
  106. else if (duration > 0)
  107. {
  108. inst.setDuration(duration);
  109. inst.setEmptyDestroyTime(0);
  110. }
  111. }
  112. protected void handleReenterTime(InstanceWorld world)
  113. {
  114. final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  115. final List<InstanceReenterTimeHolder> reenterData = inst.getReenterData();
  116. long time = -1;
  117. for (InstanceReenterTimeHolder data : reenterData)
  118. {
  119. if (data.getTime() > 0)
  120. {
  121. time = System.currentTimeMillis() + data.getTime();
  122. break;
  123. }
  124. final Calendar calendar = Calendar.getInstance();
  125. calendar.set(Calendar.AM_PM, data.getHour() >= 12 ? 1 : 0);
  126. calendar.set(Calendar.HOUR, data.getHour());
  127. calendar.set(Calendar.MINUTE, data.getMinute());
  128. calendar.set(Calendar.SECOND, 0);
  129. if (calendar.getTimeInMillis() <= System.currentTimeMillis())
  130. {
  131. calendar.add(Calendar.DAY_OF_MONTH, 1);
  132. }
  133. if (data.getDay() != null)
  134. {
  135. while (calendar.get(Calendar.DAY_OF_WEEK) != (data.getDay().getValue() + 1))
  136. {
  137. calendar.add(Calendar.DAY_OF_MONTH, 1);
  138. }
  139. }
  140. if (time == -1)
  141. {
  142. time = calendar.getTimeInMillis();
  143. }
  144. else if (calendar.getTimeInMillis() < time)
  145. {
  146. time = calendar.getTimeInMillis();
  147. }
  148. }
  149. if (time > 0)
  150. {
  151. setReenterTime(world, time);
  152. }
  153. }
  154. protected void handleRemoveBuffs(InstanceWorld world)
  155. {
  156. for (int objId : world.getAllowed())
  157. {
  158. final L2PcInstance player = L2World.getInstance().getPlayer(objId);
  159. if (player != null)
  160. {
  161. handleRemoveBuffs(player, world);
  162. }
  163. }
  164. }
  165. protected abstract void onEnterInstance(L2PcInstance player, InstanceWorld world, boolean firstEntrance);
  166. protected boolean checkConditions(L2PcInstance player, int templateId)
  167. {
  168. return checkConditions(player);
  169. }
  170. protected boolean checkConditions(L2PcInstance player)
  171. {
  172. return true;
  173. }
  174. /**
  175. * Spawns group of instance NPC's
  176. * @param groupName the name of group from XML definition to spawn
  177. * @param instanceId the instance ID
  178. * @return list of spawned NPC's
  179. */
  180. protected List<L2Npc> spawnGroup(String groupName, int instanceId)
  181. {
  182. return InstanceManager.getInstance().getInstance(instanceId).spawnGroup(groupName);
  183. }
  184. /**
  185. * Sets reenter time for every player in the instance.
  186. * @param world the instance
  187. * @param time the time in milliseconds
  188. */
  189. protected void setReenterTime(InstanceWorld world, long time)
  190. {
  191. for (int objectId : world.getAllowed())
  192. {
  193. InstanceManager.getInstance().setInstanceTime(objectId, world.getTemplateId(), time);
  194. final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  195. if ((player != null) && player.isOnline())
  196. {
  197. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INSTANT_ZONE_FROM_HERE_S1_S_ENTRY_HAS_BEEN_RESTRICTED).addString(InstanceManager.getInstance().getInstance(world.getInstanceId()).getName()));
  198. }
  199. }
  200. if (Config.DEBUG_INSTANCES)
  201. {
  202. _log.info("Time restrictions has been set for player in instance ID: " + world.getInstanceId() + " (" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time) + ")");
  203. }
  204. }
  205. private void handleRemoveBuffs(L2PcInstance player, InstanceWorld world)
  206. {
  207. final Instance inst = InstanceManager.getInstance().getInstance(world.getInstanceId());
  208. switch (inst.getRemoveBuffType())
  209. {
  210. case ALL:
  211. {
  212. player.stopAllEffectsExceptThoseThatLastThroughDeath();
  213. final L2Summon summon = player.getSummon();
  214. if (summon != null)
  215. {
  216. summon.stopAllEffectsExceptThoseThatLastThroughDeath();
  217. }
  218. break;
  219. }
  220. case WHITELIST:
  221. {
  222. for (BuffInfo info : player.getEffectList().getBuffs())
  223. {
  224. if (!inst.getBuffExceptionList().contains(info.getSkill().getId()))
  225. {
  226. info.getEffected().getEffectList().stopSkillEffects(true, info.getSkill());
  227. }
  228. }
  229. final L2Summon summon = player.getSummon();
  230. if (summon != null)
  231. {
  232. for (BuffInfo info : summon.getEffectList().getBuffs())
  233. {
  234. if (!inst.getBuffExceptionList().contains(info.getSkill().getId()))
  235. {
  236. info.getEffected().getEffectList().stopSkillEffects(true, info.getSkill());
  237. }
  238. }
  239. }
  240. break;
  241. }
  242. case BLACKLIST:
  243. {
  244. for (BuffInfo info : player.getEffectList().getBuffs())
  245. {
  246. if (inst.getBuffExceptionList().contains(info.getSkill().getId()))
  247. {
  248. info.getEffected().getEffectList().stopSkillEffects(true, info.getSkill());
  249. }
  250. }
  251. final L2Summon summon = player.getSummon();
  252. if (summon != null)
  253. {
  254. for (BuffInfo info : summon.getEffectList().getBuffs())
  255. {
  256. if (inst.getBuffExceptionList().contains(info.getSkill().getId()))
  257. {
  258. info.getEffected().getEffectList().stopSkillEffects(true, info.getSkill());
  259. }
  260. }
  261. }
  262. break;
  263. }
  264. }
  265. }
  266. }