Skip to content

Commit

Permalink
feat: add new interaction type player #1
Browse files Browse the repository at this point in the history
  • Loading branch information
swkeep committed Feb 7, 2024
1 parent 92aae4d commit 88c6341
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions interactionMenu/lua/client/interact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ local function METADATA(t)
playerPedId = t.entity,
playerIndex = NetworkGetPlayerIndexFromPed(t.entity),
}

metadata.player.serverId = GetPlayerServerId(metadata.player.playerIndex)
end

Expand Down
38 changes: 34 additions & 4 deletions interactionMenu/lua/client/menuContainer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local interactionAudio = Config.interactionAudio or {
}
}

local IsPedAPlayer = IsPedAPlayer
local GetPlayerServerId = GetPlayerServerId
local NetworkGetPlayerIndexFromPed = NetworkGetPlayerIndexFromPed
local SpatialHashGrid = Util.SpatialHashGrid
local grid = SpatialHashGrid:new(100)
local StateManager = Util.StateManager()
Expand All @@ -43,6 +46,7 @@ Container = {
indexes = {
models = {},
entities = {},
players = {},
bones = {},
globals = {
bones = {},
Expand Down Expand Up @@ -196,6 +200,11 @@ local function classifyMenuInstance(instance)
models[model] = models[model] or {}

table.insert(models[model], instance.id)
elseif instance.player then
local players = indexes.players
players[instance.player] = players[instance.player] or {}

table.insert(players[instance.player], instance.id)
end
end

Expand Down Expand Up @@ -251,6 +260,12 @@ function Container.create(t)
if t.position then
instance.position = { x = t.position.x, y = t.position.y, z = t.position.z, id = id }
grid:insert(instance.position)
elseif t.player then
if type(t.player) ~= 'number' then
warn('Player id must a integer value')
return
end
instance.player = t.player
elseif t.entity then
if not DoesEntityExist(t.entity) then
local message = (
Expand Down Expand Up @@ -480,6 +495,14 @@ local function populateMenus(container, combinedIds, id, bones, closestBoneName,
return container
end

local function IdentifyPlayerServerId(entityType, entity)
return entityType == 1 and IsPedAPlayer(entity) and GetPlayerServerId(NetworkGetPlayerIndexFromPed(entity))
end

local function isPedAPlayer(entityType, entity)
return entityType == 1 and IsPedAPlayer(entity)
end

function Container.getMenu(model, entity, menuId)
local id
local combinedIds = {}
Expand All @@ -506,6 +529,12 @@ function Container.getMenu(model, entity, menuId)
end
end

local playerId = IdentifyPlayerServerId(GetEntityType(entity), entity)

if playerId then
Util.table_merge(combinedIds, Container.indexes.players[playerId] or {})
end

-- bone
local bones = Container.indexes.bones[entity]
if bones and closestBoneName then
Expand All @@ -521,7 +550,7 @@ local function globalsExistsCheck(entity, entityType)
if not entityType or entityType == 0 then return false end
local globals = Container.indexes.globals
local specificGlobals = globals[set[entityType]]
local isPlayer = entityType == 1 and IsPedAPlayer(entity)
local isPlayer = isPedAPlayer(entityType, entity)

if isPlayer and globals['players'] and next(globals['players']) then
return true
Expand All @@ -531,26 +560,27 @@ local function globalsExistsCheck(entity, entityType)
return true
end



if specificGlobals and next(specificGlobals) then
return true
end

return false
end


function Container.getMenuType(t)
local model = t.model
local entity = t.entity
local entityType = t.entityType
local entities = Container.indexes.entities
local models = Container.indexes.models
local players = Container.indexes.players
local playerId = IdentifyPlayerServerId(entityType, entity)

if t.closestPoint and next(t.closestPoint) then
-- onPosition
return MenuTypes['ON_POSITION']
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or globalsExistsCheck(entity, entityType) then
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or players[playerId] or globalsExistsCheck(entity, entityType) then
-- onModel / onEntity / onBone
return MenuTypes['ON_ENTITY']
else
Expand Down
31 changes: 31 additions & 0 deletions interactionMenu/lua/examples/onPlayers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- _
-- | |
-- _____ _| | _____ ___ _ __
-- / __\ \ /\ / / |/ / _ \/ _ \ '_ \
-- \__ \\ V V /| < __/ __/ |_) |
-- |___/ \_/\_/ |_|\_\___|\___| .__/
-- | |
-- |_|
-- https://github.com/swkeep

if not DEVMODE then return end

CreateThread(function()
exports['interactionMenu']:create {
player = 2,
offset = vec3(0, 0, 0),
maxDistance = 1.0,
options = {
{
label = 'Just On Player Id: 2',
icon = 'fa fa-person',
action = {
type = 'sync',
func = function(data)
Util.print_table(data)
end
}
}
}
}
end)

0 comments on commit 88c6341

Please sign in to comment.