index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Contains code from true-everful-nostrum by Pinkie Pie https://github.com/pinkipi
  2. const Command = require('command')
  3. const ITEMS_NOSTRUM = 184659, // EU 152898, NA 184659, RU 201005
  4. BUFF_NOSTRUM_TD = 4030,
  5. BUFF_NOSTRUM_H = 4031,
  6. BUFF_CCB = 4610,
  7. RANDOM_MIN_MAX = [600000, 1500000], // Random Nostrum reapplication time between 10 and 25 minutes
  8. RANDOM_SHORT = [15000, 20000] // Random Nostrum reapplication time after loading screen between 15 and 20 seconds
  9. module.exports = function Essentials(dispatch) {
  10. let cid = null,
  11. player = '',
  12. slot = -1,
  13. timeout = null,
  14. timeoutCCB = null,
  15. cooldown = 0,
  16. nextUse = 0,
  17. bgZone = -1,
  18. alive = false,
  19. mounted = false,
  20. inContract = false,
  21. inBG = false,
  22. hasccb = false,
  23. enabled = true
  24. dispatch.hook('S_LOGIN', 1, event => {
  25. ({cid} = event)
  26. player = event.name
  27. nextUse = Date.now() + randomNumber(RANDOM_SHORT)
  28. setTimeout(ccb, 20000) // check if you have a CCB 20 seconds after login
  29. })
  30. dispatch.hook('S_RETURN_TO_LOBBY', 1, event => { nostrum(true) })
  31. dispatch.hook('S_PCBANGINVENTORY_DATALIST', 1, event => {
  32. for(let item of event.inventory)
  33. if(ITEMS_NOSTRUM == item.item) {
  34. slot = item.slot
  35. if(item.cooldown) cooldown = Date.now() + item.cooldown
  36. item.cooldown = 0 // Cooldowns from this packet don't seem to do anything except freeze your client briefly
  37. return true
  38. }
  39. })
  40. dispatch.hook('S_ABNORMALITY_BEGIN', 1, abnormality.bind(null, 'S_ABNORMALITY_BEGIN'))
  41. dispatch.hook('S_ABNORMALITY_REFRESH', 1, abnormality.bind(null, 'S_ABNORMALITY_REFRESH'))
  42. dispatch.hook('S_ABNORMALITY_END', 1, abnormality.bind(null, 'S_ABNORMALITY_END'))
  43. dispatch.hook('S_BATTLE_FIELD_ENTRANCE_INFO', 1, event => { bgZone = event.zone })
  44. dispatch.hook('S_LOAD_TOPO', 1, event => {
  45. nextUse = Date.now() + randomNumber(RANDOM_SHORT)
  46. mounted = inContract = false
  47. inBG = event.zone == bgZone
  48. nostrum(true)
  49. })
  50. dispatch.hook('S_SPAWN_ME', 1, event => {
  51. nostrum(!(alive = event.alive))
  52. })
  53. dispatch.hook('S_CREATURE_LIFE', 1, event => {
  54. if(event.target.equals(cid) && alive != event.alive) {
  55. setTimeout(function () {
  56. nostrum(!(alive = event.alive))
  57. }, 2000)
  58. if(!alive) {
  59. nextUse = 0
  60. mounted = inContract = false
  61. }
  62. }
  63. })
  64. dispatch.hook('S_MOUNT_VEHICLE', 1, mount.bind(null, true))
  65. dispatch.hook('S_UNMOUNT_VEHICLE', 1, mount.bind(null, false))
  66. dispatch.hook('S_REQUEST_CONTRACT', 1, contract.bind(null, true))
  67. dispatch.hook('S_ACCEPT_CONTRACT', 1, contract.bind(null, false))
  68. dispatch.hook('S_REJECT_CONTRACT', 1, contract.bind(null, false))
  69. dispatch.hook('S_CANCEL_CONTRACT', 1, contract.bind(null, false))
  70. function abnormality(type, event) {
  71. if(event.target.equals(cid) && (event.id == BUFF_NOSTRUM_TD || event.id == BUFF_NOSTRUM_H)) {
  72. if (type == 'S_ABNORMALITY_END') {
  73. nextUse = 0
  74. nostrum()
  75. }
  76. }
  77. if(event.target.equals(cid) && (event.id == BUFF_CCB)) {
  78. if (type == 'S_ABNORMALITY_BEGIN') {
  79. hasccb = true
  80. }
  81. else if (type == 'S_ABNORMALITY_END') {
  82. hasccb = false
  83. ccb()
  84. }
  85. }
  86. }
  87. function mount(enter, event) {
  88. if(event.target.equals(cid)) nostrum(mounted = enter)
  89. }
  90. function contract(enter) {
  91. nostrum(inContract = enter)
  92. }
  93. function nostrum(disable) {
  94. clearTimeout(timeout)
  95. if(!disable && alive && !mounted && !inContract && !inBG && slot != -1) {
  96. timeout = setTimeout(useNostrum, nextUse - Date.now())
  97. }
  98. }
  99. function ccb() {
  100. clearTimeout(timeoutCCB)
  101. if(!hasccb && alive && !mounted && !inContract && !inBG) useCCB()
  102. else if(!hasccb) timeoutCCB = setTimeout(ccb, 10000) // retry in 10 seconds if you were busy
  103. }
  104. function useNostrum() {
  105. if(!enabled) return
  106. let time = Date.now()
  107. if(time >= cooldown) {
  108. dispatch.toServer('C_PCBANGINVENTORY_USE_SLOT', 1, {slot})
  109. nextUse = Date.now() + randomNumber(RANDOM_MIN_MAX)
  110. nostrum()
  111. }
  112. else timeout = setTimeout(useNostrum, cooldown - time)
  113. }
  114. function useCCB() {
  115. if(!enabled) return
  116. dispatch.toServer('C_USE_ITEM', 1, {
  117. ownerId: cid,
  118. item: 70000,
  119. id: 0,
  120. unk1: 0,
  121. unk2: 0,
  122. unk3: 0,
  123. unk4: 1,
  124. unk5: 0,
  125. unk6: 0,
  126. unk7: 0,
  127. x: 0,
  128. y: 0,
  129. z: 0,
  130. w: 0,
  131. unk8: 0,
  132. unk9: 0,
  133. unk10: 0,
  134. unk11: 1
  135. })
  136. }
  137. function randomNumber([min, max]) {
  138. return Math.floor(Math.random() * (max - min + 1) + min)
  139. }
  140. // ################# //
  141. // ### Chat Hook ### //
  142. // ################# //
  143. dispatch.hook('C_WHISPER', 1, (event) => {
  144. if(event.target.toUpperCase() === "!essentials".toUpperCase()) {
  145. if (/^<FONT>on?<\/FONT>$/i.test(event.message)) {
  146. enabled = true
  147. message('Essentials <font color="#56B4E9">enabled</font>.')
  148. }
  149. else if (/^<FONT>off?<\/FONT>$/i.test(event.message)) {
  150. enabled = false
  151. message('Essentials <font color="#E69F00">disabled</font>.')
  152. }
  153. else message('Commands:<br>'
  154. + ' "on" (enable Essentials),<br>'
  155. + ' "off" (disable Essentials)'
  156. )
  157. return false
  158. }
  159. })
  160. function message(msg) {
  161. dispatch.toClient('S_WHISPER', 1, {
  162. player: cid,
  163. unk1: 0,
  164. gm: 0,
  165. unk2: 0,
  166. author: '!Essentials',
  167. recipient: player,
  168. message: msg
  169. })
  170. }
  171. dispatch.hook('C_CHAT', 1, event => {
  172. if(/^<FONT>!essentials<\/FONT>$/i.test(event.message)) {
  173. if(!enabled) {
  174. enabled = true
  175. message('Essentials <font color="#56B4E9">enabled</font>.')
  176. console.log('Essentials enabled.')
  177. }
  178. else {
  179. enabled = false
  180. message('Essentials <font color="#E69F00">disabled</font>.')
  181. console.log('Essentials disabled.')
  182. }
  183. return false
  184. }
  185. })
  186. const command = Command(dispatch)
  187. command.add('essentials', function() {
  188. if(!enabled) {
  189. enabled = true
  190. message('Essentials <font color="#56B4E9">enabled</font>.')
  191. console.log('Essentials enabled.')
  192. }
  193. else {
  194. enabled = false
  195. message('Essentials <font color="#E69F00">disabled</font>.')
  196. console.log('Essentials disabled.')
  197. }
  198. })
  199. }