Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PLUGIN.Title = "Trade"
- PLUGIN.Description = "Trade Plugin (Requires Basic Economy Plugin)"
- function PLUGIN:Init()
- print("Trade plugin loading.")
- econ_plugin = plugins.Find("economy")
- if (not econ_plugin) then
- econ_plugin = plugins.Find("econ")
- end
- self.Data = {}
- self:AddChatCommand("accept", self.cmdTradeAccept)
- self:AddChatCommand("trade", self.cmdTradeInit)
- self:AddChatCommand("offer", self.cmdTradeOffer)
- self:AddChatCommand("cancel", self.cmdTradeCancel)
- self:AddChatCommand("tradehelp", self.cmdTradeHelp)
- end
- function PLUGIN:cmdTradeAccept(netuser, cmd, args)
- local userdata = self:GetUserData( netuser )
- if (userdata.Trade.Init) then
- local targetdata = self:GetUserDataFromID(userdata.Trade.Target)
- local targetdata, targetuser = self:FindUser(netuser, targetdata.Name, false)
- if (targetuser) then
- if (targetdata.Trade.Init and targetdata.Trade.Target == userdata.ID) then
- self:AcceptTrade(netuser, userdata, targetuser, targetdata)
- else
- rust.SendChatToUser(netuser, "Target is currently not in a trade with you. Type /tradehelp for help")
- end
- else
- rust.SendChatToUser(netuser, "Could not find trade target. Type /tradehelp for help")
- end
- else
- rust.SendChatToUser(netuser, "You are currently not in a trade. Type /tradehelp for help")
- end
- end
- function PLUGIN:getInventoryCount(inventory, datablock)
- local ret = 0
- local iterator = inventory.occupiedIterator
- while (iterator:Next()) do
- local item = iterator.item
- if (item.datablock == datablock) then
- if (item.datablock.IsSplittable()) then
- ret = ret + item.uses
- else
- ret = ret + 1
- end
- end
- end
- return ret
- end
- function PLUGIN:cmdTradeOffer(netuser, cmd, args)
- local userdata = self:GetUserData( netuser )
- if (userdata.Trade.Init) then
- if (not args[1]) then
- self:DisplayTradeOffer(netuser, userdata)
- else
- local targetdata = self:GetUserDataFromID(userdata.Trade.Target, "")
- local targetdata, targetuser = self:FindUser(netuser, targetdata.Name, false)
- local qty = tonumber(args[1])
- if (targetuser) then
- if (not args[2]) then
- if (econ_plugin) then
- local econ_data = econ_plugin:GetUserData(netuser)
- if (not econ_data) then
- print("Could not find user's economy data")
- else
- if (qty >= 0) then
- if (qty <= econ_data.Money) then
- userdata.Trade.Offer.Money = qty
- self:DisplayTradeOffer(netuser, userdata)
- self:DisplayTradeOffer(targetuser, targetdata)
- else
- rust.SendChatToUser(netuser, "You do not have " .. econ_plugin:GetSymbol() .. tostring(qty) .. ". Type /tradehelp for help")
- end
- else
- rust.SendChatToUser(netuser, "You cannot offer this ammount. Type /tradehelp for help")
- end
- end
- else
- rust.SendChatToUser(netuser, "You must specify an item to offer. Type /tradehelp for help")
- end
- else
- local datablock = rust.GetDatablockByName( args[2] )
- if (not datablock) then
- rust.SendChatToUser(netuser, "Cannot add item to trade. No such Item. Type /tradehelp for help" )
- else
- local inv = netuser.playerClient.rootControllable.idMain:GetComponent( "Inventory" )
- if (not inv) then
- rust.Notice(netuser, "Inventory not found!")
- else
- --local totalNum, invItem = inv:FindItem(datablock)
- local totalNum = self:getInventoryCount(inv, datablock)
- if (totalNum > 0) then
- if (totalNum <= qty) then
- local item = {}
- local edit = 0
- item.Count = qty
- item.Name = args[2]
- for key,value in pairs(userdata.Trade.Offer.Items) do
- if (value.Name == args[2]) then
- edit = 1
- value.Count = qty
- end
- end
- if (edit == 0) then
- table.insert(userdata.Trade.Offer.Items, item)
- end
- self:DisplayTradeOffer(netuser, userdata)
- self:DisplayTradeOffer(targetuser, targetdata)
- else
- rust.SendChatToUser(netuser, "Cannot add (" .. tostring(qty) .. ") \"" .. args[2] .. "\" to trade. You only have (" .. tostring(totalNum) .. "). Type /tradehelp for help")
- end
- else
- rust.SendChatToUser(netuser, "Cannot add (" .. tostring(qty) .. ") \"" .. args[2] .. "\" to trade. Type /tradehelp for help")
- end
- end
- end
- end
- else
- rust.SendChatToUser(netuser, "Could not find trade target. Type /tradehelp for help")
- end
- end
- else
- rust.SendChatToUser(netuser, "You are currently not in a trade. Type /tradehelp for help")
- end
- end
- function PLUGIN:cmdTradeCancel(netuser, cmd, args)
- local userdata = self:GetUserData(netuser)
- if (userdata.Trade.Init) then
- self:CancelTrade(netuser, userdata, false)
- else
- rust.SendChatToUser(netuser, "You are currently not in a trade. Type /tradehelp for help")
- end
- end
- function PLUGIN:cmdTradeHelp(netuser, cmd, args)
- rust.SendChatToUser( netuser, "--------------------------------------------------------------------------------" )
- rust.SendChatToUser( netuser, "------------------ LiTHiuM's Trade Plugin ------------------------" )
- rust.SendChatToUser( netuser, "Use /tradehelp to show trade help" )
- rust.SendChatToUser( netuser, "Use /trade \"name\" to send a trade request to the specified user")
- rust.SendChatToUser( netuser, "Use /trade to accept a trade from the last requester")
- rust.SendChatToUser( netuser, "Use /trade to list the current trade offers")
- rust.SendChatToUser( netuser, "Use /offer to list the current trade offers")
- rust.SendChatToUser( netuser, "Use /offer <quantity> \"Item Name\" to add an item to the trade")
- rust.SendChatToUser( netuser, "Use /offer <number> to offer money to the trade")
- rust.SendChatToUser( netuser, "Use /accept to accept a trade")
- rust.SendChatToUser( netuser, "Use /cancel to cancel a trade")
- rust.SendChatToUser( netuser, "--------------------------------------------------------------------------------" )
- end
- function PLUGIN:cmdTradeInit(netuser, cmd, args)
- local userdata = self:GetUserData(netuser)
- local targetdata = nil
- local targetuser = nil
- local removeList = { }
- if (userdata.Trade.Init) then
- self:DisplayTradeOffer(netuser, userdata)
- else
- if (args[1]) then
- targetdata, targetuser = self:FindUser(netuser, args[1], false)
- if (not targetdata) then
- rust.SendChatToUser(netuser, "No player found with that name. Type /tradehelp for help")
- else
- if (targetdata.Trade.Init) then
- rust.SendChatToUser(netuser, "Player " .. "\"" .. targetdata.Name .. "\" already in a trade. Type /tradehelp for help")
- else
- if (targetuser ~= netuser) then
- self:InitTrade(netuser, userdata, targetuser, targetdata)
- else
- rust.SendChatToUser(netuser, "You cannot trade with your self. Type /tradehelp for help")
- end
- end
- end
- else
- local initTradeDone = false
- for key,value in pairs(self.Data) do
- if (value.Trade.Target == userdata.ID) then
- targetuser = self:FindUser(netuser, value.Name, false)
- if (targetdata) then
- self:InitTrade(netuser, userdata, targetuser, targetdata)
- initTradeDone = true
- else
- end
- end
- end
- if (not initTradeDone) then
- rust.SendChatToUser(netuser, "You have no trade requests pending. Type /tradehelp for help")
- else
- end
- end
- end
- end
- function PLUGIN:DisplayTradeOffer(netuser, userdata)
- targetdata = self:GetUserDataFromID(userdata.Trade.Target, "")
- rust.SendChatToUser(netuser, "--------------------------------------------------------------------------------" )
- rust.SendChatToUser(netuser, "--------------- Current Trade With \"" .. targetdata.Name .. "\" ---------------")
- rust.SendChatToUser(netuser, "--- Your Offer ---")
- if (econ_plugin) then
- rust.SendChatToUser(netuser, "Money: " .. econ_plugin:GetSymbol() .. tostring(userdata.Trade.Offer.Money))
- end
- for key,value in pairs(userdata.Trade.Offer.Items) do
- rust.SendChatToUser(netuser, tostring(value.Count) .. "x " .. value.Name)
- end
- rust.SendChatToUser(netuser, "--- Their Offer ---")
- if (econ_plugin) then
- rust.SendChatToUser(netuser, "Money: " .. econ_plugin:GetSymbol() .. tostring(targetdata.Trade.Offer.Money))
- end
- for key,value in pairs(targetdata.Trade.Offer.Items) do
- rust.SendChatToUser(netuser, tostring(value.Count) .. "x " .. value.Name)
- end
- rust.SendChatToUser(netuser, "End of trade data. Type /tradehelp for help")
- rust.SendChatToUser(netuser, "--------------------------------------------------------------------------------" )
- end
- function PLUGIN:CancelTrade(netuser, userdata, disconnect)
- local targetdata = self:GetUserDataFromID(userdata.Trade.Target, "")
- local targetdata, targetuser = self:FindUser(netuser, targetdata.Name, false)
- if (not disconnect) then
- userdata.Trade.Target = nil
- userdata.Trade.Init = false
- userdata.Trade.Accepted = false
- userdata.Trade.Offer = {}
- rust.SendChatToUser(netuser, "Your trade with \"" .. targetdata.Name .. "\" has been canceled. Type /tradehelp for help")
- end
- targetdata.Trade.Target = nil
- targetdata.Trade.Init = false
- targetdata.Trade.Accepted = false
- targetdata.Trade.Offer = {}
- rust.SendChatToUser(targetuser, "Your trade with \"" .. userdata.Name .. "\" has been canceled. Type /tradehelp for help")
- end
- function PLUGIN:InitTrade(netuser, userdata, targetuser, targetdata)
- if (userdata.Trade.Init) then
- rust.SendChatToUser(netuser, "You are already in a trade. Type /tradehelp for help")
- else
- if (targetdata.Trade.Init) then
- rust.SendChatToUser(netuser, "Target is already in a trade. Type /tradehelp for help")
- else
- if (targetdata.Trade.Target == userdata.ID) then
- userdata.Trade.Target = targetdata.ID
- userdata.Trade.Init = true
- userdata.Trade.Accepted = false
- targetdata.Trade.Init = true
- targetdata.Trade.Accepted = false
- userdata.Trade.Offer = {}
- userdata.Trade.Offer.Money = 0
- userdata.Trade.Offer.Items = {}
- targetdata.Trade.Offer = {}
- targetdata.Trade.Offer.Money = 0
- targetdata.Trade.Offer.Items = {}
- rust.SendChatToUser(targetuser, "You are now trading with \"" .. userdata.Name .. "\". Type /tradehelp for help")
- rust.SendChatToUser(netuser, "You are now trading with \"" .. targetdata.Name .. "\". Type /tradehelp for help")
- else
- userdata.Trade.Target = targetdata.ID
- rust.SendChatToUser(targetuser, "\"" .. userdata.Name .. "\" has sent you a trade request. Type /tradehelp for help")
- rust.SendChatToUser(netuser, "You have sent a trade request to \"" .. targetdata.Name .. "\". Type /tradehelp for help")
- end
- end
- end
- end
- function PLUGIN:AcceptTrade(netuser, userdata, targetuser, targetdata)
- local userEconData = econ_plugin:GetUserData(netuser)
- local targetEconData = econ_plugin:GetUserData(targetuser)
- local userinv = netuser.playerClient.rootControllable.idMain:GetComponent( "Inventory" )
- local targetinv = targetuser.playerClient.rootControllable.idMain:GetComponent( "Inventory" )
- if (userEconData.Money < userdata.Trade.Offer.Money and econ_plugin) then
- rust.SendChatToUser(netuser, "You do not have enough money to accept this trade. Type /tradehelp for help")
- return
- end
- for key,value in pairs(userdata.Trade.Offer.Items) do
- local datablock = rust.GetDatablockByName( value.Name )
- --local totalNum, invItem = userinv:FindItem(datablock)
- local totalNum = self:getInventoryCount(userinv, datablock)
- if (totalNum < value.Count) then
- rust.SendChatToUser(netuser, "You do not have enough \"" .. value.Name .. "\" to complete this trade. Type /tradehelp for help")
- return
- end
- end
- if (targetEconData.Money < targetdata.Trade.Offer.Money and econ_plugin) then
- rust.SendChatToUser(netuser, "Target does not have enough money to accept this trade. Type /tradehelp for help")
- return
- end
- for key,value in pairs(targetdata.Trade.Offer.Items) do
- local datablock = rust.GetDatablockByName( value.Name )
- --local totalNum, invItem = targetinv:FindItem(datablock)
- local totalNum = self:getInventoryCount(targetinv, datablock)
- if (totalNum < value.Count) then
- rust.SendChatToUser(netuser, "Target does not have enough \"" .. value.Name .. "\" to complete this trade. Type /tradehelp for help")
- return
- end
- end
- if (targetdata.Trade.Accepted) then
- if (econ_plugin) then
- if (userdata.Trade.Offer.Money > 0) then
- econ_plugin:takeMoneyFrom(netuser, userdata.Trade.Offer.Money)
- econ_plugin:giveMoneyTo(targetuser, userdata.Trade.Offer.Money)
- end
- if (targetdata.Trade.Offer.Money > 0) then
- econ_plugin:giveMoneyTo(netuser, targetdata.Trade.Offer.Money)
- econ_plugin:takeMoneyFrom(targetuser, targetdata.Trade.Offer.Money)
- end
- end
- for key,value in pairs(userdata.Trade.Offer.Items) do
- local datablock = rust.GetDatablockByName( value.Name )
- local amount = value.Count
- local i = 0
- local item = userinv:FindItem(datablock)
- if (item) then
- if (datablock.IsSplittable()) then
- while (i < amount) do
- if (item.uses > 0) then
- item:SetUses( item.uses - 1 )
- i = i + 1
- else
- userinv:RemoveItem( item )
- item = userinv:FindItem(datablock)
- if (not item) then
- break
- end
- end
- end
- else
- while (i < amount) do
- userinv:RemoveItem( item )
- i = i + 1
- item = userinv:FindItem(datablock)
- if (not item) then
- break
- end
- end
- end
- end
- end
- for key,value in pairs(targetdata.Trade.Offer.Items) do
- local datablock = rust.GetDatablockByName( value.Name )
- local amount = value.Count
- local i = 0
- local item = targetinv:FindItem(datablock)
- if (item) then
- if (datablock.IsSplittable()) then
- while (i < amount) do
- if (item.uses > 0) then
- item:SetUses( item.uses - 1 )
- i = i + 1
- else
- targetinv:RemoveItem( item )
- item = targetinv:FindItem(datablock)
- if (not item) then
- break
- end
- end
- end
- else
- while (i < amount) do
- targetinv:RemoveItem( item )
- i = i + 1
- item = targetinv:FindItem(datablock)
- if (not item) then
- break
- end
- end
- end
- end
- end
- for key,value in pairs(userdata.Trade.Offer.Items) do
- rust.RunServerCommand("inv.giveplayer \"" .. util.QuoteSafe( targetuser.displayName ) .. "\" \"" .. util.QuoteSafe( value.Name ) .. "\" " .. tostring(value.Count) )
- end
- for key,value in pairs(targetdata.Trade.Offer.Items) do
- rust.RunServerCommand("inv.giveplayer \"" .. util.QuoteSafe( netuser.displayName ) .. "\" \"" .. util.QuoteSafe( value.Name ) .. "\" " .. tostring(value.Count) )
- end
- rust.SendChatToUser(netuser, "Trade complete! Type /tradehelp for help")
- rust.SendChatToUser(targetuser, "Trade complete! Type /tradehelp for help")
- userdata.Trade.Init = false
- userdata.Trade.Accepted = false
- userdata.Trade.Offer = {}
- userdata.Trade.Target = nil
- targetdata.Trade.Init = false
- targetdata.Trade.Accepted = false
- targetdata.Trade.Offer = {}
- targetdata.Trade.Target = nil
- else
- rust.SendChatToUser(netuser, "Trade accepted. Waiting for target to accept. Type /tradehelp for help")
- rust.SendChatToUser(targetuser, "Target accepted trade. Waiting for you to /accept. Type /tradehelp for help")
- userdata.Trade.Accepted = true
- end
- end
- function PLUGIN:FindUser( netuser, name, sendMsg)
- local targetdata = nil
- local b, targetuser = rust.FindNetUsersByName( name )
- if (not b) then
- if (targetuser == 0) then
- targetdata = self:GetUserDataFromName(name)
- else
- --if (sendMsg) rust.Notice( netuser, "Multiple players found with that name!" ) end
- return nil,nil
- end
- if (not targetdata) then
- --if (sendMsg) rust.Notice(netuser, name .. " not found!") end
- return nil,nil
- end
- targetuser = self:GetUserDataFromID(targetdata.ID, "")
- if (not targetuser) then
- --if (sendMsg) rust.Notice(netuser, name .. " not found!") end
- return nil,nil
- end
- else
- targetdata = self:GetUserData( targetuser )
- end
- if (not targetdata) then
- --if (sendMsg) rust.Notice(netuser, args[1] .. " not found!") end
- return nil,nil
- end
- return targetdata, targetuser
- end
- function PLUGIN:GetUserDataFromID( userID, name )
- local userentry = self.Data[ userID ]
- if (not userentry) then
- print(name .. " user data not found")
- end
- return userentry
- end
- function PLUGIN:GetUserDataFromName(name)
- for key,value in pairs(self.Data) do
- if (value.Name == name) then
- return value
- end
- end
- return nil
- end
- function PLUGIN:GetUserData( netuser )
- local userID = rust.GetUserID( netuser )
- return self:UpdateUserDataFromID( userID, netuser.displayName )
- end
- function PLUGIN:UpdateUserDataFromID( userID, name )
- local userentry = self.Data[ userID ]
- if (not userentry) then
- userentry = {}
- userentry.ID = userID
- userentry.Name = name
- userentry.Trade = {}
- userentry.Trade.Target = nil
- userentry.Trade.Init = false
- userentry.Trade.Accepted = false
- userentry.Trade.Offer = {}
- userentry.Trade.Offer.Money = 0
- userentry.Trade.Offer.Items = {}
- self.Data[ userID ] = userentry
- end
- return userentry
- end
- function PLUGIN:OnUserConnect( netuser )
- local data = self:GetUserData( netuser )
- end
- function PLUGIN:OnUserDisconnect( netuser )
- local data = self:GetUserData( netuser )
- if (data.Trade.Init) then
- self:CancelTrade( netuser, data, true )
- end
- self.Data [ data.ID ] = nil
- end
- api.Bind(PLUGIN, "trade")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement