2020-07-22 18:07:29 +00:00
|
|
|
package com.thebrokenrail.energonrelics.block.entity;
|
|
|
|
|
|
|
|
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.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() {
|
2020-07-24 18:02:42 +00:00
|
|
|
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_IDLE, BlockBreakerBlock.POWERED, true, false));
|
2020-07-22 18:07:29 +00:00
|
|
|
|
|
|
|
if (getCachedState().get(BlockBreakerBlock.POWERED)) {
|
|
|
|
BlockPos targetPos = getPos().offset(getCachedState().get(BlockBreakerBlock.FACING));
|
|
|
|
BlockState target = Objects.requireNonNull(getWorld()).getBlockState(targetPos);
|
|
|
|
|
2020-07-25 21:19:10 +00:00
|
|
|
if (!target.isAir() && target.getHardness(getWorld(), targetPos) != -1 && (!target.isToolRequired() || HardcodedConfig.BLOCK_BREAKER_ITEM.isEffectiveOn(target))) {
|
2020-07-22 18:07:29 +00:00
|
|
|
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;
|
2020-07-24 18:02:42 +00:00
|
|
|
}, (world, pos, state) -> progress = 0));
|
2020-07-22 18:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
progress++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|