Skip to content

Commit 725dd74

Browse files
Merge pull request #49 from TovarischPootis/1.2
Fix nut.item.instances not being synced between client and server
2 parents b2b28b6 + 27f1ca7 commit 725dd74

6 files changed

Lines changed: 57 additions & 51 deletions

File tree

entities/entities/nut_item.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ if (SERVER) then
4343
local itemTable = nut.item.instances[itemID]
4444
if (not itemTable) then return self:Remove() end
4545

46+
itemTable:sync() -- sync it with the clients to be able to properly interact with the item
47+
4648
local model = itemTable.onGetDropModel
4749
and itemTable:onGetDropModel(self)
4850
or itemTable.model
@@ -57,7 +59,7 @@ if (SERVER) then
5759
self:PhysicsInit(SOLID_VPHYSICS)
5860
self:SetSolid(SOLID_VPHYSICS)
5961
self:setNetVar("id", itemTable.uniqueID)
60-
self:setNetVar("instanceID", itemTable:getID())
62+
self:setNetVar("instanceID", itemTable.id)
6163
self.nutItemID = itemID
6264

6365
if (table.Count(itemTable.data) > 0) then
@@ -142,7 +144,6 @@ else
142144
function ENT:onDrawEntityInfo(alpha)
143145
local itemTable = self:getItemTable()
144146
if (not itemTable) then return end
145-
146147
local oldEntity = itemTable.entity
147148
itemTable.entity = self
148149

@@ -193,7 +194,8 @@ else
193194
end
194195

195196
function ENT:getItemID()
196-
return self:getNetVar("instanceID", "")
197+
return self:getNetVar("instanceID")
198+
197199
end
198200

199201
function ENT:getItemTable()

gamemode/core/hooks/sv_hooks.lua

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function GM:PlayerInitialSpawn(client)
4343

4444
-- Load and send the NutScript data for the player.
4545
client:loadNutData(function(data)
46-
if (!IsValid(client)) then return end
46+
if (not IsValid(client)) then return end
4747

4848
local address = client:IPAddress()
4949
client:setNutData("lastIP", address)
@@ -53,6 +53,13 @@ function GM:PlayerInitialSpawn(client)
5353
"nutDataSync",
5454
data, client.firstJoin, client.lastJoin
5555
)
56+
57+
for _, v in pairs(nut.item.instances) do
58+
if v.entity and v.invID == 0 then
59+
v:sync(client)
60+
end
61+
end
62+
5663
hook.Run("PlayerNutDataLoaded", client)
5764
end)
5865

@@ -71,7 +78,7 @@ function GM:PlayerUse(client, entity)
7178
if (result == false) then
7279
return false
7380
else
74-
local result = hook.Run("PlayerUseDoor", client, entity)
81+
result = hook.Run("PlayerUseDoor", client, entity)
7582
if (result ~= nil) then
7683
return result
7784
end
@@ -107,12 +114,12 @@ function GM:CanPlayerDropItem(client, item)
107114

108115
if (inventory) then
109116
local items = inventory:getItems()
117+
for _, item in pairs(items) do
110118

111-
for id, item in pairs(items) do
112119
if (not item.ignoreEquipCheck and item:getData("equip") == true) then
113120
client:notifyLocalized("cantDropBagHasEquipped")
114121
return false
115-
end
122+
end
116123
end
117124
end
118125
end
@@ -192,23 +199,20 @@ function GM:PlayerLoadedChar(client, character, lastChar)
192199
if (lastChar) then
193200
local charEnts = lastChar:getVar("charEnts") or {}
194201

195-
for k, v in ipairs(charEnts) do
202+
for _, v in ipairs(charEnts) do
196203
if (v and IsValid(v)) then
197204
v:Remove()
198205
end
199206
end
200207

201-
lastChar:setVar("charEnts", nil)
208+
lastChar:setVar("charEnts", nil)
202209
end
203210

204211
if (character) then
205-
for k, v in pairs(nut.class.list) do
206-
if (v.faction == client:Team()) then
207-
if (v.isDefault) then
208-
character:setClass(v.index)
209-
210-
break
211-
end
212+
for _, v in pairs(nut.class.list) do
213+
if (v.faction == client:Team()) and v.isDefault then
214+
character:setClass(v.index)
215+
break
212216
end
213217
end
214218
end
@@ -246,10 +250,8 @@ end
246250
function GM:PlayerSay(client, message)
247251
local chatType, message, anonymous = nut.chat.parse(client, message, true)
248252

249-
if (chatType == "ic") then
250-
if (nut.command.parse(client, message)) then
251-
return ""
252-
end
253+
if (chatType == "ic") and (nut.command.parse(client, message)) then
254+
return ""
253255
end
254256

255257
nut.chat.send(client, chatType, message, anonymous)
@@ -309,7 +311,7 @@ function GM:PlayerSpawnVehicle(client, model, name, data)
309311
return client:getChar():hasFlags("C")
310312
end
311313
end
312-
314+
313315
return false
314316
end
315317

@@ -320,7 +322,7 @@ function GM:PlayerLoadout(client)
320322

321323
return
322324
end
323-
325+
324326
client:SetWeaponColor(Vector(client:GetInfo("cl_weaponcolor")))
325327
client:StripWeapons()
326328
client:setLocalVar("blur", nil)
@@ -335,7 +337,7 @@ function GM:PlayerLoadout(client)
335337
client:Give("nut_hands")
336338
client:SetWalkSpeed(nut.config.get("walkSpeed", 130))
337339
client:SetRunSpeed(nut.config.get("runSpeed", 235))
338-
340+
339341
local faction = nut.faction.indices[client:Team()]
340342

341343
if (faction) then
@@ -346,7 +348,7 @@ function GM:PlayerLoadout(client)
346348

347349
-- If the faction has default weapons, give them to the player.
348350
if (faction.weapons) then
349-
for k, v in ipairs(faction.weapons) do
351+
for _, v in ipairs(faction.weapons) do
350352
client:Give(v)
351353
end
352354
end
@@ -361,7 +363,7 @@ function GM:PlayerLoadout(client)
361363
end
362364

363365
if (class.weapons) then
364-
for k, v in ipairs(class.weapons) do
366+
for _, v in ipairs(class.weapons) do
365367
client:Give(v)
366368
end
367369
end
@@ -439,7 +441,7 @@ function GM:PlayerDisconnected(client)
439441
if (character) then
440442
local charEnts = character:getVar("charEnts") or {}
441443

442-
for k, v in ipairs(charEnts) do
444+
for _, v in ipairs(charEnts) do
443445
if (v and IsValid(v)) then
444446
v:Remove()
445447
end
@@ -450,7 +452,7 @@ function GM:PlayerDisconnected(client)
450452
hook.Run("OnCharDisconnect", client, character)
451453
character:save()
452454
end
453-
455+
454456
if (IsValid(client.nutRagdoll)) then
455457
client.nutRagdoll.nutNoReset = true
456458
client.nutRagdoll.nutIgnoreDelete = true
@@ -463,18 +465,18 @@ end
463465
function GM:PlayerAuthed(client, steamID, uniqueID)
464466
nut.log.add(client, "playerConnected", client, steamID)
465467
end
466-
468+
467469
function GM:InitPostEntity()
468470
local doors = ents.FindByClass("prop_door_rotating")
469471

470-
for k, v in ipairs(doors) do
472+
for _, v in ipairs(doors) do
471473
local parent = v:GetOwner()
472474

473475
if (IsValid(parent)) then
474476
v.nutPartner = parent
475477
parent.nutPartner = v
476478
else
477-
for k2, v2 in ipairs(doors) do
479+
for _, v2 in ipairs(doors) do
478480
if (v2:GetOwner() == v) then
479481
v2.nutPartner = v
480482
v.nutPartner = v2
@@ -505,7 +507,7 @@ function GM:ShutDown()
505507

506508
hook.Run("SaveData")
507509

508-
for k, v in ipairs(player.GetAll()) do
510+
for _, v in ipairs(player.GetAll()) do
509511
v:saveNutData()
510512

511513
if (v:getChar()) then
@@ -519,7 +521,7 @@ function GM:PlayerDeathSound()
519521
end
520522

521523
function GM:InitializedSchema()
522-
if (!nut.data.get("date", nil, false, true)) then
524+
if (not nut.data.get("date", nil, false, true)) then
523525
nut.data.set("date", os.time(), false, true)
524526
end
525527

@@ -534,21 +536,21 @@ end
534536

535537
function GM:PlayerCanHearPlayersVoice(listener, speaker)
536538
local allowVoice = nut.config.get("allowVoice")
537-
538-
if (!allowVoice) then
539+
540+
if (not allowVoice) then
539541
return false, false
540542
end
541-
543+
542544
if (listener:GetPos():DistToSqr(speaker:GetPos()) > nut.config.squaredVoiceDistance) then
543545
return false, false
544546
end
545-
547+
546548
return true, true
547549
end
548550

549551
function GM:OnPhysgunFreeze(weapon, physObj, entity, client)
550552
-- Object is already frozen (!?)
551-
if (!physObj:IsMoveable()) then return false end
553+
if (not physObj:IsMoveable()) then return false end
552554
if (entity:GetUnFreezable()) then return false end
553555

554556
physObj:EnableMotion(false)
@@ -598,15 +600,16 @@ function GM:CharacterPreSave(character)
598600
if (not character:getInv()) then
599601
return
600602
end
601-
for k, v in pairs(character:getInv():getItems()) do
603+
for _, v in pairs(character:getInv():getItems()) do
602604
if (v.onSave) then
603605
v:call("onSave", client)
604606
end
605607
end
606608
end
607609

608610
function GM:OnServerLog(client, logType, ...)
609-
for k, v in pairs(nut.util.getAdmins()) do
611+
for _, v in pairs(nut.util.getAdmins()) do
612+
610613
if (hook.Run("CanPlayerSeeLog", v, logType) ~= false) then
611614
nut.log.send(v, nut.log.getString(client, logType, ...))
612615
end
@@ -650,14 +653,14 @@ PUBLIC SERVICE ANNOUNCEMENT FOR NUTSCRIPT SERVER OWNERS
650653
651654
There is a ENOURMOUS performance issue with ULX Admin mod.
652655
Nutscript Development Team found ULX is the main issue
653-
that make the server freeze when player count is higher
656+
that make the server freeze when player count is higher
654657
than 20-30. The duration of freeze will be increased as you get
655658
more players on your server.
656659
657660
If you're planning to open big server with ULX/ULib, Nutscript
658661
Development Team does not recommend your plan. Server Performance
659-
Issues with ULX/Ulib on your server will be ignored and we're
660-
going to consider that you're taking the risk of ULX/Ulib's
662+
Issues with ULX/Ulib on your server will be ignored and we're
663+
going to consider that you're taking the risk of ULX/Ulib's
661664
critical performance issue.
662665
663666
Nutscript 1.2 only displays this message when you have ULX or
@@ -723,10 +726,10 @@ function GM:CreateSalaryTimer(client)
723726

724727
local faction = nut.faction.indices[character:getFaction()]
725728
local class = nut.class.list[character:getClass()]
726-
729+
727730
local pay = hook.Run("GetSalaryAmount", client, faction, class) or (class and class.pay) or (faction and faction.pay) or nil
728-
729-
if (!pay) then return end
731+
732+
if (not pay) then return end
730733

731734
local timerID = "nutSalary"..client:SteamID()
732735
local timerFunc = timer.Exists(timerID) and timer.Adjust or timer.Create

gamemode/core/libs/item/sv_item.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function nut.item.instance(index, uniqueID, itemData, x, y, callback)
5858

5959
if (MYSQLOO_PREPARED and isnumber(index)) then
6060
nut.db.preparedCall(
61-
"itemInstance", onItemCreated, index, uniqueID, itemData, x, y, itemTable.maxQuantity or 1
61+
"itemInstance", onItemCreated, index, uniqueID, itemData, x, y, itemTable.maxQuantity or 1
6262
)
6363
else
6464
nut.db.insertTable({
@@ -67,7 +67,7 @@ function nut.item.instance(index, uniqueID, itemData, x, y, callback)
6767
_data = itemData,
6868
_x = x,
6969
_y = y,
70-
_quantity = itemTable.maxQuantity or 1
70+
_quantity = itemTable.maxQuantity or 1
7171
}, onItemCreated, "items")
7272
end
7373

gamemode/core/libs/item/sv_networking.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ util.AddNetworkString("nutItemInstance")
44

55
netstream.Hook("invAct", function(client, action, item, invID, data)
66
local character = client:getChar()
7-
if (!character) then
7+
if (not character) then
88
return
99
end
1010

gamemode/core/libs/sh_item.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function nut.item.loadFromDir(directory)
124124
end
125125

126126
function nut.item.new(uniqueID, id)
127+
127128
id = id and tonumber(id) or id
128129
assert(isnumber(id), "non-number ID given to nut.item.new")
129130

plugins/logging.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PLUGIN.author = "Black Tea"
44
PLUGIN.desc = "You can modfiy the logging text/lists on this plugin."
55

66
if (SERVER) then
7-
local L, type, IsValid = Format, type, IsValid
7+
local L, IsValid = Format, IsValid
88

99
nut.log.addType("playerHurt", function(client, attacker, damage, health)
1010
attacker = tostring(attacker)
@@ -104,7 +104,7 @@ if (SERVER) then
104104
local arg = {...}
105105
return (L("%s purchased the door", client:Name()))
106106
end)
107-
107+
108108
nut.log.addType("observerEnter", function(client, ...)
109109
return string.format("%s has entered observer.", client:Name())
110110
end)
@@ -126,7 +126,7 @@ if (SERVER) then
126126
function PLUGIN:OnCharCreated(client, character)
127127
nut.log.add(client, "charCreate", character)
128128
end
129-
129+
130130
function PLUGIN:OnTakeShipmentItem(client, itemClass, amount)
131131
local itemTable = nut.item.list[itemClass]
132132
nut.log.add(client, "shipment", itemTable.name)

0 commit comments

Comments
 (0)