Skip to content

Commit

Permalink
fire_bullets event fixes
Browse files Browse the repository at this point in the history
originally from #1350, we kind of forgot about it and OP deleted his repo

the fixes consist of renaming the variable find injected in the callback function (stopping error spam as it was expected to be read as find_ammo) and adding some hackery to make the event work when in singleplayer

also adding enums for the ammo names

but many addon weapons I tested don't seem to work to begin with, maybe they don't call the FireBullets hook in the first place
  • Loading branch information
pingu7867 committed Jan 2, 2025
1 parent 78d1f05 commit e168426
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
49 changes: 39 additions & 10 deletions lua/pac3/core/client/parts/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1589,13 +1589,31 @@ PART.OldEvents = {

fire_bullets = {
operator_type = "string", preferred_operator = "find simple",
tutorial_explanation = "fire_bullets supposedly checks what types of bullets you're firing",
tutorial_explanation = "fire_bullets checks what types of bullets you're firing.\nDoesn't seem to work with many addon weapons. The event relies on the FireBullets hook",
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() then
if self:GetPlayerOwner() == pac.LocalPlayer then
if 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
end
end

local data = ent.pac_fire_bullets
local b = false

Expand Down Expand Up @@ -3561,16 +3579,27 @@ pac.AddHook("EntityEmitSound", "emit_sound", function(data)
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}
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")
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}

if ent.pac_hide_bullets then
return false
end
end)
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.AllowPlayerButtons")
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.Receive("pac_chat_typing_mirror", function(len, ply)
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()
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())
end)
end

0 comments on commit e168426

Please sign in to comment.