WAC/Entity Ownership with Locking/Unlocking

I am currently attempting to get WAC vehicles to have locking features by basically making them a door, so it can be locked/unlocked. The reason for this is because they are just entities and are not considered vehicles so DarkRP doesn’t recognize them and I want to be able to lock/unlock them for players to use. I looked at some older code that I had and attempted this. The older code was used in MetroRP so it’s a bit out of date.

Here are the files im working on:
**
sh_doors (Only the part i’ve edited)**


local meta = FindMetaTable("Entity")
local plyMeta = FindMetaTable("Player")

local SentVehicles = {}
SentVehicles["sent_sakariashelicopter"] = true
SentVehicles["sent_sc-1"] = true
SentVehicles["sent_yacht"] = true
SentVehicles["wac_hc_littlebird_ah6"] = true
SentVehicles["sent_fishingboat"] = true
SentVehicles["wac_hc_uh1d"] = true
SentVehicles["wac_hc_206b_amphib"] = true
SentVehicles["wac_hc_s64"] = true
SentVehicles["wac_hc_littlebird_h500"] = true
SentVehicles["wac_hc_blackhawk_uh60"] = true
SentVehicles["wac_hc_r22"] = true
SentVehicles["sent_wakesetter"] = true
SentVehicles["wac_hc_ah1z_viper"] = true
SentVehicles["sent_speedboat"] = true
SentVehicles["sent_rhib"] = true
SentVehicles["sent_jetmax"] = true
SentVehicles["sent_antares"] = true

function meta:isKeysOwnable()
    if not IsValid(self) then return false end
    local class = self:GetClass()

    if ((class == "func_door" or class == "func_door_rotating" or class == "prop_door_rotating") or SentVehicles[class] or
            (GAMEMODE.Config.allowvehicleowning and self:IsVehicle() and (not IsValid(self:GetParent()) or not self:GetParent():IsVehicle()))) then
            return true
        end
    return false
end

function meta:IsSentVehicle()
	return SentVehicles[self:GetClass()]
end

function meta:isDoor()
    if not IsValid(self) then return false end
    local class = self:GetClass()

    if	SentVehicles[class] or
		class == "func_door" or
        class == "func_door_rotating" or
        class == "prop_door_rotating" or
        class == "func_movelinear" or
        class == "prop_dynamic" then
        return true
    end
    return false
end

keys/shared.lua (Unedited)


AddCSLuaFile()

if SERVER then
    AddCSLuaFile("cl_menu.lua")
end

if CLIENT then
    SWEP.PrintName = "Keys"
    SWEP.Slot = 1
    SWEP.SlotPos = 1
    SWEP.DrawAmmo = false
    SWEP.DrawCrosshair = false

    include("cl_menu.lua")
end

SWEP.Author = "DarkRP Developers"
SWEP.Instructions = "Left click to lock
Right click to unlock
Reload for door settings or animation menu"
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.IsDarkRPKeys = true

SWEP.WorldModel = ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix  = "rpg"

SWEP.UseHands = true

SWEP.Spawnable = true
SWEP.AdminOnly = true
SWEP.Category = "DarkRP (Utility)"
SWEP.Sound = "doors/door_latch3.wav"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

function SWEP:Initialize()
    self:SetHoldType("normal")
end

function SWEP:Deploy()
    if CLIENT or not IsValid(self:GetOwner()) then return true end
    self:GetOwner():DrawWorldModel(false)
    return true
end

function SWEP:Holster()
    return true
end

function SWEP:PreDrawViewModel()
    return true
end

local function lookingAtLockable(ply, ent)
    local eyepos = ply:EyePos()
    return IsValid(ent)             and
        ent:isKeysOwnable()         and
        (
            ent:isDoor()    and eyepos:DistToSqr(ent:GetPos()) < 4225
            or
            ent:IsVehicle() and eyepos:DistToSqr(ent:NearestPoint(eyepos)) < 10000
        )

end

local function lockUnlockAnimation(ply, snd)
    ply:EmitSound("npc/metropolice/gear" .. math.floor(math.Rand(1,7)) .. ".wav")
    timer.Simple(0.9, function() if IsValid(ply) then ply:EmitSound(snd) end end)

    local RP = RecipientFilter()
    RP:AddAllPlayers()

    umsg.Start("anim_keys", RP)
        umsg.Entity(ply)
        umsg.String("usekeys")
    umsg.End()

    ply:AnimRestartGesture(GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_GMOD_GESTURE_ITEM_PLACE, true)
end

local function doKnock(ply, sound)
    ply:EmitSound(sound, 100, math.random(90, 110))
    umsg.Start("anim_keys")
        umsg.Entity(ply)
        umsg.String("knocking")
    umsg.End()

    ply:AnimRestartGesture(GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_HL2MP_GESTURE_RANGE_ATTACK_FIST, true)
end

function SWEP:PrimaryAttack()
    local trace = self:GetOwner():GetEyeTrace()

    if not lookingAtLockable(self:GetOwner(), trace.Entity) then return end

    self:SetNextPrimaryFire(CurTime() + 0.3)

    if CLIENT then return end

    if self:GetOwner():canKeysLock(trace.Entity) then
        trace.Entity:keysLock() -- Lock the door immediately so it won't annoy people
        lockUnlockAnimation(self:GetOwner(), self.Sound)
    elseif trace.Entity:IsVehicle() then
        DarkRP.notify(self:GetOwner(), 1, 3, DarkRP.getPhrase("do_not_own_ent"))
    else
        doKnock(self:GetOwner(), "physics/wood/wood_crate_impact_hard2.wav")
    end
end

function SWEP:SecondaryAttack()
    local trace = self:GetOwner():GetEyeTrace()

    if not lookingAtLockable(self:GetOwner(), trace.Entity) then return end

    self:SetNextSecondaryFire(CurTime() + 0.3)

    if CLIENT then return end

    if self:GetOwner():canKeysUnlock(trace.Entity) then
        trace.Entity:keysUnLock() -- Unlock the door immediately so it won't annoy people
        lockUnlockAnimation(self:GetOwner(), self.Sound)
    elseif trace.Entity:IsVehicle() then
        DarkRP.notify(self:GetOwner(), 1, 3, DarkRP.getPhrase("do_not_own_ent"))
    else
        doKnock(self:GetOwner(), "physics/wood/wood_crate_impact_hard3.wav")
    end
end

function SWEP:Reload()
    local trace = self:GetOwner():GetEyeTrace()
    if not IsValid(trace.Entity) or (IsValid(trace.Entity) and ((not trace.Entity:isDoor() and not trace.Entity:IsVehicle()) or self.Owner:EyePos():DistToSqr(trace.HitPos) > 40000)) then
        if CLIENT and not DarkRP.disabledDefaults["modules"]["animations"] then RunConsoleCommand("_DarkRP_AnimationMenu") end
        return
    end
    if SERVER then
        umsg.Start("KeysMenu", self:GetOwner())
        umsg.End()
    end
end


I managed to get the vehicles ownable, I just need to figure out how to get them to lock/unlock.
Yes, I know i’m editing the main DarkRP files… please dont hurt me.


 local function lookingAtLockable(ply, ent)
    local eyepos = ply:EyePos()
    return IsValid(ent)             and
        ent:isKeysOwnable()         and
        (
            ent:isDoor()    and eyepos:DistToSqr(ent:GetPos()) < 4225
            or
            ent:IsVehicle() and eyepos:DistToSqr(ent:NearestPoint(eyepos)) < 10000
        )

end 

Adding a check here might work. WAC might not be returning true for


 ent:IsVehicle 

so maybe try adding a check in there?

So fucking sorry. I don’t read my shit before I post sometimes.
Adding


 ent:GetClass() == "wac_whatever" and eyepos:DistToSqr(ent:GetPos()) < 10000 

after the ent:IsVehicle check might work.

That obfuscation level is amazing

indeed

That check is working because I made WAC vehicles “isDoor()”. Im just not completely sure why they cant be locked/unlocked.