From e1679a2d11e19a32b7fe02f115809b25334517a9 Mon Sep 17 00:00:00 2001 From: vietdungdev Date: Wed, 20 May 2026 23:48:50 +0700 Subject: [PATCH] update code --- pkg/data/items.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/data/items.go b/pkg/data/items.go index dd2dc50..ae3d5a4 100644 --- a/pkg/data/items.go +++ b/pkg/data/items.go @@ -17,7 +17,8 @@ type Inventory struct { } func isMatchOwner(owner UnitID, it Item) bool { - if it.Location.LocationType == item.LocationGround { + if it.Location.LocationType == item.LocationUnknown || + it.Location.LocationType == item.LocationGround || it.Location.LocationType == item.LocationVendor { return true } if it.Location.LocationType != item.LocationMercenary && it.Owner == 1 { @@ -29,14 +30,32 @@ func isMatchOwner(owner UnitID, it Item) bool { func (i Inventory) Find(name item.Name, locations ...item.LocationType) (Item, bool) { for _, it := range i.AllItems { - if strings.EqualFold(string(it.Name), string(name)) { + if strings.EqualFold(string(it.Name), string(name)) && isMatchOwner(i.PlayerID, it) { // If no locations are specified, return the first item found if len(locations) == 0 { return it, true } for _, l := range locations { - if it.Location.LocationType == l && isMatchOwner(i.PlayerID, it) { + if it.Location.LocationType == l { + return it, true + } + } + } + } + + return Item{}, false +} + +func (i Inventory) FindByType(t string, locations ...item.LocationType) (Item, bool) { + for _, it := range i.AllItems { + if it.Type().IsType(t) && isMatchOwner(i.PlayerID, it) { + if len(locations) == 0 { + return it, true + } + + for _, l := range locations { + if it.Location.LocationType == l { return it, true } }