package com.thebrokenrail.energonrelics.block.entity; import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity; import com.thebrokenrail.energonrelics.api.energy.Action; import com.thebrokenrail.energonrelics.block.BlockBreakerBlock; import com.thebrokenrail.energonrelics.config.HardcodedConfig; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.util.math.BlockPos; import net.minecraft.world.explosion.Explosion; import java.util.Objects; public class BlockBreakerBlockEntity extends EnergyReceiverBlockEntity { private int progress = 0; public BlockBreakerBlockEntity(BlockEntityType type) { super(type); } public void resetProgress() { progress = 0; } @Override public CompoundTag toTag(CompoundTag tag) { super.toTag(tag); tag.putInt("Progress", progress); return tag; } @Override public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); progress = tag.getInt("Progress"); } @Override protected void energyTick() { addAction(Action.createBlockStatePropertyAction(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_IDLE, BlockBreakerBlock.POWERED, true, false)); if (getCachedState().get(BlockBreakerBlock.POWERED)) { BlockPos targetPos = getPos().offset(getCachedState().get(BlockBreakerBlock.FACING)); BlockState target = Objects.requireNonNull(getWorld()).getBlockState(targetPos); if (!target.isAir() && target.getHardness(getWorld(), targetPos) != -1 && (!target.isToolRequired() || HardcodedConfig.BLOCK_BREAKER_ITEM.isEffectiveOn(target))) { if (progress >= HardcodedConfig.BLOCK_BREAKER_TIME) { addAction(new Action(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_BREAK, (world, pos, state) -> { world.createExplosion(null, targetPos.getX() + 0.5d, targetPos.getY() + 0.5d, targetPos.getZ() + 0.5d, 0.5f, Explosion.DestructionType.NONE); Block.dropStacks(target, world, targetPos, target.getBlock().hasBlockEntity() ? world.getBlockEntity(targetPos) : null, null, new ItemStack(HardcodedConfig.BLOCK_BREAKER_ITEM)); world.breakBlock(targetPos, false); }, (world, pos, state) -> {})); progress = 0; } else { progress++; } } } } }