Advertisement
Guest User

modmain.lua

a guest
Nov 22nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.31 KB | None | 0 0
  1. local KEY_ALERT = GetModConfigData("Key_Alert")
  2. local ICON_HEAL = GetModConfigData("Icon_Heal")
  3. local ICON_METEOR = GetModConfigData("Icon_Meteor")
  4. local ICON_PETRIFY = GetModConfigData("Icon_Petrify")
  5. local ICON_DEAD = GetModConfigData("Icon_Dead")
  6. local LANGUAGE = GetModConfigData("Language")
  7. local DISABLE_ICONS = GetModConfigData("Disable_Icons")
  8. local KEY_REFRESH = GetModConfigData("Key_Refresh")
  9.  
  10. local Player
  11. local Widget = GLOBAL.require("widgets/widget")
  12. local Image = GLOBAL.require("widgets/image")
  13. local ImageButton = GLOBAL.require("widgets/imagebutton")
  14. local Button = GLOBAL.require("widgets/button")
  15.  
  16. --[[local ICONS = {
  17.     ICON_HEAL,
  18.     ICON_METEOR,
  19.     ICON_PETRIFY,
  20.     ICON_DEAD
  21. }]]--
  22.  
  23. local cantButtons = 2
  24.  
  25. local button = {}
  26. local icon_button = {}
  27. local actual_item = nil
  28.  
  29. local button_order = {1,2}
  30.  
  31. local player_dead = false
  32.  
  33. local finish_init = false
  34.  
  35. local default_icon = {
  36.     "blowdart_lava",
  37.     "skull_wes"
  38. }
  39.  
  40. local weapons = {
  41.     --"blowdart_lava",
  42.     "healingstaff",
  43.     "fireballstaff",
  44.     "book_fossil"
  45.     --[["spear",
  46.     "axe",
  47.     "pickaxe",
  48.     "shovel"]]--
  49. }
  50.  
  51. local lang = {}
  52.  
  53. lang["en"] = 1
  54. lang["es"] = 2
  55. lang["chs"] = 3
  56. lang["cht"] = 4
  57.  
  58. local heal_phrase = {
  59.     "HEAL (DON'T ATTACK)",
  60.     "VOY A CURAR (NO ATAQUEN)",
  61.     "治疗术(停止攻击)",
  62.     "治療術(停止攻擊)"
  63. }
  64.  
  65. local meteor_phrase = {
  66.     "METEOR",
  67.     "METEORITO",
  68.     "陨石!",
  69.     "陨石!"
  70. }
  71.  
  72. local petrify_phrase = {
  73.     "PETRIFY (DON'T ATTACK)",
  74.     "PETRIFICARE (NO ATAQUEN)",
  75.     "石化术(请勿攻击)",
  76.     "石化術(請勿攻擊)"
  77. }
  78.  
  79. local dead_phrase = {
  80.     "NEED RESURRECTION",
  81.     "RESUCITENME",
  82.     "请求复活",
  83.     "請求復活"
  84. }
  85.  
  86. if (DISABLE_ICONS == "true") then
  87.     ICON_HEAL = ""
  88.     ICON_METEOR = ""
  89.     ICON_PETRIFY = ""
  90.     ICON_DEAD = ""
  91. else
  92.     if (ICON_HEAL ~= "false") then
  93.         ICON_HEAL = ":"..ICON_HEAL..":"
  94.     else
  95.         ICON_HEAL = ""
  96.     end
  97.    
  98.     if (ICON_METEOR ~= "false") then
  99.         ICON_METEOR = ":"..ICON_METEOR..":"
  100.     else
  101.         ICON_METEOR = ""
  102.     end
  103.    
  104.     if (ICON_PETRIFY ~= "false") then
  105.         ICON_PETRIFY = ":"..ICON_PETRIFY..":"
  106.     else
  107.         ICON_PETRIFY = ""
  108.     end
  109.    
  110.     if (ICON_DEAD ~= "false") then
  111.         ICON_DEAD = ":"..ICON_DEAD..":"
  112.     else
  113.         ICON_DEAD = ""
  114.     end
  115. end
  116.  
  117. local function IsInItemGroup(item,group)
  118.     for i,v in pairs(group) do
  119.         if (item ~= nil and v == item) then
  120.             return true
  121.         end
  122.     end
  123.     return false
  124. end
  125.  
  126. local function SendAlert(index)
  127.     --GLOBAL.TheNet:Say(tostring(Player.replica.health:IsDead()),true)
  128.     if (not Player.replica.health:IsDead()) then
  129.         --GLOBAL.TheNet:Say(tostring(actual_item),true)
  130.         if (actual_item ~= nil) then
  131.             if (actual_item.prefab == "healingstaff") then
  132.                 GLOBAL.TheNet:Say(ICON_HEAL.." "..heal_phrase[lang[LANGUAGE]],false)
  133.             elseif (actual_item.prefab == "fireballstaff") then
  134.                 GLOBAL.TheNet:Say(ICON_METEOR.." "..meteor_phrase[lang[LANGUAGE]],false)
  135.             elseif (actual_item.prefab == "book_fossil") then
  136.                 GLOBAL.TheNet:Say(ICON_PETRIFY.." "..petrify_phrase[lang[LANGUAGE]],false)
  137.             end
  138.         end
  139.     else
  140.         GLOBAL.TheNet:Say(ICON_DEAD.." "..dead_phrase[lang[LANGUAGE]],false)
  141.     end
  142. end
  143.  
  144. local function IsInGroup(item,group)
  145.     if (item ~= nil) then
  146.         for i,v in pairs(group) do
  147.             if (v == item.prefab) then
  148.                 return true
  149.             end
  150.         end
  151.     end
  152.     return false
  153. end
  154.  
  155. local function IsItemEquipped(item)
  156.     return IsInItemGroup(item, Player.replica.inventory:GetEquips())
  157. end
  158.  
  159. local function ChangeButtonIconItem(index,item)
  160.     if (item ~= nil) then
  161.         if (icon_button[index] ~= nil and button[index] ~= nil) then
  162.             button[index]:RemoveChild(icon_button[index])
  163.             icon_button[index]:Kill()
  164.  
  165.             icon_button[index] = Image(item.replica.inventoryitem:GetAtlas(),item.replica.inventoryitem:GetImage())
  166.             icon_button[index]:SetScale(0.8,0.8,0.8)
  167.             button[index]:AddChild(icon_button[index])
  168.         end
  169.     end
  170. end
  171.  
  172. local function ChangeButtonIcon(index)
  173.     if (icon_button[index] ~= nil and button[index] ~= nil) then
  174.         icon_button[index]:SetTint(1,1,1,1)
  175.     end
  176. end
  177.  
  178. local function CheckButtonItem(item)
  179.     if (not Player.replica.health:IsDead()) then
  180.         if (IsInGroup(item,weapons)) then
  181.             actual_item = item
  182.             ChangeButtonIconItem(1,actual_item)
  183.             button[1]:Show()
  184.         end
  185.     end
  186. end
  187.  
  188. local function CheckButton()
  189.     if (Player.replica.health:IsDead()) then
  190.         ChangeButtonIcon(2)
  191.         button[2]:Show()
  192.     end
  193. end
  194.  
  195. local function ClearButton(index)
  196.     actual_item = nil
  197.     if (icon_button[index] ~= nil and button[index] ~= nil) then
  198.         button[index]:RemoveChild(icon_button[index])
  199.         icon_button[index]:Kill()
  200.        
  201.         if (default_icon[index] ~= nil) then
  202.             icon_button[index] = Image("images/inventoryimages.xml",default_icon[index]..".tex")
  203.         else
  204.             icon_button[index] = Image("images/inventoryimages.xml","spear.tex")
  205.         end
  206.         icon_button[index]:SetScale(0.8,0.8,0.8)
  207.         icon_button[index]:SetTint(0,0,0,0.7)
  208.         button[index]:AddChild(icon_button[index])
  209.         button[index]:Hide()
  210.     end
  211. end
  212.  
  213. local function ClearAllButton()
  214.     for i=1, cantButtons do
  215.         ClearButton(i)
  216.     end
  217. end
  218.  
  219. local function CheckAllButton()
  220.     if (finish_init) then
  221.         ClearAllButton()
  222.         for i,v in pairs(Player.replica.inventory:GetEquips()) do
  223.             CheckButtonItem(v)
  224.         end
  225.         if (Player.replica.inventory:GetActiveItem() ~= nil) then
  226.             CheckButtonItem(Player.replica.inventory:GetActiveItem())
  227.         end
  228.         CheckButton()
  229.     end
  230. end
  231.  
  232. local function InventoryEvents(inst)
  233.     --EQUIP EVENT--
  234.     inst:ListenForEvent("equip", function(inst, data)
  235.         --GLOBAL.TheNet:Say("equip, "..tostring(data.item),true)
  236.         CheckAllButton()
  237.     end)
  238.     --UNEQUIP EVENT--
  239.     inst:ListenForEvent("unequip", function(inst, data)
  240.         --GLOBAL.TheNet:Say("unequip",true)
  241.         CheckAllButton()
  242.     end)
  243.     --ATTACKED EVENT--
  244.     inst:ListenForEvent("healthdelta", function(inst, data)
  245.         --GLOBAL.TheNet:Say("healthdelta",true)
  246.         if (Player.replica.health:IsDead()) then
  247.             --GLOBAL.TheNet:Say("dead",true)
  248.             --player_dead = true
  249.             CheckAllButton()
  250.         end
  251.     end)
  252.    
  253.     --[[inst:ListenForEvent("healthdelta", function(inst, data)
  254.         GLOBAL.TheNet:Say("healthdelta",true)
  255.         if (Player.replica.health:IsDead()) then
  256.             --GLOBAL.TheNet:Say("dead",true)
  257.             player_dead = true
  258.             CheckAllButton()
  259.         else
  260.             player_dead = false
  261.             CheckAllButton()
  262.         end
  263.     end)]]--
  264. end
  265.  
  266. local function AddKeybindButton(index)
  267.     button[index] = Player.HUD.controls.bottom_root:AddChild(ImageButton("images/hud.xml","inv_slot_spoiled.tex","inv_slot.tex","inv_slot_spoiled.tex","inv_slot_spoiled.tex","inv_slot_spoiled.tex"))
  268.    
  269.     local x = 0--68*(button_order[index]-1)
  270.  
  271.     button[index]:SetScale(0.8,0.8,0.8)
  272.     button[index]:SetPosition(x,150,0)
  273.     button[index]:SetOnClick(function(inst) return SendAlert() end)
  274.     button[index]:MoveToFront()
  275.    
  276.     if (default_icon[index] ~= nil) then
  277.         icon_button[index] = Image("images/inventoryimages.xml",default_icon[index]..".tex")
  278.     else
  279.         icon_button[index] = Image("images/inventoryimages.xml","spear.tex")
  280.     end
  281.     icon_button[index]:SetScale(0.8,0.8,0.8)
  282.     icon_button[index]:SetTint(0,0,0,0.7)
  283.     button[index]:AddChild(icon_button[index])
  284. end
  285.  
  286. local function InitKeybindButtons()
  287.     for i=1, cantButtons do
  288.         icon_button[i] = nil
  289.     end
  290.    
  291.     AddKeybindButton(1)
  292.     AddKeybindButton(2)
  293.    
  294.     finish_init = true
  295. end
  296. --AddClassPostConstruct("widgets/inventorybar", InitKeybindButtons)
  297.  
  298. local function Init(inst)
  299.     inst:DoTaskInTime(1,function()
  300.         Player = GLOBAL.ThePlayer
  301.        
  302.         InitKeybindButtons()
  303.        
  304.         InventoryEvents(inst)
  305.        
  306.         CheckAllButton()
  307.     end)
  308. end
  309. AddPlayerPostInit(Init)
  310.  
  311. local function IsDefaultScreen()
  312.     if GLOBAL.TheFrontEnd:GetActiveScreen() and GLOBAL.TheFrontEnd:GetActiveScreen().name and type(GLOBAL.TheFrontEnd:GetActiveScreen().name) == "string" and GLOBAL.TheFrontEnd:GetActiveScreen().name == "HUD" then
  313.         return true
  314.     else
  315.         return false
  316.     end
  317. end
  318.  
  319. local TKey = "T"
  320.  
  321. --[[GLOBAL.TheInput:AddKeyUpHandler(
  322.     TKey:lower():byte(),
  323.     function()
  324.         if not GLOBAL.IsPaused() and IsDefaultScreen() then
  325.             GLOBAL.TheNet:Say(tostring(Player.replica.health:IsDead()),true)
  326.         end
  327.     end
  328. )]]--
  329.  
  330. if (KEY_ALERT ~= "false") then
  331.     GLOBAL.TheInput:AddKeyUpHandler(
  332.         KEY_ALERT:lower():byte(),
  333.         function()
  334.             if not GLOBAL.IsPaused() and IsDefaultScreen() then
  335.                 SendAlert()
  336.             end
  337.         end
  338.     )
  339. end
  340.  
  341. if (KEY_REFRESH ~= "false") then
  342.     GLOBAL.TheInput:AddKeyUpHandler(
  343.         KEY_REFRESH:lower():byte(),
  344.         function()
  345.             if not GLOBAL.IsPaused() and IsDefaultScreen() then
  346.                 CheckAllButton()
  347.             end
  348.         end
  349.     )
  350. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement