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/06/09 06:13] – Interface fix boogiemonster1o1tutorial:tools [2023/09/07 05:32] (current) – [Creating Tools] drakonkinst
Line 1: Line 1:
 ====== Adding Tools ====== ====== Adding Tools ======
  
-==== Introduction ==== +==== Creating a Tool Material ====
-Creating a tool is very similar to making armor. It's a bit more complicated than making an item, but it's easy to make multiple of them+
  
-==== Creating Tool Material class ====+Tools require ''<yarn class_1832>'', which defines the following behavior: 
 +  * durability 
 +  * mining speed 
 +  * attack damage 
 +  * mining level 
 +  * enchantability 
 +  * repair ingredient
  
-The first step to creating a tool is to implement the `ToolMaterial` interface in a class+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.
  
-It's best to make the class as an enum, as it doesnt require you to create an object regular class, and enum constants make adding tool materials easier.+Vanilla Tool Materials can be found in ''<yarn class_1834>''. We will create a separate class for our material:
  
- +<yarncode java [enable_line_numbers=true]> 
-<code java [enable_line_numbers=true]> +public class PotatoToolMaterial implements class_1832 
-public enum CustomToolMaterial implements ToolMaterial {; +     
-    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) { +    [...]
-  +
-    }+
 } }
-</code>+</yarncode>
  
-What each argument does:- +''<yarn class_1832>'' has number of methods you will need to implement:
-1. `miningLevel` refers to the strength of the tool necessary to mine any material. Wood is 1, Stone is 2, Iron is 3, Diamond is 4. +
-2. `itemDurability` refers to the initial durability of tool. Gold is 32, Iron is 250, Netherite is 2031. +
-3. `miningSpeed` refers to how fast your tools can break blocks. Gold is 12.0f, Diamond is 8.0f, Iron is 6.0f. +
-4. `attackDamage` refers to the melee damage that your tools perform. Wood is 0.0f, Stone is 1.0f, Diamond is 3.0f +
-5. `enchantability` refers to the chance of getting high level enchantments on your tools. Wood is 15, Stone is 5, Gold is 22, Iron is 14. +
-6. `repairIngredient` refers to the item that can repair your tools in an anvil. This will not be stored in an `Ingredient`, but in a `Lazy<Ingredient>`, which requires a `Supplier` in the constructor.+
  
-To make these values accessible from outside the constructorcreate a final field for each of them and assign their values in the constructor+=== Durability ===  
-<code java [enable_line_numbers=true]> +''<yarn method_8025>'' defines the base durability tools will have when they use this material. In vanillaall tools of the same type have the same durability
-public enum CustomToolMaterial implements ToolMaterial {; +<yarncode java [enable_line_numbers=true]> 
-    private final int miningLevel+@Override 
-    private final int itemDurability; +public int method_8025() 
-    private final float miningSpeed; +    return 500
-    private final float attackDamage; +} 
-    private final int enchantability; +</yarncode>
-    private final Lazy<IngredientrepairIngredient;+
  
-    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<IngredientrepairIngredient) { +=== Mining Speed ===  
-        this.miningLevel = miningLevel; +''<yarn method_8027>'' defines how fast tools are when mining blocksFor a general sense of scale, Wooden has a speed of 2.0F, and Diamond has a speed of 8.0F
-        this.itemDurability = itemDurability; +<yarncode java [enable_line_numbers=true]> 
-        this.miningSpeed = miningSpeed; +@Override 
-        this.attackDamage = attackDamage; +public float method_8027() { 
-        this.enchantability enchantability; +    return 5.0F;
-        this.repairIngredient = new Lazy<>(repairIngredient); +
-    }+
 } }
-</code>+</yarncode>
  
-Now its time to implement the methods from the `ToolMaterial` interface +=== Attack Damage ===  
-You should have something like thisChange the return value of each implemented method to the corresponding field+''<yarn method_8028>'' returns the base damage of the toolNote 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]> 
-public enum CustomToolMaterial implements ToolMaterial {; +@Override 
-    private final int miningLevel+public float method_8028() 
-    private final int itemDurability; +    return 3.0F
-    private final float miningSpeed; +} 
-    private final float attackDamage; +</yarncode>
-    private final int enchantability; +
-    private final Lazy<IngredientrepairIngredient;+
  
-    CustomToolMaterial(int miningLevel, int itemDurability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) { +=== Mining Level ===  
-        this.miningLevel miningLevel; +''<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
-        this.itemDurability itemDurability; +<yarncode java [enable_line_numbers=true]> 
-        this.miningSpeed miningSpeed; +@Override 
-        this.attackDamage attackDamage; +public int method_8024() 
-        this.enchantability = enchantability; +    return 2
-        this.repairIngredient new Lazy<>(repairIngredient); +} 
-    }+</yarncode>
  
-    @Override +=== Enchantability ===  
-    public int getDurability() { +''<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. 
-        return this.itemDurability+<yarncode java [enable_line_numbers=true]> 
-    }+@Override 
 +public int method_8026() { 
 +    return 15
 +} 
 +</yarncode>
  
-    @Override +=== Repair Ingredient ===  
-    public float getMiningSpeed() { +''<yarn method_8023>'' returns the ''<yarn class_1856>'' required to repair a tool in an anvil
-        return this.miningSpeed; +<yarncode java [enable_line_numbers=true]> 
-    } +@Override 
- +public class_1856 method_8023() { 
-    @Override +    return class_1856.method_8091(class_1802.field_8567);
-    public float getAttackDamage() { +
-        return this.attackDamage; +
-    +
- +
-    @Override +
-    public int getMiningLevel() { +
-        return this.miningLevel; +
-    } +
- +
-    @Override +
-    public int getEnchantability() { +
-        return this.enchantability; +
-    } +
- +
-    @Override +
-    public Ingredient getRepairIngredient() { +
-        return this.repairIngredient.get(); +
-    }+
 } }
-</code>+</yarncode>
  
-Next, create an enum constantYou can create multiple enum constants if you need multiple tool materials.  +''<yarn class_1832>''s do //not// have to be registeredA 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: 
-This enum constant is for potato tools.+<yarncode java [enable_line_numbers=true]> 
 +public class PotatoToolMaterial implements class_1832 {
  
-<code java [enable_line_numbers=true]> +    public static final PotatoToolMaterial INSTANCE new PotatoToolMaterial();
-public enum CustomToolMaterial implements ToolMaterial { +
-    POTATO(1, 167, 4.8F, 1.1F, 11, () -> Ingredient.ofItems(Items.POTATO));+
          
     [...]     [...]
 } }
-</code>+</yarncode>
  
 +''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 the tool objects ====+==== Creating Tools ====
  
-Swordsshovels, pickaxes and axes take in four arguments : The Tool Material, The Attack Damage, The Attack Speed and Item Settings+**In newer versionsall 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(CustomToolMaterial.POTATO, 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(CustomToolMaterial.POTATO3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT)); +public static class_1831 POTATO_AXE = new AxeItem(PotatoToolMaterial.INSTANCE7.0F, -3.2F, new FabricItemSettings()); 
-public static ToolItem POTATO_HOE = new HoeItem(CustomToolMaterial.POTATO, -2.0F, new Item.Settings().group(ItemGroup.TOOLS)); +public static class_1831 POTATO_HOE = new HoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new FabricItemSettings()); 
-</code+</yarncode> 
-Unfortunately`PickaxeItem` and `AxeItem` only have protected constructorso you'll have to make classes that extends each of them. Creating a subclass makes making multiple pickaxes or axes easier. + 
-<code java [enable_line_numbers=true]> +=== Creating Tool Subclasses === 
-public class PickaxeSubclass extends PickaxeItem + 
-    public PickaxeSubclass(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) {+**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. 
 + 
 +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 constructorswhich 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>
-To make a pickaxe and axe, create objects of the subclasses. +
-<code java> +
-public static ToolItem POTATO_PICKAXE = new PickaxeSubclass(CustomToolMaterial.POTATO, 1, -2.8F, ItemGroup.TOOLS); +
-public static ToolItem POTATO_AXE = new AxeSubclass(CustomToolMaterial.POTATO, 7.0F, -3.2F, ItemGroup.TOOLS); +
-</code>+
  
-If you want to add any special attributes or behavior to your tool, create a class that extends one of the tool itemsand 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 ====
  
-Registering tools is done the same way you would register normal item.+For recap on registering items, read through the item tutorial [[tutorial:items|here]].
  
-<code java [enable_line_numbers=true]> +==== Making your tool work with non-vanilla blocks ====
-    [...] +
-     +
-    @Override +
-    public void onInitialize() { +
-        Registry.register(Registry.ITEM,new Identifier("tutorial","potato_pickaxe"), POTATO_PICKAXE); +
- Registry.register(Registry.ITEM,new Identifier("tutorial","potato_axe"), POTATO_AXE); +
- Registry.register(Registry.ITEM,new Identifier("tutorial","potato_sword"), POTATO_SWORD); +
- Registry.register(Registry.ITEM,new Identifier("tutorial","potato_hoe"), POTATO_HOE); +
-        Registry.register(Registry.ITEM,new Identifier("tutorial","potato_shovel"), POTATO_SHOVEL); +
-    } +
-</code>+
  
 +Visit the last section of https://fabricmc.net/wiki/tutorial:mining_levels
tutorial/tools.1591683208.txt.gz · Last modified: 2020/06/09 06:13 by boogiemonster1o1