Skip to content

Commit

Permalink
Update GroovyScript & improve material API for groovy (#2389)
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 authored Mar 17, 2024
1 parent fea6440 commit dc389f8
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
// Published dependencies
api("codechicken:codechickenlib:3.2.3.358")
api("com.cleanroommc:modularui:2.4.1") { transitive = false }
api("com.cleanroommc:groovyscript:0.7.3") { transitive = false }
api("com.cleanroommc:groovyscript:0.8.0") { transitive = false }
api("CraftTweaker2:CraftTweaker2-MC1120-Main:1.12-4.1.20.684")
api rfg.deobf("curse.maven:ae2-extended-life-570458:4402048") // AE2UEL 0.55.6
api rfg.deobf("curse.maven:ctm-267602:2915363") // CTM 1.0.2.31
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package gregtech.api.unification.material.properties;

import gregtech.integration.groovy.GroovyScriptModule;

import crafttweaker.CraftTweakerAPI;
import org.jetbrains.annotations.NotNull;

public class BlastProperty implements IMaterialProperty {

/**
* Blast Furnace Temperature of this Material.
* If below 1000K, Primitive Blast Furnace recipes will be also added.
* Blast Furnace Temperature of this Material. If below 1000K, Primitive Blast Furnace recipes will be also added.
* If above 1750K, a Hot Ingot and its Vacuum Freezer recipe will be also added.
* <p>
* If a Material with this Property has a Fluid, its temperature
* will be set to this if it is the default Fluid temperature.
* If a Material with this Property has a Fluid, its temperature will be set to this if it is the default Fluid
* temperature.
*/
private int blastTemperature;

Expand Down Expand Up @@ -132,7 +133,10 @@ public void verifyProperty(MaterialProperties properties) {

public static GasTier validateGasTier(String gasTierName) {
if (gasTierName == null) return null;
else if ("LOW".equalsIgnoreCase(gasTierName)) return GasTier.LOW;
if (GroovyScriptModule.isCurrentlyRunning()) {
return GroovyScriptModule.parseAndValidateEnumValue(GasTier.class, gasTierName, "gas tier");
}
if ("LOW".equalsIgnoreCase(gasTierName)) return GasTier.LOW;
else if ("MID".equalsIgnoreCase(gasTierName)) return GasTier.MID;
else if ("HIGH".equalsIgnoreCase(gasTierName)) return GasTier.HIGH;
else if ("HIGHER".equalsIgnoreCase(gasTierName)) return GasTier.HIGHER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gregtech.api.fluids.FluidState;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.stack.MaterialStack;
import gregtech.integration.groovy.GroovyScriptModule;

import com.google.common.collect.ImmutableList;
import crafttweaker.CraftTweakerAPI;
Expand All @@ -17,7 +18,9 @@ protected static ImmutableList<MaterialStack> validateComponentList(MaterialStac
protected static FluidState validateFluidState(String fluidTypeName) {
if (fluidTypeName == null || fluidTypeName.equals("fluid"))
return FluidState.LIQUID;

if (GroovyScriptModule.isCurrentlyRunning()) {
return GroovyScriptModule.parseAndValidateEnumValue(FluidState.class, fluidTypeName, "fluid type");
}
if (fluidTypeName.equals("liquid")) return FluidState.LIQUID;
if (fluidTypeName.equals("gas")) return FluidState.GAS;
if (fluidTypeName.equals("plasma")) return FluidState.PLASMA;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/gregtech/integration/groovy/GroovyExpansions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gregtech.integration.groovy;

import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.event.MaterialEvent;

import net.minecraft.util.ResourceLocation;

import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.api.GroovyLog;

public class GroovyExpansions {

public static <R extends RecipeBuilder<R>> RecipeBuilder<R> property(RecipeBuilder<R> builder, String key,
Object value) {
if (!builder.applyProperty(key, value)) {
GroovyLog.get().error("Failed to add property '{}' with '{}' to recipe", key, value);
}
return builder;
}

public static Material.Builder materialBuilder(MaterialEvent event, int id, ResourceLocation resourceLocation) {
return new Material.Builder(id, resourceLocation);
}

public static Material.Builder materialBuilder(MaterialEvent event, int id, String domain, String path) {
return materialBuilder(event, id, domain, path);
}

public static Material.Builder materialBuilder(MaterialEvent event, int id, String s) {
String domain, path;
if (s.contains(":")) {
String[] parts = s.split(":", 2);
domain = parts[0];
path = parts[1];
} else {
domain = GroovyScript.getRunConfig().getPackId();
path = s;
}
return materialBuilder(event, id, new ResourceLocation(domain, path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import gregtech.api.unification.material.info.MaterialFlag;
import gregtech.api.unification.material.info.MaterialIconSet;
import gregtech.api.unification.material.properties.BlastProperty;
import gregtech.api.unification.stack.MaterialStack;

import net.minecraft.util.ResourceLocation;

import com.cleanroommc.groovyscript.api.GroovyLog;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

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

Expand Down Expand Up @@ -85,22 +89,30 @@ public static Material.Builder blastTemp(Material.Builder builder, int temp, Str

public static Material.Builder blastTemp(Material.Builder builder, int temp, String raw, int eutOverride,
int durationOverride, int vacuumEUtOverride, int vacuumDurationOverride) {
BlastProperty.GasTier gasTier = null;
String name = raw.toUpperCase();
for (BlastProperty.GasTier gasTier1 : BlastProperty.GasTier.VALUES) {
if (gasTier1.name().equals(name)) {
gasTier = gasTier1;
break;
}
}
final BlastProperty.GasTier finalGasTier = gasTier;
if (GroovyScriptModule.validateNonNull(gasTier, () -> "Can't find gas tier for " + name +
" in material builder. Valid values are 'low', 'mid', 'high', 'higher', 'highest'!")) {
BlastProperty.GasTier gasTier = GroovyScriptModule.parseAndValidateEnumValue(BlastProperty.GasTier.class, raw,
"gas tier");
if (gasTier != null) {
return builder.blast(b -> b
.temp(temp, finalGasTier)
.temp(temp, gasTier)
.blastStats(eutOverride, durationOverride)
.vacuumStats(vacuumEUtOverride, vacuumDurationOverride));
}
return builder;
}

public static Material.Builder components(Material.Builder builder, Object... objects) {
ObjectArrayList<MaterialStack> materialStacks = new ObjectArrayList<>();
for (Object o : objects) {
if (o instanceof MaterialStack materialStack) {
materialStacks.add(materialStack);
} else if (o instanceof Material material) {
materialStacks.add(new MaterialStack(material, 1));
} else {
GroovyLog.get()
.error("Material components must be of type Material or MaterialStack, but was of type {}",
o == null ? null : o.getClass());
}
}
return builder.components(materialStacks.toArray(new MaterialStack[0]));
}
}

This file was deleted.

58 changes: 56 additions & 2 deletions src/main/java/gregtech/integration/groovy/GroovyScriptModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.event.MaterialEvent;
import gregtech.api.unification.material.event.PostMaterialEvent;
import gregtech.api.unification.material.registry.MaterialRegistry;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.util.Mods;
Expand Down Expand Up @@ -35,13 +37,20 @@
import com.cleanroommc.groovyscript.api.GroovyPlugin;
import com.cleanroommc.groovyscript.api.IGameObjectHandler;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.event.EventBusType;
import com.cleanroommc.groovyscript.event.GroovyEventManager;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager;
import com.cleanroommc.groovyscript.helper.EnumHelper;
import com.cleanroommc.groovyscript.sandbox.LoadStage;
import com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper;
import com.google.common.collect.ImmutableList;
import groovy.lang.Closure;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -77,6 +86,18 @@ public static boolean isCurrentlyRunning() {
GroovyScript.getSandbox().isRunning();
}

public static <T extends Enum<T>> T parseAndValidateEnumValue(Class<T> clazz, String raw, String type) {
T t = EnumHelper.valueOfNullable(clazz, raw, false);
if (t == null) {
GroovyLog.get().error("Can't find {} for {} in material builder. Valid values are {};",
type,
raw,
Arrays.toString(clazz.getEnumConstants()));
return null;
}
return t;
}

public static GroovyContainer<?> getInstance() {
return modSupportContainer;
}
Expand Down Expand Up @@ -191,10 +212,42 @@ public static void loadMetaItemBracketHandler() {
}

@Override
public @NotNull String getModName() {
public @NotNull String getContainerName() {
return "GregTech";
}

@Override
public @Nullable ModPropertyContainer createModPropertyContainer() {
return new ModPropertyContainer() {

public void materialEvent(EventPriority priority, Closure<?> eventListener) {
if (isCurrentlyRunning() && GroovyScript.getSandbox().getCurrentLoader() != LoadStage.PRE_INIT) {
GroovyLog.get().error("GregTech's material event can only be used in pre init!");
return;
}
GroovyEventManager.INSTANCE.listen(priority, EventBusType.FORGE, MaterialEvent.class,
eventListener);
}

public void materialEvent(Closure<?> eventListener) {
materialEvent(EventPriority.NORMAL, eventListener);
}

public void lateMaterialEvent(EventPriority priority, Closure<?> eventListener) {
if (isCurrentlyRunning() && GroovyScript.getSandbox().getCurrentLoader() != LoadStage.PRE_INIT) {
GroovyLog.get().error("GregTech's material event can only be used in pre init!");
return;
}
GroovyEventManager.INSTANCE.listen(priority, EventBusType.FORGE, PostMaterialEvent.class,
eventListener);
}

public void lateMaterialEvent(Closure<?> eventListener) {
materialEvent(EventPriority.NORMAL, eventListener);
}
};
}

@Override
public void onCompatLoaded(GroovyContainer<?> groovyContainer) {
modSupportContainer = groovyContainer;
Expand All @@ -213,6 +266,7 @@ public void onCompatLoaded(GroovyContainer<?> groovyContainer) {
ExpansionHelper.mixinClass(Material.class, MaterialExpansion.class);
ExpansionHelper.mixinClass(Material.class, MaterialPropertyExpansion.class);
ExpansionHelper.mixinClass(Material.Builder.class, GroovyMaterialBuilderExpansion.class);
ExpansionHelper.mixinClass(RecipeBuilder.class, GroovyRecipeBuilderExpansion.class);
ExpansionHelper.mixinMethod(RecipeBuilder.class, GroovyExpansions.class, "property");
ExpansionHelper.mixinMethod(MaterialEvent.class, GroovyExpansions.class, "materialBuilder");
}
}

0 comments on commit dc389f8

Please sign in to comment.