Skip to content

Commit

Permalink
NC Active Cooler Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 27, 2024
1 parent 1ccf4ab commit 370c85f
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/groovy-tests/miscTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import com.nomiceu.nomilabs.groovy.NCActiveCoolerHelper

import static nc.enumm.MetaEnums.CoolerType.*

/**
* Change a NuclearCraft Active Cooler Recipe.<br>
* Inputs:<br>
* FluidStack newFluid: The new fluid which should be accepted as input to Active Fluid Cooler<br>
* CoolerType type: The type of active cooler fluid which this recipe should replace, having that recipe removed as
* well as using the cooling and consumption from that type.
*/

NCActiveCoolerHelper.changeCoolerRecipe(fluid('liquid_helium'), HELIUM)

NCActiveCoolerHelper.changeCoolerRecipe(fluid('moltenempoweredemeradic'), EMERALD)
12 changes: 12 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.nomiceu.nomilabs.gregtech.recipe.LabsRecipeMaps;
import com.nomiceu.nomilabs.gregtech.recipe.PerfectGemsCutterRecipes;
import com.nomiceu.nomilabs.groovy.GroovyScriptHandManager;
import com.nomiceu.nomilabs.groovy.NCActiveCoolerHelper;
import com.nomiceu.nomilabs.integration.architecturecraft.LabsShapes;
import com.nomiceu.nomilabs.integration.jei.JEIPlugin;
import com.nomiceu.nomilabs.integration.top.TOPTooltipManager;
Expand Down Expand Up @@ -201,5 +202,16 @@ public static void gsHandAdditions(GsHandEvent event) {
public static void onScriptReload(ScriptRunEvent.Pre event) {
JEIPlugin.onReload();
LabsTooltipHelper.clearAll();

if (Loader.isModLoaded(LabsValues.NUCLEARCRAFT_MODID)) {
NCActiveCoolerHelper.onReload();
}
}

@SubscribeEvent
public static void afterScriptLoad(ScriptRunEvent.Post event) {
if (Loader.isModLoaded(LabsValues.NUCLEARCRAFT_MODID)) {
NCActiveCoolerHelper.afterScriptLoad();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.nomiceu.nomilabs.groovy;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Optional;

import com.cleanroommc.groovyscript.api.GroovyLog;
import com.nomiceu.nomilabs.LabsValues;
import com.nomiceu.nomilabs.integration.nuclearcraft.AccessibleCoolerType;

import nc.config.NCConfig;
import nc.enumm.MetaEnums;
import nc.recipe.NCRecipes;
import nc.recipe.other.ActiveCoolerRecipes;

public class NCActiveCoolerHelper {

/*
* Reference must not be CHANGED! References are stored in TileActiveCooler!
* ID - 1 -> Fluid Name
*/
public static final List<String> fluidNamesFromIDs = new ArrayList<>();

static {
reloadFluidNames();
}

@SuppressWarnings("ConstantValue")
private static void reloadFluidNames() {
fluidNamesFromIDs.clear();
for (int i = 1; i < MetaEnums.CoolerType.values().length; i++) {
fluidNamesFromIDs.add(i - 1,
((AccessibleCoolerType) (Object) MetaEnums.CoolerType.values()[i]).getOriginalFluidName());
}
}

public static void onReload() {
// Usually Active Cooler is null on Pre-Init or Init Load
if (NCRecipes.active_cooler == null) return;

reloadFluidNames();

NCRecipes.active_cooler.removeAllRecipes();
NCRecipes.active_cooler.addRecipes();
}

public static void afterScriptLoad() {
// Usually Active Cooler is null on Pre-Init or Init Load
if (NCRecipes.active_cooler == null) return;

NCRecipes.active_cooler.refreshCache();
}

@Optional.Method(modid = LabsValues.NUCLEARCRAFT_MODID)
public static void changeCoolerRecipe(FluidStack newStack, MetaEnums.CoolerType type) {
if (NCRecipes.active_cooler == null) {
GroovyLog.get().error("Active Cooler Recipes not Found!");
return;
}

boolean removed = false;
for (var recipe : NCRecipes.active_cooler.getRecipeList()) {
if (recipe.fluidIngredients().size() == 1) {
var ing = recipe.fluidIngredients().get(0);
if (ing.getInputStackList().size() == 1 &&
Objects.equals(ing.getInputStackList().get(0).getFluid().getName(), type.getFluidName())) {
NCRecipes.active_cooler.removeRecipe(recipe);
removed = true;
break;
}
}
}
if (!removed) {
GroovyLog.get().error("Could not find ActiveCooler recipe for " + type.getFluidName() + "!");
}

NCRecipes.active_cooler.addRecipe(
ActiveCoolerRecipes.fluidStack(newStack.getFluid().getName(), NCConfig.active_cooler_max_rate),
Math.round(NCConfig.fission_active_cooling_rate[type.getID() - 1] * NCConfig.active_cooler_max_rate /
20.0),
Math.round(NCConfig.fusion_active_cooling_rate[type.getID() - 1] * NCConfig.active_cooler_max_rate /
20.0));
fluidNamesFromIDs.set(type.getID() - 1, newStack.getFluid().getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nomiceu.nomilabs.integration.nuclearcraft;

public interface AccessibleCoolerType {

String getOriginalFluidName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nomiceu.nomilabs.mixin.nuclearcraft;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.nomiceu.nomilabs.groovy.NCActiveCoolerHelper;
import com.nomiceu.nomilabs.integration.nuclearcraft.AccessibleCoolerType;

import nc.enumm.MetaEnums;

/**
* Allows for proper checking of custom Active Cooler Fluids.
*/
@Mixin(value = MetaEnums.CoolerType.class, remap = false)
public class CoolerTypeMixin implements AccessibleCoolerType {

@Shadow
private int id;

@Shadow
private String fluidName;

@Inject(method = "getFluidName", at = @At("HEAD"), cancellable = true)
private void getProperFluidName(CallbackInfoReturnable<String> cir) {
if (id == 0)
cir.setReturnValue(fluidName);
else
cir.setReturnValue(NCActiveCoolerHelper.fluidNamesFromIDs.get(id - 1));
}

@Override
@Unique
public String getOriginalFluidName() {
return fluidName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.nomiceu.nomilabs.mixin.nuclearcraft;

import java.util.List;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.nomiceu.nomilabs.groovy.NCActiveCoolerHelper;

import nc.tile.fluid.TileActiveCooler;

/**
* Allows for Tile Active Coolers to accept custom fluids.
*/
@Mixin(value = TileActiveCooler.class, remap = false)
public class TileActiveCoolerMixin {

@Inject(method = "validFluids", at = @At("HEAD"), cancellable = true)
private static void getProperValidFluids(CallbackInfoReturnable<List<String>> cir) {
cir.setReturnValue(NCActiveCoolerHelper.fluidNamesFromIDs);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/mixins.nomilabs.nuclearcraft.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"GTCEuRecipeIntegration"
"CoolerTypeMixin",
"GTCEuRecipeIntegration",
"TileActiveCoolerMixin"
],
"client": [],
"server": []
Expand Down

0 comments on commit 370c85f

Please sign in to comment.