Guest User

Untitled

a guest
Feb 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. package mnm.plugins.rpg
  2.  
  3. import mnm.plugins.rpg.api.ability.Ability
  4. import mnm.plugins.rpg.api.ability.ClassProfile
  5. import mnm.plugins.rpg.api.event.impl.RPGEventFactory
  6. import mnm.plugins.rpg.command.CastCommand
  7. import org.spongepowered.api.entity.living.player.Player
  8. import org.spongepowered.api.scheduler.Task
  9.  
  10. class AbilityManager(plugin: RPGPlugin) {
  11.  
  12. private val queue = mutableListOf<AbilityTask>()
  13.  
  14. init {
  15. sponge.commandManager.register(plugin, CastCommand.spec, "cast")
  16.  
  17. Task.builder()
  18. .intervalTicks(1)
  19. .execute(::run)
  20. .submit(plugin)
  21. }
  22.  
  23. fun activateAbility(player: Player, profile: ClassProfile, ability: Ability) {
  24. sponge.causeStackManager.pushCauseFrame().use {
  25. val event = RPGEventFactory.createAbilityEventStart(it.currentCause, ability, player, 0)
  26. if (!event.isCancelled) {
  27. profile.mana -= ability.manaCost
  28. profile.stamina -= ability.staminaCost
  29.  
  30. if (event.remainingTicks > 0)
  31. // add the task to the queue to be completed next tick.
  32. queue += AbilityTask(player, profile, ability, event.remainingTicks)
  33. else
  34. sponge.causeStackManager.pushCauseFrame().use {
  35. it.pushCause(event)
  36. sponge.eventManager.post(RPGEventFactory.createAbilityEventStop(it.currentCause, ability, player))
  37. }
  38. }
  39. }
  40. }
  41.  
  42. private fun run() {
  43. // remove any players which don't exist
  44. queue.filterNot { it.player.isRemoved }
  45. queue.removeAll { data ->
  46. sponge.causeStackManager.pushCauseFrame().use {
  47. val event = RPGEventFactory.createAbilityEventTick(it.currentCause, data.ability, data.player, data.ticks - 1)
  48.  
  49. // remove any completed abilities from the queue
  50. if (event.remainingTicks <= 0) {
  51. // send the stop event
  52. sponge.causeStackManager.pushCauseFrame().use {
  53. it.pushCause(event)
  54. sponge.eventManager.post(RPGEventFactory.createAbilityEventStop(it.currentCause, data.ability, data.player))
  55. }
  56. return@removeAll true
  57. }
  58. // reassign the ticks to the task
  59. data.ticks = event.remainingTicks
  60. }
  61. false
  62. }
  63.  
  64. }
  65.  
  66. private data class AbilityTask(
  67. val player: Player,
  68. val profile: ClassProfile,
  69. val ability: Ability,
  70. var ticks: Int)
  71.  
  72. }
Add Comment
Please, Sign In to add comment