forked from TheBrokenRail/EnergonRelics
108 lines
4.3 KiB
Java
108 lines
4.3 KiB
Java
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 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 IndustrialLaserBlockEntity extends FieldProjectorBlockEntity {
|
|
private BlockPos targetPos;
|
|
private BlockState targetState;
|
|
|
|
private int progress = 0;
|
|
|
|
public IndustrialLaserBlockEntity(BlockEntityType<?> type) {
|
|
super(type, state -> EnergonRelics.INDUSTRIAL_LASER_BLOCK);
|
|
}
|
|
|
|
@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());
|
|
|
|
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.VERIDIUM_ORE_BLOCK) {
|
|
AreaEffectCloudEntity areaEffectCloudEntity = new AreaEffectCloudEntity(getWorld(), targetPos.getX() + 0.5d, targetPos.getY() + 0.5d, targetPos.getZ() + 0.5d);
|
|
areaEffectCloudEntity.setRadius(2.1f);
|
|
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(), 1, 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");
|
|
}
|
|
}
|