ActiveEffectBase.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package me.km.effects;
  2. import me.km.KajetansMod;
  3. import me.km.environment.EnvironmentAPI;
  4. import me.km.snuviscript.ScriptEvents;
  5. import net.minecraft.entity.player.EntityPlayerMP;
  6. public abstract class ActiveEffectBase
  7. {
  8. protected abstract int getManaCost(int manaFactor);
  9. public int getCoolDown()
  10. {
  11. return 0;
  12. }
  13. public boolean run(EntityPlayerMP p, int power, int manaFactor, EffectCause cause)
  14. {
  15. if(cause != EffectCause.NOTHING)
  16. {
  17. if(KajetansMod.playerbank.getData(p).hasData("shadow"))
  18. {
  19. p.setInvisible(false);
  20. KajetansMod.playerbank.getData(p).removeData("shadow");
  21. }
  22. if(KajetansMod.playerbank.getData(p).hasData("silence"))
  23. {
  24. KajetansMod.effects.send(p, "Du kannst gerade keine Skills nutzen.");
  25. return false;
  26. }
  27. }
  28. int manaCost = getManaCost(manaFactor);
  29. // Effect-Event-Start
  30. PlayerUsesEffectEvent e = new PlayerUsesEffectEvent(p, power, manaCost, cause, this.getClass());
  31. KajetansMod.scripts.getEvent(ScriptEvents.class).useEffectEvent(e);
  32. if(e.isCanceled())
  33. {
  34. return false;
  35. }
  36. else
  37. {
  38. power = e.getPower();
  39. manaFactor = e.getMana();
  40. }
  41. // Effect-Event-End
  42. if(p.isCreative())
  43. {
  44. return executeEffect(p, power);
  45. }
  46. if(!KajetansMod.playerbank.getData(p).hasData(this.getClass().getSimpleName()))
  47. {
  48. if(manaFactor == 0)
  49. {
  50. if(executeEffect(p, power))
  51. {
  52. int cooldown = getCoolDown();
  53. if(cooldown > 0)
  54. {
  55. KajetansMod.playerbank.getData(p).addTimedData(this.getClass().getSimpleName(), cooldown);
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. int mana = EnvironmentAPI.getMana(p);
  62. if(mana < manaCost)
  63. {
  64. KajetansMod.effects.send(p, "Du hast zu wenig Mana. (§a" + mana + "§r/§c" + manaCost + "§r)");
  65. return false;
  66. }
  67. if(executeEffect(p, power))
  68. {
  69. int cooldown = getCoolDown();
  70. if(cooldown > 0)
  71. {
  72. KajetansMod.playerbank.getData(p).addTimedData(this.getClass().getSimpleName(), cooldown);
  73. }
  74. EnvironmentAPI.changeMana(p, -manaCost);
  75. return true;
  76. }
  77. return false;
  78. }
  79. KajetansMod.effects.send(p, "Der Cooldown läuft noch.");
  80. return false;
  81. }
  82. protected abstract boolean executeEffect(EntityPlayerMP p, int power);
  83. public static boolean executeEffect(Class<? extends ActiveEffectBase> c, EntityPlayerMP p, int power, int manaFactor, EffectCause cause)
  84. {
  85. if(c == null)
  86. {
  87. return false;
  88. }
  89. try
  90. {
  91. return c.newInstance().run(p, power, manaFactor, cause);
  92. }
  93. catch(SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException ex)
  94. {
  95. throw new IllegalArgumentException();
  96. }
  97. }
  98. }