FuncTemplate.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 com.l2jserver.gameserver.skills.funcs;
  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.gameserver.skills.Env;
  21. import com.l2jserver.gameserver.skills.Stats;
  22. import com.l2jserver.gameserver.skills.conditions.Condition;
  23. /**
  24. * @author mkizub
  25. */
  26. public final class FuncTemplate
  27. {
  28. protected static final Logger _log = Logger.getLogger(FuncTemplate.class.getName());
  29. public Condition attachCond;
  30. public Condition applayCond;
  31. public final Class<?> func;
  32. public final Constructor<?> constructor;
  33. public final Stats stat;
  34. public final int order;
  35. public final Lambda lambda;
  36. public FuncTemplate(Condition pAttachCond, Condition pApplayCond, String pFunc, Stats pStat, int pOrder, Lambda pLambda)
  37. {
  38. attachCond = pAttachCond;
  39. applayCond = pApplayCond;
  40. stat = pStat;
  41. order = pOrder;
  42. lambda = pLambda;
  43. try
  44. {
  45. func = Class.forName("com.l2jserver.gameserver.skills.funcs.Func" + pFunc);
  46. }
  47. catch (ClassNotFoundException e)
  48. {
  49. throw new RuntimeException(e);
  50. }
  51. try
  52. {
  53. constructor = func.getConstructor(new Class<?>[]
  54. {
  55. // Stats to update
  56. Stats.class,
  57. // Order of execution
  58. Integer.TYPE,
  59. // Owner
  60. Object.class,
  61. // Value for function
  62. Lambda.class
  63. });
  64. }
  65. catch (NoSuchMethodException e)
  66. {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. public Func getFunc(Env env, Object owner)
  71. {
  72. if ((attachCond != null) && !attachCond.test(env))
  73. {
  74. return null;
  75. }
  76. try
  77. {
  78. Func f = (Func) constructor.newInstance(stat, order, owner, lambda);
  79. if (applayCond != null)
  80. {
  81. f.setCondition(applayCond);
  82. }
  83. return f;
  84. }
  85. catch (IllegalAccessException e)
  86. {
  87. _log.log(Level.WARNING, "", e);
  88. return null;
  89. }
  90. catch (InstantiationException e)
  91. {
  92. _log.log(Level.WARNING, "", e);
  93. return null;
  94. }
  95. catch (InvocationTargetException e)
  96. {
  97. _log.log(Level.WARNING, "", e);
  98. return null;
  99. }
  100. }
  101. }