tutorial:tools
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:tools [2020/08/07 04:07] – [Creating a Tool Material class] boogiemonster1o1 | tutorial:tools [2021/10/31 09:43] – Updated to fix sentence that said attackDamage was int, while it's float for AxeItem metaltxus | ||
---|---|---|---|
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 | + | Tools require |
+ | * durability | ||
+ | * mining speed | ||
+ | * attack damage | ||
+ | * mining level | ||
+ | * enchantability | ||
+ | * repair ingredient | ||
- | The first step to creating a tool is to implement | + | In other words, Tool Materials defines |
- | + | ||
- | It's best to make the class as an enum, as it doesn' | + | |
+ | Vanilla Tool Materials can be found in '' | ||
<code java [enable_line_numbers=true]> | <code java [enable_line_numbers=true]> | ||
- | public | + | public |
- | | + | |
- | + | | |
- | | + | |
} | } | ||
</ | </ | ||
- | What each argument does:- | + | '' |
- | 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 a tool. Gold is 32, Iron is 250, Netherite is 2031. | + | |
- | 3. `miningSpeedMultiplier ` 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 | + | |
- | 6. `repairIngredient` refers | + | |
- | To make these values accessible from outside | + | === Durability === |
+ | '' | ||
<code java [enable_line_numbers=true]> | <code java [enable_line_numbers=true]> | ||
- | public | + | @Override |
- | | + | public |
- | | + | |
- | | + | } |
- | private final float attackDamage; | + | </code> |
- | private final int enchantability; | + | |
- | private final Lazy<Ingredient> repairIngredient; | + | |
- | CustomToolMaterial(int miningLevel, | + | === Mining Speed === |
- | this.miningLevel | + | '' |
- | this.itemDurability | + | <code java [enable_line_numbers=true]> |
- | this. miningSpeedMultiplier | + | @Override |
- | this.attackDamage | + | public float getMiningSpeedMultiplier() { |
- | this.enchantability = enchantability; | + | |
- | | + | |
- | | + | |
} | } | ||
</ | </ | ||
- | Now its time to implement | + | === Attack Damage === |
- | You should have something like this. Change | + | '' |
<code java [enable_line_numbers=true]> | <code java [enable_line_numbers=true]> | ||
- | public | + | @Override |
- | | + | public |
- | | + | |
- | | + | } |
- | private final float attackDamage; | + | </code> |
- | private final int enchantability; | + | |
- | private final Lazy<Ingredient> repairIngredient; | + | |
- | CustomToolMaterial(int miningLevel, | + | === Mining Level === |
- | this.miningLevel | + | '' |
- | this.itemDurability | + | <code java [enable_line_numbers=true]> |
- | this.miningSpeedMultiplier | + | @Override |
- | this.attackDamage | + | public int getMiningLevel() { |
- | this.enchantability = enchantability; | + | return 2; |
- | | + | } |
- | } | + | </ |
- | | + | === Enchantability === |
- | public int getDurability() { | + | '' |
- | return | + | <code java [enable_line_numbers=true]> |
- | } | + | @Override |
+ | public int getEnchantability() { | ||
+ | return | ||
+ | } | ||
+ | </ | ||
- | @Override | + | === Repair Ingredient === |
- | | + | '' |
- | return this.miningSpeedMultiplier; | + | <code java [enable_line_numbers=true]> |
- | } | + | @Override |
- | + | public | |
- | | + | return |
- | public | + | |
- | return this.attackDamage; | + | |
- | | + | |
- | + | ||
- | @Override | + | |
- | public int getMiningLevel() { | + | |
- | | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int getEnchantability() { | + | |
- | return this.enchantability; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public Ingredient getRepairIngredient() { | + | |
- | return this.repairIngredient.get(); | + | |
- | } | + | |
} | } | ||
</ | </ | ||
- | Next, create an enum constant. You can create multiple enum constants if you need multiple tool materials. | + | '' |
- | This enum constant is for potato tools. | + | <code java [enable_line_numbers=true]> |
+ | public class PotatoToolMaterial implements ToolMaterial { | ||
- | <code java [enable_line_numbers=true]> | + | public static final PotatoToolMaterial INSTANCE |
- | public enum CustomToolMaterial implements ToolMaterial { | + | |
- | POTATO(1, 167, 4.8F, 1.1F, 11, () -> { | + | |
- | return Ingredient.ofItems(Items.POTATO); | + | |
- | }); | + | |
| | ||
[...] | [...] | ||
Line 113: | Line 88: | ||
</ | </ | ||
+ | '' | ||
- | ==== Creating | + | ==== Creating |
- | Swords, shovels, pickaxes and axes take in four arguments : The Tool Material, The Attack Damage, The Attack Speed and Item Settings | + | All base tool classes ('' |
<code java [enable_line_numbers=true]> | <code java [enable_line_numbers=true]> | ||
- | public static ToolItem POTATO_SHOVEL = new ShovelItem(CustomToolMaterial.POTATO, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS)); | + | public static ToolItem POTATO_SHOVEL = new ShovelItem(PotatoToolMaterial.INSTANCE, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS)); |
- | public static ToolItem POTATO_SWORD = new SwordItem(CustomToolMaterial.POTATO, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT)); | + | public static ToolItem POTATO_SWORD = new SwordItem(PotatoToolMaterial.INSTANCE, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT)); |
</ | </ | ||
- | Unfortunately, | + | |
+ | `PickaxeItem` , `HoeItem` and `AxeItem` have protected constructors, | ||
<code java [enable_line_numbers=true]> | <code java [enable_line_numbers=true]> | ||
- | public class PickaxeSubclass | + | public class CustomPickaxeItem |
- | public | + | public |
super(material, | super(material, | ||
} | } | ||
} | } | ||
</ | </ | ||
- | To make a pickaxe, hoe and axe, create objects of the subclasses. | + | |
+ | Using the public classes: | ||
<code java> | <code java> | ||
- | public static ToolItem POTATO_PICKAXE = new PickaxeSubclass(CustomToolMaterial.POTATO, 1, -2.8F, new Item.Settings().group(ItemGroup.TOOLS)); | + | public static ToolItem POTATO_PICKAXE = new CustomPickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new Item.Settings().group(ItemGroup.TOOLS)); |
- | public static ToolItem POTATO_AXE = new AxeSubclass(CustomToolMaterial.POTATO, 7.0F, -3.2F, 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 HoeSubclass(CustomToolMaterial.POTATO, 7, -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)); |
</ | </ | ||
- | If you want to add any special attributes or behavior | + | If you want to add any special attributes or behaviors |
==== Registering Tools ==== | ==== Registering Tools ==== | ||
- | Registering tools is done the same way you would register a normal | + | For a recap on registering items, read through |
- | + | ||
- | <code java [enable_line_numbers=true]> | + | |
- | | + | |
- | + | ||
- | @Override | + | |
- | public void onInitialize() { | + | |
- | Registry.register(Registry.ITEM, | + | |
- | Registry.register(Registry.ITEM, | + | |
- | Registry.register(Registry.ITEM, | + | |
- | Registry.register(Registry.ITEM, | + | |
- | Registry.register(Registry.ITEM, | + | |
- | } | + | |
- | </ | + | |
==== Making your tool work with non-vanilla blocks ==== | ==== Making your tool work with non-vanilla blocks ==== | ||
Visit the last section of https:// | Visit the last section of https:// |
tutorial/tools.txt · Last modified: 2024/08/23 13:25 by solidblock