From b1b907ebef2254a8213075c9ccefd5b1ad0ec713 Mon Sep 17 00:00:00 2001 From: rearth Date: Wed, 11 Dec 2024 23:18:29 +0100 Subject: [PATCH] Allow reactors to output energy --- .../reactor/ReactorControllerBlockEntity.java | 38 +++++++++++++++++-- .../reactor/ReactorEnergyPortEntity.java | 14 ++++++- .../oritech/init/BlockEntitiesContent.java | 2 +- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorControllerBlockEntity.java b/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorControllerBlockEntity.java index 99cb2a98..a8d90f4d 100644 --- a/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorControllerBlockEntity.java +++ b/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorControllerBlockEntity.java @@ -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; @@ -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; @@ -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 activeComponents = new HashMap<>(); // 2d local position on the first layer containing the reactor blocks private final HashMap fuelPorts = new HashMap<>(); // same grid, but contains a reference to the port at the ceiling private final HashMap absorberPorts = new HashMap<>(); // same private final HashMap componentHeats = new HashMap<>(); // same grid, contains the current heat of the component private final HashMap componentStats = new HashMap<>(); // mainly for client displays, same grid - private final HashSet energyPorts = new HashSet<>(); // list of all energy port outputs (e.g. the targets to output to) + private final HashSet> 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; @@ -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(); @@ -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(); } @@ -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; diff --git a/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorEnergyPortEntity.java b/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorEnergyPortEntity.java index a67bf17c..10866272 100644 --- a/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorEnergyPortEntity.java +++ b/common/src/main/java/rearth/oritech/block/entity/reactor/ReactorEnergyPortEntity.java @@ -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; + } } diff --git a/common/src/main/java/rearth/oritech/init/BlockEntitiesContent.java b/common/src/main/java/rearth/oritech/init/BlockEntitiesContent.java index ec96e2ed..1a30ab84 100644 --- a/common/src/main/java/rearth/oritech/init/BlockEntitiesContent.java +++ b/common/src/main/java/rearth/oritech/init/BlockEntitiesContent.java @@ -150,10 +150,10 @@ public class BlockEntitiesContent implements ArchitecturyRegistryContainer SPAWNER_CONTROLLER_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(SpawnerControllerBlockEntity::new, BlockContent.SPAWNER_CONTROLLER_BLOCK).build(); - @AssignSidedEnergy public static final BlockEntityType REACTOR_CONTROLLER_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorControllerBlockEntity::new, BlockContent.REACTOR_CONTROLLER).build(); public static final BlockEntityType REACTOR_FUEL_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorFuelPortEntity::new, BlockContent.REACTOR_FUEL_PORT).build(); public static final BlockEntityType REACTOR_ABSORBER_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorAbsorberPortEntity::new, BlockContent.REACTOR_ABSORBER_PORT).build(); + @AssignSidedEnergy public static final BlockEntityType REACTOR_ENERGY_PORT_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ReactorEnergyPortEntity::new, BlockContent.REACTOR_ENERGY_PORT).build(); @AssignSidedInventory