Wednesday 7 January 2015

Calculating rate of damage when mining

The speed at which a block is mined by a given tool is determined by the rate of damage per tick.
The entry point for calculating the rate of damage per tick is
Block.getPlayerRelativeBlockHardness()

By default, this performs the following sequence of calculations for ordinary Items:
1. Block.getBlockHardness()

  • The block hardness is usually set when constructing the block, using .setHardness() or .setUnbreakable().  Typical values are 0.5 for dirt up to 2000 for obsidian.
  • If <0, the block is invulnerable so return 0.0, otherwise:
2. Calculates the digging speed (damage per tick, where 1.0 = 100% damage) and returns it to the caller:



   2a. ForgeHooks.canHarvestBlock returns true if the given tool can harvest the given block:

  • If the block material doesn’t require a tool, return true
  • If the Block.getHarvestTool() has a particular ToolClass, the Item has a matching ToolClass, and the harvest level of the Item is >= the Block harvest level, then return true.
  • Otherwise, raise PlayerEvent.HarvestCheck event to decide (defaults to false).
   2b player.getBreakSpeed() is calculated by multiplying the following numbers together:

  • If the player isn’t holding an item, the base speed is 1.0, otherwise the base digging speed is calculated from block and item using Item.getDigSpeed() below
  • Speed multipliers based on item enchantments and potions
  • Speed slowdowns based on potions, mining underwater or while falling
  •  Modification by the PlayerEvent.BreakSpeed event (if reduced to below zero, does no damage at all).
    2b1 Item.getDigSpeed():

  • This function returns a number from 1 (no modification) to EfficiencyOnProperMaterial, which is a speedup factor based on the material the tool is made of (eg Wood, Iron) – see Item.ToolMaterial.  Ranges from 2.0 for Wood to 12.0 for gold.
  • If the Item is an ItemTool and Block.isToolEffective(ToolClass) returns true for any of the Item’s ToolClasses (which by default checks whether the ToolClass matches Block.getHarvestTool()), it returns EfficiencyOnProperMaterial.
  • Otherwise, it calls Item.getStrVsBlock() to get the speedup factor.
    2b2 Item.getStrVsBlock():

  • For ordinary items, returns 1.0.
  • For ItemTools, it looks in the set of effective blocks, which was provided in the ItemTool constructor.  If any match, it returns EfficiencyOnProperMaterial.  Otherwise 1.0.
  • ItemPickAxe, ItemSword and similar have some special cases as well.

Further Links

~Mining Blocks With Tools
    Logic flow of vanilla block mining code
    Calculating rate of damage when mining


No comments:

Post a Comment