AbstractInstance.java 8.8 KB

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