index.js 5.3 KB

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