ActiveEffectBase.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package me.km.effects;
  2. import me.km.KajetansMod;
  3. import me.km.environment.EnvironmentAPI;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. public abstract class ActiveEffectBase
  6. {
  7. protected abstract int getManaCost(int manaFactor);
  8. public int getCoolDown()
  9. {
  10. return 0;
  11. }
  12. public boolean run(EntityPlayer p, int power, int manaFactor, EffectCause cause)
  13. {
  14. if(cause != EffectCause.NOTHING)
  15. {
  16. if(KajetansMod.playerbank.getData(p).hasData("shadow"))
  17. {
  18. p.setInvisible(false);
  19. KajetansMod.playerbank.getData(p).removeData("shadow");
  20. }
  21. if(KajetansMod.playerbank.getData(p).hasData("silence"))
  22. {
  23. KajetansMod.effects.send(p, "Du kannst gerade keine Skills nutzen.");
  24. return false;
  25. }
  26. }
  27. int manaCost = getManaCost(manaFactor);
  28. // Effect-Event-Start
  29. PlayerUsesEffectEvent e = new PlayerUsesEffectEvent(p, power, manaCost, cause, this.getClass());
  30. // TODO send pseudo event to SnuviScript
  31. //Bukkit.getPluginManager().callEvent(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(EntityPlayer p, int power);
  83. public static boolean executeEffect(Class<? extends ActiveEffectBase> c, EntityPlayer 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. }