Skip to content

Commit

Permalink
playerblips: various improvements (#225)
Browse files Browse the repository at this point in the history
* playerblips: use LF, move main script, modify meta tags

* playerblips: cleanup code

* playerblips: add optional playercolors integration

* playerblips: add blip_range setting

* playerblips: fix settings config so they appear in admin panel

Due to the changes to the setting names which were necesarry for admin to recognize the settings as public, this commit breaks backwards compatibility. Server admins will need to set their settings again via admin panel. The major version was bumped accordingly.

* playerblips: add isElement checks to fix occasional debug errors

Other resources (including race) delete blips attached to players, even those created by other resources.

* playerblips: include playerblips in freeroam, remove duplicate code

* playerblips: update resource authors

* playerblips: fix event handlers

* playerblips: gravy

* playerblips: add missing error check
  • Loading branch information
jlillis authored Jan 16, 2023
1 parent 9be00d6 commit 9e79974
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 87 deletions.
4 changes: 0 additions & 4 deletions [gameplay]/freeroam/fr_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ function joinHandler(player)
local r, g, b = math.random(50, 255), math.random(50, 255), math.random(50, 255)
setPlayerNametagColor(player, r, g, b)
g_PlayerData[player] = { vehicles = {}, settings={} }
g_PlayerData[player].blip = createBlipAttachedTo(player, 0, 2, r, g, b)
addEventHandler("onFreeroamLocalSettingChange",player,onLocalSettingChange)
if getOption('welcometextonstart') then
outputChatBox('Welcome to Freeroam', player, 0, 255, 0)
Expand Down Expand Up @@ -552,9 +551,6 @@ function unloadVehicle(vehicle)
end

function quitHandler(player)
if g_PlayerData[source].blip and isElement(g_PlayerData[source].blip) then
destroyElement(g_PlayerData[source].blip)
end
if sawnoffAntiAbuse[source] and isTimer (sawnoffAntiAbuse[source]) then
killTimer (sawnoffAntiAbuse[source])
sawnoffAntiAbuse[source] = nil
Expand Down
1 change: 1 addition & 0 deletions [gameplay]/freeroam/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<oop>true</oop>
<info name="Freeroam GUI" author="arc_" version="1.5.7" />
<min_mta_version client="1.5.6-9.16362"></min_mta_version>
<include resource="playerblips" />

<script src="util_server.lua" type="server" />
<script src="fr_server.lua" type="server" />
Expand Down
66 changes: 55 additions & 11 deletions [gameplay]/playerblips/meta.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
<meta>
<info author="Noki" type="script" name="playerblips"/>
<script src="playerblips.lua" type="server"/>
<settings>
<setting name="use_team_colors" value="false"/> <!-- Set to true to enable, anything else will not use teams. Default: false -->
<setting name="blip_color" value="[ [ 0, 255, 0 ] ]"/> <!-- Default blip color if se_team_colors is not enabled. Default: [ [ 0, 255, 0 ] ] -->
<setting name="blip_size" value="2"/> <!-- Size of a player blip. Default: 2 -->
<setting name="blip_alpha" value="255"/> <!-- Alpha value of a player blip (0 - 255). Default: 255 -->
</settings>
<oop>true</oop>
</meta>
<meta>
<info name="Player Blips" author="Noki, jlillis" version="2.0.0" type="script" description="Attaches blips to players."/>
<oop>true</oop>
<script src="server/main.lua" type="server"/>
<settings>
<setting
name="*use_team_colors"
value="false"
friendlyname="Use team colors"
group="Color settings"
accept="true,false"
desc="Use team colors for player blips."
/>
<setting
name="*use_nametag_colors"
value="true"
friendlyname="Use nametag colors"
group="Color settings"
accept="true,false"
desc="Use nametag colors for player blips. Takes precedence over team colors."
/>
<setting
name="*blip_color"
value="[[0, 255, 0]]"
friendlyname="Default blip color"
group="Color settings"
accept=""
desc="Default blip color. Used if team and nametag colors are not used."
/>
<setting
name="*blip_size"
value="2"
friendlyname="Blip size"
group="Blip settings"
accept="1-25"
desc="Player blip size."
/>
<setting
name="*blip_alpha"
value="255"
friendlyname="Blip transparency"
group="Blip settings"
accept="0-255"
desc="Player blip transparency."
/>
<setting
name="*blip_range"
value="16383"
friendlyname="Blip visibility range"
group="Blip settings"
accept="0-65535"
desc="Player blip visibility range on the minimap. Blips are always visible on the map."
/>
</settings>
</meta>
72 changes: 0 additions & 72 deletions [gameplay]/playerblips/playerblips.lua

This file was deleted.

77 changes: 77 additions & 0 deletions [gameplay]/playerblips/server/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local useTeams = get("*use_team_colors") == "true"
local useNametags = get("*use_nametag_colors") == "true"
local blipSize = get("*blip_size")
local blipAlpha = get("*blip_alpha")
local color = get("*blip_color")
local blipRange = get("*blip_range")
local colors = {}
local blips = {}

local function resourceStart()
for i, player in ipairs(Element.getAllByType("player")) do
createPlayerBlip(player)
end

local playercolorsResource = getResourceFromName("playercolors")
if playercolorsResource and getResourceState(playercolorsResource) ~= "running" then
outputDebugString("playerblips: playercolors resource not running; using blip_color. Restart this resource after starting playercolors.", 4, 255, 125, 0)
useNametags = false
end

if not (useTeams or useNametags) then
addCommandHandler("setblipcolor", setBlipColor)
end
end
addEventHandler("onResourceStart", resourceRoot, resourceStart)

function createPlayerBlip(player)
if (not player or not isElement(player) or player.type ~= "player") then return false end
local r, g, b
if (useTeams and player.team) then
r, g, b = player.team:getColor()
elseif useNametags then
r, g, b = getPlayerNametagColor(player)
elseif (colors[player]) then
r, g, b = colors[player][1], colors[player][2], colors[player][3]
else
r, g, b = color[1], color[2], color[3]
end
if isElement(blips[player]) then
blips[player]:setColor(r, g, b, blipAlpha)
else
blips[player] = Blip.createAttachedTo(player, 0, blipSize, r, g, b, blipAlpha, 0, blipRange)
end
end

function setBlipColor(player, _, r, g, b)
r, g, b = tonumber(r), tonumber(g), tonumber(b)
if (r and g and b) then
if (r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255) then
colors[player] = {r, g, b}
createPlayerBlip(player)
else
outputChatBox("Couldn't change blip color - numbers must be between 0 and 255", player, 255, 0, 0)
end
else
outputChatBox("Couldn't change blip color - invalid arguments specified", player, 255, 0, 0)
end
end

function destroyPlayerBlip(player)
if (not player or not isElement(player) or player.type ~= "player") then return false end
if isElement(blips[player]) then
blips[player]:destroy()
end
blips[player] = nil
colors[player] = nil
end

addEventHandler("onPlayerQuit", root, function()
destroyPlayerBlip(source)
end)
addEventHandler("onPlayerWasted", root, function()
destroyPlayerBlip(source)
end)
addEventHandler("onPlayerSpawn", root, function()
createPlayerBlip(source)
end)

0 comments on commit 9e79974

Please sign in to comment.