Anvils fixes

This commit is contained in:
paulevsGitch 2021-07-20 03:40:34 +03:00
parent b2431153dc
commit b2bb33d644
9 changed files with 14 additions and 168 deletions

View file

@ -1,22 +1,16 @@
package ru.betterend.blocks;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.item.material.EndToolMaterial;
import ru.betterend.registry.EndBlocks;
public class AeterniumAnvil extends EndAnvilBlock {
public AeterniumAnvil() {
super(EndBlocks.AETERNIUM_BLOCK.defaultMaterialColor(), EndToolMaterial.AETERNIUM.getLevel());
}
@Override
public IntegerProperty getDurability() {
if (durability == null) {
this.maxDurability = 8;
this.durability = IntegerProperty.create("durability", 0, maxDurability);
}
return durability;
public int getMaxDurability() {
return 8;
}
}

View file

@ -1,34 +1,15 @@
package ru.betterend.blocks.basis;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block;
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 org.jetbrains.annotations.NotNull;
import ru.bclib.blocks.BaseAnvilBlock;
import ru.betterend.blocks.complex.MetalMaterial;
import ru.betterend.item.EndAnvilItem;
import java.util.List;
import java.util.Objects;
public class EndAnvilBlock extends BaseAnvilBlock {
protected final int level;
protected final Item anvilItem;
protected IntegerProperty durability;
protected MetalMaterial metalMaterial;
protected int maxDurability;
protected final int level;
public EndAnvilBlock(MaterialColor color, int level) {
super(color);
this.anvilItem = new EndAnvilItem(this);
this.level = level;
}
@ -37,84 +18,7 @@ public class EndAnvilBlock extends BaseAnvilBlock {
this.metalMaterial = metalMaterial;
}
public int getDurability(BlockState blockState) {
Block anvilBlock = blockState.getBlock();
if (anvilBlock instanceof EndAnvilBlock) {
blockState.getValue(durability);
}
return 0;
}
public IntegerProperty getDurability() {
if (durability == null) {
this.maxDurability = 5;
this.durability = IntegerProperty.create("durability", 0, maxDurability);
}
return durability;
}
public int getMaxDurability() {
return maxDurability;
}
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
List<ItemStack> drops = super.getDrops(state, builder);
ItemStack itemStack = drops.get(0);
itemStack.getOrCreateTag().putInt(EndAnvilItem.DURABILITY, state.getValue(durability));
return drops;
}
@Override
public Item asItem() {
return anvilItem;
}
@Override
public BlockState getStateForPlacement(@NotNull BlockPlaceContext blockPlaceContext) {
return Objects.requireNonNull(super.getStateForPlacement(blockPlaceContext))
.setValue(durability, maxDurability);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
builder.add(getDurability());
}
public int getCraftingLevel() {
return level;
}
public static BlockState applyDamage(BlockState blockState) {
Block anvilBlock = blockState.getBlock();
if (anvilBlock instanceof EndAnvilBlock endAnvilBlock) {
IntegerProperty durability = endAnvilBlock.getDurability();
int damage = blockState.getValue(durability) - 1;
if (damage > 0) {
return blockState.setValue(durability, damage);
}
int maxDurability = endAnvilBlock.getMaxDurability();
blockState = blockState.setValue(durability, maxDurability);
}
return getDamagedState(blockState);
}
private static BlockState getDamagedState(BlockState fallingState) {
Block anvilBlock = fallingState.getBlock();
if (anvilBlock instanceof EndAnvilBlock) {
IntegerProperty destructionProperty = EndAnvilBlock.DESTRUCTION;
int destruction = fallingState.getValue(destructionProperty) + 1;
if (destructionProperty.getPossibleValues().contains(destruction)) {
try {
return fallingState.setValue(destructionProperty, destruction);
}
catch (Exception ex) {
return null;
}
}
return null;
}
return AnvilBlock.damage(fallingState);
}
}

View file

@ -203,7 +203,7 @@ public class MetalMaterial {
);
boots = EndItems.registerEndItem(name + "_boots", new EndArmorItem(armor, EquipmentSlot.FEET, itemSettings));
anvilBlock = EndBlocks.registerAnvil(
anvilBlock = EndBlocks.registerBlock(
name + "_anvil",
new EndAnvilBlock(this, block.defaultMaterialColor(), level)
);

View file

@ -1,43 +0,0 @@
package ru.betterend.item;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import ru.bclib.interfaces.ItemModelProvider;
import ru.bclib.items.BaseAnvilItem;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.registry.EndBlocks;
public class EndAnvilItem extends BaseAnvilItem {
public final static String DURABILITY = "durability";
public EndAnvilItem(Block anvilBlock) {
super(anvilBlock, EndBlocks.makeBlockItemSettings());
}
@Override
protected BlockState getPlacementState(BlockPlaceContext blockPlaceContext) {
BlockState blockState = super.getPlacementState(blockPlaceContext);
ItemStack stack = blockPlaceContext.getItemInHand();
int durability = stack.getOrCreateTag().getInt(DURABILITY);
if (blockState != null) {
blockState = blockState.setValue(((EndAnvilBlock) blockState.getBlock()).getDurability(), durability);
}
return blockState;
}
@Override
@Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) {
Block block = getBlock();
ResourceLocation blockId = Registry.BLOCK.getKey(block);
return ((ItemModelProvider) block).getItemModel(blockId);
}
}

View file

@ -18,6 +18,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.blocks.BaseAnvilBlock;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.interfaces.AnvilScreenHandlerExtended;
import ru.betterend.recipe.builders.AnvilRecipe;
@ -72,15 +73,15 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
slotsChanged(inputSlots);
access.execute((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos);
if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom()
.nextDouble() < 0.1) {
BlockState landingState = EndAnvilBlock.applyDamage(anvilState);
if (landingState == null) {
BaseAnvilBlock anvil = (BaseAnvilBlock) anvilState.getBlock();
if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextDouble() < 0.1) {
BlockState damagedState = anvil.damageAnvilUse(anvilState, player.getRandom());
if (damagedState == null) {
world.removeBlock(blockPos, false);
world.levelEvent(1029, blockPos, 0);
}
else {
world.setBlock(blockPos, landingState, 2);
world.setBlock(blockPos, damagedState, 2);
world.levelEvent(1030, blockPos, 0);
}
}

View file

@ -53,7 +53,7 @@ public class AnvilRecipes {
.setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_HAMMER_HEAD)
.setAnvilLevel(anvilLevel)
.setToolLevel(anvilLevel)
.setToolLevel(EndToolMaterial.THALLASIUM.getLevel())
.setDamage(6)
.build();
AnvilRecipe.Builder.create("aeternium_sword_blade")

View file

@ -673,7 +673,7 @@ public class EndBlocks extends BlocksRegistry {
public static final Block END_STONE_SMELTER = registerBlock("end_stone_smelter", new EndStoneSmelter());
public static final Block ETERNAL_PEDESTAL = registerBlock("eternal_pedestal", new EternalPedestal());
public static final Block INFUSION_PEDESTAL = registerBlock("infusion_pedestal", new InfusionPedestal());
public static final Block AETERNIUM_ANVIL = registerAnvil("aeternium_anvil", new AeterniumAnvil());
public static final Block AETERNIUM_ANVIL = registerBlock("aeternium_anvil", new AeterniumAnvil());
// Technical
public static final Block END_PORTAL_BLOCK = registerEndBlockOnly("end_portal_block", new EndPortalBlock());
@ -691,16 +691,6 @@ public class EndBlocks extends BlocksRegistry {
.collect(Collectors.toList());
}
public static Block registerAnvil(String name, EndAnvilBlock anvilBlock) {
if (!Configs.BLOCK_CONFIG.getBooleanRoot(name, true)) {
return anvilBlock;
}
BlocksRegistry registry = getBlockRegistry();
registry.registerBlockOnly(name, anvilBlock);
registry.registerBlockItem(BetterEnd.makeID(name + "_item"), anvilBlock.asItem());
return anvilBlock;
}
public static Block registerBlock(ResourceLocation id, Block block) {
if (!Configs.BLOCK_CONFIG.getBooleanRoot(id.getPath(), true)) {
return block;

View file

@ -54,7 +54,7 @@ public class EndTags {
Properties properties = ((AbstractBlockAccessor) block).getSettings();
Material material = ((AbstractBlockSettingsAccessor) properties).getMaterial();
if (material.equals(Material.STONE) || material.equals(Material.METAL)) {
if (material.equals(Material.STONE) || material.equals(Material.METAL) || material.equals(Material.HEAVY_METAL)) {
TagHelper.addTag(TagAPI.MINEABLE_PICKAXE, block);
}
else if (material.equals(Material.WOOD)) {