diff --git a/fabric-tool-attribute-api-v1/build.gradle b/fabric-tool-attribute-api-v1/build.gradle index 25f771ee3..94fb1e049 100644 --- a/fabric-tool-attribute-api-v1/build.gradle +++ b/fabric-tool-attribute-api-v1/build.gradle @@ -2,8 +2,8 @@ archivesBaseName = "fabric-tool-attribute-api-v1" version = getSubprojectVersion(project, "1.2.3") dependencies { - compile project(path: ':fabric-api-base', configuration: 'dev') - compile project(path: ':fabric-tag-extensions-v0', configuration: 'dev') - testmodCompile project(path: ':fabric-object-builder-api-v1', configuration: 'dev') - testmodCompile project(path: ':fabric-events-lifecycle-v0', configuration: 'dev') + compile project(path: ':fabric-api-base', configuration: 'dev') + compile project(path: ':fabric-tag-extensions-v0', configuration: 'dev') + testmodCompile project(path: ':fabric-object-builder-api-v1', configuration: 'dev') + testmodCompile project(path: ':fabric-lifecycle-events-v1', configuration: 'dev') } diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/api/tool/attribute/v1/DynamicAttributeTool.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/api/tool/attribute/v1/DynamicAttributeTool.java index a8f8313bd..229879a54 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/api/tool/attribute/v1/DynamicAttributeTool.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/api/tool/attribute/v1/DynamicAttributeTool.java @@ -107,7 +107,7 @@ public interface DynamicAttributeTool { /** * Add modifiers for any {@link net.minecraft.entity.attribute.EntityAttributes} your item should give when equipped, based on the stack. * - *

Appends to either attribute modifier NBT or the result from {@link net.minecraft.item.Item#getModifiers(EquipmentSlot)}.

+ *

Appends to either attribute modifier NBT or the result from {@link net.minecraft.item.Item#getAttributeModifiers(EquipmentSlot)}.

* * @param slot The equipment slot this item is equipped in. * @param stack The stack that's equipped. diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/ToolManagerImpl.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/ToolManagerImpl.java index eec098273..7e12f8754 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/ToolManagerImpl.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/ToolManagerImpl.java @@ -22,6 +22,7 @@ import java.util.IdentityHashMap; import java.util.Map; import java.util.Objects; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import net.minecraft.block.Block; @@ -120,6 +121,7 @@ public final class ToolManagerImpl { private static ToolHandler toolHandlerInvoker(ToolHandler[] toolHandlers) { return new ToolHandler() { + @NotNull @Override public ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { for (ToolHandler toolHandler : toolHandlers) { @@ -133,6 +135,7 @@ public final class ToolManagerImpl { return ActionResult.PASS; } + @NotNull @Override public TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { for (ToolHandler toolHandler : toolHandlers) { @@ -152,6 +155,7 @@ public final class ToolManagerImpl { return ENTRIES.computeIfAbsent(block, (bb) -> new EntryImpl()); } + @Nullable public static Entry entryNullable(Block block) { return ENTRIES.get(block); } @@ -240,6 +244,7 @@ public final class ToolManagerImpl { * @param user the user involved in breaking the block, null if not applicable. * @return the result of effectiveness */ + @NotNull default ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, @Nullable LivingEntity user) { return ActionResult.PASS; } @@ -253,8 +258,9 @@ public final class ToolManagerImpl { * @param user the user involved in breaking the block, null if not applicable. * @return the result of mining speed. */ + @NotNull default TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, @Nullable LivingEntity user) { - return null; + return TypedActionResult.pass(1.0F); } } } diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsModdedBlocksToolHandler.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsModdedBlocksToolHandler.java index ff2d67f43..f72454e50 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsModdedBlocksToolHandler.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsModdedBlocksToolHandler.java @@ -16,6 +16,8 @@ package net.fabricmc.fabric.impl.tool.attribute.handlers; +import org.jetbrains.annotations.NotNull; + import net.minecraft.block.BlockState; import net.minecraft.entity.LivingEntity; import net.minecraft.item.Item; @@ -34,6 +36,7 @@ import net.fabricmc.fabric.impl.tool.attribute.ToolManagerImpl; *

Only applicable to modded blocks that are registered, as only they have the registered required mining level.

*/ public class ModdedToolsModdedBlocksToolHandler implements ToolManagerImpl.ToolHandler { + @NotNull @Override public ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (stack.getItem() instanceof DynamicAttributeTool) { @@ -50,6 +53,7 @@ public class ModdedToolsModdedBlocksToolHandler implements ToolManagerImpl.ToolH return ActionResult.PASS; } + @NotNull @Override public TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (stack.getItem() instanceof DynamicAttributeTool) { diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsVanillaBlocksToolHandler.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsVanillaBlocksToolHandler.java index c3841a0e0..428021e52 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsVanillaBlocksToolHandler.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ModdedToolsVanillaBlocksToolHandler.java @@ -18,6 +18,8 @@ package net.fabricmc.fabric.impl.tool.attribute.handlers; import java.util.List; +import org.jetbrains.annotations.NotNull; + import net.minecraft.block.BlockState; import net.minecraft.entity.LivingEntity; import net.minecraft.item.Item; @@ -50,6 +52,7 @@ public class ModdedToolsVanillaBlocksToolHandler implements ToolManagerImpl.Tool return (ToolItem) vanillaItems.get(miningLevel); } + @NotNull @Override public ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (stack.getItem() instanceof DynamicAttributeTool) { @@ -69,6 +72,7 @@ public class ModdedToolsVanillaBlocksToolHandler implements ToolManagerImpl.Tool return ActionResult.PASS; } + @NotNull @Override public TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (stack.getItem() instanceof DynamicAttributeTool) { diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ShearsVanillaBlocksToolHandler.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ShearsVanillaBlocksToolHandler.java index 14c4bb656..ef1f716c2 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ShearsVanillaBlocksToolHandler.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/ShearsVanillaBlocksToolHandler.java @@ -16,6 +16,8 @@ package net.fabricmc.fabric.impl.tool.attribute.handlers; +import org.jetbrains.annotations.NotNull; + import net.minecraft.block.BlockState; import net.minecraft.entity.LivingEntity; import net.minecraft.item.Item; @@ -41,6 +43,7 @@ import net.fabricmc.fabric.impl.tool.attribute.ToolManagerImpl; public class ShearsVanillaBlocksToolHandler implements ToolManagerImpl.ToolHandler { private final Item vanillaItem = Items.SHEARS; + @NotNull @Override public ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (ToolManagerImpl.entryNullable(state.getBlock()) != null) { @@ -59,6 +62,7 @@ public class ShearsVanillaBlocksToolHandler implements ToolManagerImpl.ToolHandl return ActionResult.PASS; } + @NotNull @Override public TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { float speed = 1.0F; diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/VanillaToolsModdedBlocksToolHandler.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/VanillaToolsModdedBlocksToolHandler.java index 7efc70a3c..71dde018c 100644 --- a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/VanillaToolsModdedBlocksToolHandler.java +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/VanillaToolsModdedBlocksToolHandler.java @@ -16,6 +16,8 @@ package net.fabricmc.fabric.impl.tool.attribute.handlers; +import org.jetbrains.annotations.NotNull; + import net.minecraft.block.BlockState; import net.minecraft.entity.LivingEntity; import net.minecraft.item.Item; @@ -36,6 +38,7 @@ import net.fabricmc.fabric.impl.tool.attribute.ToolManagerImpl; *

Only applicable to modded blocks that are registered, as only they have the registered required mining level.

*/ public class VanillaToolsModdedBlocksToolHandler implements ToolManagerImpl.ToolHandler { + @NotNull @Override public ActionResult isEffectiveOn(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (!(stack.getItem() instanceof DynamicAttributeTool)) { @@ -51,12 +54,13 @@ public class VanillaToolsModdedBlocksToolHandler implements ToolManagerImpl.Tool return ActionResult.PASS; } + @NotNull @Override public TypedActionResult getMiningSpeedMultiplier(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (!(stack.getItem() instanceof DynamicAttributeTool)) { ToolManagerImpl.Entry entry = ToolManagerImpl.entryNullable(state.getBlock()); - if (entry != null && entry.getMiningLevel(tag) >= 0 && tag.contains(stack.getItem())) { + if (entry != null && entry.getMiningLevel(tag) >= 0) { float multiplier = stack.getItem() instanceof ToolItem ? ((ToolItem) stack.getItem()).getMaterial().getMiningSpeedMultiplier() : stack.getItem().getMiningSpeedMultiplier(stack, state); if (multiplier != 1.0F) return TypedActionResult.success(multiplier); } diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/package-info.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/package-info.java new file mode 100644 index 000000000..d05b632c3 --- /dev/null +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/handlers/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Implementation handlers of the fabric tool attribute module. + */ +@ApiStatus.Internal +package net.fabricmc.fabric.impl.tool.attribute.handlers; + +import org.jetbrains.annotations.ApiStatus; + diff --git a/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/package-info.java b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/package-info.java new file mode 100644 index 000000000..11f2bc03f --- /dev/null +++ b/fabric-tool-attribute-api-v1/src/main/java/net/fabricmc/fabric/impl/tool/attribute/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Implementation of the fabric tool attribute module. + */ +@ApiStatus.Internal +package net.fabricmc.fabric.impl.tool.attribute; + +import org.jetbrains.annotations.ApiStatus; + diff --git a/fabric-tool-attribute-api-v1/src/testmod/java/net/fabricmc/fabric/test/tool/attribute/ToolAttributeTest.java b/fabric-tool-attribute-api-v1/src/testmod/java/net/fabricmc/fabric/test/tool/attribute/ToolAttributeTest.java index f1866260c..dce2f97c5 100644 --- a/fabric-tool-attribute-api-v1/src/testmod/java/net/fabricmc/fabric/test/tool/attribute/ToolAttributeTest.java +++ b/fabric-tool-attribute-api-v1/src/testmod/java/net/fabricmc/fabric/test/tool/attribute/ToolAttributeTest.java @@ -27,6 +27,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.item.ToolItem; +import net.minecraft.item.ToolMaterials; import net.minecraft.server.MinecraftServer; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.tag.Tag; @@ -34,15 +35,18 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.event.server.ServerTickCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricMaterialBuilder; +import net.fabricmc.fabric.api.tag.TagRegistry; import net.fabricmc.fabric.api.tool.attribute.v1.DynamicAttributeTool; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; public class ToolAttributeTest implements ModInitializer { private static final float DEFAULT_BREAK_SPEED = 1.0F; private static final float TOOL_BREAK_SPEED = 10.0F; + // A custom tool type, taters + private static final Tag TATER = TagRegistry.item(new Identifier("fabric-tool-attribute-api-v1-testmod", "taters")); private boolean hasValidated = false; @@ -51,12 +55,18 @@ public class ToolAttributeTest implements ModInitializer { Item testShovel; Item testPickaxe; + Item testStoneLevelTater; + Item testStoneDynamicLevelTater; + Item testDiamondLevelTater; + Item testDiamondDynamicLevelTater; + Block taterEffectiveBlock; + @Override public void onInitialize() { // Register a custom shovel that has a mining level of 2 (iron) dynamically. - testShovel = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_shovel"), new TestTool(new Item.Settings(), FabricToolTags.SHOVELS)); + testShovel = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_shovel"), new TestTool(new Item.Settings(), FabricToolTags.SHOVELS, 2)); //Register a custom pickaxe that has a mining level of 2 (iron) dynamically. - testPickaxe = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_pickaxe"), new TestTool(new Item.Settings(), FabricToolTags.PICKAXES)); + testPickaxe = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_pickaxe"), new TestTool(new Item.Settings(), FabricToolTags.PICKAXES, 2)); // Register a block that requires a shovel that is as strong or stronger than an iron one. gravelBlock = Registry.register(Registry.BLOCK, new Identifier("fabric-tool-attribute-api-v1-testmod", "hardened_gravel_block"), new Block(FabricBlockSettings.of(new FabricMaterialBuilder(MaterialColor.SAND).build(), MaterialColor.STONE) @@ -74,7 +84,24 @@ public class ToolAttributeTest implements ModInitializer { .sounds(BlockSoundGroup.STONE))); Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "hardened_stone_block"), new BlockItem(stoneBlock, new Item.Settings())); - ServerTickCallback.EVENT.register(this::validate); + // Register a tater that has a mining level of 1 (stone). + testStoneLevelTater = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_stone_level_tater"), new ToolItem(ToolMaterials.STONE, new Item.Settings())); + // Register a tater that has a mining level of 1 (stone) dynamically. + testStoneDynamicLevelTater = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_stone_dynamic_level_tater"), new TestTool(new Item.Settings(), TATER, 1)); + //Register a tater that has a mining level of 3 (diamond). + testDiamondLevelTater = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_diamond_level_tater"), new ToolItem(ToolMaterials.DIAMOND, new Item.Settings())); + //Register a tater that has a mining level of 3 (diamond) dynamically. + testDiamondDynamicLevelTater = Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "test_diamond_dynamic_level_tater"), new TestTool(new Item.Settings(), TATER, 3)); + + taterEffectiveBlock = Registry.register(Registry.BLOCK, new Identifier("fabric-tool-attribute-api-v1-testmod", "tater_effective_block"), + new Block(FabricBlockSettings.of(Material.ORGANIC_PRODUCT, MaterialColor.ORANGE) + .breakByTool(TATER, 2) // requires iron tater + .requiresTool() + .strength(0.6F) + .sounds(BlockSoundGroup.CROP))); + Registry.register(Registry.ITEM, new Identifier("fabric-tool-attribute-api-v1-testmod", "tater_effective_block"), new BlockItem(taterEffectiveBlock, new Item.Settings())); + + ServerTickEvents.START_SERVER_TICK.register(this::validate); } private void validate(MinecraftServer server) { @@ -117,6 +144,18 @@ public class ToolAttributeTest implements ModInitializer { testToolOnBlock(new ItemStack(testShovel), Blocks.GRAVEL, false, TOOL_BREAK_SPEED); testToolOnBlock(new ItemStack(testPickaxe), Blocks.GRAVEL, false, DEFAULT_BREAK_SPEED); testToolOnBlock(new ItemStack(testPickaxe), Blocks.STONE, true, TOOL_BREAK_SPEED); + + //Test taters respect our tater block + testToolOnBlock(new ItemStack(testDiamondDynamicLevelTater), taterEffectiveBlock, true, TOOL_BREAK_SPEED); + testToolOnBlock(new ItemStack(testDiamondLevelTater), taterEffectiveBlock, true, ToolMaterials.DIAMOND.getMiningSpeedMultiplier()); + testToolOnBlock(new ItemStack(testStoneDynamicLevelTater), taterEffectiveBlock, false, TOOL_BREAK_SPEED); + testToolOnBlock(new ItemStack(testStoneLevelTater), taterEffectiveBlock, false, ToolMaterials.STONE.getMiningSpeedMultiplier()); + + //Test other tools on our tater block + testToolOnBlock(new ItemStack(testPickaxe), taterEffectiveBlock, false, DEFAULT_BREAK_SPEED); + testToolOnBlock(new ItemStack(testShovel), taterEffectiveBlock, false, DEFAULT_BREAK_SPEED); + testToolOnBlock(new ItemStack(Items.IRON_PICKAXE), taterEffectiveBlock, false, DEFAULT_BREAK_SPEED); + testToolOnBlock(new ItemStack(Items.IRON_SHOVEL), taterEffectiveBlock, false, DEFAULT_BREAK_SPEED); } private void testToolOnBlock(ItemStack item, Block block, boolean inEffective, float inSpeed) { @@ -132,16 +171,18 @@ public class ToolAttributeTest implements ModInitializer { private static class TestTool extends Item implements DynamicAttributeTool { final Tag toolType; + final int miningLevel; - private TestTool(Settings settings, Tag toolType) { + private TestTool(Settings settings, Tag toolType, int miningLevel) { super(settings); this.toolType = toolType; + this.miningLevel = miningLevel; } @Override public int getMiningLevel(Tag tag, BlockState state, ItemStack stack, LivingEntity user) { if (tag.equals(toolType)) { - return 2; + return this.miningLevel; } return 0; diff --git a/fabric-tool-attribute-api-v1/src/testmod/resources/data/fabric-tool-attribute-api-v1-testmod/tags/items/taters.json b/fabric-tool-attribute-api-v1/src/testmod/resources/data/fabric-tool-attribute-api-v1-testmod/tags/items/taters.json new file mode 100644 index 000000000..d2d6a9250 --- /dev/null +++ b/fabric-tool-attribute-api-v1/src/testmod/resources/data/fabric-tool-attribute-api-v1-testmod/tags/items/taters.json @@ -0,0 +1,9 @@ +{ + "replace": false, + "values": [ + "fabric-tool-attribute-api-v1-testmod:test_stone_level_tater", + "fabric-tool-attribute-api-v1-testmod:test_stone_dynamic_level_tater", + "fabric-tool-attribute-api-v1-testmod:test_diamond_level_tater", + "fabric-tool-attribute-api-v1-testmod:test_diamond_dynamic_level_tater" + ] +}