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

133 lines
4.3 KiB
Java

package com.thebrokenrail.energonrelics.block.entity.battery;
import com.thebrokenrail.energonrelics.Config;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.battery.BatteryControllerBlock;
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
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.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
public class BatteryControllerBlockEntity extends EnergyReceiverBlockEntity implements EnergyGenerator {
public BatteryControllerBlockEntity(BlockEntityType<?> type) {
super(type);
}
private static class UnlimitedAction implements Action.PropagatedAction {
private final World world;
private final BlockPos pos;
private UnlimitedAction(World world, BlockPos pos) {
this.world = world;
this.pos = pos;
}
@Override
public void expandPayments(int amount) {
}
@Override
public long amountOwed() {
return Long.MAX_VALUE;
}
@Override
public void pay(long amount) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof BatteryControllerBlockEntity) {
BatteryControllerBlockEntity battery = (BatteryControllerBlockEntity) entity;
battery.setEnergy(battery.getEnergy() + amount);
}
}
}
@Override
protected void tickEnergy() {
long charge = Config.BATTERY_CHARGE_RATE;
if (!isActiveController()) {
addAction(new Action(charge, (world, pos, state) -> {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof BatteryControllerBlockEntity) {
BatteryControllerBlockEntity battery = (BatteryControllerBlockEntity) entity;
battery.setEnergy(battery.getEnergy() + charge);
}
}, (world, pos, state) -> {}));
} else {
propagateAction(new UnlimitedAction(getWorld(), getPos()));
}
}
protected boolean isActiveController() {
return false;
}
@Override
public void setEnergy(long value) {
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) {
handlePropagatedActionWithGenerator(action);
}
@Override
public boolean isEnergyProvider() {
return true;
}
private BatteryCoreBlockEntity getBatteryCore() {
if (hasWorld()) {
Direction facing = getCachedState().get(BatteryControllerBlock.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 || state.getBlock() != EnergonRelics.THERMAL_CASING_BLOCK) {
valid = false;
break;
}
}
}
if (valid) {
return (BatteryCoreBlockEntity) entity;
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
}
}