Clean up to tool-attributes, add annotations and add test for custom … (#1073)

* Clean up to tool-attributes, add annotations and add test for custom tool types.

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Change version to 1.2.3

Signed-off-by: shedaniel <daniel@shedaniel.me>

* remove unrelated change

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Turn build.gradle to tabs

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Fix checkstyle

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Fix some review issues

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Add license to package-info.java

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Remove line between the javadoc and the package line

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Move the @NotNull annotation

Signed-off-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
shedaniel 2020-10-19 01:32:16 +08:00 committed by GitHub
parent 16a658fec4
commit e1e947a65f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 133 additions and 13 deletions

View file

@ -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')
}

View file

@ -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.
*
* <p>Appends to either attribute modifier NBT or the result from {@link net.minecraft.item.Item#getModifiers(EquipmentSlot)}.</p>
* <p>Appends to either attribute modifier NBT or the result from {@link net.minecraft.item.Item#getAttributeModifiers(EquipmentSlot)}.</p>
*
* @param slot The equipment slot this item is equipped in.
* @param stack The stack that's equipped.

View file

@ -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<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> 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<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> tag, BlockState state, ItemStack stack, @Nullable LivingEntity user) {
return null;
return TypedActionResult.pass(1.0F);
}
}
}

View file

@ -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;
* <p>Only applicable to modded blocks that are registered, as only they have the registered required mining level.</p>
*/
public class ModdedToolsModdedBlocksToolHandler implements ToolManagerImpl.ToolHandler {
@NotNull
@Override
public ActionResult isEffectiveOn(Tag<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
if (stack.getItem() instanceof DynamicAttributeTool) {

View file

@ -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<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
if (stack.getItem() instanceof DynamicAttributeTool) {

View file

@ -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<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
float speed = 1.0F;

View file

@ -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;
* <p>Only applicable to modded blocks that are registered, as only they have the registered required mining level.</p>
*/
public class VanillaToolsModdedBlocksToolHandler implements ToolManagerImpl.ToolHandler {
@NotNull
@Override
public ActionResult isEffectiveOn(Tag<Item> 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<Float> getMiningSpeedMultiplier(Tag<Item> 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);
}

View file

@ -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;

View file

@ -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;

View file

@ -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<Item> 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<Item> toolType;
final int miningLevel;
private TestTool(Settings settings, Tag<Item> toolType) {
private TestTool(Settings settings, Tag<Item> toolType, int miningLevel) {
super(settings);
this.toolType = toolType;
this.miningLevel = miningLevel;
}
@Override
public int getMiningLevel(Tag<Item> tag, BlockState state, ItemStack stack, LivingEntity user) {
if (tag.equals(toolType)) {
return 2;
return this.miningLevel;
}
return 0;

View file

@ -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"
]
}