SummonEffectsTable.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.data.sql.impl;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.concurrent.CopyOnWriteArrayList;
  26. import com.l2jserver.gameserver.model.actor.L2Summon;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  30. import com.l2jserver.gameserver.model.skills.Skill;
  31. /**
  32. * @author Nyaran
  33. */
  34. public class SummonEffectsTable
  35. {
  36. /** Servitors **/
  37. // Map tree
  38. // -> key: charObjectId, value: classIndex Map
  39. // --> key: classIndex, value: servitors Map
  40. // ---> key: servitorSkillId, value: Effects list
  41. private final Map<Integer, Map<Integer, Map<Integer, List<SummonEffect>>>> _servitorEffects = new HashMap<>();
  42. private Map<Integer, List<SummonEffect>> getServitorEffects(L2PcInstance owner)
  43. {
  44. final Map<Integer, Map<Integer, List<SummonEffect>>> servitorMap = _servitorEffects.get(owner.getObjectId());
  45. if (servitorMap == null)
  46. {
  47. return null;
  48. }
  49. return servitorMap.get(owner.getClassIndex());
  50. }
  51. private List<SummonEffect> getServitorEffects(L2PcInstance owner, int referenceSkill)
  52. {
  53. return containsOwner(owner) ? getServitorEffects(owner).get(referenceSkill) : null;
  54. }
  55. private boolean containsOwner(L2PcInstance owner)
  56. {
  57. return _servitorEffects.getOrDefault(owner.getObjectId(), Collections.emptyMap()).containsKey(owner.getClassIndex());
  58. }
  59. private void removeEffects(List<SummonEffect> effects, int skillId)
  60. {
  61. if ((effects != null) && !effects.isEmpty())
  62. {
  63. for (SummonEffect effect : effects)
  64. {
  65. final Skill skill = effect.getSkill();
  66. if ((skill != null) && (skill.getId() == skillId))
  67. {
  68. effects.remove(effect);
  69. }
  70. }
  71. }
  72. }
  73. private void applyEffects(L2Summon summon, List<SummonEffect> summonEffects)
  74. {
  75. if (summonEffects == null)
  76. {
  77. return;
  78. }
  79. for (SummonEffect se : summonEffects)
  80. {
  81. if (se != null)
  82. {
  83. se.getSkill().applyEffects(summon, summon, false, se.getEffectCurTime());
  84. }
  85. }
  86. }
  87. public boolean containsSkill(L2PcInstance owner, int referenceSkill)
  88. {
  89. return containsOwner(owner) && getServitorEffects(owner).containsKey(referenceSkill);
  90. }
  91. public void clearServitorEffects(L2PcInstance owner, int referenceSkill)
  92. {
  93. if (containsOwner(owner))
  94. {
  95. getServitorEffects(owner).getOrDefault(referenceSkill, Collections.emptyList()).clear();
  96. }
  97. }
  98. public void addServitorEffect(L2PcInstance owner, int referenceSkill, Skill skill, int effectCurTime)
  99. {
  100. _servitorEffects.putIfAbsent(owner.getObjectId(), new HashMap<Integer, Map<Integer, List<SummonEffect>>>());
  101. _servitorEffects.get(owner.getObjectId()).putIfAbsent(owner.getClassIndex(), new HashMap<Integer, List<SummonEffect>>());
  102. getServitorEffects(owner).putIfAbsent(referenceSkill, new CopyOnWriteArrayList<SummonEffect>());
  103. getServitorEffects(owner).get(referenceSkill).add(new SummonEffect(skill, effectCurTime));
  104. }
  105. public void removeServitorEffects(L2PcInstance owner, int referenceSkill, int skillId)
  106. {
  107. removeEffects(getServitorEffects(owner, referenceSkill), skillId);
  108. }
  109. public void applyServitorEffects(L2ServitorInstance l2ServitorInstance, L2PcInstance owner, int referenceSkill)
  110. {
  111. applyEffects(l2ServitorInstance, getServitorEffects(owner, referenceSkill));
  112. }
  113. /** Pets **/
  114. private final Map<Integer, List<SummonEffect>> _petEffects = new HashMap<>(); // key: petItemObjectId, value: Effects list
  115. public void addPetEffect(int controlObjectId, Skill skill, int effectCurTime)
  116. {
  117. _petEffects.computeIfAbsent(controlObjectId, k -> new ArrayList<>()).add(new SummonEffect(skill, effectCurTime));
  118. }
  119. public boolean containsPetId(int controlObjectId)
  120. {
  121. return _petEffects.containsKey(controlObjectId);
  122. }
  123. public void applyPetEffects(L2PetInstance l2PetInstance, int controlObjectId)
  124. {
  125. applyEffects(l2PetInstance, _petEffects.get(controlObjectId));
  126. }
  127. public void clearPetEffects(int controlObjectId)
  128. {
  129. _petEffects.getOrDefault(controlObjectId, Collections.emptyList()).clear();
  130. }
  131. public void removePetEffects(int controlObjectId, int skillId)
  132. {
  133. removeEffects(_petEffects.get(controlObjectId), skillId);
  134. }
  135. private class SummonEffect
  136. {
  137. Skill _skill;
  138. int _effectCurTime;
  139. public SummonEffect(Skill skill, int effectCurTime)
  140. {
  141. _skill = skill;
  142. _effectCurTime = effectCurTime;
  143. }
  144. public Skill getSkill()
  145. {
  146. return _skill;
  147. }
  148. public int getEffectCurTime()
  149. {
  150. return _effectCurTime;
  151. }
  152. }
  153. public static SummonEffectsTable getInstance()
  154. {
  155. return SingletonHolder._instance;
  156. }
  157. private static class SingletonHolder
  158. {
  159. protected static final SummonEffectsTable _instance = new SummonEffectsTable();
  160. }
  161. }