Advertisement
Guest User

AirDrop Fixed

a guest
Dec 27th, 2014
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.78 KB | None | 0 0
  1. PLUGIN.Title = "Airdrops"
  2. PLUGIN.Description = "Temporary hotfix for Rust's broken Airdrops"
  3. PLUGIN.Version = V(1, 1, 3)
  4. PLUGIN.Author = "BedIntruder319"
  5. PLUGIN.HasConfig = true
  6.  
  7. Parachutes = {}
  8.  
  9. function PLUGIN:Init()
  10.     print("-----------------------------")
  11.     print("Airdrops: Loaded Successfully")
  12.     print("-----------------------------")
  13.    
  14.     Parachutes = {}
  15.     self:CheckConfig()
  16.     command.AddChatCommand("airdrop", self.Object, "cmdAirdrop")
  17.     command.AddChatCommand("removeparalol", self.Object, "cmdRemovePAra")
  18.     timer.Repeat(self.Config.Settings.SupplyDropDelayInSeconds, 0, function() self:SpawnSupplies(nil, nil) end)
  19. end
  20.  
  21. function PLUGIN:cmdRemovePAra(player, com, args)
  22.     parachutes = UnityEngine.Object.FindObjectsOfTypeAll( global.BaseEntity._type )
  23.     for i = 0, parachutes.Length - 1 do
  24.         if parachutes[i].model then
  25.             if tostring( parachutes[i] ):find("parachute") then
  26.                 parachutes[i]:KillMessage()
  27.             end
  28.         end
  29.     end
  30. end
  31.  
  32. function PLUGIN:InitializeTable()
  33.     self.Table = {}
  34.     local itemlist = global.ItemManager.GetItemDefinitions();
  35.     local it = itemlist:GetEnumerator()
  36.     while (it:MoveNext()) do
  37.         local correctname = string.lower(it.Current.displayname)
  38.         self.Table[correctname] = tostring(it.Current.shortname)
  39.         --print(string.lower(it.Current.displayname) .. "|" .. tostring(it.Current.shortname))
  40.     end
  41. end
  42.  
  43. function PLUGIN:OnEntitySpawn(entity)
  44.     if (self.Config.Settings.PreventRustAirdrops) then
  45.         if (entity:GetComponentInParent(global.CargoPlane._type)) then
  46.             print(self.Config.ChatSettings.ChatName .. ": " .. self.Config.ChatSettings.RustAirdropPrevented)
  47.             entity:GetComponent("BaseEntity"):KillMessage()
  48.         end
  49.     end
  50. end
  51.  
  52. function PLUGIN:cmdAirdrop(player, com, args)
  53.     if (player:GetComponent("BaseNetworkable").net.connection.authLevel == self.Config.Settings.RequiredAuthenticationLevel) then
  54.         if (args.Length == 0) then
  55.             self:SpawnSupplies(nil, nil)
  56.         elseif (args.Length == 1) then
  57.             local targetPlayer = self:FindPlayerByName(args[0])
  58.             if (#targetPlayer == 0) then
  59.                 player:SendConsoleCommand("chat.add \"" .. self.Config.ChatSettings.ChatName .. "\" \"" .. self.Config.ChatSettings.NoPlayerFoundFromName .. "\"")
  60.                 return
  61.             elseif(#targetPlayer > 1) then
  62.                 player:SendConsoleCommand("chat.add \"" .. self.Config.ChatSettings.ChatName .. "\" \"" .. self.Config.ChatSettings.MultiplePlayersFoundFromName .. "\"")
  63.                 return
  64.             else
  65.                 local target = targetPlayer[1]
  66.                 self:SpawnSupplies(math.floor(target.transform.position.x), math.floor(target.transform.position.z))
  67.             end
  68.         elseif (args.Length == 2) then
  69.             if (tonumber(args[0]) and tonumber(args[1])) then
  70.                 self:SpawnSupplies(tonumber(args[0]), tonumber(args[1]))
  71.             else
  72.                 player:SendConsoleCommand("chat.add \"" .. self.Config.ChatSettings.ChatName .. "\" \"" .. self.Config.ChatSettings.InvalidCoordiantes .. "\"")
  73.             end
  74.         else
  75.             player:SendConsoleCommand("chat.add \"" .. self.Config.ChatSettings.ChatName .. "\" \"" .. self.Config.ChatSettings.InvalidCommandUsage .. "\"")
  76.         end
  77.     end
  78. end
  79.  
  80. function PLUGIN:FindPlayerByName(playerName)
  81.     if not playerName then return end
  82.  
  83.     playerName = string.lower(playerName)
  84.    
  85.     local matches = {}
  86.     local itPlayerList = global.BasePlayer.activePlayerList:GetEnumerator()
  87.  
  88.     while itPlayerList:MoveNext() do
  89.         local displayName = string.lower(itPlayerList.Current.displayName)
  90.  
  91.         if string.find(displayName, playerName, 1, true) then
  92.             table.insert(matches, itPlayerList.Current)
  93.         end
  94.     end
  95.  
  96.     return matches
  97. end
  98.  
  99. function PLUGIN:SpawnSupplies(x, z)
  100.     local destination = new(UnityEngine.Vector3._type, nil)
  101.     if (x == nil) then
  102.         destination.x = math.random(self.Config.Settings.MinimumXCoord, self.Config.Settings.MaximumXCoord)
  103.     else
  104.         destination.x = x
  105.     end
  106.     destination.y = 500
  107.     if (z == nil) then
  108.         destination.z = math.random(self.Config.Settings.MinimumZCoord, self.Config.Settings.MaximumZCoord)
  109.     else
  110.         destination.z = z
  111.     end
  112.    
  113.     local quaternion = new(UnityEngine.Quaternion._type, nil)
  114.     quaternion.x = 0
  115.     quaternion.y = 0
  116.     quaternion.z = 0
  117.    
  118.     local Supply_Drop = global.GameManager.CreateEntity("items/supply_drop", destination, quaternion)
  119.            
  120.     Supply_Drop:Spawn(true)
  121.  
  122.     local Parachute = global.GameManager.CreateEntity("parachute", destination, quaternion)
  123.  
  124.     if Parachute then
  125.         Parachute:SetParent(Supply_Drop, "parachute_attach")
  126.         Parachute:Spawn(true)
  127.         local ParachuteTimer = timer.Once(110, function() self:DestroyParachute(Parachute, Supply_Drop) end)
  128.         local ParachuteInfo = {Parachute, Parachute.transform.position, ParachuteTimer}
  129.         table.insert(Parachutes, ParachuteInfo)
  130.     end
  131.    
  132.     if (self.Config.ChatSettings.ShowChatNotifications) then
  133.         print(self.Config.ChatSettings.ChatName .. self.Config.ChatSettings.AirdropOccuring .. " " .. destination.x .. ", " .. destination.z)
  134.         global.ConsoleSystem.Broadcast("chat.add \"" .. self.Config.ChatSettings.ChatName .. "\" \"" .. self.Config.ChatSettings.AirdropOccuring .. " " .. destination.x .. ", " .. destination.z .. "\"")
  135.     end
  136. end
  137.  
  138. function PLUGIN:DestroyParachute(parachute, supplydrop)
  139.     if parachute then
  140.         parachute:KillMessage()
  141.     Parachutes = {}
  142.         end
  143. end
  144.  
  145. function PLUGIN:OnPlayerAttack(player, hitinfo)
  146.     if(not self.Table) then self:InitializeTable() end
  147.    
  148.     if (player.svActiveItem) then
  149.         local hasTool = false
  150.         local weapon = player.svActiveItem.info.displayname
  151.         for _,toolName in pairs(self.Config.Settings.LootTools) do
  152.             if (string.lower(toolName) == string.lower(weapon)) then
  153.                 hasTool = true
  154.             end
  155.         end
  156.        
  157.         if (hasTool) then
  158.             if (hitinfo.HitEntity) then
  159.                 if (hitinfo.HitEntity:GetComponentInParent(global.SupplyDrop._type)) then
  160.            
  161.                     hitinfo.HitEntity:GetComponent("BaseEntity"):KillMessage()
  162.                    
  163.                
  164.                     local inv = player.inventory
  165.                     local pref = inv.containerMain
  166.                     local supplies = self.Config.AirdropSupplies
  167.                     for _,supplyItem in pairs(supplies) do
  168.                    
  169.                         local name = supplyItem["name"]
  170.                         local minAmount = supplyItem["minAmount"]
  171.                         local maxAmount = supplyItem["maxAmount"]
  172.                         local probability = supplyItem["probability"]
  173.                        
  174.                         local value = math.random(minAmount, maxAmount);
  175.                         local getItem = math.random(0, 100)
  176.                        
  177.                         local name2 = string.lower(name)
  178.                         local itemname = self.Table[name2]
  179.                        
  180.                         if (getItem <= probability) then   
  181.                             local item = global.ItemManager.CreateByName(itemname, value)
  182.                             if not item then
  183.                                 return
  184.                             end
  185.                             inv:GiveItem(item, pref)
  186.                         end
  187.                     end
  188.                 end
  189.             end
  190.         end
  191.     end
  192. end
  193.  
  194. function PLUGIN:Unload()
  195.     Parachutes = {}
  196. end
  197.  
  198. function PLUGIN:LoadDefaultConfig()
  199.     self.Config.PluginSettings = {
  200.         Title = "Airdrops",
  201.         Version = "1.1.3"
  202.     }
  203.     self.Config.Settings = {
  204.         RequiredAuthenticationLevel = 2,
  205.         PreventRustAirdrops = true,
  206.         LootTools = {
  207.             "Salvaged Axe",
  208.             "Hammer",
  209.             "Salvaged Hammer",
  210.             "Hatchet",
  211.             "Salvaged Icepick",
  212.             "Pick Axe",
  213.             "Rock",
  214.             "Stone Hatchet"
  215.         },
  216.         SupplyDropDelayInSeconds = 1800,
  217.         MinimumXCoord = -114,
  218.         MaximumXCoord = 293,
  219.         MinimumZCoord = -178,
  220.         MaximumZCoord = 42
  221.     }
  222.     self.Config.ChatSettings = {
  223.         ChatName = "Airdrops",
  224.         ShowChatNotifications = true,
  225.         RustAirdropPrevented = "A Rust default airdrop event was prevented",
  226.         NoPlayerFoundFromName = "Could not find a player from provided name",
  227.         MultiplePlayersFoundFromName = "Found multiple players with this name or partial",
  228.         InvalidCoordiantes = "The given coordinates are invalid",
  229.         InvalidCommandUsage = "Invalid command usage: /airdrop (x) (z)",
  230.         AirdropOccuring = "An airdrop is occurring at"
  231.     }
  232.     self.Config.AirdropSupplies = {
  233.         {
  234.             ["name"] = "Pistol Bullet",
  235.             ["minAmount"] = 250,
  236.             ["maxAmount"] = 500,
  237.             ["probability"] = 80
  238.         },
  239.         {
  240.             ["name"] = "Revolver",
  241.             ["minAmount"] = 1,
  242.             ["maxAmount"] = 1,
  243.             ["probability"] = 90
  244.         },
  245.         {
  246.             ["name"] = "Thompson",
  247.             ["minAmount"] = 1,
  248.             ["maxAmount"] = 1,
  249.             ["probability"] = 25
  250.         },
  251.         {
  252.             ["name"] = "Cooked Human Meat",
  253.             ["minAmount"] = 20,
  254.             ["maxAmount"] = 40,
  255.             ["probability"] = 90
  256.         },
  257.         {
  258.             ["name"] = "Wood",
  259.             ["minAmount"] = 10000,
  260.             ["maxAmount"] = 25000,
  261.             ["probability"] = 100
  262.         },
  263.         {
  264.             ["name"] = "Stones",
  265.             ["minAmount"] = 7500,
  266.             ["maxAmount"] = 15000,
  267.             ["probability"] = 100
  268.         },
  269.         {
  270.             ["name"] = "Metal Fragments",
  271.             ["minAmount"] = 500,
  272.             ["maxAmount"] = 1500,
  273.             ["probability"] = 75
  274.         },
  275.     }
  276. end
  277.  
  278. function PLUGIN:CheckConfig()
  279.     if self.Config.PluginSettings.Version ~= "1.1.3" then
  280.         self:UpdateConfig()
  281.     end
  282. end
  283.  
  284. function PLUGIN:UpdateConfig()
  285.     self:LoadDefaultConfig()
  286.     self:SaveConfig()
  287.     print("Airdrops: Default configuration file loaded")
  288. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement