index.js 5.1 KB

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