Skip to content

Commit 1cf286c

Browse files
committed
Fix pass by ref/val problems + inline call
- v wasn't updated, cache[i2] will be - attributeWeapon func is pretty short and only used once
1 parent 6410361 commit 1cf286c

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

datatables.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,21 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
267267

268268
var cache [maxWeapons]int
269269

270-
for i, v := range cache {
270+
for i := range cache {
271271
i2 := i // Copy for passing to handler
272272
playerEntity.FindProperty(wepPrefix + fmt.Sprintf("%03d", i)).RegisterPropertyUpdateHandler(func(val st.PropValue) {
273273
idx := val.IntVal & indexMask
274274
if idx != indexMask {
275-
if v != 0 {
275+
if cache[i2] != 0 {
276276
// Player already has a weapon in this slot.
277277
pl.RawWeapons[cache[i2]] = nil
278-
cache[i2] = 0
279278
}
280279
cache[i2] = idx
281-
p.attributeWeapon(idx, pl)
280+
281+
// Attribute weapon to player
282+
wep := &p.weapons[idx]
283+
wep.Owner = pl
284+
pl.RawWeapons[idx] = wep
282285
} else {
283286
if cache[i2] != 0 && pl.RawWeapons[cache[i2]] != nil {
284287
pl.RawWeapons[cache[i2]].Owner = nil
@@ -297,12 +300,6 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
297300
}
298301
}
299302

300-
func (p *Parser) attributeWeapon(index int, player *common.Player) {
301-
wep := &p.weapons[index]
302-
wep.Owner = player
303-
player.RawWeapons[index] = wep
304-
}
305-
306303
func (p *Parser) bindWeapons() {
307304
for i := 0; i < maxEntities; i++ {
308305
p.weapons[i] = common.NewEquipment("")

sendtables/entity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ func readFieldIndex(reader *bit.BitReader, lastIndex int, newWay bool) int {
104104
// and we can then take the map as baseline for the new entity.
105105
func (e *Entity) CollectProperties(ppBase *map[int]PropValue) {
106106
for i := range e.props {
107+
i2 := i // Copy for the adder
107108
adder := func(val PropValue) {
108-
(*ppBase)[e.props[i].index] = val
109+
(*ppBase)[e.props[i2].index] = val
109110
}
110111

111112
e.props[i].RegisterPropertyUpdateHandler(adder)

0 commit comments

Comments
 (0)