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

View file

@ -1,34 +1,15 @@
package ru.betterend.blocks.basis; 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.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.NotNull;
import ru.bclib.blocks.BaseAnvilBlock; import ru.bclib.blocks.BaseAnvilBlock;
import ru.betterend.blocks.complex.MetalMaterial; import ru.betterend.blocks.complex.MetalMaterial;
import ru.betterend.item.EndAnvilItem;
import java.util.List;
import java.util.Objects;
public class EndAnvilBlock extends BaseAnvilBlock { public class EndAnvilBlock extends BaseAnvilBlock {
protected final int level;
protected final Item anvilItem;
protected IntegerProperty durability;
protected MetalMaterial metalMaterial; protected MetalMaterial metalMaterial;
protected int maxDurability; protected final int level;
public EndAnvilBlock(MaterialColor color, int level) { public EndAnvilBlock(MaterialColor color, int level) {
super(color); super(color);
this.anvilItem = new EndAnvilItem(this);
this.level = level; this.level = level;
} }
@ -37,84 +18,7 @@ public class EndAnvilBlock extends BaseAnvilBlock {
this.metalMaterial = metalMaterial; 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() { public int getCraftingLevel() {
return level; 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)); boots = EndItems.registerEndItem(name + "_boots", new EndArmorItem(armor, EquipmentSlot.FEET, itemSettings));
anvilBlock = EndBlocks.registerAnvil( anvilBlock = EndBlocks.registerBlock(
name + "_anvil", name + "_anvil",
new EndAnvilBlock(this, block.defaultMaterialColor(), level) 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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.blocks.BaseAnvilBlock;
import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.interfaces.AnvilScreenHandlerExtended; import ru.betterend.interfaces.AnvilScreenHandlerExtended;
import ru.betterend.recipe.builders.AnvilRecipe; import ru.betterend.recipe.builders.AnvilRecipe;
@ -72,15 +73,15 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
slotsChanged(inputSlots); slotsChanged(inputSlots);
access.execute((world, blockPos) -> { access.execute((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos); BlockState anvilState = world.getBlockState(blockPos);
if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom() BaseAnvilBlock anvil = (BaseAnvilBlock) anvilState.getBlock();
.nextDouble() < 0.1) { if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextDouble() < 0.1) {
BlockState landingState = EndAnvilBlock.applyDamage(anvilState); BlockState damagedState = anvil.damageAnvilUse(anvilState, player.getRandom());
if (landingState == null) { if (damagedState == null) {
world.removeBlock(blockPos, false); world.removeBlock(blockPos, false);
world.levelEvent(1029, blockPos, 0); world.levelEvent(1029, blockPos, 0);
} }
else { else {
world.setBlock(blockPos, landingState, 2); world.setBlock(blockPos, damagedState, 2);
world.levelEvent(1030, blockPos, 0); world.levelEvent(1030, blockPos, 0);
} }
} }

View file

@ -53,7 +53,7 @@ public class AnvilRecipes {
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_HAMMER_HEAD) .setOutput(EndItems.AETERNIUM_HAMMER_HEAD)
.setAnvilLevel(anvilLevel) .setAnvilLevel(anvilLevel)
.setToolLevel(anvilLevel) .setToolLevel(EndToolMaterial.THALLASIUM.getLevel())
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_sword_blade") 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 END_STONE_SMELTER = registerBlock("end_stone_smelter", new EndStoneSmelter());
public static final Block ETERNAL_PEDESTAL = registerBlock("eternal_pedestal", new EternalPedestal()); 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 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 // Technical
public static final Block END_PORTAL_BLOCK = registerEndBlockOnly("end_portal_block", new EndPortalBlock()); 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()); .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) { public static Block registerBlock(ResourceLocation id, Block block) {
if (!Configs.BLOCK_CONFIG.getBooleanRoot(id.getPath(), true)) { if (!Configs.BLOCK_CONFIG.getBooleanRoot(id.getPath(), true)) {
return block; return block;

View file

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

View file

@ -45,7 +45,7 @@
"fabricloader": ">=0.11.6", "fabricloader": ">=0.11.6",
"fabric": ">=0.36.0", "fabric": ">=0.36.0",
"minecraft": ">=1.17", "minecraft": ">=1.17",
"bclib": ">=0.2.4" "bclib": ">=0.3.0"
}, },
"suggests": { "suggests": {
"byg": ">=1.1.3", "byg": ">=1.1.3",