2020-07-23 17:39:40 +00:00
|
|
|
package com.thebrokenrail.energonrelics.block.entity;
|
|
|
|
|
|
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
|
|
|
import com.thebrokenrail.energonrelics.energy.core.EnergyProviderBlockEntity;
|
|
|
|
import com.thebrokenrail.energonrelics.energy.core.util.Action;
|
|
|
|
import com.thebrokenrail.energonrelics.energy.helper.EnergyGenerator;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.LightningEntity;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.world.LightType;
|
|
|
|
|
2020-07-24 17:25:50 +00:00
|
|
|
import java.util.List;
|
2020-07-23 17:39:40 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class LightningRodBlockEntity extends EnergyProviderBlockEntity implements EnergyGenerator {
|
|
|
|
public LightningRodBlockEntity(BlockEntityType<?> type) {
|
|
|
|
super(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getLight() {
|
|
|
|
if (hasWorld() && Objects.requireNonNull(getWorld()).getDimension().hasSkyLight() && getWorld().isThundering()) {
|
|
|
|
assert getWorld() != null;
|
|
|
|
return getWorld().getLightLevel(LightType.SKY, getPos());
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int cooldown = 0;
|
|
|
|
private long energy = 0;
|
|
|
|
|
|
|
|
private final Random random = new Random();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getDisplayEnergy() {
|
|
|
|
return energy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getEnergy() {
|
|
|
|
return energy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnergy(long energy) {
|
|
|
|
this.energy = energy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-24 17:25:50 +00:00
|
|
|
public List<EnergyProviderBlockEntity> startTick() {
|
2020-07-23 17:39:40 +00:00
|
|
|
if (cooldown <= 0) {
|
|
|
|
energy = 0;
|
|
|
|
|
|
|
|
if (getLight() > 0) {
|
|
|
|
if (random.nextDouble() <= HardcodedConfig.LIGHTNING_ROD_CHANCE) {
|
|
|
|
LightningEntity entity = EntityType.LIGHTNING_BOLT.create(getWorld());
|
|
|
|
assert entity != null;
|
2020-07-23 18:36:50 +00:00
|
|
|
entity.updatePosition(getPos().getX() + 0.5d, getPos().getY(), getPos().getZ() + 0.5d);
|
2020-07-23 17:39:40 +00:00
|
|
|
entity.method_29498(true);
|
|
|
|
Objects.requireNonNull(getWorld()).spawnEntity(entity);
|
|
|
|
|
|
|
|
cooldown = HardcodedConfig.LIGHTNING_ROD_COOLDOWN;
|
|
|
|
energy = HardcodedConfig.LIGHTNING_ROD_ENERGY_OUTPUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cooldown--;
|
|
|
|
}
|
2020-07-24 17:25:50 +00:00
|
|
|
return super.startTick();
|
2020-07-23 17:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag toTag(CompoundTag tag) {
|
|
|
|
super.toTag(tag);
|
|
|
|
tag.putInt("Cooldown", cooldown);
|
|
|
|
tag.putLong("Energy", energy);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromTag(BlockState state, CompoundTag tag) {
|
|
|
|
super.fromTag(state, tag);
|
|
|
|
cooldown = tag.getInt("Cooldown");
|
|
|
|
energy = tag.getLong("Energy");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void handlePropagatedAction(Action.PropagatedAction action) {
|
|
|
|
super.handlePropagatedAction(action);
|
|
|
|
handlePropagatedActionWithGenerator(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnergyProvider() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|