index.js 5.5 KB

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