index.js 4.6 KB

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