package com.thebrokenrail.energonrelics.block.entity.forcefield.laser; import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.block.entity.forcefield.FieldProjectorBlockEntity; import com.thebrokenrail.energonrelics.block.forcefield.laser.IndustrialLaserProjectorBlock; import com.thebrokenrail.energonrelics.config.HardcodedConfig; import com.thebrokenrail.energonrelics.potion.CustomPotions; import com.thebrokenrail.energonrelics.registry.laser.IndustrialLaserRegistry; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.TntBlock; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.AreaEffectCloudEntity; import net.minecraft.nbt.CompoundTag; import net.minecraft.particle.ParticleTypes; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.explosion.Explosion; import java.util.Objects; public class IndustrialLaserProjectorBlockEntity extends FieldProjectorBlockEntity { private BlockPos targetPos; private BlockState targetState; private int progress = 0; public IndustrialLaserProjectorBlockEntity(BlockEntityType type) { super(type, state -> EnergonRelics.Blocks.INDUSTRIAL_LASER); } @Override protected void setLastBlock(BlockPos newPos, BlockState newState) { if (!Objects.equals(newPos, targetPos) || targetState != newState) { targetPos = newPos; targetState = newState; progress = 0; } } @Override protected long getCost() { return HardcodedConfig.INDUSTRIAL_LASER_ENERGY_REQUIRED; } @Override protected void energyTick() { super.energyTick(); if (getCachedState().get(IndustrialLaserProjectorBlock.POWERED) && targetState != null && targetPos != null) { assert getWorld() != null; if (targetState.getBlock() == Blocks.TNT) { TntBlock.primeTnt(getWorld(), targetPos); getWorld().setBlockState(targetPos, Blocks.AIR.getDefaultState()); if (progress != 0) { progress = 0; markDirty(); } } else if (IndustrialLaserRegistry.has(targetState.getBlock())) { if (progress >= HardcodedConfig.INDUSTRIAL_LASER_BEAM_TIME) { getWorld().createExplosion(null, targetPos.getX() + 0.5d, targetPos.getY() + 0.5d, targetPos.getZ() + 0.5d, 0.5f, Explosion.DestructionType.NONE); Block.dropStack(getWorld(), targetPos, IndustrialLaserRegistry.get(targetState.getBlock(), getWorld().random)); getWorld().breakBlock(targetPos, false); if (targetState.getBlock() == EnergonRelics.Blocks.VERIDIUM_ORE) { AreaEffectCloudEntity areaEffectCloudEntity = new AreaEffectCloudEntity(getWorld(), targetPos.getX() + 0.5d, targetPos.getY() + 0.5d, targetPos.getZ() + 0.5d); areaEffectCloudEntity.setRadius(1.2f); areaEffectCloudEntity.setRadiusOnUse(-0.5f); areaEffectCloudEntity.setWaitTime(10); areaEffectCloudEntity.setDuration(areaEffectCloudEntity.getDuration() / 2); areaEffectCloudEntity.setRadiusGrowth(-areaEffectCloudEntity.getRadius() / (float) areaEffectCloudEntity.getDuration()); areaEffectCloudEntity.addEffect(CustomPotions.VERIDIUM_POISON_EFFECT.statusEffectInstance); getWorld().spawnEntity(areaEffectCloudEntity); } progress = 0; } else { Vec3d vec = Vec3d.ofCenter(targetPos); ((ServerWorld) getWorld()).spawnParticles(ParticleTypes.SMOKE, vec.getX(), vec.getY(), vec.getZ(), 2, 0.31f, 0.31f, 0.31f, 0f); progress++; } markDirty(); } } else if (progress != 0) { progress = 0; markDirty(); } } @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"); } }