EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/PassiveBatteryControllerBlo...

137 lines
4.3 KiB
Java

package com.thebrokenrail.energonrelics.block.entity.battery;
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.api.block.entity.helper.EnergyGenerator;
import com.thebrokenrail.energonrelics.api.energy.Action;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.battery.PassiveBatteryControllerBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEntity implements EnergyGenerator {
public PassiveBatteryControllerBlockEntity(BlockEntityType<?> type) {
super(type);
}
private static long add(long a, long b) {
long diff = Long.MAX_VALUE - a;
if (b >= diff) {
return Long.MAX_VALUE;
} else {
return a + b;
}
}
private static class UnlimitedAction implements Action.PropagatedAction {
private final PassiveBatteryControllerBlockEntity battery;
private UnlimitedAction(PassiveBatteryControllerBlockEntity battery) {
this.battery = battery;
}
@Override
public void expandPayments(int amount) {
}
@Override
public long amountOwed() {
return Long.MAX_VALUE;
}
@Override
public void pay(long amount) {
battery.setEnergy(add(battery.getEnergy(), amount));
}
}
@Override
protected void energyTick() {
long charge = HardcodedConfig.BATTERY_CHARGE_RATE;
if (!isActiveController()) {
addAction(new Action(charge, (world, pos, state) -> PassiveBatteryControllerBlockEntity.this.setEnergy(add(PassiveBatteryControllerBlockEntity.this.getEnergy(), charge)), (world, pos, state) -> {}));
} else {
propagateAction(new UnlimitedAction(this));
}
}
protected boolean isActiveController() {
return false;
}
public static boolean isInvalidBlock(BlockState block) {
return !block.isOf(EnergonRelics.THERMAL_GLASS_BLOCK) && !block.isOf(EnergonRelics.THERMAL_CASING_BLOCK);
}
@Override
public void setEnergy(long value) {
if (value < 0) {
throw new UnsupportedOperationException();
}
BatteryCoreBlockEntity core = getBatteryCore();
if (core != null) {
core.setEnergy(value);
}
}
@Override
public long getEnergy() {
BatteryCoreBlockEntity core = getBatteryCore();
if (core != null) {
return core.getEnergy();
} else {
return 0;
}
}
@Override
public long getDisplayEnergy() {
return getEnergy();
}
@Override
protected void handlePropagatedAction(Action.PropagatedAction action) {
super.handlePropagatedAction(action);
handlePropagatedActionWithGenerator(action);
}
@Override
public boolean isEnergyProvider() {
return true;
}
private BatteryCoreBlockEntity getBatteryCore() {
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 BatteryCoreBlockEntity) {
boolean valid = true;
for (Direction side : Direction.values()) {
if (side != facing) {
BlockState state = getWorld().getBlockState(corePos.offset(side));
if (state == null || isInvalidBlock(state)) {
valid = false;
break;
}
}
}
if (valid) {
return (BatteryCoreBlockEntity) entity;
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
}
}