Skip to content

Commit

Permalink
Write an annotation processor to collect the parameter names of metho…
Browse files Browse the repository at this point in the history
…ds for use in our CraftTweaker example datagen
  • Loading branch information
pupnewfster committed Aug 7, 2021
1 parent 604ce60 commit fa506a2
Show file tree
Hide file tree
Showing 35 changed files with 251 additions and 81 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ MDK.zip
*.blend1
*.blend2
/bin
/logs
/logs
/src/datagen/main/resources
20 changes: 20 additions & 0 deletions annotation-processor/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'java'
}

group 'mekanism.annotation-processor'
version '1.0'

repositories {
maven {
name 'CraftTweaker'
url "https://maven.blamejared.com"
content {
includeGroup 'com.blamejared.crafttweaker'
}
}
}

dependencies {
implementation "com.blamejared.crafttweaker:CraftTweaker-${minecraft_version}:${crafttweaker_version}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package mekanism;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Types;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.openzen.zencode.java.ZenCodeType;

public class CraftTweakerParamNameMapper extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Map<String, List<String>> methods = new HashMap<>();
Types typeUtils = processingEnv.getTypeUtils();
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(ZenCodeType.Method.class)) {
if (annotatedElement.getKind() == ElementKind.METHOD) {
ExecutableElement executableElement = (ExecutableElement) annotatedElement;
List<? extends VariableElement> parameters = executableElement.getParameters();
if (!parameters.isEmpty()) {
StringBuilder keyBuilder = new StringBuilder(executableElement.getSimpleName());
keyBuilder.append('(');
StringBuilder resultBuilder = new StringBuilder();
for (VariableElement parameter : parameters) {
//Erase the type as we don't have it during reflection
keyBuilder.append(typeUtils.erasure(parameter.asType())).append(';');
resultBuilder.append(parameter.getSimpleName()).append(',');
}
methods.computeIfAbsent(executableElement.getEnclosingElement().toString(), clazz -> new ArrayList<>()).add(keyBuilder + ")=" + resultBuilder);
}
}
}
if (!methods.isEmpty()) {
Filer filer = processingEnv.getFiler();
try {
FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "crafttweaker_param_names.txt");
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(resource.openOutputStream(), StandardCharsets.UTF_8))) {
for (Map.Entry<String, List<String>> entry : methods.entrySet()) {
writer.println("Class: " + entry.getKey());
for (String method : entry.getValue()) {
writer.println(method);
}
}
if (writer.checkError()) {
throw new IOException("Error writing to the file");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//Don't mark the annotation as used to allow other processors to process them if we ever end up having any
return false;
}

@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton(ZenCodeType.Method.class.getCanonicalName());
}

@Override
public SourceVersion getSupportedSourceVersion() {
//TODO - 1.17: Java 16
return SourceVersion.RELEASE_8;
}
}
20 changes: 17 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ def setupSourceSet(SourceSet sourceSet, String name) {
// base module to the dependencies of the data generators source set that we are setting up
def setupDataGenSourceSet(SourceSet datagenSourceSet, SourceSet baseSourceSet, String name) {
datagenSourceSet.java.srcDirs = ['src/datagen/' + name + '/java']
//Data gen sourceSets have no resources
datagenSourceSet.resources.srcDirs = []
datagenSourceSet.compileClasspath += project.sourceSets.api.output
datagenSourceSet.compileClasspath += project.sourceSets.main.output
if (baseSourceSet != null) {
if (baseSourceSet == null) {
//Base source set has resources that we put files that our annotation processor generates for consumption by our datagen
datagenSourceSet.resources.srcDirs = ['src/datagen/' + name + '/resources']
} else {
//Data gen sourceSets outside of the main one have no resources
datagenSourceSet.resources.srcDirs = []
//Only case it is null is when we are doing datagenmain so other cases also add access to the stuff
// in datagenmain so we can add helper classes
datagenSourceSet.compileClasspath += project.sourceSets.datagenmain.output
Expand Down Expand Up @@ -391,6 +394,8 @@ test {
dependencies {
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"

annotationProcessor project(":annotation-processor")

testImplementation "org.junit.jupiter:junit-jupiter-api:${junit_version}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit_version}"
//We use https://github.com/quicktheories/QuickTheories to allow for implementing property based testing
Expand Down Expand Up @@ -459,6 +464,7 @@ processAdditionsResources { setupProcessResources(processAdditionsResources, add
processDefenseResources { setupProcessResources(processDefenseResources, defenseReplaceResources) }
processGeneratorsResources { setupProcessResources(processGeneratorsResources, generatorsReplaceResources) }
processToolsResources { setupProcessResources(processToolsResources, toolsReplaceResources) }
processDatagenmainResources { dependsOn(compileJava) }

def setupProcessResources(ProcessResources process, TaskProvider<Task> replaceResources) {
process.duplicatesStrategy(DuplicatesStrategy.FAIL)
Expand Down Expand Up @@ -577,6 +583,14 @@ tasks.withType(JavaCompile).configureEach({
it.options.encoding = 'UTF-8'
})

compileJava {
options.annotationProcessorPath = configurations.annotationProcessor
options.annotationProcessorGeneratedSourcesDirectory = layout.buildDirectory.file("src/datagen/main/resources/").map(r -> r.asFile)
options.compilerArgs = [
"-processor", "mekanism.CraftTweakerParamNameMapper"
]
}

artifacts {
archives apiJar
archives jar
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include 'annotation-processor'
54 changes: 27 additions & 27 deletions src/datagen/generated/mekanism/.cache/cache
Original file line number Diff line number Diff line change
Expand Up @@ -4149,35 +4149,35 @@ c6194b01656da10ae68e9a54003fb87ef8ad1ec2 data/mekanism/recipes/upgrade/gas.json
22e23669d3edb40656d3976533d595bacf98dc46 data/mekanism/recipes/upgrade/muffling.json
87545c2a7c6a9197ced562127d8988ca3a65ab02 data/mekanism/recipes/upgrade/speed.json
5bea5a02d8d74afeea042cdc1e2daa628ba9e354 data/mekanism/recipes/upgrade/stone_generator.json
0e943f7510e2277c6c4c3d1701ac02564333690c data/mekanism/scripts/mekanism_activating.zs
fa3f1814bb1395a68d17db354d6edc5ab72aea82 data/mekanism/scripts/mekanism_centrifuging.zs
241ccddef25bfa1a622338ee91e0f1368c99d3a1 data/mekanism/scripts/mekanism_chemical_infusing.zs
b13bc972147f1c58ff1424839410175c296baaa4 data/mekanism/scripts/mekanism_combining.zs
f90fdfad6c6a8d9c1eac9baf274466609ad63bf2 data/mekanism/scripts/mekanism_compressing.zs
dd27ee28ae174ffce53c14a7ac3240cc70c3b98f data/mekanism/scripts/mekanism_crushing.zs
3459808febb67df5211f793d44815994f0ac3014 data/mekanism/scripts/mekanism_crystallizer.zs
8cedb9322616659a94936cd81adde3ed4d79bb46 data/mekanism/scripts/mekanism_activating.zs
e1b84bbb9b0ad19a944a7a90d38d3dd22d7a34b0 data/mekanism/scripts/mekanism_centrifuging.zs
055a271b1e5a05e897f79d5022bc8e9505d69884 data/mekanism/scripts/mekanism_chemical_infusing.zs
f19ea677102a007b44fb455c3085827a1b6d9b60 data/mekanism/scripts/mekanism_combining.zs
4bf82756b7ebdd6c1d8b00e21511ac3cfaba7538 data/mekanism/scripts/mekanism_compressing.zs
d3d5e10adda5b9f87cf6feda4d1748f83c3d7b79 data/mekanism/scripts/mekanism_crushing.zs
3b38c900c34a612385da7bb90ddb5c4c5788ddf7 data/mekanism/scripts/mekanism_crystallizer.zs
a963e2d6ce171693c3975f4351a122a1d620182c data/mekanism/scripts/mekanism_custom_chemicals.zs
8e7780efaf782a6e25aeed27b459ddce63ad3a56 data/mekanism/scripts/mekanism_dissolution.zs
b65904a4a95ca6eba128ab5de097e527e33fb647 data/mekanism/scripts/mekanism_energy_conversion.zs
47ea5e47559661585230d1718cef76081d2e506b data/mekanism/scripts/mekanism_enriching.zs
ea3571f1f453808a3989e3d0d9fd46a1f5abe720 data/mekanism/scripts/mekanism_evaporating.zs
27d783df11bec9b92cf705210647908a86201944 data/mekanism/scripts/mekanism_gas_conversion.zs
9b8cea90928c20451dd3f546888e67ecd60f73eb data/mekanism/scripts/mekanism_infusion_conversion.zs
0eb5a03916836c32f765cc46710bb3e9fb056089 data/mekanism/scripts/mekanism_injecting.zs
eda44d7aac538ee748bf7947fdc08a42fedf6236 data/mekanism/scripts/mekanism_dissolution.zs
7d5d347c6a58b1da4a9485a516a719d29d379420 data/mekanism/scripts/mekanism_energy_conversion.zs
56e0f17d3cef477f3fe569e77334a2adcf8b0239 data/mekanism/scripts/mekanism_enriching.zs
a04d68ebd4933eec212778eff56c2890b91b8701 data/mekanism/scripts/mekanism_evaporating.zs
6e31b17494a94c3cabb7f365c0ad50e5353e5a72 data/mekanism/scripts/mekanism_gas_conversion.zs
5b72ded07f9dad3ba1c9c133107922c12a53c6a1 data/mekanism/scripts/mekanism_infusion_conversion.zs
3ba403a5d9d649893be2798f90587b1900d2a91c data/mekanism/scripts/mekanism_injecting.zs
c1b2a2e527f453527d4fab4cbe9f31f988b06813 data/mekanism/scripts/mekanism_jeitweaker_integration.zs
298d598afae8227b771bd20c16df9a401f17c032 data/mekanism/scripts/mekanism_metallurgic_infusing.zs
ab6c865fb96bef2cc360a86b13080001fa60f858 data/mekanism/scripts/mekanism_nucleosynthesizing.zs
6fca3e9d39524610088009a33595df31f63a00b5 data/mekanism/scripts/mekanism_oxidizing.zs
19dcc85457797c0dcac3e1c655f6c96517ed4586 data/mekanism/scripts/mekanism_painting.zs
7d7214f512d1c39f0c66feb1f89d5ad9c6b5efcd data/mekanism/scripts/mekanism_pigment_extracting.zs
d25c981c8e18c451ce12c784173ece24e8403ddd data/mekanism/scripts/mekanism_pigment_mixing.zs
77858faa16e41b14bea7e9eec246cdfe122effe3 data/mekanism/scripts/mekanism_purifying.zs
6875188bdf08f67b9d91989b579d4f89efda53ad data/mekanism/scripts/mekanism_reaction.zs
edbc2c89a60e91ee859d393f358a520d47a8e2fe data/mekanism/scripts/mekanism_rotary.zs
be5768886884ed0663f6da4bd3f2663770bfbd3a data/mekanism/scripts/mekanism_sawing.zs
8a9168afbdeb30a144cec0e4fe39efde9f5ccd0f data/mekanism/scripts/mekanism_separating.zs
06b9fb3fb2d3c4cb4b06a0409086a0f6269f20cc data/mekanism/scripts/mekanism_smelting.zs
66a08d6b97ae222ee371ddc49a880c2feb1052fa data/mekanism/scripts/mekanism_washing.zs
90f4575845a780abcc689fb3a80ccba5ec18c7c4 data/mekanism/scripts/mekanism_metallurgic_infusing.zs
92add5e3210003ed2f4cb69fcca80fe724f6d81c data/mekanism/scripts/mekanism_nucleosynthesizing.zs
a621c6bc9a6e79604c83168bcc0ad0d5d2412b6b data/mekanism/scripts/mekanism_oxidizing.zs
df81dead57f0b366e46bc82feae9e4a1e8ca557b data/mekanism/scripts/mekanism_painting.zs
7a699c2c56f927e3dfb0e82ced814f0065470895 data/mekanism/scripts/mekanism_pigment_extracting.zs
b16f4c489cc153666df81e49b1013a2fc6f55765 data/mekanism/scripts/mekanism_pigment_mixing.zs
c2324f19319bbf8fdf24fbb034bfb5fa5f8fe088 data/mekanism/scripts/mekanism_purifying.zs
a43be144cab423edab83f481125633f4002351a5 data/mekanism/scripts/mekanism_reaction.zs
5c77818c216b79f6e8711518b21170b007a50d2d data/mekanism/scripts/mekanism_rotary.zs
55a04d535763c11abe8d4d3e8d00191bb08217cb data/mekanism/scripts/mekanism_sawing.zs
73542500ea54bfb691ecd390cb398ef5fce514d4 data/mekanism/scripts/mekanism_separating.zs
a9ec6697b7a6667e47df85d86ecf340ef18c6958 data/mekanism/scripts/mekanism_smelting.zs
9d1f6aeb6d5c507636e0a40b39ad661eb668862b data/mekanism/scripts/mekanism_washing.zs
e7c1958e38ecc18729eb4d83c1612a1e2ae27172 data/mekanism/tags/blocks/atomic_disassembler_ore.json
95c6f35711466ec14e033d2092d280f9694efc40 data/mekanism/tags/blocks/cardboard_blacklist.json
8bf0776c32ded17ca0604606b7837e909aba0f54 data/mekanism/tags/gases/waste_barrel_decay_blacklist.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mods.mekanism.api.ingredient.ChemicalStackIngredient.GasStackIngredient;

//Adds an Activating Recipe that converts 1 mB of Water Vapor to 1 mB of Gaseous Brine.

// <recipetype:mekanism:activating>.addRecipe(arg0 as string, arg1 as GasStackIngredient, arg2 as ICrTGasStack)
// <recipetype:mekanism:activating>.addRecipe(name as string, input as GasStackIngredient, output as ICrTGasStack)

<recipetype:mekanism:activating>.addRecipe("activate_water_vapor", GasStackIngredient.from(<gas:mekanism:water_vapor>), <gas:mekanism:brine>);
//An alternate implementation of the above recipe are shown commented below. This implementation makes use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mods.mekanism.api.ingredient.ChemicalStackIngredient.GasStackIngredient;

//Adds a Centrifuging Recipe that converts 1 mB of Gaseous Brine into 1 mB of Hydrogen Chloride.

// <recipetype:mekanism:centrifuging>.addRecipe(arg0 as string, arg1 as GasStackIngredient, arg2 as ICrTGasStack)
// <recipetype:mekanism:centrifuging>.addRecipe(name as string, input as GasStackIngredient, output as ICrTGasStack)

<recipetype:mekanism:centrifuging>.addRecipe("centrifuge_brine", GasStackIngredient.from(<gas:mekanism:brine>), <gas:mekanism:hydrogen_chloride>);
//An alternate implementation of the above recipe are shown commented below. This implementation makes use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mods.mekanism.api.ingredient.ChemicalStackIngredient.GasStackIngredient;

//Adds a Chemical Infusing Recipe that uses 1 mB of Hydrogen Chloride and 1 mB of Water Vapor to produce 2 mB of Gaseous Brine.

// <recipetype:mekanism:chemical_infusing>.addRecipe(arg0 as string, arg1 as GasStackIngredient, arg2 as GasStackIngredient, arg3 as ICrTGasStack)
// <recipetype:mekanism:chemical_infusing>.addRecipe(name as string, leftInput as GasStackIngredient, rightInput as GasStackIngredient, output as ICrTGasStack)

<recipetype:mekanism:chemical_infusing>.addRecipe("gaseous_brine", GasStackIngredient.from(<gas:mekanism:hydrogen_chloride>), GasStackIngredient.from(<gas:mekanism:water_vapor>), <gas:mekanism:brine> * 2);
//Alternate implementations of the above recipe are shown commented below. These implementations make use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mods.mekanism.api.ingredient.ItemStackIngredient;
* 2) Adds a recipe that combines eight Prismarine Shards and one Black Dye into a block of Dark Prismarine.
*/

// <recipetype:mekanism:combining>.addRecipe(arg0 as string, arg1 as ItemStackIngredient, arg2 as ItemStackIngredient, arg3 as IItemStack)
// <recipetype:mekanism:combining>.addRecipe(name as string, mainInput as ItemStackIngredient, extraInput as ItemStackIngredient, output as IItemStack)

<recipetype:mekanism:combining>.addRecipe("combining/bookshelf", ItemStackIngredient.from(<item:minecraft:book> * 3), ItemStackIngredient.from(<tag:items:minecraft:planks>, 6), <item:minecraft:bookshelf>);
//Alternate implementations of the above recipe are shown commented below. These implementations make use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mods.mekanism.api.ingredient.ItemStackIngredient;

//Adds a Compressing Recipe that compresses Emerald Dust into an Emerald.

// <recipetype:mekanism:compressing>.addRecipe(arg0 as string, arg1 as ItemStackIngredient, arg2 as IChemicalStackIngredient, arg3 as IItemStack)
// <recipetype:mekanism:compressing>.addRecipe(name as string, itemInput as ItemStackIngredient, chemicalInput as IChemicalStackIngredient, output as IItemStack)

<recipetype:mekanism:compressing>.addRecipe("compress_emerald", ItemStackIngredient.from(<tag:items:forge:dusts/emerald>), GasStackIngredient.from(<gas:mekanism:liquid_osmium>), <item:minecraft:emerald>);
//Alternate implementations of the above recipe are shown commented below. These implementations make use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mods.mekanism.api.ingredient.ItemStackIngredient;

//Adds a Crushing Recipe to crush Brick Blocks into four Bricks.

// <recipetype:mekanism:crushing>.addRecipe(arg0 as string, arg1 as ItemStackIngredient, arg2 as IItemStack)
// <recipetype:mekanism:crushing>.addRecipe(name as string, input as ItemStackIngredient, output as IItemStack)

<recipetype:mekanism:crushing>.addRecipe("crush_bricks", ItemStackIngredient.from(<item:minecraft:bricks>), <item:minecraft:brick> * 4);
//An alternate implementation of the above recipe are shown commented below. This implementation makes use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import mods.mekanism.api.ingredient.ChemicalStackIngredient.InfusionStackIngredi
* 2) Adds a recipe that produces one Gold Nugget out of 9 mB of the Gold Infuse Type.
*/

// <recipetype:mekanism:crystallizing>.addRecipe(arg0 as string, arg1 as IChemicalStackIngredient, arg2 as IItemStack)
// <recipetype:mekanism:crystallizing>.addRecipe(name as string, input as IChemicalStackIngredient, output as IItemStack)

<recipetype:mekanism:crystallizing>.addRecipe("paste_to_carrots", GasStackIngredient.from(<gas:mekanism:nutritional_paste> * 150), <item:minecraft:carrot>);
//An alternate implementation of the above recipe are shown commented below. This implementation makes use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mods.mekanism.api.ingredient.ItemStackIngredient;

//Adds a Dissolution Recipe that uses 100 mB of Sulfuric Acid (1 mB per tick) to convert Salt into 10 mB of Hydrogen Chloride.

// <recipetype:mekanism:dissolution>.addRecipe(arg0 as string, arg1 as ItemStackIngredient, arg2 as GasStackIngredient, arg3 as ICrTChemicalStack)
// <recipetype:mekanism:dissolution>.addRecipe(name as string, itemInput as ItemStackIngredient, gasInput as GasStackIngredient, output as ICrTChemicalStack)

<recipetype:mekanism:dissolution>.addRecipe("salt_to_hydrogen_chloride", ItemStackIngredient.from(<item:mekanism:salt>), GasStackIngredient.from(<gas:mekanism:sulfuric_acid>), <gas:mekanism:hydrogen_chloride> * 10);
//Alternate implementations of the above recipe are shown commented below. These implementations make use of implicit casting to allow easier calling:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mods.mekanism.api.ingredient.ItemStackIngredient;

//Adds an Energy Conversion Recipe that allows converting Redstone Ore into 45 kJ of power.

// <recipetype:mekanism:energy_conversion>.addRecipe(arg0 as string, arg1 as ItemStackIngredient, arg2 as FloatingLong)
// <recipetype:mekanism:energy_conversion>.addRecipe(name as string, input as ItemStackIngredient, output as FloatingLong)

<recipetype:mekanism:energy_conversion>.addRecipe("redstone_ore_to_power", ItemStackIngredient.from(<tag:items:forge:ores/redstone>), FloatingLong.create(45000));
//Alternate implementations of the above recipe are shown commented below. These implementations make use of implicit casting to allow easier calling:
Expand Down
Loading

0 comments on commit fa506a2

Please sign in to comment.