index.js 5.0 KB

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