package ru.bclib.blocks; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.PickaxeItem; import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import org.jetbrains.annotations.Nullable; import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.PatternsHelper; import ru.bclib.interfaces.BlockModelProvider; import ru.bclib.interfaces.CustomItemProvider; import ru.bclib.items.BaseAnvilItem; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Random; public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider, CustomItemProvider { public static final IntegerProperty DESTRUCTION = BlockProperties.DESTRUCTION; public IntegerProperty durability; public BaseAnvilBlock(MaterialColor color) { this(FabricBlockSettings.copyOf(Blocks.ANVIL).color(color)); } public BaseAnvilBlock(BlockBehaviour.Properties properties) { super(properties); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { super.createBlockStateDefinition(builder); if (getMaxDurability() != 3) { durability = IntegerProperty.create("durability", 0, getMaxDurability()); } else { durability = BlockProperties.DEFAULT_ANVIL_DURABILITY; } builder.add(DESTRUCTION, durability); } @Override @Environment(EnvType.CLIENT) public BlockModel getItemModel(ResourceLocation blockId) { return getBlockModel(blockId, defaultBlockState()); } @Override @Environment(EnvType.CLIENT) public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { int destruction = blockState.getValue(DESTRUCTION); String name = blockId.getPath(); Map textures = Maps.newHashMap(); textures.put("%modid%", blockId.getNamespace()); textures.put("%anvil%", name); textures.put("%top%", name + "_top_" + destruction); Optional pattern = PatternsHelper.createJson(BasePatterns.BLOCK_ANVIL, textures); return ModelsHelper.fromPattern(pattern); } @Override @Environment(EnvType.CLIENT) public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map modelCache) { int destruction = blockState.getValue(DESTRUCTION); String modId = stateId.getNamespace(); String modelId = "block/" + stateId.getPath() + "_top_" + destruction; ResourceLocation modelLocation = new ResourceLocation(modId, modelId); registerBlockModel(stateId, modelLocation, blockState, modelCache); return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING), false, false); } @Override public BlockItem getCustomItem(ResourceLocation blockID, FabricItemSettings settings) { return new BaseAnvilItem(this, settings); } @Override @SuppressWarnings("deprecation") public List getDrops(BlockState state, LootContext.Builder builder) { int destruction = state.getValue(DESTRUCTION); int durability = state.getValue(getDurabilityProp()); int value = destruction * getMaxDurability() + durability; ItemStack tool = builder.getParameter(LootContextParams.TOOL); if (tool != null && tool.getItem() instanceof PickaxeItem) { ItemStack itemStack = new ItemStack(this); itemStack.getOrCreateTag().putInt(BaseAnvilItem.DESTRUCTION, value); return Lists.newArrayList(itemStack); } return Collections.emptyList(); } public IntegerProperty getDurabilityProp() { return durability; } public int getMaxDurability() { return 3; } public BlockState damageAnvilUse(BlockState state, Random random) { IntegerProperty durability = getDurabilityProp(); int value = state.getValue(durability); if (value < getMaxDurability() && random.nextInt(8) == 0) { return state.setValue(durability, value + 1); } value = state.getValue(DESTRUCTION); return value < 2 ? state.setValue(DESTRUCTION, value + 1).setValue(durability, 0) : null; } public BlockState damageAnvilFall(BlockState state) { int destruction = state.getValue(DESTRUCTION); return destruction < 2 ? state.setValue(DESTRUCTION, destruction + 1) : null; } }