EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/BlockBreakerBlockEntity.java

66 lines
2.7 KiB
Java

package com.thebrokenrail.energonrelics.block.entity;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.BlockBreakerBlock;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.energy.core.util.Action;
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.Identifier;
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 tickEnergy() {
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_IDLE, BlockBreakerBlock.POWERED, true, false, new Identifier(EnergonRelics.NAMESPACE, "block_breaker_idle")));
if (getCachedState().get(BlockBreakerBlock.POWERED)) {
BlockPos targetPos = getPos().offset(getCachedState().get(BlockBreakerBlock.FACING));
BlockState target = Objects.requireNonNull(getWorld()).getBlockState(targetPos);
if (!target.isAir() && (!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);
progress = 0;
}, (world, pos, state) -> progress = 0, new Identifier(EnergonRelics.NAMESPACE, "block_breaker_break")));
}
progress++;
}
}
}
}