index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict'
  2. const ITEMS_NOSTRUM = [152898, 184659, 201005, 201006, 201007, 201008, 201022, 855604], // EU, NA, RU, TW, ?, ?, JP , TH
  3. BUFF_NOSTRUM = [4020, 4021, 4022, 4023, 4030, 4031, 4032, 4044], // Nostrum abnormalities
  4. BUFF_CCB = [4610, 4611, 4612, 4613, 4615, 4616, 4950, 5000003, 5020003], // Complete Crystalbind abnormalities
  5. BUFF_INVINCIBILITY = [1134, 6007] // Invincibility abnormalities on resurrection
  6. module.exports = function Essentials(mod) {
  7. mod.game.initialize(['me', 'me.abnormalities', 'contract']);
  8. let item = null,
  9. interval = null,
  10. enabled = true,
  11. counter = 0,
  12. resetcount = null
  13. // ############# //
  14. // ### Hooks ### //
  15. // ############# //
  16. mod.game.on('enter_game', () => { setTimeout(start, 6000) })
  17. mod.game.on('leave_game', () => { stop() })
  18. mod.game.me.on('resurrect', () => { start() })
  19. mod.hook('S_PREMIUM_SLOT_DATALIST', 2, event => {
  20. for(let set of event.sets)
  21. for(let entry of set.inventory)
  22. if(ITEMS_NOSTRUM.includes(entry.id)) {
  23. item = {
  24. set: set.id,
  25. slot: entry.slot,
  26. type: entry.type,
  27. id: entry.id
  28. }
  29. entry.cooldown = 0n // Cooldowns from this packet don't seem to do anything except freeze your client briefly
  30. return true
  31. }
  32. })
  33. if(mod.settings.log) {
  34. mod.hook('C_USE_ITEM', 3, event => {
  35. mod.command.message('Used item ID: ' + event.id)
  36. })
  37. }
  38. // ################# //
  39. // ### Functions ### //
  40. // ################# //
  41. function abnormalityDuration(id) {
  42. const abnormality = mod.game.me.abnormalities[id]
  43. return abnormality ? abnormality.remaining : 0
  44. }
  45. function checkItems() {
  46. for(let buff of BUFF_INVINCIBILITY) // Do not overwrite invincibility buff
  47. if(abnormalityDuration(buff) > 0) return
  48. useNostrum()
  49. useCCB()
  50. }
  51. function useNostrum() {
  52. for(let buff of BUFF_NOSTRUM) // Use Nostrum only when less than nostrumTime
  53. if(abnormalityDuration(buff) > mod.settings.nostrumTime * 60000 || !mod.settings.useNostrum) return
  54. if(!mod.game.isIngame || mod.game.isInLoadingScreen || !mod.game.me.alive || mod.game.me.mounted || mod.game.me.inBattleground || mod.game.contract.active) return
  55. if(!mod.game.me.inDungeon && mod.settings.dungeonOnly) return
  56. if(enabled) {
  57. if(item) mod.send('C_USE_PREMIUM_SLOT', 1, item)
  58. else useItem(mod.settings.nostrum)
  59. }
  60. }
  61. function useCCB() {
  62. for(let buff of BUFF_CCB) // Use CCB only when less than CCBTime
  63. if(abnormalityDuration(buff) > mod.settings.CCBTime * 60000 || !mod.settings.useCCB) return
  64. if(!mod.game.isIngame || mod.game.isInLoadingScreen || !mod.game.me.alive || mod.game.me.mounted || mod.game.me.inBattleground || mod.game.contract.active) return
  65. if(!mod.game.me.inDungeon && mod.settings.dungeonOnly) return
  66. if(enabled) useItem(mod.settings.ccb)
  67. }
  68. function useItem(item) {
  69. counter++
  70. if(counter > 5) {
  71. let missing = (item == mod.settings.nostrum) ? 'Nostrums' : 'Crystalbinds'
  72. enabled = false
  73. mod.command.message('You ran out of ' + missing + ' (ID: ' + item + '). Essentials has been disabled. Please restock and enable the module again by typing "ess" in this chat.')
  74. console.log('[Essentials] You ran out of ' + missing + ' (ID: ' + item + '). Essentials has been disabled. Please restock and enable the module again by typing "ess" in proxy chat.')
  75. return
  76. }
  77. if(!resetcount) resetcount = setTimeout(() => { counter = 0; resetcount = null }, 15000)
  78. mod.toServer('C_USE_ITEM', 3, {
  79. gameId: mod.game.me.gameId,
  80. id: item,
  81. dbid: 0,
  82. target: 0,
  83. amount: 1,
  84. dest: {x: 0, y: 0, z: 0},
  85. loc: {x: 0, y: 0, z: 0},
  86. w: 0,
  87. unk1: 0,
  88. unk2: 0,
  89. unk3: 0,
  90. unk4: 1
  91. })
  92. }
  93. function start() {
  94. stop()
  95. interval = setInterval(checkItems, 1500)
  96. }
  97. function stop() {
  98. if (interval) {
  99. clearInterval(interval)
  100. interval = null
  101. }
  102. }
  103. // ################ //
  104. // ### Commands ### //
  105. // ################ //
  106. mod.command.add(['essentials', 'ess'], (cmd) => {
  107. if(cmd == null) {
  108. enabled = !enabled
  109. mod.command.message('Essentials ' + (enabled ? '<font color="#56B4E9">enabled</font>' : '<font color="#E69F00">disabled</font>'))
  110. console.log('Essentials ' + (enabled ? 'enabled' : 'disabled'))
  111. }
  112. else if(cmd == "dungeon" || cmd == "dungeons" || cmd == "dung") {
  113. mod.settings.dungeonOnly = !mod.settings.dungeonOnly
  114. mod.command.message('Items will be used ' + (mod.settings.dungeonOnly ? 'everywhere' : 'in dungeons only'))
  115. }
  116. else mod.command.message('Commands:\n'
  117. + ' "ess" (enable/disable Essentials),\n'
  118. + ' "ess dungeon" (switch between using items everywhere or only in dungeons)'
  119. )
  120. })
  121. }