EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/forcefield/laser/IndustrialLaserProjectorBlo...

110 lines
4.4 KiB
Java
Raw Normal View History

2020-07-28 20:46:34 +00:00
package com.thebrokenrail.energonrelics.block.entity.forcefield.laser;
2020-07-28 20:38:21 +00:00
import com.thebrokenrail.energonrelics.EnergonRelics;
2020-07-28 20:46:34 +00:00
import com.thebrokenrail.energonrelics.block.entity.forcefield.FieldProjectorBlockEntity;
import com.thebrokenrail.energonrelics.block.forcefield.laser.IndustrialLaserProjectorBlock;
2020-07-28 20:38:21 +00:00
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.potion.CustomPotions;
2020-08-20 00:40:49 +00:00
import com.thebrokenrail.energonrelics.registry.laser.IndustrialLaserRegistry;
2020-07-28 20:38:21 +00:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
2020-07-28 21:45:42 +00:00
import net.minecraft.block.Blocks;
import net.minecraft.block.TntBlock;
2020-07-28 20:38:21 +00:00
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.AreaEffectCloudEntity;
2020-07-28 20:38:21 +00:00
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;
2020-07-28 21:45:42 +00:00
import java.util.Objects;
2020-08-27 01:12:47 +00:00
public class IndustrialLaserProjectorBlockEntity extends FieldProjectorBlockEntity {
2020-07-28 20:38:21 +00:00
private BlockPos targetPos;
private BlockState targetState;
2020-07-28 20:46:34 +00:00
2020-07-28 20:38:21 +00:00
private int progress = 0;
2020-08-27 01:12:47 +00:00
public IndustrialLaserProjectorBlockEntity(BlockEntityType<?> type) {
2020-08-20 23:03:47 +00:00
super(type, state -> EnergonRelics.Blocks.INDUSTRIAL_LASER);
2020-07-28 20:38:21 +00:00
}
@Override
protected void setLastBlock(BlockPos newPos, BlockState newState) {
2020-07-28 21:45:42 +00:00
if (!Objects.equals(newPos, targetPos) || targetState != newState) {
2020-07-28 20:38:21 +00:00
targetPos = newPos;
targetState = newState;
progress = 0;
}
}
@Override
protected long getCost() {
return HardcodedConfig.INDUSTRIAL_LASER_ENERGY_REQUIRED;
}
@Override
protected void energyTick() {
super.energyTick();
2020-07-28 21:45:42 +00:00
if (getCachedState().get(IndustrialLaserProjectorBlock.POWERED) && targetState != null && targetPos != null) {
2020-07-28 20:38:21 +00:00
assert getWorld() != null;
2020-07-28 21:45:42 +00:00
if (targetState.getBlock() == Blocks.TNT) {
TntBlock.primeTnt(getWorld(), targetPos);
getWorld().setBlockState(targetPos, Blocks.AIR.getDefaultState());
2020-07-28 20:38:21 +00:00
2020-08-07 02:07:10 +00:00
if (progress != 0) {
progress = 0;
markDirty();
}
2020-07-28 21:45:42 +00:00
} 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);
2020-07-28 20:38:21 +00:00
2020-08-20 23:03:47 +00:00
if (targetState.getBlock() == EnergonRelics.Blocks.VERIDIUM_ORE) {
AreaEffectCloudEntity areaEffectCloudEntity = new AreaEffectCloudEntity(getWorld(), targetPos.getX() + 0.5d, targetPos.getY() + 0.5d, targetPos.getZ() + 0.5d);
2020-07-28 22:22:44 +00:00
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);
}
2020-07-28 21:45:42 +00:00
progress = 0;
} else {
Vec3d vec = Vec3d.ofCenter(targetPos);
2020-07-28 20:38:21 +00:00
2020-07-30 19:49:18 +00:00
((ServerWorld) getWorld()).spawnParticles(ParticleTypes.SMOKE, vec.getX(), vec.getY(), vec.getZ(), 2, 0.31f, 0.31f, 0.31f, 0f);
2020-07-28 21:45:42 +00:00
progress++;
}
markDirty();
}
2020-07-28 20:38:21 +00:00
} 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");
}
}