Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "fire_bullets" event from erroring. #1350

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions lua/pac3/core/client/parts/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

local BUILDER, PART = pac.PartTemplate("base")

PART.ClassName = "event"

Check warning on line 14 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'single quoted strings' and 'double quoted strings'

PART.ThinkTime = 0
PART.AlwaysThink = true
PART.Icon = 'icon16/clock.png'

Check warning on line 18 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'single quoted strings' and 'double quoted strings'

BUILDER:StartStorableVars()
BUILDER:GetSet("Event", "", {enums = function(part)
Expand All @@ -29,7 +29,7 @@

return output
end})
BUILDER:GetSet("Operator", "find simple", {enums = function(part) local tbl = {} for i,v in ipairs(part.Operators) do tbl[v] = v end return tbl end})

Check warning on line 32 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
BUILDER:GetSet("Arguments", "", {hidden = false})
BUILDER:GetSet("Invert", true)
BUILDER:GetSet("RootOwner", true)
Expand All @@ -39,18 +39,18 @@
BUILDER:GetSetPart("DestinationPart", {editor_friendly = "TargetedPart"})
BUILDER:EndStorableVars()

local registered_command_event_series = {}

Check warning on line 42 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: registered_command_event_series

function PART:register_command_event(str,b)

Check warning on line 44 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
local ply = self:GetPlayerOwner()

local event = str
local flush = b

local num = tonumber(string.sub(event, string.find(event,"[%d]+$") or 0)) or 0

Check warning on line 50 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma

if string.find(event,"[%d]+$") then

Check warning on line 52 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
event = string.gsub(event,"[%d]+$","")

Check warning on line 53 in lua/pac3/core/client/parts/event.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
end
ply.pac_command_event_sequencebases = ply.pac_command_event_sequencebases or {}

Expand Down Expand Up @@ -1462,10 +1462,21 @@
operator_type = "string", preferred_operator = "find simple",
tutorial_explanation = "fire_bullets supposedly checks what types of bullets you're firing",
arguments = {{find_ammo = "string"}, {time = "number"}},
callback = function(self, ent, find, time)
userdata = {{default = "AR2", enums = function()
local tbl = {}
for i=-1,512,1 do
local name = game.GetAmmoName(i)
if name then
tbl[name .. " (ID ="..i..")"] = name
end
end
return tbl
end}, {default = 0.1}},
callback = function(self, ent, find_ammo, time)
time = time or 0.1

ent = try_viewmodel(ent)
if game.SinglePlayer() and ent.pac_hide_bullets ~= ent:GetNWBool("pac_hide_bullets", false) then net.Start("pac_hide_bullets_get") net.WriteBool(ent.pac_hide_bullets) net.SendToServer() end

local data = ent.pac_fire_bullets
local b = false
Expand Down Expand Up @@ -3377,16 +3388,24 @@
end
end)

pac.AddHook("EntityFireBullets", "firebullets", function(ent, data)
if not ent:IsValid() or not ent.pac_has_parts then return end
ent.pac_fire_bullets = {name = data.AmmoType, time = pac.RealTime, reset = true}

pac.CallRecursiveOnAllParts("OnFireBullets")

if ent.pac_hide_bullets then
return false
end
Comment on lines -3385 to -3388
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk if removing this is right. Someone else verify

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first glance, if we search "pac_hide_bullets", it seemed as though the field isn't created anywhere (so, accessing it would always be nil if that was the case)
But, if we run the search without the pac_ prefix, it leads to EntityField functions, the builder thing that set an entity field with that prefix.
(for OP's information) This one wasn't obvious, that prefix business is just a thing that we do a few times throughout the code.

We still want that return because we still want the option to hide the bullets.

But there's some side issues in singleplayer. I tried to test, it appears the EntityFireBullets hook is ineffective. But it's fine with local server and p2p setups for some reason... oh well!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unknao can you revert this change?

end)
if game.SinglePlayer() then
net.Receive("pac_fire_bullets_for_singleplayer", function()
local ent = net.ReadEntity()
if not ent:IsValid() or not ent.pac_has_parts then return end
local ammo_type = net.ReadUInt(8)
ent.pac_fire_bullets = {name = game.GetAmmoName(ammo_type), time = pac.RealTime, reset = true}
pac.CallRecursiveOnAllParts("OnFireBullets")
end)
else
pac.AddHook("EntityFireBullets", "firebullets", function(ent, data)
if not ent:IsValid() or not ent.pac_has_parts then return end
ent.pac_fire_bullets = {name = data.AmmoType, time = pac.RealTime, reset = true}
pac.CallRecursiveOnAllParts("OnFireBullets")
if ent.pac_hide_bullets then
return false
end
end)
end

--for regaining focus on cameras from first person, hacky thing to not loop through localparts every time
--only if the received command name matches that of a camera's linked command event
Expand Down
14 changes: 14 additions & 0 deletions lua/pac3/core/server/net_messages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
util.AddNetworkString("pac.BroadcastPlayerButton")
util.AddNetworkString("pac_chat_typing_mirror")
util.AddNetworkString("pac_chat_typing_mirror_broadcast")
util.AddNetworkString("pac_fire_bullets_for_singleplayer")
util.AddNetworkString("pac_hide_bullets_get")

do -- button event
net.Receive("pac.AllowPlayerButtons", function(length, client)
Expand Down Expand Up @@ -39,3 +41,15 @@
net.WriteEntity(ply)
net.Broadcast()
end)

if game.SinglePlayer() then
hook.Add("EntityFireBullets", "pac_bullet_singleplayer_hack", function(ent, data)
if ent:IsPlayer() then
net.Start("pac_fire_bullets_for_singleplayer") net.WriteEntity(ent) net.WriteUInt(game.GetAmmoID(data.AmmoType),8) net.Broadcast()

Check warning on line 48 in lua/pac3/core/server/net_messages.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
end
if ent:GetNWBool("pac_hide_bullets", false) then return false end
end)
net.Receive("pac_hide_bullets_get", function(len, ply)
ply:SetNWBool("pac_hide_bullets",net.ReadBool())

Check warning on line 53 in lua/pac3/core/server/net_messages.lua

View workflow job for this annotation

GitHub Actions / lint

"Space after comma"

Style: Please add a space after the comma
end)
end
Loading