Advertisement
Guest User

trader

a guest
Sep 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. --- unload trade, load trader //trade alexandrite
  2. _addon = {'Trader'}
  3. _addon.commands = {'Trade'}
  4.  
  5. require 'pack'
  6. require 'strings'
  7. require('logger')
  8. res_items = require('resources').items
  9.  
  10.  
  11. source_bag_id = 0
  12. dest_bag_id = 0
  13. item_id_raw = 0;
  14. item_id1 = 0
  15. item_id2 = 0
  16.  
  17. ------------------------------------
  18. -- Get item id
  19. ------------------------------------
  20. function get_item_res(item)
  21. item = windower.convert_auto_trans(item)
  22. for k,v in pairs(res_items) do
  23. if v.en:lower() == item:lower() or v.enl:lower() == item:lower() then
  24. return v
  25. end
  26. end
  27. return nil
  28. end
  29.  
  30. ------------------------------------
  31. -- Number to hex
  32. ------------------------------------
  33. function tohex(num)
  34. local hexstr = '0123456789abcdef'
  35. local s = ''
  36. while num > 0 do
  37. local mod = math.fmod(num, 16)
  38. s = string.sub(hexstr, mod+1, mod+1) .. s
  39. num = math.floor(num / 16)
  40. end
  41. if s == '' then s = '0' end
  42. return s
  43. end
  44.  
  45. function ConfirmTrade()
  46. windower.packets.inject_outgoing(0x033,string.char(0x34,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x80,0x40))
  47. end
  48.  
  49. function AcceptTrade()
  50. windower.packets.inject_outgoing(0x033,string.char(0x34,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40))
  51. end
  52.  
  53. ------------------------------------
  54. -- Trades items and accepts
  55. ------------------------------------
  56. function Trade()
  57. local trade_slot = 1
  58. local items = windower.ffxi.get_items()
  59. for k,v in pairs(items.inventory) do
  60. if type(v) == "table" and v.id == item_id_raw then
  61.  
  62. local hex_string = tohex(v.count)
  63. if string.len(hex_string) == 7 then
  64. hex_string = "0"..hex_string
  65. elseif string.len(hex_string) == 6 then
  66. hex_string = "00"..hex_string
  67. elseif string.len(hex_string) == 5 then
  68. hex_string = "000"..hex_string
  69. elseif string.len(hex_string) == 4 then
  70. hex_string = "0000"..hex_string
  71. elseif string.len(hex_string) == 3 then
  72. hex_string = "00000"..hex_string
  73. elseif string.len(hex_string) == 2 then
  74. hex_string = "000000"..hex_string
  75. elseif string.len(hex_string) == 1 then
  76. hex_string = "0000000"..hex_string
  77. end
  78. local count1 = tonumber(string.sub(hex_string, 7, 8), 16)
  79. local count2 = tonumber(string.sub(hex_string, 5, 6), 16)
  80. local count3 = tonumber(string.sub(hex_string, 3, 4), 16)
  81. local count4 = tonumber(string.sub(hex_string, 1, 2), 16)
  82.  
  83. windower.packets.inject_outgoing(0x034,string.char(0x34,0x00,0x00,0x00,count1,count2,count3,count4,item_id1,item_id2,v.slot,trade_slot))
  84. trade_slot = trade_slot + 1
  85.  
  86. if trade_slot == 9 then
  87. ConfirmTrade()
  88. return
  89. end
  90. end
  91. end
  92.  
  93. ConfirmTrade()
  94. end
  95.  
  96. ------------------------------------
  97. -- Called on startup
  98. ------------------------------------
  99. windower.register_event('load',function ()
  100. end)
  101.  
  102. ------------------------------------
  103. -- Called onan incoming packet
  104. ------------------------------------
  105. windower.register_event('incoming chunk', function(id,original,modified,injected,blocked)
  106. if id == 0x021 then
  107. AcceptTrade()
  108. end
  109. end)
  110.  
  111. ------------------------------------
  112. -- Called when given the command
  113. ------------------------------------
  114. windower.register_event('addon command', function (...)
  115. args = {...};
  116.  
  117. local item_name = table.concat(args, ' ',1,#args):lower()
  118. item_id_raw = get_item_res(item_name).id
  119.  
  120. local hex_string = tohex(item_id_raw)
  121. if string.len(hex_string) == 3 then
  122. hex_string = "0"..hex_string
  123. elseif string.len(hex_string) == 2 then
  124. hex_string = "00"..hex_string
  125. elseif string.len(hex_string) == 1 then
  126. hex_string = "000"..hex_string
  127. end
  128. item_id1 = tonumber(string.sub(hex_string, 3, 4), 16)
  129. item_id2 = tonumber(string.sub(hex_string, 1, 2), 16)
  130.  
  131. coroutine.schedule(Trade,0)
  132. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement