FuncTemplate.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * TODO To change the template for this generated type comment go to
  25. * Window - Preferences - Java - Code Style - Code Templates
  26. */
  27. public final class FuncTemplate {
  28. public Condition attachCond;
  29. public Condition applayCond;
  30. public final Class<?> func;
  31. public final Constructor<?> constructor;
  32. public final Stats stat;
  33. public final int order;
  34. public final Lambda lambda;
  35. public FuncTemplate(Condition pAttachCond, Condition pApplayCond, String pFunc, Stats pStat, int pOrder, Lambda pLambda)
  36. {
  37. attachCond = pAttachCond;
  38. applayCond = pApplayCond;
  39. stat = pStat;
  40. order = pOrder;
  41. lambda = pLambda;
  42. try {
  43. func = Class.forName("com.l2jserver.gameserver.skills.funcs.Func"+pFunc);
  44. } catch (ClassNotFoundException e) {
  45. throw new RuntimeException(e);
  46. }
  47. try {
  48. constructor = func.getConstructor(
  49. new Class[]{
  50. Stats.class, // stats to update
  51. Integer.TYPE, // order of execution
  52. Object.class, // owner
  53. Lambda.class // value for function
  54. });
  55. } catch (NoSuchMethodException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. public Func getFunc(Env env, Object owner)
  60. {
  61. if (attachCond != null && !attachCond.test(env))
  62. return null;
  63. try {
  64. Func f = (Func)constructor.newInstance(stat, order, owner, lambda);
  65. if (applayCond != null)
  66. f.setCondition(applayCond);
  67. return f;
  68. } catch (IllegalAccessException e) {
  69. e.printStackTrace();
  70. return null;
  71. } catch (InstantiationException e) {
  72. e.printStackTrace();
  73. return null;
  74. } catch (InvocationTargetException e) {
  75. e.printStackTrace();
  76. return null;
  77. }
  78. }
  79. }