EffectTemplate.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.skills.effects;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.gameserver.model.L2Effect;
  20. import net.sf.l2j.gameserver.skills.Env;
  21. import net.sf.l2j.gameserver.skills.conditions.Condition;
  22. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  23. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  24. /**
  25. * @author mkizub
  26. *
  27. */
  28. public final class EffectTemplate
  29. {
  30. static Logger _log = Logger.getLogger(EffectTemplate.class.getName());
  31. private final Class<?> _func;
  32. private final Constructor<?> _constructor;
  33. public final Condition attachCond;
  34. public final Condition applayCond;
  35. public final Lambda lambda;
  36. public final int counter;
  37. public final int period; // in seconds
  38. public final int abnormalEffect;
  39. public FuncTemplate[] funcTemplates;
  40. public final String stackType;
  41. public final float stackOrder;
  42. public final boolean icon;
  43. public EffectTemplate(Condition pAttachCond, Condition pApplayCond, String func, Lambda pLambda, int pCounter, int pPeriod, int pAbnormalEffect, String pStackType, float pStackOrder, boolean showicon)
  44. {
  45. attachCond = pAttachCond;
  46. applayCond = pApplayCond;
  47. lambda = pLambda;
  48. counter = pCounter;
  49. period = pPeriod;
  50. abnormalEffect = pAbnormalEffect;
  51. stackType = pStackType;
  52. stackOrder = pStackOrder;
  53. icon = showicon;
  54. try
  55. {
  56. _func = Class.forName("net.sf.l2j.gameserver.skills.effects.Effect" + func);
  57. }
  58. catch (ClassNotFoundException e)
  59. {
  60. throw new RuntimeException(e);
  61. }
  62. try
  63. {
  64. _constructor = _func.getConstructor(Env.class, EffectTemplate.class);
  65. }
  66. catch (NoSuchMethodException e)
  67. {
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. public L2Effect getEffect(Env env)
  72. {
  73. if (attachCond != null && !attachCond.test(env))
  74. return null;
  75. try
  76. {
  77. L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
  78. return effect;
  79. }
  80. catch (IllegalAccessException e)
  81. {
  82. e.printStackTrace();
  83. return null;
  84. }
  85. catch (InstantiationException e)
  86. {
  87. e.printStackTrace();
  88. return null;
  89. }
  90. catch (InvocationTargetException e)
  91. {
  92. _log.warning("Error creating new instance of Class " + _func + " Exception was:");
  93. e.getTargetException().printStackTrace();
  94. return null;
  95. }
  96. }
  97. public void attach(FuncTemplate f)
  98. {
  99. if (funcTemplates == null)
  100. {
  101. funcTemplates = new FuncTemplate[] { f };
  102. }
  103. else
  104. {
  105. int len = funcTemplates.length;
  106. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  107. System.arraycopy(funcTemplates, 0, tmp, 0, len);
  108. tmp[len] = f;
  109. funcTemplates = tmp;
  110. }
  111. }
  112. }