index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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("contract")
  8. let item = null,
  9. interval = null,
  10. enabled = true,
  11. abnormalities = {},
  12. counter = 0,
  13. resetcount = null,
  14. niceName = mod.proxyAuthor !== 'caali' ? '[Essentials] ' : ''
  15. // ############# //
  16. // ### Hooks ### //
  17. // ############# //
  18. mod.game.on('enter_game', () => { setTimeout(start, 6000) })
  19. mod.game.on('leave_game', () => {
  20. stop()
  21. abnormalities = {}
  22. })
  23. mod.game.me.on('resurrect', () => {
  24. abnormalities = {}
  25. start()
  26. })
  27. if(mod.majorPatchVersion >= 82) {
  28. mod.hook('S_PREMIUM_SLOT_DATALIST', 2, event => {
  29. for(let set of event.sets)
  30. for(let entry of set.inventory)
  31. if(ITEMS_NOSTRUM.includes(entry.id)) {
  32. item = {
  33. set: set.id,
  34. slot: entry.slot,
  35. type: entry.type,
  36. id: entry.id
  37. }
  38. entry.cooldown = 0n // Cooldowns from this packet don't seem to do anything except freeze your client briefly
  39. return true
  40. }
  41. })
  42. }
  43. else {
  44. mod.hook('S_PCBANGINVENTORY_DATALIST', 1, event => {
  45. for(let entry of event.inventory)
  46. if(ITEMS_NOSTRUM.includes(entry.item)) {
  47. item = { slot: entry.slot }
  48. entry.cooldown = 0 // Cooldowns from this packet don't seem to do anything except freeze your client briefly
  49. return true
  50. }
  51. })
  52. }
  53. mod.hook('S_ABNORMALITY_BEGIN', 3, abnormality.bind(null, 'S_ABNORMALITY_BEGIN'))
  54. mod.hook('S_ABNORMALITY_REFRESH', 1, abnormality.bind(null, 'S_ABNORMALITY_REFRESH'))
  55. mod.hook('S_ABNORMALITY_END', 1, abnormality.bind(null, 'S_ABNORMALITY_END'))
  56. if(mod.settings.log) {
  57. mod.hook('C_USE_ITEM', 3, event => {
  58. mod.command.message(niceName + 'Used item ID: ' + event.id)
  59. })
  60. }
  61. // ################# //
  62. // ### Functions ### //
  63. // ################# //
  64. function abnormality(type, event) {
  65. if(mod.game.me.is(event.target)) {
  66. if(type === 'S_ABNORMALITY_END')
  67. delete abnormalities[event.id]
  68. else abnormalities[event.id] = Date.now() + event.duration
  69. }
  70. }
  71. function abnormalityDuration(id) {
  72. if(!abnormalities[id]) return 0
  73. return abnormalities[id] - Date.now()
  74. }
  75. function checkItems() {
  76. for(let buff of BUFF_INVINCIBILITY) // Do not overwrite invincibility buff
  77. if(abnormalityDuration(buff) > 0) return
  78. useNostrum()
  79. useCCB()
  80. }
  81. function useNostrum() {
  82. for(let buff of BUFF_NOSTRUM) // Use Nostrum only when less than nostrumTime
  83. if(abnormalityDuration(buff) > mod.settings.nostrumTime * 60000 || !mod.settings.useNostrum) return
  84. if(!mod.game.isIngame || mod.game.isInLoadingScreen || !mod.game.me.alive || mod.game.me.mounted || mod.game.me.inBattleground || mod.game.contract.active) return
  85. if(mod.game.me.zone < 9000 && mod.settings.dungeonOnly) return
  86. if(enabled) {
  87. if(item) {
  88. if(mod.majorPatchVersion >= 82) mod.send('C_USE_PREMIUM_SLOT', 1, item)
  89. else mod.send('C_PCBANGINVENTORY_USE_SLOT', 1, item)
  90. }
  91. else useItem(mod.settings.nostrum)
  92. }
  93. }
  94. function useCCB() {
  95. for(let buff of BUFF_CCB) // Use CCB only when less than CCBTime
  96. if(abnormalityDuration(buff) > mod.settings.CCBTime * 60000 || !mod.settings.useCCB) return
  97. if(!mod.game.isIngame || mod.game.isInLoadingScreen || !mod.game.me.alive || mod.game.me.mounted || mod.game.me.inBattleground || mod.game.contract.active) return
  98. if(mod.game.me.zone < 9000 && mod.settings.dungeonOnly) return
  99. if(enabled) useItem(mod.settings.ccb)
  100. }
  101. function useItem(item) {
  102. counter++
  103. if(counter > 5) {
  104. let missing = (item == mod.settings.nostrum) ? 'Nostrums' : 'Crystalbinds'
  105. enabled = false
  106. mod.command.message(niceName + 'You ran out of ' + missing + ' (ID: ' + item + '). Essentials has been disabled. Please restock and enable the module again by typing "ess" in this chat.')
  107. 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.')
  108. return
  109. }
  110. if(!resetcount) resetcount = setTimeout(() => { counter = 0; resetcount = null }, 15000)
  111. mod.toServer('C_USE_ITEM', 3, {
  112. gameId: mod.game.me.gameId,
  113. id: item,
  114. dbid: 0,
  115. target: 0,
  116. amount: 1,
  117. dest: {x: 0, y: 0, z: 0},
  118. loc: {x: 0, y: 0, z: 0},
  119. w: 0,
  120. unk1: 0,
  121. unk2: 0,
  122. unk3: 0,
  123. unk4: 1
  124. })
  125. }
  126. function start() {
  127. stop()
  128. interval = setInterval(checkItems, 1500)
  129. }
  130. function stop() {
  131. if (interval) {
  132. clearInterval(interval)
  133. interval = null
  134. }
  135. }
  136. // ################ //
  137. // ### Commands ### //
  138. // ################ //
  139. mod.command.add(['essentials', 'ess'], (cmd) => {
  140. if(cmd == null) {
  141. enabled = !enabled
  142. mod.command.message(niceName + 'Essentials ' + (enabled ? '<font color="#56B4E9">enabled</font>' : '<font color="#E69F00">disabled</font>'))
  143. console.log('Essentials ' + (enabled ? 'enabled' : 'disabled'))
  144. }
  145. else if(cmd == "dungeon" || cmd == "dungeons" || cmd == "dung") {
  146. mod.settings.dungeonOnly = !mod.settings.dungeonOnly
  147. mod.command.message(niceName + 'Items will be used ' + (mod.settings.dungeonOnly ? 'everywhere' : 'in dungeons only'))
  148. }
  149. else mod.command.message('Commands:\n'
  150. + ' "ess" (enable/disable Essentials),\n'
  151. + ' "ess dungeon" (switch between using items everywhere or only in dungeons)'
  152. )
  153. })
  154. }