index.js 5.1 KB

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