From 630d4b3817f64f0d0c4477adb794707b90b5d8f2 Mon Sep 17 00:00:00 2001 From: Ali <47182802+UnknownOre@users.noreply.github.com> Date: Sun, 26 May 2024 23:28:52 +0300 Subject: [PATCH 1/4] strictly remove arrow if it has been picked up --- server/entity/projectile.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/entity/projectile.go b/server/entity/projectile.go index a6dfec91c..e081da618 100644 --- a/server/entity/projectile.go +++ b/server/entity/projectile.go @@ -221,6 +221,12 @@ func (lt *ProjectileBehaviour) tryPickup(e *Ent) { if !ok { continue } + + c := collector.Collect(lt.conf.PickupItem) + if c == 0 { + continue + } + // A collector was within range to pick up the entity. lt.close = true for _, viewer := range w.Viewers(e.pos) { @@ -229,7 +235,6 @@ func (lt *ProjectileBehaviour) tryPickup(e *Ent) { if lt.conf.PickupItem.Empty() { return } - _ = collector.Collect(lt.conf.PickupItem) } } From f7b8a8cd9c5c128a6321d83d302a64e481bcfd64 Mon Sep 17 00:00:00 2001 From: Ali <47182802+UnknownOre@users.noreply.github.com> Date: Sun, 26 May 2024 23:30:35 +0300 Subject: [PATCH 2/4] format --- server/entity/projectile.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/entity/projectile.go b/server/entity/projectile.go index e081da618..4b2bbcff8 100644 --- a/server/entity/projectile.go +++ b/server/entity/projectile.go @@ -222,8 +222,7 @@ func (lt *ProjectileBehaviour) tryPickup(e *Ent) { continue } - c := collector.Collect(lt.conf.PickupItem) - if c == 0 { + if collector.Collect(lt.conf.PickupItem) == 0 { continue } From 88f23884a8360258c6b535dfe4dcb6ee423ea32a Mon Sep 17 00:00:00 2001 From: UnknownOre <47182802+UnknownOre@users.noreply.github.com> Date: Wed, 29 May 2024 06:00:21 +0300 Subject: [PATCH 3/4] Add CanCollect to the Collector interface --- server/entity/item_behaviour.go | 5 +++++ server/entity/projectile.go | 4 +++- server/player/player.go | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/server/entity/item_behaviour.go b/server/entity/item_behaviour.go index 61de607d1..31c1d78d7 100644 --- a/server/entity/item_behaviour.go +++ b/server/entity/item_behaviour.go @@ -132,6 +132,9 @@ func (i *ItemBehaviour) merge(e *Ent, other *Ent) bool { // collect makes a collector collect the item (or at least part of it). func (i *ItemBehaviour) collect(e *Ent, collector Collector) { + if !collector.CanCollect() { + return + } w, pos := e.World(), e.Position() n := collector.Collect(i.i) if n == 0 { @@ -156,6 +159,8 @@ func (i *ItemBehaviour) collect(e *Ent, collector Collector) { // a player or a zombie. type Collector interface { world.Entity + // CanCollect returns whenever the Collector is able to pick up an item or not + CanCollect() bool // Collect collects the stack passed. It is called if the Collector is standing near an item entity that // may be picked up. // The count of items collected from the stack n is returned. diff --git a/server/entity/projectile.go b/server/entity/projectile.go index 4b2bbcff8..812400785 100644 --- a/server/entity/projectile.go +++ b/server/entity/projectile.go @@ -222,7 +222,7 @@ func (lt *ProjectileBehaviour) tryPickup(e *Ent) { continue } - if collector.Collect(lt.conf.PickupItem) == 0 { + if !collector.CanCollect() { continue } @@ -234,6 +234,8 @@ func (lt *ProjectileBehaviour) tryPickup(e *Ent) { if lt.conf.PickupItem.Empty() { return } + + _ = collector.Collect(lt.conf.PickupItem) } } diff --git a/server/player/player.go b/server/player/player.go index 6f12298d2..67f1611df 100644 --- a/server/player/player.go +++ b/server/player/player.go @@ -2046,13 +2046,21 @@ func (p *Player) Rotation() cube.Rotation { return cube.Rotation{p.yaw.Load(), p.pitch.Load()} } -// Collect makes the player collect the item stack passed, adding it to the inventory. The amount of items that could -// be added is returned. -func (p *Player) Collect(s item.Stack) int { +func (p *Player) CanCollect() bool { if p.Dead() { - return 0 + return false } if !p.GameMode().AllowsInteraction() { + return false + } + + return true +} + +// Collect makes the player collect the item stack passed, adding it to the inventory. The amount of items that could +// be added is returned. +func (p *Player) Collect(s item.Stack) int { + if !p.CanCollect() { return 0 } ctx := event.C() From 6c2a10d8b3ba5609104cad830f22784cee7e7602 Mon Sep 17 00:00:00 2001 From: UnknownOre <47182802+UnknownOre@users.noreply.github.com> Date: Wed, 29 May 2024 06:04:49 +0300 Subject: [PATCH 4/4] document new functions --- server/entity/item_behaviour.go | 2 +- server/player/player.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/server/entity/item_behaviour.go b/server/entity/item_behaviour.go index 31c1d78d7..0d55f742b 100644 --- a/server/entity/item_behaviour.go +++ b/server/entity/item_behaviour.go @@ -159,7 +159,7 @@ func (i *ItemBehaviour) collect(e *Ent, collector Collector) { // a player or a zombie. type Collector interface { world.Entity - // CanCollect returns whenever the Collector is able to pick up an item or not + // CanCollect returns whether the Collector is able to pick up an item or not. CanCollect() bool // Collect collects the stack passed. It is called if the Collector is standing near an item entity that // may be picked up. diff --git a/server/player/player.go b/server/player/player.go index 67f1611df..7ca1972e8 100644 --- a/server/player/player.go +++ b/server/player/player.go @@ -2046,6 +2046,7 @@ func (p *Player) Rotation() cube.Rotation { return cube.Rotation{p.yaw.Load(), p.pitch.Load()} } +// CanCollect returns whether the player is able to pick up an item stack or not. func (p *Player) CanCollect() bool { if p.Dead() { return false