Entity Use Function + Checking jobs in a Table

If I had a list of entity names in a table, how would I call a function in my server-side code when an entity in the table is used? Then check if the person who used the entity is in a job defined in a table with many other jobs? I’m coding an addon for my DarkRP server. The entities in the list are not part of the addon, but any entity in general.

e.g.



Entities = {
	"money_printer",
	"drugs_meth"
}

Jobs = {
	"Bartender",
	"Civil Protection",
	"Citizen",
	"Bank Guard",
	"Hacker",
}


Thank you for your help. Any and all answers are greatly appreciated.

I believe you need to edit each entities code and change the ENT:Use code for an example

money_printer’s Init.lua

UNTESTED CODE + Jobs table is not added just an example



function ENT:Use(activator,caller)
	if(table.HasValue(Jobs,team.GetName(activator:Team()))) then return false end
	if(self:GetstoredMoney()<=0) then return false end
	local amount = self:GetstoredMoney()

	activator:AddMoney(amount or 0)
	GAMEMODE:Notify(activator, 0, 4, "You have found " .. GAMEMODE.Config.currency .. (self:GetstoredMoney() or 0) .. "!")
	self:SetstoredMoney(0)
end


Would there be a way to do it without modifying the code for each entity? I believe it might be possible.

GM/PlayerUse
[lua]local Entities = {

[ "money_printer" ] = true,
[ "drugs_meth" ] = true,

}

local Jobs = {

[ "Bartender" ] = true,
[ "Civil Protection" ] = true,
[ "Citizen" ] = true,
[ "Bank Guard" ] = true,
[ "Hacker" ] = true,

}

hook.Add( “PlayerUse”, “PlayerUseJobCheck”, function( ply, ent )

--if the entity class is in Entities
--and the team name is in Jobs
if Entities[ ent:GetClass ] == true and Jobs[ team.GetName( ply:Team() ) ] == true then
	
	--code
	--return false here to prevent using the entity
	
end

end )[/lua]

GM/PlayerUse
Use this and check if the entities class is one of your entities

[editline]5th November 2015[/editline]

Other way would be without the true thing in those tables and using instead

table.HasValue

PlayerUse is called around a dozen times per second if you hold the button down, so if the table gets big enough, you probably shouldn’t.

table.HasValue is inefficient for this cause. It is essentially a loop that simply returns true if the value is found. You should use AwfulRanger’s way, as it allows you to quickly check if a table contains that key. More efficient and generally a better practice.

Thank you all very much for your answers. Removing the entity before using it in AwfulRanger’s example would be done with ent:Remove()?

Well, it appears that I have another problem. I want to call a derma window when the PlayerUse hook is ran, but only if the entity and the job using it are both in the list. Since the hook is server-side and derma is client side, how would I pass the ply and ent arguments over to the derma in my client file? I tried using net.Send but it apparently doesn’t support sending arguments over. I also want the entity to not be used while the window is open and to be used once one of the two buttons in the derma is pressed, the other would close the derma and remove it. Calling return false doesn’t work.