Skip to content

Commit

Permalink
added ability for coolants to store more than 1 HU
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursivePineapple committed Jul 23, 2024
1 parent 7663b0d commit 6dc3308
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void init(FMLInitializationEvent event) {
// PacketDispatcher.TileEntityUpdatedMessage.init();

SimulationItems.init();

FluidList.registerCoolants();
}

// postInit "Handle interaction with other mods, complete your setup based on this." (Remove if not needed)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.recursive_pineapple.nuclear_horizons.reactors.fluids;

import java.util.HashMap;
import java.util.Objects;

import net.minecraftforge.fluids.Fluid;

public class CoolantRegistry {

private static final HashMap<Fluid, Coolant> coolantsByColdFluid = new HashMap<>();
private static final HashMap<Fluid, Coolant> coolantsByHotFluid = new HashMap<>();
private static final HashMap<Fluid, Coolant> coolantsByFluid = new HashMap<>();

private CoolantRegistry() {

}

/**
* Registers a coolant that can be used to cool a nuclear reactor
* @param cold The cold input coolant
* @param hot The heated output coolant
* @param specificHeatCapacity The amount of HU that can be stored in one mB of coolant
*/
public static void registerCoolant(Fluid cold, Fluid hot, int specificHeatCapacity) {
Objects.requireNonNull(cold);
Objects.requireNonNull(hot);
if(specificHeatCapacity <= 0) throw new IllegalArgumentException("specificHeatCapacity");

Coolant coolant = new Coolant(cold, hot, specificHeatCapacity);

coolantsByColdFluid.put(cold, coolant);
coolantsByHotFluid.put(hot, coolant);
coolantsByFluid.put(cold, coolant);
coolantsByFluid.put(hot, coolant);
}

public static boolean isColdCoolant(Fluid fluid) {
return coolantsByColdFluid.containsKey(fluid);
}

public static boolean isHotCoolant(Fluid fluid) {
return coolantsByHotFluid.containsKey(fluid);
}

public static Coolant getCoolantInfo(Fluid fluid) {
return coolantsByFluid.get(fluid);
}

public static class Coolant {
public final Fluid cold, hot;
public final int specificHeatCapacity;

public Coolant(Fluid cold, Fluid hot, int specificHeatCapacity) {
this.cold = cold;
this.hot = hot;
this.specificHeatCapacity = specificHeatCapacity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public class FluidList {

public static final String COOLANT_NAME = "nh_coolant";
public static final String HOT_COOLANT_NAME = "nh_hot_coolant";
public static final String HOT_SUPER_COOLANT_NAME = "nh_hot_super_coolant";

public static Fluid COOLANT;
public static Fluid HOT_COOLANT;
public static Fluid HOT_SUPER_COOLANT;

public static void registerFluids() {
COOLANT = new Fluid(COOLANT_NAME) {
Expand All @@ -36,9 +38,30 @@ public net.minecraft.util.IIcon getFlowingIcon() {
};
};

// Temporary, will probably be removed
HOT_SUPER_COOLANT = new Fluid(HOT_SUPER_COOLANT_NAME) {

public net.minecraft.util.IIcon getStillIcon() {
return BlockList.COOLANT_BLOCK.stillIcon;
};

public net.minecraft.util.IIcon getFlowingIcon() {
return BlockList.COOLANT_BLOCK.flowingIcon;
};
};

FluidRegistry.registerFluid(COOLANT);

HOT_COOLANT.setTemperature(500);
HOT_COOLANT.setTemperature(273 + 500);
FluidRegistry.registerFluid(HOT_COOLANT);

HOT_SUPER_COOLANT.setTemperature(273 + 1000);
FluidRegistry.registerFluid(HOT_SUPER_COOLANT);
}

public static void registerCoolants() {
CoolantRegistry.registerCoolant(COOLANT, HOT_COOLANT, 1);

CoolantRegistry.registerCoolant(FluidRegistry.getFluid("supercoolant"), HOT_SUPER_COOLANT, 5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;

import com.recursive_pineapple.nuclear_horizons.reactors.fluids.FluidList;
import com.recursive_pineapple.nuclear_horizons.reactors.fluids.CoolantRegistry;

public class TileFluidPort extends TileEntity implements IFluidHandler, IReactorBlock {

Expand Down Expand Up @@ -94,16 +94,15 @@ public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
return 0;
}

if (resource.getFluid() == FluidList.COOLANT) {
int remaining = reactor.maxCoolant - reactor.storedCoolant;
// this should only be called a few times a second, so we don't need to cache it
if(!CoolantRegistry.isColdCoolant(resource.getFluid())) {
return 0;
}

int consumed = Math.min(remaining, resource.amount);
var tank = reactor.coolantTank;

if (doFill) {
reactor.storedCoolant += consumed;
}

return consumed;
if(tank.getFluid() == null || resource.getFluid() == tank.getFluid().getFluid()) {
return tank.fill(resource, doFill);
} else {
return 0;
}
Expand All @@ -117,14 +116,10 @@ public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrai
return null;
}

if (resource.getFluid() == FluidList.HOT_COOLANT) {
int consumed = Math.min(reactor.storedHotCoolant, resource.amount);
var tank = reactor.hotCoolantTank;

if (doDrain) {
reactor.storedHotCoolant -= consumed;
}

return new FluidStack(FluidList.HOT_COOLANT, consumed);
if(tank.getFluid() != null && resource.getFluid() == tank.getFluid().getFluid()) {
return tank.drain(resource.amount, doDrain);
} else {
return null;
}
Expand All @@ -138,23 +133,23 @@ public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return null;
}

int consumed = Math.min(reactor.storedHotCoolant, maxDrain);
var tank = reactor.hotCoolantTank;

if (doDrain) {
reactor.storedHotCoolant -= consumed;
if(tank.getFluid() != null) {
return tank.drain(maxDrain, doDrain);
} else {
return null;
}

return new FluidStack(FluidList.HOT_COOLANT, consumed);
}

@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return fluid == FluidList.COOLANT;
return CoolantRegistry.isColdCoolant(fluid);
}

@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return fluid == FluidList.HOT_COOLANT;
return CoolantRegistry.isHotCoolant(fluid);
}

@Override
Expand All @@ -166,9 +161,8 @@ public FluidTankInfo[] getTankInfo(ForgeDirection from) {
}

return new FluidTankInfo[] {
new FluidTankInfo(new FluidStack(FluidList.COOLANT, reactor.storedCoolant), reactor.maxCoolant),
new FluidTankInfo(
new FluidStack(FluidList.HOT_COOLANT, reactor.storedHotCoolant),
reactor.maxHotCoolant), };
reactor.coolantTank.getInfo(),
reactor.hotCoolantTank.getInfo()
};
}
}
Loading

0 comments on commit 6dc3308

Please sign in to comment.