Skip to content

Commit

Permalink
Add reactor absorber block item absorption
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Dec 6, 2024
1 parent 791e5e9 commit 71623d9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -17,6 +18,7 @@
import rearth.oritech.client.init.ModScreens;
import rearth.oritech.client.ui.BasicMachineScreenHandler;
import rearth.oritech.init.BlockEntitiesContent;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.*;

import java.util.List;
Expand All @@ -26,10 +28,43 @@ public class ReactorAbsorberPortEntity extends BlockEntity implements ExtendedSc
private final SimpleSidedInventory inventory = new SimpleSidedInventory(1, new InventorySlotAssignment(0, 1, 1, 0));
private final InventoryStorage inventoryStorage = InventoryStorage.of(inventory, null);

public int availableFuel;
public int currentFuelOriginalCapacity;

public ReactorAbsorberPortEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.REACTOR_ABSORBER_PORT_BLOCK_ENTITY, pos, state);
}

public int getAvailableFuel() {
if (availableFuel > 0) {
return availableFuel;
}

// try consume item
var inputStack = inventory.getStack(0);
if (inputStack.isEmpty()) return 0;

if (inputStack.getItem().equals(Items.BLUE_ICE)) {
var capacity = 1000;
currentFuelOriginalCapacity = capacity;
availableFuel = capacity;
inputStack.decrement(1);
}

return availableFuel;
}

public void consumeFuel(int amount) {
if (availableFuel >= amount) {
availableFuel -= amount;
}

}

public void updateNetwork() {
NetworkContent.MACHINE_CHANNEL.serverHandle(this).send(new NetworkContent.ReactorPortDataPacket(pos, currentFuelOriginalCapacity, availableFuel));
}

@Override
public Object getScreenOpeningData(ServerPlayerEntity player) {
return new ModScreens.BasicData(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,21 @@ public void tick(World world, BlockPos pos, BlockState state, ReactorControllerB
} else if (component instanceof ReactorAbsorberBlock absorberBlock) {

var sumRemovedHeat = 0;
var fuelAvailable = absorberPorts.get(localPos).getAvailableFuel();

// take heat in from neighbors and remove it
for (var neighbor : getNeighborsInBounds(localPos, activeComponents.keySet())) {
var neighborHeat = componentHeats.get(neighbor);
if (neighborHeat <= 0) continue;
neighborHeat -= 6;
sumRemovedHeat += 6;
componentHeats.put(neighbor, neighborHeat);
if (fuelAvailable >= reactorStackHeight) {
// take heat in from neighbors and remove it
for (var neighbor : getNeighborsInBounds(localPos, activeComponents.keySet())) {
var neighborHeat = componentHeats.get(neighbor);
if (neighborHeat <= 0) continue;
neighborHeat -= 6;
sumRemovedHeat += 6;
componentHeats.put(neighbor, neighborHeat);
}
}

if (sumRemovedHeat > 0) {
absorberPorts.get(localPos).consumeFuel(reactorStackHeight);
}

componentStats.put(localPos, new ComponentStatistics((short) 0, componentHeat, (short) sumRemovedHeat, (short) (0)));
Expand Down Expand Up @@ -334,7 +341,7 @@ private void sendUINetworkData() {
if (!active || activeComponents.isEmpty() || !isActivelyViewed()) return;

for (var port : fuelPorts.values()) port.updateNetwork();
// for (var port : absorberPorts.values()) port.updateNetwork();
for (var port : absorberPorts.values()) port.updateNetwork();

var positionsFlat = activeComponents.keySet();
var positions = positionsFlat.stream().map(pos -> areaMin.add(pos.x + 1, 1, pos.y + 1)).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import rearth.oritech.block.blocks.reactor.ReactorHeatPipeBlock;
import rearth.oritech.block.blocks.reactor.ReactorHeatVentBlock;
import rearth.oritech.block.blocks.reactor.ReactorRodBlock;
import rearth.oritech.block.entity.reactor.ReactorAbsorberPortEntity;
import rearth.oritech.block.entity.reactor.ReactorControllerBlockEntity;
import rearth.oritech.block.entity.reactor.ReactorFuelPortEntity;
import rearth.oritech.client.ui.components.ReactorBlockRenderComponent;
Expand Down Expand Up @@ -136,7 +137,7 @@ protected void handledScreenTick() {
for (var overlay : activeOverlays) {
var data = getStatsAtPosition(overlay.pos);

var isEmpty = data.storedHeat() <= 0;
var isEmpty = data.storedHeat() <= 10;
if (isEmpty) {
overlay.state = Blocks.AIR.getDefaultState();
continue;
Expand Down Expand Up @@ -241,7 +242,13 @@ public void addStatsToTooltip(BlockPos pos, BlockState state, FlowLayout contain
} else if (state.getBlock() instanceof ReactorHeatVentBlock pipeBlock) {
container.child(Components.label(Text.translatable("text.oritech.reactor.removed_heat", stats.heatChanged()).formatted(Formatting.WHITE)));
} else if (state.getBlock() instanceof ReactorAbsorberBlock absorberBlock) {

if (!(portEntity instanceof ReactorAbsorberPortEntity absorberPortEntity)) return;
var availableFuel = absorberPortEntity.availableFuel;
var maxFuel = absorberPortEntity.currentFuelOriginalCapacity;

container.child(Components.label(Text.translatable("text.oritech.reactor.absorbed_heat", stats.heatChanged()).formatted(Formatting.WHITE)));
container.child(Components.label(Text.translatable("text.oritech.reactor.absorbant", availableFuel, maxFuel).formatted(Formatting.WHITE)));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import rearth.oritech.block.entity.interaction.*;
import rearth.oritech.block.entity.pipes.ItemFilterBlockEntity;
import rearth.oritech.block.entity.processing.CentrifugeBlockEntity;
import rearth.oritech.block.entity.reactor.ReactorAbsorberPortEntity;
import rearth.oritech.block.entity.reactor.ReactorControllerBlockEntity;
import rearth.oritech.block.entity.reactor.ReactorFuelPortEntity;
import rearth.oritech.init.ComponentContent;
Expand Down Expand Up @@ -502,9 +503,13 @@ public static void registerChannels() {

var entity = access.player().getWorld().getBlockEntity(message.position);

// this is what happens when you're too lazy to add an interface
if (entity instanceof ReactorFuelPortEntity port) {
port.currentFuelOriginalCapacity = message.capacity;
port.availableFuel = message.remaining;
} else if (entity instanceof ReactorAbsorberPortEntity port) {
port.currentFuelOriginalCapacity = message.capacity;
port.availableFuel = message.remaining;
}

}));
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/assets/oritech/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@
"text.oritech.reactor.heat_to_reactor": "Heat to Reactor: %s K/t",
"text.oritech.reactor.heat": "Heat: %s K",
"text.oritech.reactor.fuel": "Fuel: %s/%s",
"text.oritech.reactor.absorbant": "Coolant: %s/%s",
"text.oritech.reactor.collected_heat": "Ingested Heat: %s K/t",
"text.oritech.reactor.removed_heat": "Dispersed Heat: %s K/t",
"text.oritech.reactor.absorbed_heat": "Absorbed Heat: %s K/t",
Expand Down

0 comments on commit 71623d9

Please sign in to comment.