Skip to content

Commit

Permalink
Allow reactors to output energy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Dec 11, 2024
1 parent 66eb0f1 commit b1b907e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3i;
Expand All @@ -21,6 +23,7 @@
import rearth.oritech.client.ui.ReactorScreenHandler;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.Geometry;
import rearth.oritech.util.energy.EnergyApi;
import rearth.oritech.util.energy.containers.SimpleEnergyStorage;

Expand All @@ -34,15 +37,15 @@ public class ReactorControllerBlockEntity extends BlockEntity implements BlockEn
public static final int MAX_SIZE = 64;
public static final int RF_PER_PULSE = 32;
public static final int ABSORBER_RATE = 10;
public static final int VENT_BASE_RATE = 6;
public static final int VENT_RELATIVE_RATE = 50;
public static final int VENT_BASE_RATE = 4;
public static final int VENT_RELATIVE_RATE = 100;

private final HashMap<Vector2i, BaseReactorBlock> activeComponents = new HashMap<>(); // 2d local position on the first layer containing the reactor blocks
private final HashMap<Vector2i, ReactorFuelPortEntity> fuelPorts = new HashMap<>(); // same grid, but contains a reference to the port at the ceiling
private final HashMap<Vector2i, ReactorAbsorberPortEntity> absorberPorts = new HashMap<>(); // same
private final HashMap<Vector2i, Integer> componentHeats = new HashMap<>(); // same grid, contains the current heat of the component
private final HashMap<Vector2i, ComponentStatistics> componentStats = new HashMap<>(); // mainly for client displays, same grid
private final HashSet<BlockPos> energyPorts = new HashSet<>(); // list of all energy port outputs (e.g. the targets to output to)
private final HashSet<Pair<BlockPos, Direction>> energyPorts = new HashSet<>(); // list of all energy port outputs (e.g. the targets to output to)

public SimpleEnergyStorage energyStorage = new SimpleEnergyStorage(0, 1_000_000, 10_000_000, this::markDirty);
public boolean active = false;
Expand Down Expand Up @@ -170,6 +173,8 @@ public void tick(World world, BlockPos pos, BlockState state, ReactorControllerB
componentHeats.put(localPos, componentHeat);
}

outputEnergy();

if (world.getTime() % 2 == 0)
sendUINetworkData();

Expand Down Expand Up @@ -211,9 +216,15 @@ public void init(PlayerEntity player) {
var block = world.getBlockState(pos).getBlock();
return block instanceof ReactorWallBlock;
} else if (isOnWall(pos, finalCornerA, finalCornerB)) {
var block = world.getBlockState(pos).getBlock();
var state = world.getBlockState(pos);
var block = state.getBlock();

// load wall energy ports
if (block instanceof ReactorEnergyPortBlock) {
var facing = state.get(Properties.FACING);
var blockInFront = pos.add(Geometry.getForward(facing));
energyPorts.add(new Pair<>(blockInFront, Direction.fromVector(Geometry.getBackward(facing).getX(), Geometry.getBackward(facing).getY(), Geometry.getBackward(facing).getZ())));
}

return !(block instanceof BaseReactorBlock reactorBlock) || reactorBlock.validForWalls();
}
Expand Down Expand Up @@ -340,6 +351,25 @@ private BlockPos expandWall(BlockPos from, Vec3i direction, boolean allReactorBl

}

private void outputEnergy() {

var totalMoved = 0;

for (var candidateData : energyPorts) {
var candidate = EnergyApi.BLOCK.find(world, candidateData.getLeft(), candidateData.getRight());
if (candidate == null) continue;
var moved = EnergyApi.transfer(energyStorage, candidate, energyStorage.getAmount(), false);

if (moved > 0)
candidate.update();

totalMoved += moved;
}

if (totalMoved > 0)
energyStorage.update();
}

@Override
public EnergyApi.EnergyContainer getStorage(Direction direction) {
return energyStorage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.util.energy.EnergyApi;
import rearth.oritech.util.energy.containers.SimpleEnergyStorage;

public class ReactorEnergyPortEntity extends BlockEntity {
public class ReactorEnergyPortEntity extends BlockEntity implements EnergyApi.BlockProvider {

// this block is just an energy provider so that pipes will connect. The energy is actually output from the controller
private final SimpleEnergyStorage dummyStorage = new SimpleEnergyStorage(0, 0, 0);

public ReactorEnergyPortEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.REACTOR_ENERGY_PORT_BLOCK_ENTITY, pos, state);
}

@Override
public EnergyApi.EnergyContainer getStorage(Direction direction) {
return dummyStorage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ public class BlockEntitiesContent implements ArchitecturyRegistryContainer<Block

public static final BlockEntityType<SpawnerControllerBlockEntity> SPAWNER_CONTROLLER_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(SpawnerControllerBlockEntity::new, BlockContent.SPAWNER_CONTROLLER_BLOCK).build();

@AssignSidedEnergy
public static final BlockEntityType<ReactorControllerBlockEntity> REACTOR_CONTROLLER_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorControllerBlockEntity::new, BlockContent.REACTOR_CONTROLLER).build();
public static final BlockEntityType<ReactorFuelPortEntity> REACTOR_FUEL_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorFuelPortEntity::new, BlockContent.REACTOR_FUEL_PORT).build();
public static final BlockEntityType<ReactorAbsorberPortEntity> REACTOR_ABSORBER_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorAbsorberPortEntity::new, BlockContent.REACTOR_ABSORBER_PORT).build();
@AssignSidedEnergy
public static final BlockEntityType<ReactorEnergyPortEntity> REACTOR_ENERGY_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorEnergyPortEntity::new, BlockContent.REACTOR_ENERGY_PORT).build();

@AssignSidedInventory
Expand Down

0 comments on commit b1b907e

Please sign in to comment.