From 853e6e6882969f24547d782d42a3c19df4395064 Mon Sep 17 00:00:00 2001 From: Gargaj Date: Sat, 14 Nov 2015 16:42:26 +0100 Subject: change from single followable item to multiple --- Server/Plugins/APIDump/APIDesc.lua | 2 ++ src/Item.cpp | 30 ++++++++++++++++++++++++++++++ src/Item.h | 2 ++ src/Mobs/Chicken.h | 5 ++++- src/Mobs/Cow.h | 5 ++++- src/Mobs/Mooshroom.h | 5 ++++- src/Mobs/PassiveMonster.cpp | 8 +++++--- src/Mobs/PassiveMonster.h | 5 ++--- src/Mobs/Pig.h | 5 ++++- src/Mobs/Rabbit.h | 7 ++++++- src/Mobs/Sheep.h | 5 ++++- 11 files changed, 67 insertions(+), 12 deletions(-) diff --git a/Server/Plugins/APIDump/APIDesc.lua b/Server/Plugins/APIDump/APIDesc.lua index 02ce1f894..ab3f2c65b 100644 --- a/Server/Plugins/APIDump/APIDesc.lua +++ b/Server/Plugins/APIDump/APIDesc.lua @@ -1550,6 +1550,8 @@ end { Params = "Index, ItemType, ItemCount, ItemDamage", Return = "", Notes = "Sets the item at the specified index to the specified item" }, }, Size = { Params = "", Return = "number", Notes = "Returns the number of items in the collection" }, + Contains = { Params = "{{cItem|cItem}}", Return = "bool", Notes = "Returns true if the collection contains an item that is fully equivalent to the parameter" }, + ContainsType = { Params = "{{cItem|cItem}}", Return = "bool", Notes = "Returns true if the collection contains an item that is the same type as the parameter" }, }, }, -- cItems diff --git a/src/Item.cpp b/src/Item.cpp index 97cda0b95..2eb009d4a 100644 --- a/src/Item.cpp +++ b/src/Item.cpp @@ -459,3 +459,33 @@ void cItems::Set(int a_Idx, short a_ItemType, char a_ItemCount, short a_ItemDama + +bool cItems::Contains(const cItem & a_Item) +{ + for (auto itr : *this) + { + if (a_Item.IsEqual(itr)) + { + return true; + } + } + return false; +} + + + + + +bool cItems::ContainsType(const cItem & a_Item) +{ + for (auto itr : *this) + { + if (a_Item.IsSameType(itr)) + { + return true; + } + } + return false; +} + + diff --git a/src/Item.h b/src/Item.h index 8f47c4177..46b3c2ef1 100644 --- a/src/Item.h +++ b/src/Item.h @@ -239,6 +239,8 @@ public: void Clear (void) {clear(); } size_t Size (void) const { return size(); } void Set (int a_Idx, short a_ItemType, char a_ItemCount, short a_ItemDamage); + bool Contains(const cItem & a_Item); + bool ContainsType(const cItem & a_Item); void Add (short a_ItemType, char a_ItemCount, short a_ItemDamage) { diff --git a/src/Mobs/Chicken.h b/src/Mobs/Chicken.h index 05cf0ed39..ca70657b4 100644 --- a/src/Mobs/Chicken.h +++ b/src/Mobs/Chicken.h @@ -19,7 +19,10 @@ public: virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_SEEDS); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_SEEDS); + } virtual void HandleFalling(void) override; diff --git a/src/Mobs/Cow.h b/src/Mobs/Cow.h index ee1a4fafe..6ce51e806 100644 --- a/src/Mobs/Cow.h +++ b/src/Mobs/Cow.h @@ -20,7 +20,10 @@ public: virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override; virtual void OnRightClicked(cPlayer & a_Player) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_WHEAT); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_WHEAT); + } } ; diff --git a/src/Mobs/Mooshroom.h b/src/Mobs/Mooshroom.h index 754725c92..61cbfc083 100644 --- a/src/Mobs/Mooshroom.h +++ b/src/Mobs/Mooshroom.h @@ -20,7 +20,10 @@ public: virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override; virtual void OnRightClicked(cPlayer & a_Player) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_WHEAT); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_WHEAT); + } } ; diff --git a/src/Mobs/PassiveMonster.cpp b/src/Mobs/PassiveMonster.cpp index c220a7128..a3d51da35 100644 --- a/src/Mobs/PassiveMonster.cpp +++ b/src/Mobs/PassiveMonster.cpp @@ -43,15 +43,17 @@ void cPassiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { CheckEventLostPlayer(); } - cItem FollowedItem = GetFollowedItem(); - if (FollowedItem.IsEmpty()) + cItems FollowedItems; + GetFollowedItems(FollowedItems); + if (FollowedItems.Size() <= 0) { return; } cPlayer * a_Closest_Player = m_World->FindClosestPlayer(GetPosition(), static_cast(m_SightDistance)); if (a_Closest_Player != nullptr) { - if (a_Closest_Player->GetEquippedItem().IsEqual(FollowedItem)) + cItem EquippedItem = a_Closest_Player->GetEquippedItem(); + if (FollowedItems.ContainsType(EquippedItem)) { Vector3d PlayerPos = a_Closest_Player->GetPosition(); MoveToPosition(PlayerPos); diff --git a/src/Mobs/PassiveMonster.h b/src/Mobs/PassiveMonster.h index 7f20c3092..a7e574a1d 100644 --- a/src/Mobs/PassiveMonster.h +++ b/src/Mobs/PassiveMonster.h @@ -20,9 +20,8 @@ public: /** When hit by someone, run away */ virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override; - /** Returns the item that the animal of this class follows when a player holds it in hand - Return an empty item not to follow (default). */ - virtual const cItem GetFollowedItem(void) const { return cItem(); } + /** Returns the items that the animal of this class follows when a player holds it in hand. */ + virtual void GetFollowedItems(cItems & a_Items) { } } ; diff --git a/src/Mobs/Pig.h b/src/Mobs/Pig.h index 0fe4b4fed..56c9f7ed6 100644 --- a/src/Mobs/Pig.h +++ b/src/Mobs/Pig.h @@ -24,7 +24,10 @@ public: virtual void OnRightClicked(cPlayer & a_Player) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_CARROT); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_CARROT); + } bool IsSaddled(void) const { return m_bIsSaddled; } diff --git a/src/Mobs/Rabbit.h b/src/Mobs/Rabbit.h index 56181e3d0..79a9afe15 100644 --- a/src/Mobs/Rabbit.h +++ b/src/Mobs/Rabbit.h @@ -34,7 +34,12 @@ public: CLASS_PROTODEF(cRabbit) virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_CARROT); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_CARROT); + a_Items.Add(E_ITEM_GOLDEN_CARROT); + a_Items.Add(E_BLOCK_DANDELION); + } eRabbitType GetRabbitType() const { return m_Type; } UInt8 GetRabbitTypeAsNumber() const { return static_cast(GetRabbitType()); } diff --git a/src/Mobs/Sheep.h b/src/Mobs/Sheep.h index b6c99ac2a..ff7632eb7 100644 --- a/src/Mobs/Sheep.h +++ b/src/Mobs/Sheep.h @@ -26,7 +26,10 @@ public: virtual void OnRightClicked(cPlayer & a_Player) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; - virtual const cItem GetFollowedItem(void) const override { return cItem(E_ITEM_WHEAT); } + virtual void GetFollowedItems(cItems & a_Items) override + { + a_Items.Add(E_ITEM_WHEAT); + } /** Generates a random color for the sheep like the vanilla server. The percent's where used are from the wiki: http://minecraft.gamepedia.com/Sheep#Breeding */ -- cgit v1.2.3