This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/BlockBreakerBlockEntity.java

64 lines
2.6 KiB
Java
Raw Normal View History

2020-07-22 18:07:29 +00:00
package com.thebrokenrail.energonrelics.block.entity;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.api.energy.Action;
2020-07-22 18:07:29 +00:00
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
2020-07-27 18:06:46 +00:00
protected void energyTick() {
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);
2020-07-28 20:38:21 +00:00
}, (world, pos, state) -> {}));
progress = 0;
} else {
progress++;
2020-07-22 18:07:29 +00:00
}
}
}
}
}