index.js 5.5 KB

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