TheBrokenRail
487a1aa6e0
All checks were successful
EnergonRelics/pipeline/head This commit looks good
121 lines
4.7 KiB
Java
121 lines
4.7 KiB
Java
package com.thebrokenrail.energonrelics.block.entity.reactor;
|
|
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
|
import com.thebrokenrail.energonrelics.block.battery.PassiveBatteryControllerBlock;
|
|
import com.thebrokenrail.energonrelics.block.entity.battery.PassiveBatteryControllerBlockEntity;
|
|
import com.thebrokenrail.energonrelics.block.reactor.ReactorControllerBlock;
|
|
import com.thebrokenrail.energonrelics.energy.helper.EnergyGeneratorBlockEntity;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.Direction;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class ReactorControllerBlockEntity extends EnergyGeneratorBlockEntity {
|
|
private static class Reactor {
|
|
private final ReactorCoreBlockEntity core;
|
|
private final ReactorInputBlockEntity[] inputs;
|
|
|
|
private Reactor(ReactorCoreBlockEntity core, ReactorInputBlockEntity[] inputs) {
|
|
this.core = core;
|
|
this.inputs = inputs;
|
|
}
|
|
}
|
|
|
|
public ReactorControllerBlockEntity(BlockEntityType<?> type) {
|
|
super(type);
|
|
}
|
|
|
|
@Override
|
|
protected void serverTick() {
|
|
if (getCachedState().get(ReactorControllerBlock.POWERED)) {
|
|
Reactor reactor = getReactor();
|
|
if (reactor != null && !reactor.core.isReacting()) {
|
|
float fuelMultiplier = 0f;
|
|
for (ReactorInputBlockEntity input : reactor.inputs) {
|
|
if (fuelMultiplier != 0f) {
|
|
break;
|
|
}
|
|
for (int i = 0; i < input.size(); i++) {
|
|
Item item = input.getStack(i).getItem();
|
|
if (item == EnergonRelics.VERIDIUM_INGOT_ITEM) {
|
|
fuelMultiplier = 1f;
|
|
} else if (item == EnergonRelics.VERIDIUM_BLOCK_BLOCK.asItem()) {
|
|
fuelMultiplier = 9f;
|
|
} else if (item == EnergonRelics.VERIDIUM_POWDER_ITEM) {
|
|
fuelMultiplier = 0.25f;
|
|
}
|
|
if (fuelMultiplier != 0f) {
|
|
input.removeStack(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (fuelMultiplier != 0f) {
|
|
reactor.core.startReaction(fuelMultiplier);
|
|
}
|
|
}
|
|
}
|
|
setEnergy(getDisplayEnergy());
|
|
super.serverTick();
|
|
}
|
|
|
|
@Override
|
|
public long getDisplayEnergy() {
|
|
if (getCachedState().get(ReactorControllerBlock.POWERED)) {
|
|
Reactor reactor = getReactor();
|
|
if (reactor != null && reactor.core.isReacting()) {
|
|
return HardcodedConfig.REACTOR_ENERGY_OUTPUT;
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private Reactor getReactor() {
|
|
if (hasWorld()) {
|
|
Direction facing = getCachedState().get(PassiveBatteryControllerBlock.FACING);
|
|
BlockPos corePos = getPos().offset(facing.getOpposite());
|
|
|
|
assert getWorld() != null;
|
|
BlockEntity entity = getWorld().getBlockEntity(corePos);
|
|
if (entity instanceof ReactorCoreBlockEntity) {
|
|
boolean valid = true;
|
|
List<ReactorInputBlockEntity> inputs = new ArrayList<>();
|
|
for (Direction side : Direction.values()) {
|
|
if (side != facing) {
|
|
BlockPos checkPos = corePos.offset(side);
|
|
|
|
BlockEntity potentialInput = getWorld().getBlockEntity(checkPos);
|
|
if (potentialInput instanceof ReactorInputBlockEntity) {
|
|
inputs.add((ReactorInputBlockEntity) potentialInput);
|
|
} else {
|
|
BlockState state = getWorld().getBlockState(checkPos);
|
|
if (state == null || PassiveBatteryControllerBlockEntity.isInvalidBlock(state)) {
|
|
valid = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (valid) {
|
|
return new Reactor((ReactorCoreBlockEntity) entity, inputs.toArray(new ReactorInputBlockEntity[0]));
|
|
} else {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|