Making Tools/1.17

Revision as of 05:50, 6 December 2021 by ShrimpBot (talk | contribs) (Copy Making Tools to MC1.17 archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Tools are simply an extension of the Item class. Their implementations mainly rely on extending a specific class, the TierSortingRegistry, and tags.

Tier

To create any tool that does not derive from vanilla tiers, you will need your own implementation of Tier. This is the basis of which all tool levels are created. If you would like to use a vanilla tier, then you should specify one of the enums within Tiers.

Here are what each methods defines:

Method Return Type Description
getUses Integer The durability of all items in this tier.
getSpeed Float The efficiency multiplier of all items in this tier.
getAttackDamageBonus Float The base attack damage of all items in this tier.
getLevel Integer The harvest level of this tier. Vanilla uses values 0-4. As of 37.0.31, Forge deprecates this value in favor of the sorting registry system discussed below. This value will be used if the tier is not registered however.
getEnchantmentValue Integer How enchantable an item of this tier is.
getRepairIngredient Ingredient What ingredient can be used to repair this item.
getTag Tag<Block> What blocks this tier can mine. This should only encompass those blocks which are specific for this tier. Any blocks below this tier will be declared while registering. The naming convention of this tag is <modid>:needs_<tier_name>_tool.

Important

An ingredient should be wrapped in a supplier to avoid calling the object directly. Tiers are loaded before registries are populated, so the call to the item needs to be deferred.
Forge has a implementation class called ForgeTier to create a common tier, though this does not have to be used.

TierSortingRegistry

For a tier to be used in the new system, they need to be registered before items most commonly by static initialization. A tier can be registered via TierSortingRegistry#registerTier. Any tier not defined in the sorting system will be defaulted to vanilla behavior.

This has four parameters:

Parameter Type Description
tier Tier The tier object being registered.
name ResourceLocation The name of the tier for dependency resolution.
after List<Object> The list of tiers to place this tier after or the tiers of the same level that are weaker than this one. This can either be a String, ResourceLocation, or Tier where the first two are located by the name parameter above.
before List<Object> The list of tiers to place this tier before or the tiers of the same level that are stronger than this one. This can either be a String, ResourceLocation, or Tier where the first two are located by the name parameter above.
If your tier is equivalent to another tier, then Tier#getTag should return an empty tag reference defined by BlockTags#createOptional and have the equivalent tier be placed in the after list when registering. If the tier is a vanilla tier, you should also specify the next tier above in the before list.

DiggerItem

DiggerItem is the base of which all tool items extend. You do not necessarily have to use this or any of its supertypes/subtypes. However, it is convenient if you are looking for standard behavior. The subtypes normally referenced are AxeItem, HoeItem, PickaxeItem, ShovelItem.

Each tool has four parameters: tier, attack damage, attack speed, and properties. Tiers and properties have already been explained in this and within the item docs. Attack damage specifies how much damage to do above the current base set for that specific tier. Attack speed specifies the speed modifier to apply to your current attack speed (the base attack speed for a player is 4.0D).

If you decide to extend DiggerItem, there will be a fifth parameter which defines a tag of which blocks this item is effective on. The naming convention of these tags is <modid>:mineable/<tool_name>. If this tag should be shared among other mods, the forge namespace should be used instead.

A tool's compatibility and partial implementation is defined by three methods:

Method Return Type Description
getDestroySpeed Float Defines whether a block will be mined faster than an empty hand or wrong tool.
isCorrectToolForDrops Boolean Defines which blocks can drop loot with your tool. If this returns false, then the destroy speed will always slow down mining. This should only be overridden if your item is not a DiggerItem to implement TierSortingRegistry#isCorrectTierForDrops which handles this logic.
canPerformAction Boolean Queries whether a block can perform the associated action. By default, this will do nothing on its own since a ToolAction is just a string. This can be combined with other logic to get the result of the performed action, usually by doing BlockState#getToolModifiedState. New tool actions can be defined via ToolAction#get.

Registering

A tool must be registered the same as an item. Defining the tool type and tier level is done via the mineable tag implemented on your tool and the tier tag on your Tier.