Skip to content

Commit

Permalink
GroovyScript 1.0.0 (GregTechCEu#2425)
Browse files Browse the repository at this point in the history
Co-authored-by: TechLord22 <[email protected]>
  • Loading branch information
brachy84 and TechLord22 authored Apr 4, 2024
1 parent 251a9ac commit 16ed04a
Show file tree
Hide file tree
Showing 9 changed files with 760 additions and 53 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.3") { transitive = false }
api("com.cleanroommc:groovyscript:0.8.0") { transitive = false }
api("com.cleanroommc:groovyscript:1.0.1") { 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
2 changes: 1 addition & 1 deletion src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
dependencies = "required:forge@[14.23.5.2847,);" + "required-after:codechickenlib@[3.2.3,);" +
"required-after:modularui@[2.3,);" + "required-after:mixinbooter@[8.0,);" + "after:appliedenergistics2;" +
"after:forestry;" + "after:extrabees;" + "after:extratrees;" + "after:genetics;" + "after:magicbees;" +
"after:jei@[4.15.0,);" + "after:crafttweaker@[4.1.20,);" + "after:groovyscript@[0.7.0,);" +
"after:jei@[4.15.0,);" + "after:crafttweaker@[4.1.20,);" + "after:groovyscript@[1.0.1,);" +
"after:theoneprobe;" + "after:hwyla;")
public class GregTechMod {

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/gregtech/integration/groovy/GroovyExpansions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package gregtech.integration.groovy;

import gregtech.api.fluids.FluidBuilder;
import gregtech.api.fluids.attribute.FluidAttributes;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.unification.Element;
import gregtech.api.unification.Elements;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.event.MaterialEvent;
import gregtech.api.unification.material.properties.ToolProperty;

import net.minecraft.util.ResourceLocation;

Expand Down Expand Up @@ -39,4 +44,26 @@ public static Material.Builder materialBuilder(MaterialEvent event, int id, Stri
}
return materialBuilder(event, id, new ResourceLocation(domain, path));
}

public static ToolProperty.Builder toolBuilder(MaterialEvent event, float harvestSpeed, float attackDamage,
int durability, int harvestLevel) {
return ToolProperty.Builder.of(harvestSpeed, attackDamage, durability, harvestLevel);
}

public static ToolProperty.Builder toolBuilder(MaterialEvent event) {
return toolBuilder(event, 1.0F, 1.0F, 100, 2);
}

public static FluidBuilder fluidBuilder(MaterialEvent event) {
return new FluidBuilder();
}

public static Element addElement(MaterialEvent event, long protons, long neutrons, long halfLifeSeconds,
String decayTo, String name, String symbol, boolean isIsotope) {
return Elements.add(protons, neutrons, halfLifeSeconds, decayTo, name, symbol, isIsotope);
}

public static FluidBuilder acidic(FluidBuilder builder) {
return builder.attributes(FluidAttributes.ACID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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.material.properties.ToolProperty;
import gregtech.api.unification.stack.MaterialStack;

import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -115,4 +116,13 @@ public static Material.Builder components(Material.Builder builder, Object... ob
}
return builder.components(materialStacks.toArray(new MaterialStack[0]));
}

public static Material.Builder toolStats(Material.Builder builder, ToolProperty.Builder toolBuilder) {
return builder.toolStats(toolBuilder.build());
}

public static Material.Builder toolStats(Material.Builder builder, float harvestSpeed, float attackDamage,
int durability, int harvestLevel) {
return builder.toolStats(ToolProperty.Builder.of(harvestSpeed, attackDamage, durability, harvestLevel).build());
}
}
143 changes: 93 additions & 50 deletions src/main/java/gregtech/integration/groovy/GroovyScriptModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.fluids.FluidBuilder;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.modules.GregTechModule;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.unification.Element;
import gregtech.api.unification.Elements;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.event.MaterialEvent;
import gregtech.api.unification.material.event.PostMaterialEvent;
Expand All @@ -20,8 +23,6 @@
import gregtech.common.pipelike.fluidpipe.BlockFluidPipe;
import gregtech.common.pipelike.itempipe.BlockItemPipe;
import gregtech.integration.IntegrationSubmodule;
import gregtech.integration.crafttweaker.material.MaterialExpansion;
import gregtech.integration.crafttweaker.material.MaterialPropertyExpansion;
import gregtech.modules.GregTechModules;

import net.minecraft.item.ItemStack;
Expand All @@ -35,24 +36,25 @@
import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.api.GroovyPlugin;
import com.cleanroommc.groovyscript.api.IGameObjectHandler;
import com.cleanroommc.groovyscript.api.IGameObjectParser;
import com.cleanroommc.groovyscript.api.Result;
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.gameobjects.GameObjectHandler;
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.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.jetbrains.annotations.Contract;
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.NoSuchElementException;
import java.util.Objects;
import java.util.function.Supplier;

Expand All @@ -68,7 +70,7 @@
public class GroovyScriptModule extends IntegrationSubmodule implements GroovyPlugin {

private static GroovyContainer<?> modSupportContainer;
private static final Map<String, Map<String, ItemStack>> metaItems = new Object2ObjectOpenHashMap<>();
private static final Object2ObjectOpenHashMap<String, Map<String, ItemStack>> metaItems = new Object2ObjectOpenHashMap<>();

@NotNull
@Override
Expand All @@ -87,12 +89,20 @@ public static boolean isCurrentlyRunning() {
}

public static <T extends Enum<T>> T parseAndValidateEnumValue(Class<T> clazz, String raw, String type) {
return parseAndValidateEnumValue(clazz, raw, type, false);
}

@Contract("_,_,_,true -> !null")
public static <T extends Enum<T>> T parseAndValidateEnumValue(Class<T> clazz, String raw, String type,
boolean crash) {
T t = EnumHelper.valueOfNullable(clazz, raw, false);
if (t == null) {
GroovyLog.get().error("Can't find {} for {} in material builder. Valid values are {};",
String msg = GroovyLog.format("Can't find {} for {} in material builder. Valid values are {};",
type,
raw,
Arrays.toString(clazz.getEnumConstants()));
if (crash) throw new NoSuchElementException(msg);
GroovyLog.get().error(msg);
return null;
}
return t;
Expand Down Expand Up @@ -216,57 +226,90 @@ public static void loadMetaItemBracketHandler() {
return "GregTech";
}

@Optional.Method(modid = Mods.Names.GROOVY_SCRIPT)
@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);
}
};
return new PropertyContainer();
}

@Override
public void onCompatLoaded(GroovyContainer<?> groovyContainer) {
modSupportContainer = groovyContainer;
GameObjectHandlerManager.registerGameObjectHandler(GTValues.MODID, "recipemap",
IGameObjectHandler.wrapStringGetter(RecipeMap::getByName));

GameObjectHandlerManager.registerGameObjectHandler(GTValues.MODID, "material",
IGameObjectHandler.wrapStringGetter(GregTechAPI.materialManager::getMaterial));

GameObjectHandlerManager.registerGameObjectHandler(GTValues.MODID, "oreprefix",
IGameObjectHandler.wrapStringGetter(OrePrefix::getPrefix));

GameObjectHandlerManager.registerGameObjectHandler(GTValues.MODID, "metaitem",
IGameObjectHandler.wrapStringGetter(GroovyScriptModule::getMetaItem), ItemStack.EMPTY);
GroovyScriptModule.modSupportContainer = groovyContainer;
GameObjectHandler.builder("recipemap", RecipeMap.class)
.mod(GTValues.MODID)
.parser(IGameObjectParser.wrapStringGetter(RecipeMap::getByName))
.completerOfNamed(RecipeMap::getRecipeMaps, RecipeMap::getUnlocalizedName)
.register();
GameObjectHandler.builder("material", Material.class)
.mod(GTValues.MODID)
.parser(IGameObjectParser.wrapStringGetter(GregTechAPI.materialManager::getMaterial))
.completerOfNamed(GregTechAPI.materialManager::getRegisteredMaterials,
mat -> mat.getResourceLocation().toString())
.register();

GameObjectHandler.builder("oreprefix", OrePrefix.class)
.mod(GTValues.MODID)
.parser(IGameObjectParser.wrapStringGetter(OrePrefix::getPrefix))
.completerOfNamed(OrePrefix::values, v -> v.name)
.register();

GameObjectHandler.builder("metaitem", ItemStack.class)
.mod(GTValues.MODID)
.parser(IGameObjectParser.wrapStringGetter(GroovyScriptModule::getMetaItem))
.completer((paramIndex, items) -> {
if (paramIndex != 0) return;
for (var iterator = metaItems.object2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
var entry = iterator.next();
String mod = entry.getKey();
for (String key : entry.getValue().keySet()) {
var item = new CompletionItem(mod + ":" + key);
item.setKind(CompletionItemKind.Constant);
items.add(item);
}
}
})
.register();

GameObjectHandler.builder("element", Element.class)
.mod(GTValues.MODID)
.parser((s, args) -> {
Element element = Elements.get(s);
if (element != null) return Result.some(element);
if (s.length() <= 6) {
for (Element element1 : Elements.getAllElements()) {
if (element1.symbol.equals(s)) {
return Result.some(element1);
}
}
}
return Result.error();
})
.completerOfNamed(Elements::getAllElements, Element::getName)
.register();

ExpansionHelper.mixinClass(Material.class, MaterialExpansion.class);
ExpansionHelper.mixinClass(Material.class, MaterialPropertyExpansion.class);
ExpansionHelper.mixinClass(Material.Builder.class, GroovyMaterialBuilderExpansion.class);
ExpansionHelper.mixinMethod(RecipeBuilder.class, GroovyExpansions.class, "property");
ExpansionHelper.mixinMethod(MaterialEvent.class, GroovyExpansions.class, "materialBuilder");
ExpansionHelper.mixinMethod(MaterialEvent.class, GroovyExpansions.class, "toolBuilder");
ExpansionHelper.mixinMethod(MaterialEvent.class, GroovyExpansions.class, "fluidBuilder");
ExpansionHelper.mixinMethod(PostMaterialEvent.class, GroovyExpansions.class, "toolBuilder");
ExpansionHelper.mixinMethod(PostMaterialEvent.class, GroovyExpansions.class, "fluidBuilder");
ExpansionHelper.mixinMethod(FluidBuilder.class, GroovyExpansions.class, "acidic");
}

protected static boolean checkFrozen(String description) {
if (!GregTechAPI.materialManager.canModifyMaterials()) {
GroovyLog.get().error("Cannot {} now, must be done in preInit loadStage and material event", description);
return true;
}
return false;
}

protected static void logError(Material m, String cause, String type) {
GroovyLog.get().error(
"Cannot {0} of a Material with no {1}! Try calling \"add{1}\" in your late material event first if this is intentional. Material: {2}",
cause, type, m.getUnlocalizedName());
}
}
Loading

0 comments on commit 16ed04a

Please sign in to comment.