forked from TheBrokenRail/EnergonRelics
138 lines
4.6 KiB
Java
138 lines
4.6 KiB
Java
package com.thebrokenrail.energonrelics.block.entity.battery;
|
|
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
|
import com.thebrokenrail.energonrelics.block.battery.PassiveBatteryControllerBlock;
|
|
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 PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEntity implements EnergyGenerator {
|
|
public PassiveBatteryControllerBlockEntity(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 PassiveBatteryControllerBlockEntity) {
|
|
PassiveBatteryControllerBlockEntity battery = (PassiveBatteryControllerBlockEntity) entity;
|
|
battery.setEnergy(battery.getEnergy() + amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void tickEnergy() {
|
|
long charge = HardcodedConfig.BATTERY_CHARGE_RATE;
|
|
if (!isActiveController()) {
|
|
addAction(new Action(charge, (world, pos, state) -> {
|
|
BlockEntity entity = world.getBlockEntity(pos);
|
|
if (entity instanceof PassiveBatteryControllerBlockEntity) {
|
|
PassiveBatteryControllerBlockEntity battery = (PassiveBatteryControllerBlockEntity) entity;
|
|
battery.setEnergy(battery.getEnergy() + charge);
|
|
}
|
|
}, (world, pos, state) -> {}));
|
|
} else {
|
|
propagateAction(new UnlimitedAction(getWorld(), getPos()));
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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;
|
|
}
|
|
}
|
|
}
|