From 9cbb3a339f98f1737ea78af7f40e1ae7d25027e8 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 24 Apr 2014 20:41:25 +0200 Subject: Fix armor in survival mode. --- src/Items/ItemArmor.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/Items/ItemHandler.cpp | 7 ++++++ src/Items/ItemHandler.h | 30 +++++++++++++------------- 3 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 src/Items/ItemArmor.h (limited to 'src/Items') diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h new file mode 100644 index 000000000..0f40ac443 --- /dev/null +++ b/src/Items/ItemArmor.h @@ -0,0 +1,54 @@ + +#pragma once + +#include "ItemHandler.h" +#include "../World.h" + + + + + +class cItemArmorHandler : + public cItemHandler +{ +public: + cItemArmorHandler(int a_ItemType) : + cItemHandler(a_ItemType) + { + } + + virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override + { + int SlotNum = -1; + if (ItemCategory::IsHelmet(a_Item.m_ItemType)) + { + SlotNum = 0; + } + else if (ItemCategory::IsChestPlate(a_Item.m_ItemType)) + { + SlotNum = 1; + } + else if (ItemCategory::IsLeggings(a_Item.m_ItemType)) + { + SlotNum = 2; + } + else if (ItemCategory::IsBoots(a_Item.m_ItemType)) + { + SlotNum = 3; + } + + if (!a_Player->GetInventory().GetArmorSlot(SlotNum).IsEmpty()) + { + return false; + } + + a_Player->GetInventory().SetArmorSlot(SlotNum, a_Item.CopyOne()); + a_Player->GetInventory().SetHotbarSlot(a_Player->GetInventory().GetEquippedSlotNum(), cItem()); + return true; + } + +} ; + + + + diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index 5151eac38..2cb1471aa 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -8,6 +8,7 @@ #include "../BlockInServerPluginInterface.h" // Handlers: +#include "ItemArmor.h" #include "ItemBed.h" #include "ItemBoat.h" #include "ItemBow.h" @@ -90,6 +91,12 @@ cItemHandler * cItemHandler::GetItemHandler(int a_ItemType) cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType) { + // Armor + if (ItemCategory::IsArmor(a_ItemType)) + { + return new cItemArmorHandler(a_ItemType); + } + switch(a_ItemType) { default: return new cItemHandler(a_ItemType); diff --git a/src/Items/ItemHandler.h b/src/Items/ItemHandler.h index f3d78335e..4993eac85 100644 --- a/src/Items/ItemHandler.h +++ b/src/Items/ItemHandler.h @@ -21,13 +21,13 @@ class cItemHandler public: cItemHandler(int a_ItemType); - // Force virtual destructor + /** Force virtual destructor */ virtual ~cItemHandler() {} - /// Called when the player tries to use the item (right mouse button). Return false to make the item unusable. DEFAULT: False + /** Called when the player tries to use the item (right mouse button). Return false to make the item unusable. DEFAULT: False */ virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir); - /// Called when the client sends the SHOOT status in the lclk packet + /** Called when the client sends the SHOOT status in the lclk packet */ virtual void OnItemShoot(cPlayer *, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) { UNUSED(a_BlockX); @@ -36,7 +36,7 @@ public: UNUSED(a_BlockFace); } - /// Called every tick while the item is on the player's inventory (Used by maps) - For now, called only for equipped items + /** Called every tick while the item is on the player's inventory (Used by maps) - For now, called only for equipped items */ virtual void OnUpdate(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item) { UNUSED(a_World); @@ -44,16 +44,16 @@ public: UNUSED(a_Item); } - /// Called while the player diggs a block using this item + /** Called while the player diggs a block using this item */ virtual bool OnDiggingBlock(cWorld * a_World, cPlayer * a_Player, const cItem & a_HeldItem, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace); - /// Called when the player destroys a block using this item. This also calls the drop function for the destroyed block + /** Called when the player destroys a block using this item. This also calls the drop function for the destroyed block */ virtual void OnBlockDestroyed(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_X, int a_Y, int a_Z); - /// Called after the player has eaten this item. + /** Called after the player has eaten this item. */ virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item); - /// Returns the maximum stack size for a given item + /** Returns the maximum stack size for a given item */ virtual char GetMaxStackSize(void); struct FoodInfo @@ -70,22 +70,22 @@ public: } } ; - /// Returns the FoodInfo for this item. (FoodRecovery, Saturation and PoisionChance) + /** Returns the FoodInfo for this item. (FoodRecovery, Saturation and PoisionChance) */ virtual FoodInfo GetFoodInfo(); - /// Lets the player eat a selected item. Returns true if the player ate the item + /** Lets the player eat a selected item. Returns true if the player ate the item */ virtual bool EatItem(cPlayer *a_Player, cItem *a_Item); - /// Indicates if this item is a tool + /** Indicates if this item is a tool */ virtual bool IsTool(void); - /// Indicates if this item is food + /** Indicates if this item is food */ virtual bool IsFood(void); - /// Blocks simply get placed + /** Blocks simply get placed */ virtual bool IsPlaceable(void); - /** Called before a block is placed into a world. + /** Called before a block is placed into a world. The handler should return true to allow placement, false to refuse. Also, the handler should set a_BlockType and a_BlockMeta to correct values for the newly placed block. */ @@ -96,7 +96,7 @@ public: BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta ); - /// Returns whether this tool/item can harvest a specific block (e.g. wooden pickaxe can harvest stone, but wood can´t) DEFAULT: False + /** Returns whether this tool/item can harvest a specific block (e.g. wooden pickaxe can harvest stone, but wood can�t) DEFAULT: False */ virtual bool CanHarvestBlock(BLOCKTYPE a_BlockType); static cItemHandler * GetItemHandler(int a_ItemType); -- cgit v1.2.3 From 376dc880b37e1b104c86efebef0f294c34ca3c42 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 24 Apr 2014 23:03:04 +0200 Subject: Add armor to switch() in ItemHandler.cpp --- src/Items/ItemHandler.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/Items') diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index 2cb1471aa..ce9593a70 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -91,12 +91,6 @@ cItemHandler * cItemHandler::GetItemHandler(int a_ItemType) cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType) { - // Armor - if (ItemCategory::IsArmor(a_ItemType)) - { - return new cItemArmorHandler(a_ItemType); - } - switch(a_ItemType) { default: return new cItemHandler(a_ItemType); @@ -229,6 +223,31 @@ cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType) { return new cItemFoodHandler(a_ItemType); } + + // Armor: + case E_ITEM_LEATHER_CAP: + case E_ITEM_GOLD_HELMET: + case E_ITEM_CHAIN_HELMET: + case E_ITEM_IRON_HELMET: + case E_ITEM_DIAMOND_HELMET: + case E_ITEM_LEATHER_TUNIC: + case E_ITEM_GOLD_CHESTPLATE: + case E_ITEM_CHAIN_CHESTPLATE: + case E_ITEM_IRON_CHESTPLATE: + case E_ITEM_DIAMOND_CHESTPLATE: + case E_ITEM_LEATHER_PANTS: + case E_ITEM_GOLD_LEGGINGS: + case E_ITEM_CHAIN_LEGGINGS: + case E_ITEM_IRON_LEGGINGS: + case E_ITEM_DIAMOND_LEGGINGS: + case E_ITEM_LEATHER_BOOTS: + case E_ITEM_GOLD_BOOTS: + case E_ITEM_CHAIN_BOOTS: + case E_ITEM_IRON_BOOTS: + case E_ITEM_DIAMOND_BOOTS: + { + return new cItemArmorHandler(a_ItemType); + } } } -- cgit v1.2.3 From cee70390fae9c240337186e50445e50518ccdba8 Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 25 Apr 2014 00:09:22 +0200 Subject: Add unkown armor warning and fix armor stacks. --- src/Items/ItemArmor.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/Items') diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h index 0f40ac443..a786990fc 100644 --- a/src/Items/ItemArmor.h +++ b/src/Items/ItemArmor.h @@ -19,7 +19,7 @@ public: virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { - int SlotNum = -1; + int SlotNum; if (ItemCategory::IsHelmet(a_Item.m_ItemType)) { SlotNum = 0; @@ -36,6 +36,11 @@ public: { SlotNum = 3; } + else + { + LOGWARNING("Used unknown armor: %i", a_Item.m_ItemType); + return false; + } if (!a_Player->GetInventory().GetArmorSlot(SlotNum).IsEmpty()) { @@ -43,7 +48,14 @@ public: } a_Player->GetInventory().SetArmorSlot(SlotNum, a_Item.CopyOne()); - a_Player->GetInventory().SetHotbarSlot(a_Player->GetInventory().GetEquippedSlotNum(), cItem()); + + cItem Item(a_Item); + Item.m_ItemCount--; + if (Item.m_ItemCount <= 0) + { + Item.Empty(); + } + a_Player->GetInventory().SetHotbarSlot(a_Player->GetInventory().GetEquippedSlotNum(), Item); return true; } -- cgit v1.2.3 From ccd9023202e2a9f97780fd96438c024a83618052 Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 25 Apr 2014 00:10:52 +0200 Subject: Add commit what the code is doing. --- src/Items/ItemArmor.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/Items') diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h index a786990fc..08cddb1ad 100644 --- a/src/Items/ItemArmor.h +++ b/src/Items/ItemArmor.h @@ -17,6 +17,7 @@ public: { } + /** Move the armor to the armor slot of the player's inventory */ virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { int SlotNum; -- cgit v1.2.3 From 0e0c9d82483e01840faf7027fc01a998d5cf2f18 Mon Sep 17 00:00:00 2001 From: archshift Date: Fri, 25 Apr 2014 12:24:09 -0700 Subject: Missed these CMakeLists. --- src/Items/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/Items') diff --git a/src/Items/CMakeLists.txt b/src/Items/CMakeLists.txt index 44a9f594f..a6fe6ea70 100644 --- a/src/Items/CMakeLists.txt +++ b/src/Items/CMakeLists.txt @@ -4,4 +4,9 @@ project (MCServer) include_directories ("${PROJECT_SOURCE_DIR}/../") -add_library(Items ItemHandler) +file(GLOB SOURCE + "*.cpp" + "*.h" +) + +add_library(Items ${SOURCE}) -- cgit v1.2.3 From 477b3e7861ca03aec5c46b4c192f5cc1b5f07c4d Mon Sep 17 00:00:00 2001 From: archshift Date: Sat, 26 Apr 2014 16:45:39 -0700 Subject: Moved cArrowEntity out of ProjectileEntity.h --- src/Items/ItemBow.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Items') diff --git a/src/Items/ItemBow.h b/src/Items/ItemBow.h index 410c5f512..a53d59b58 100644 --- a/src/Items/ItemBow.h +++ b/src/Items/ItemBow.h @@ -9,7 +9,7 @@ #pragma once -#include "../Entities/ProjectileEntity.h" +#include "../Entities/ProjectileArrow.h" -- cgit v1.2.3 From 9b0cb3fd97701370915a9a5b3d5b237fa7f90e06 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 27 Apr 2014 17:03:06 -0700 Subject: Fixed projectile source filenames, indentations --- src/Items/ItemBow.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Items') diff --git a/src/Items/ItemBow.h b/src/Items/ItemBow.h index a53d59b58..8c0b3a0a3 100644 --- a/src/Items/ItemBow.h +++ b/src/Items/ItemBow.h @@ -9,7 +9,7 @@ #pragma once -#include "../Entities/ProjectileArrow.h" +#include "../Entities/ArrowEntity.h" -- cgit v1.2.3 From 5c7876c2dd6aa966fbab6dc0df95ed8d2a17c5c8 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 2 May 2014 19:13:57 +0200 Subject: Added a sanitizer for Spawn egg damage value. This disallows spawning unknown mobs from unknown spawn eggs. Ref.: #928. --- src/Items/ItemSpawnEgg.h | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/Items') diff --git a/src/Items/ItemSpawnEgg.h b/src/Items/ItemSpawnEgg.h index 0d6019398..bba97afa1 100644 --- a/src/Items/ItemSpawnEgg.h +++ b/src/Items/ItemSpawnEgg.h @@ -33,7 +33,10 @@ public: a_BlockY--; } - if (a_World->SpawnMob(a_BlockX + 0.5, a_BlockY, a_BlockZ + 0.5, (cMonster::eType)(a_Item.m_ItemDamage)) >= 0) + cMonster::eType MonsterType = ItemDamageToMonsterType(a_Item.m_ItemDamage); + if ( + (MonsterType != cMonster::mtInvalidType) && // Valid monster type + (a_World->SpawnMob(a_BlockX + 0.5, a_BlockY, a_BlockZ + 0.5, MonsterType) >= 0)) // Spawning succeeded { if (!a_Player->IsGameModeCreative()) { @@ -45,6 +48,41 @@ public: return false; } + + + /** Converts the Spawn egg item damage to the monster type to spawn. + Returns mtInvalidType for invalid damage values. */ + static cMonster::eType ItemDamageToMonsterType(short a_ItemDamage) + { + switch (a_ItemDamage) + { + case E_META_SPAWN_EGG_BAT: return cMonster::mtBat; + case E_META_SPAWN_EGG_BLAZE: return cMonster::mtBlaze; + case E_META_SPAWN_EGG_CAVE_SPIDER: return cMonster::mtCaveSpider; + case E_META_SPAWN_EGG_CHICKEN: return cMonster::mtChicken; + case E_META_SPAWN_EGG_COW: return cMonster::mtCow; + case E_META_SPAWN_EGG_CREEPER: return cMonster::mtCreeper; + case E_META_SPAWN_EGG_ENDERMAN: return cMonster::mtEnderman; + case E_META_SPAWN_EGG_GHAST: return cMonster::mtGhast; + case E_META_SPAWN_EGG_HORSE: return cMonster::mtHorse; + case E_META_SPAWN_EGG_MAGMA_CUBE: return cMonster::mtMagmaCube; + case E_META_SPAWN_EGG_MOOSHROOM: return cMonster::mtMooshroom; + case E_META_SPAWN_EGG_OCELOT: return cMonster::mtOcelot; + case E_META_SPAWN_EGG_PIG: return cMonster::mtPig; + case E_META_SPAWN_EGG_SHEEP: return cMonster::mtSheep; + case E_META_SPAWN_EGG_SILVERFISH: return cMonster::mtSilverfish; + case E_META_SPAWN_EGG_SKELETON: return cMonster::mtSkeleton; + case E_META_SPAWN_EGG_SLIME: return cMonster::mtSlime; + case E_META_SPAWN_EGG_SPIDER: return cMonster::mtSpider; + case E_META_SPAWN_EGG_SQUID: return cMonster::mtSquid; + case E_META_SPAWN_EGG_VILLAGER: return cMonster::mtVillager; + case E_META_SPAWN_EGG_WITCH: return cMonster::mtWitch; + case E_META_SPAWN_EGG_WOLF: return cMonster::mtWolf; + case E_META_SPAWN_EGG_ZOMBIE: return cMonster::mtZombie; + case E_META_SPAWN_EGG_ZOMBIE_PIGMAN: return cMonster::mtZombiePigman; + } + return cMonster::mtInvalidType; + } } ; -- cgit v1.2.3