FuncTemplate.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(
  54. new Class[]
  55. {
  56. Stats.class, // stats to update
  57. Integer.TYPE, // order of execution
  58. Object.class, // owner
  59. Lambda.class // value for function
  60. });
  61. }
  62. catch (NoSuchMethodException e)
  63. {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. public Func getFunc(Env env, Object owner)
  68. {
  69. if (attachCond != null && !attachCond.test(env))
  70. return null;
  71. try
  72. {
  73. Func f = (Func) constructor.newInstance(stat, order, owner, lambda);
  74. if (applayCond != null)
  75. f.setCondition(applayCond);
  76. return f;
  77. }
  78. catch (IllegalAccessException e)
  79. {
  80. _log.log(Level.WARNING, "", e);
  81. return null;
  82. }
  83. catch (InstantiationException e)
  84. {
  85. _log.log(Level.WARNING, "", e);
  86. return null;
  87. }
  88. catch (InvocationTargetException e)
  89. {
  90. _log.log(Level.WARNING, "", e);
  91. return null;
  92. }
  93. }
  94. }