User Tools

Site Tools


tutorial:tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:tools [2020/09/14 01:49] – Reformat away from enum, some formatting/cleanup draylartutorial:tools [2023/09/07 05:32] (current) – [Creating Tools] drakonkinst
Line 3: Line 3:
 ==== Creating a Tool Material ==== ==== Creating a Tool Material ====
  
-Tools require a ''ToolMaterial'', which defines the following behavior:+Tools require a ''<yarn class_1832>'', which defines the following behavior:
   * durability   * durability
   * mining speed   * mining speed
Line 13: Line 13:
 In other words, Tool Materials defines the //base// functionality for tools of that type, and tools can choose to use the values provided by the material, or roll with their own. In other words, Tool Materials defines the //base// functionality for tools of that type, and tools can choose to use the values provided by the material, or roll with their own.
  
-Vanilla Tool Materials can be found in ''ToolMaterials''. We will create a separate class for our material:+Vanilla Tool Materials can be found in ''<yarn class_1834>''. We will create a separate class for our material:
  
-<code java [enable_line_numbers=true]> +<yarncode java [enable_line_numbers=true]> 
-public class PotatoToolMaterial implements ToolMaterial {+public class PotatoToolMaterial implements class_1832 {
          
     [...]     [...]
 } }
-</code>+</yarncode>
  
-''ToolMaterial'' has a number of methods you will need to implement:+''<yarn class_1832>'' has a number of methods you will need to implement:
  
 === Durability ===  === Durability === 
-''getDurability'' defines the base durability tools will have when they use this material. In vanilla, all tools of the same type have the same durability. +''<yarn method_8025>'' defines the base durability tools will have when they use this material. In vanilla, all tools of the same type have the same durability. 
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public int getDurability() {+public int method_8025() {
     return 500;     return 500;
 } }
-</code>+</yarncode>
  
 === Mining Speed ===  === Mining Speed === 
-''getMiningSpeedMultiplier'' defines how fast tools are when mining blocks. For a general sense of scale, Wooden has a speed of 2.0F, and Diamond has a speed of 8.0F. +''<yarn method_8027>'' defines how fast tools are when mining blocks. For a general sense of scale, Wooden has a speed of 2.0F, and Diamond has a speed of 8.0F. 
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public float getMiningSpeedMultiplier() {+public float method_8027() {
     return 5.0F;     return 5.0F;
 } }
-</code>+</yarncode>
  
 === Attack Damage ===  === Attack Damage === 
-''getAttackDamage'' returns the base damage of the tool. Note that //most// tools ask for an integer damage amount in their constructor, which means the resulting damage is ''(float) materialDamage + (int) toolDamage + 1''. If you want your tool to entirely control its damage amount in its constructor, you can make your material return an attack damage of 0F.  +''<yarn method_8028>'' returns the base damage of the tool. Note that //most// tools ask for an integer damage amount in their constructor, which means the resulting damage is ''(float) materialDamage + (int) toolDamage + 1''. If you want your tool to entirely control its damage amount in its constructor, you can make your material return an attack damage of 0F.  
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public float getAttackDamage() {+public float method_8028() {
     return 3.0F;     return 3.0F;
 } }
-</code>+</yarncode>
  
 === Mining Level ===  === Mining Level === 
-''getMiningLevel'' sets the mining level of a tool. Diamond has a mining level of 3, and a value of 3+ is required to mine Obsidian. +''<yarn method_8024>'' sets the mining level of a tool. Diamond has a mining level of 3, and a value of 3+ is required to mine Obsidian. 
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public int getMiningLevel() {+public int method_8024() {
     return 2;     return 2;
 } }
-</code>+</yarncode>
  
 === Enchantability ===  === Enchantability === 
-''getEnchantability'' defines how enchantable a tool is. Gold comes in at 22 Enchatability, while Diamond sits at 10. Higher enchantability means better (and higher-level) enchantments. +''<yarn method_8026>'' defines how enchantable a tool is. Gold comes in at 22 Enchatability, while Diamond sits at 10. Higher enchantability means better (and higher-level) enchantments. 
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public int getEnchantability() {+public int method_8026() {
     return 15;     return 15;
 } }
-</code>+</yarncode>
  
 === Repair Ingredient ===  === Repair Ingredient === 
-''getRepairIngredient'' returns the ''Ingredient'' required to repair a tool in an anvil. +''<yarn method_8023>'' returns the ''<yarn class_1856>'' required to repair a tool in an anvil. 
-<code java [enable_line_numbers=true]>+<yarncode java [enable_line_numbers=true]>
 @Override @Override
-public Ingredient getRepairIngredient() { +public class_1856 method_8023() { 
-    return Ingredient.ofItems(Items.POTATO);+    return class_1856.method_8091(class_1802.field_8567);
 } }
-</code>+</yarncode>
  
-''ToolMaterial''s do //not// have to be registered. A good way to pass them out to tools that require them is by keeping an instance somewhere (and then referencing it when you need it). In this case, we will put our instance at the top of the Tool Material class: +''<yarn class_1832>''s do //not// have to be registered. A good way to pass them out to tools that require them is by keeping an instance somewhere (and then referencing it when you need it). In this case, we will put our instance at the top of the Tool Material class: 
-<code java [enable_line_numbers=true]> +<yarncode java [enable_line_numbers=true]> 
-public class PotatoToolMaterial implements ToolMaterial {+public class PotatoToolMaterial implements class_1832 {
  
     public static final PotatoToolMaterial INSTANCE = new PotatoToolMaterial();     public static final PotatoToolMaterial INSTANCE = new PotatoToolMaterial();
Line 86: Line 86:
     [...]     [...]
 } }
-</code>+</yarncode>
  
-''PotatoToolMaterial'' can now be referenced with ''PotatoToolMaterial.INSTANCE''.+''PotatoToolMaterial'' can now be referenced with ''PotatoToolMaterial.INSTANCE''. Alternatively, you can implement an enum similar to vanilla's ''ToolMaterials'' class that implements ''ToolMaterial'', which provides the instance and also allows easy creation of multiple tool materials.
  
 ==== Creating Tools ==== ==== Creating Tools ====
  
-All base tool classes (''PickaxeItem'', ''ShovelItem'', ''HoeItem'', ''AxeItem'', ''SwordItem'') require a ''ToolMaterial'', an attack speed (float), an additional attack damage amount (int), and an ''Item.Settings'' instance.+**In newer versions, all base tool class constructors are public and can be used directly to register the item.** This constructor lets you specify attack damage and attack speed of the tool.
  
-<code java [enable_line_numbers=true]+<yarncode java> 
-public static ToolItem POTATO_SHOVEL = new ShovelItem(PotatoToolMaterial.INSTANCE, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS)); +public static class_1831 POTATO_PICKAXE = new PickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new FabricItemSettings()); 
-public static ToolItem POTATO_SWORD = new SwordItem(PotatoToolMaterial.INSTANCE, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT)); +public static class_1831 POTATO_AXE = new AxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new FabricItemSettings()); 
-</code>+public static class_1831 POTATO_HOE = new HoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new FabricItemSettings()); 
 +</yarncode>
  
-`PickaxeItem` `HoeItem` and `AxeItem` have protected constructors, which means you will need to create your own sub-class with public constructor:  +=== Creating Tool Subclasses === 
-<code java [enable_line_numbers=true]> + 
-public class CustomPickaxeItem extends PickaxeItem +**This section is not necessary in the current version of Fabric.** This is a good way to implement special attributes or behaviors for your tool, however. 
-    public PickaxeSubclass(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) {+ 
 +In older versions, all base tool classes (''<yarn class_1810>'', ''<yarn class_1821>'', ''<yarn class_1794>'', ''<yarn class_1743>'', ''<yarn class_1829>'') require a ''<yarn class_1832>'', an attack speed (float), an additional attack damage amount (float for <yarn class_1743>; int for the rest), and an ''<yarn class_1792.class_1793>'' instance. 
 + 
 +''<yarn class_1810>'' , ''<yarn class_1794>'' and ''<yarn class_1743>'' have protected constructors, which means you will need to create your own classes with public constructors:  
 +<yarncode java [enable_line_numbers=true]> 
 +public class CustomPickaxeItem extends class_1810 
 +    public CustomPickaxeItem(class_1832 material, int attackDamage, float attackSpeed, class_1793 settings) {
         super(material, attackDamage, attackSpeed, settings);         super(material, attackDamage, attackSpeed, settings);
     }     }
 } }
-</code> +</yarncode>
- +
-Using the custom subclass: +
-<code java> +
-public static ToolItem POTATO_PICKAXE = new CustomPickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new Item.Settings().group(ItemGroup.TOOLS)); +
-public static ToolItem POTATO_AXE = new CustomAxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new Item.Settings().group(ItemGroup.TOOLS)); +
-public static ToolItem POTATO_HOE = new CustomHoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new Item.Settings().group(ItemGroup.TOOLS)); +
-</code>+
  
-If you want to add any special attributes or behaviors to your tool, create a subclass that extends one of the base tool classes, and override any required methods.+Using the public classes
 +<yarncode java> 
 +public static class_1831 POTATO_PICKAXE = new CustomPickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new class_1792.class_1793()); 
 +public static class_1831 POTATO_AXE = new CustomAxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new class_1792.class_1793()); 
 +public static class_1831 POTATO_HOE = new CustomHoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2Fnew class_1792.class_1793()); 
 +</yarncode>
  
 ==== Registering Tools ==== ==== Registering Tools ====
tutorial/tools.1600048155.txt.gz · Last modified: 2020/09/14 01:49 by draylar