FuncTemplate.java 2.5 KB

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