1
0
Fork 0

Item durability loss now depends on the item used. (#4123)

Armour durability also no longer changes when it
is used to break blocks or attack mobs.

Fixes #4119
This commit is contained in:
Alexander Harkness 2018-01-05 11:28:06 +00:00 committed by GitHub
parent 757231cc6e
commit b4aa19f329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 111 additions and 208 deletions

View File

@ -3,6 +3,7 @@
#include "BlockHandler.h"
#include "ChunkInterface.h"
#include "../Items/ItemHandler.h"
@ -132,6 +133,7 @@ public:
)
{
a_ChunkInterface.SetBlock(BlockPos.x, BlockPos.y, BlockPos.z, 0, 0);
a_Player.UseEquippedItem(cItemHandler::dlaBreakBlockInstant);
}
}
@ -184,7 +186,3 @@ public:
return 7;
}
} ;

View File

@ -2330,35 +2330,25 @@ bool cPlayer::SaveToDisk()
void cPlayer::UseEquippedItem(int a_Amount)
void cPlayer::UseEquippedItem(short a_Damage)
{
if (IsGameModeCreative() || IsGameModeSpectator()) // No damage in creative or spectator
// No durability loss in creative or spectator modes:
if (IsGameModeCreative() || IsGameModeSpectator())
{
return;
}
// If the item has an unbreaking enchantment, give it a random chance of not breaking:
// If the item has an unbreaking enchantment, give it a chance of escaping damage:
// Ref: https://minecraft.gamepedia.com/Enchanting#Unbreaking
cItem Item = GetEquippedItem();
int UnbreakingLevel = static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchUnbreaking));
if (UnbreakingLevel > 0)
double chance = 1 - (1.0 / (UnbreakingLevel + 1));
if (GetRandomProvider().RandBool(chance))
{
double chance = 0.0;
if (ItemCategory::IsArmor(Item.m_ItemType))
{
chance = 0.6 + (0.4 / (UnbreakingLevel + 1));
}
else
{
chance = 1.0 / (UnbreakingLevel + 1);
}
if (GetRandomProvider().RandBool(chance))
{
return;
}
return;
}
if (GetInventory().DamageEquippedItem(static_cast<Int16>(a_Amount)))
if (GetInventory().DamageEquippedItem(a_Damage))
{
m_World->BroadcastSoundEffect("entity.item.break", GetPosition(), 0.5f, static_cast<float>(0.75 + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
}
@ -2368,6 +2358,21 @@ void cPlayer::UseEquippedItem(int a_Amount)
void cPlayer::UseEquippedItem(cItemHandler::eDurabilityLostAction a_Action)
{
// Get item being used:
cItem Item = GetEquippedItem();
// Get base damage for action type:
short Dmg = cItemHandler::GetItemHandler(Item)->GetDurabilityLossByAction(a_Action);
UseEquippedItem(Dmg);
}
void cPlayer::HandleFood(void)
{
// Ref.: https://minecraft.gamepedia.com/Hunger

View File

@ -5,6 +5,7 @@
#include "../Inventory.h"
#include "../Defines.h"
#include "../World.h"
#include "../Items/ItemHandler.h"
#include "../Statistics.h"
@ -413,7 +414,13 @@ public:
If the player is not riding a horse or if the horse is untamed, does nothing. */
void OpenHorseInventory();
void UseEquippedItem(int a_Amount = 1);
/** Damage the player's equipped item by a_Damage, possibly less if the
equipped item is enchanted. */
void UseEquippedItem(short a_Damage = 1);
/** Damage the player's equipped item by the amount of damage such an item
is damaged by when used for a_Action */
void UseEquippedItem(cItemHandler::eDurabilityLostAction a_Action);
void SendHealth(void);

View File

@ -270,7 +270,3 @@ public:
int m_MaxAmount;
int m_Weight;
} ;

View File

@ -18,7 +18,20 @@ public:
}
virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block)
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) override
{
switch (a_Action)
{
case dlaAttackEntity: return 2;
case dlaBreakBlock: return 1;
case dlaBreakBlockInstant: return 0;
}
}
virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block) override
{
if (!IsBlockMaterialWood(a_Block) && !IsBlockMaterialPlants(a_Block) && !IsBlockMaterialVine(a_Block))
{
@ -40,7 +53,3 @@ public:
}
} ;

View File

@ -1,12 +1,4 @@
// ItemBigFlower.h
// Declares the cItemBigFlower class representing the cItemHandler for big flowers
#pragma once
#include "ItemHandler.h"
@ -61,7 +53,3 @@ public:
return true;
}
};

View File

@ -1,10 +1,3 @@
// ItemBoat.h
// Declares the various boat ItemHandlers
#pragma once
@ -109,7 +102,3 @@ public:
return true;
}
} ;

View File

@ -1,15 +1,8 @@
// ItemBow.h
// Declares the cItemBowHandler class representing the itemhandler for bows
#pragma once
#include "../Entities/ArrowEntity.h"
#include "ItemHandler.h"
@ -101,8 +94,3 @@ public:
}
}
} ;

View File

@ -1,12 +1,4 @@
// ItemChest.h
// Declares the cItemChestHandler class representing the cItemHandler descendant responsible for chests
#pragma once
#include "ItemHandler.h"
@ -164,7 +156,3 @@ public:
private:
cItemChestHandler(const cItemChestHandler &) = delete;
};

View File

@ -1,10 +1,4 @@
// ItemEmptyMap.h
#pragma once
#include "../Item.h"
@ -64,7 +58,3 @@ public:
return true;
}
} ;

View File

@ -1,12 +1,4 @@
// ItemFishingRod.h
// Declares the various fishing rod ItemHandlers
#pragma once
#include "../Entities/Floater.h"
@ -240,7 +232,3 @@ public:
return true;
}
} ;

View File

@ -1,3 +1,4 @@
#pragma once
#include "ItemFood.h"
@ -48,7 +49,3 @@ public:
}
};

View File

@ -494,10 +494,8 @@ void cItemHandler::OnBlockDestroyed(cWorld * a_World, cPlayer * a_Player, const
Handler->DropBlock(ChunkInterface, *a_World, PluginInterface, a_Player, a_BlockX, a_BlockY, a_BlockZ, CanHarvestBlock(Block));
}
if (!cBlockInfo::IsOneHitDig(Block))
{
a_Player->UseEquippedItem(GetDurabilityLossByAction(dlaBreakBlock));
}
auto Action = (cBlockInfo::IsOneHitDig(Block) ? dlaBreakBlockInstant : dlaBreakBlock);
a_Player->UseEquippedItem(Action);
}
@ -507,7 +505,7 @@ void cItemHandler::OnBlockDestroyed(cWorld * a_World, cPlayer * a_Player, const
void cItemHandler::OnEntityAttack(cPlayer * a_Attacker, cEntity * a_AttackedEntity)
{
UNUSED(a_AttackedEntity);
a_Attacker->UseEquippedItem(GetDurabilityLossByAction(dlaAttackEntity));
a_Attacker->UseEquippedItem(dlaAttackEntity);
}
@ -527,15 +525,9 @@ void cItemHandler::OnFoodEaten(cWorld * a_World, cPlayer * a_Player, cItem * a_I
short cItemHandler::GetDurabilityLossByAction(eDurabilityLostAction a_Action)
{
switch (a_Action)
{
case dlaAttackEntity: return 2;
case dlaBreakBlock: return 1;
}
UNUSED(a_Action);
#ifndef __clang__
return 0;
#endif
}
@ -865,4 +857,3 @@ float cItemHandler::GetBlockBreakingStrength(BLOCKTYPE a_Block)
{
return 1.0f;
}

View File

@ -21,10 +21,14 @@ class cItemHandler
{
public:
/** Actions that may cause durability of an item may be lost, where the
magnitude of the loss depends on the specific item used to perform the
action */
enum eDurabilityLostAction
{
dlaBreakBlock,
dlaAttackEntity,
dlaBreakBlock,
dlaBreakBlockInstant,
};
cItemHandler(int a_ItemType);
@ -107,7 +111,9 @@ public:
/** Called after the player has eaten this item. */
virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item);
/** Get the durability lost which the item will get, when a specified action was performed. */
/** Get the durability lost which the item will get, when a specified action
was performed. This is only relevant for uses where the damage taken may
depend on the item used. */
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action);
/** Returns the maximum stack size for a given item */

View File

@ -69,9 +69,11 @@ public:
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) override
{
return 0;
switch (a_Action)
{
case dlaAttackEntity: return 1;
case dlaBreakBlock: return 0;
case dlaBreakBlockInstant: return 0;
}
}
} ;

View File

@ -81,7 +81,3 @@ public:
return false;
}
} ;

View File

@ -1,3 +1,4 @@
#pragma once
#include "ItemHandler.h"
@ -104,7 +105,3 @@ public:
return false;
}
};

View File

@ -1,10 +1,4 @@
// ItemMap.h
#pragma once
#include "../Item.h"

View File

@ -1,6 +1,10 @@
#pragma once
class cItemMilkHandler:
public cItemHandler
{

View File

@ -1,10 +1,3 @@
// ItemMinecart.h
// Declares the various minecart ItemHandlers
#pragma once
@ -73,7 +66,3 @@ public:
}
} ;

View File

@ -16,6 +16,20 @@ public:
}
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) override
{
switch (a_Action)
{
case dlaAttackEntity: return 2;
case dlaBreakBlock: return 1;
case dlaBreakBlockInstant: return 0;
}
}
char PickaxeLevel()
{
switch (m_ItemType)
@ -173,7 +187,3 @@ public:
}
} ;

View File

@ -1,12 +1,4 @@
// ItemPumpkin.h
// Declares the cItemPumpkinHandler class representing the pumpkin block in its item form
#pragma once
#include "ItemHandler.h"
@ -150,7 +142,3 @@ public:
return false;
}
};

View File

@ -63,7 +63,12 @@ public:
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) override
{
return 1;
switch (a_Action)
{
case dlaAttackEntity: return 0;
case dlaBreakBlock: return 0;
case dlaBreakBlockInstant: return 1;
}
}
@ -95,7 +100,3 @@ public:
}
} ;

View File

@ -21,6 +21,20 @@ public:
{
}
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) override
{
switch (a_Action)
{
case dlaAttackEntity: return 2;
case dlaBreakBlock: return 1;
case dlaBreakBlockInstant: return 0;
}
}
virtual bool OnDiggingBlock(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override
{
BLOCKTYPE Block = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
@ -31,7 +45,7 @@ public:
BlockHandler(Block)->DropBlock(ChunkInterface, *a_World, PluginInterface, a_Player, a_BlockX, a_BlockY, a_BlockZ);
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
a_Player->UseEquippedItem();
a_Player->UseEquippedItem(cItemHandler::dlaBreakBlock);
return true;
}
return false;

View File

@ -1,12 +1,4 @@
// ItemSlab.h
// Declares the cItemSlabHandler responsible for handling slabs, when in their item form.
#pragma once
#include "ItemHandler.h"
@ -126,7 +118,3 @@ protected:
/** The block type to use when the slab combines into a doubleslab block. */
BLOCKTYPE m_DoubleSlabBlockType;
};

View File

@ -46,13 +46,10 @@ public:
{
switch (a_Action)
{
case dlaAttackEntity: return 1;
case dlaBreakBlock: return 2;
case dlaAttackEntity: return 1;
case dlaBreakBlock: return 2;
case dlaBreakBlockInstant: return 0;
}
#ifndef __clang__
return 0;
#endif
}
@ -83,7 +80,3 @@ public:
}
} ;

View File

@ -1,12 +1,6 @@
// ItemThrowable.h
// Declares the itemhandlers for throwable items: eggs, snowballs and ender pearls
#pragma once
@ -157,7 +151,3 @@ public:
}
};

View File

@ -156,4 +156,3 @@ void cCreeper::OnRightClicked(cPlayer & a_Player)
m_BurnedWithFlintAndSteel = true;
}
}

View File

@ -77,4 +77,3 @@ void cMooshroom::OnRightClicked(cPlayer & a_Player)
} break;
}
}

View File

@ -208,4 +208,3 @@ NIBBLETYPE cSheep::GenerateNaturalRandomColor(void)
return E_META_WOOL_PINK;
}
}