Skip to content

Commit

Permalink
Turn to proper plugin with registered task
Browse files Browse the repository at this point in the history
  • Loading branch information
Griefed committed Nov 8, 2023
1 parent 0d6f6e2 commit 592689e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gradlePlugin {
plugins {
create("block-and-item-code-generator") {
id = "de.griefed.generation.block-and-item-code-generator"
implementationClass = "de.griefed.generation.BlockAndItemCodeGenerator"
implementationClass = "de.griefed.generation.BlockAndItemCodeGeneratorPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.griefed.generation;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

/**
* Generates Forge, Fabric and Quilt block-implementations from block-definitions in a JSON-file.<br>
* See <code>definitions/blocks.json</code><br>
* Take note that textures for blocks must be present in <code>definitions/blocks</code> and the name of the textures
* must match the ID of the block.
*/
public class BlockAndItemCodeGeneratorPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getTasks().register("blockAndItemCodeGen", GenerateBlocksAndItemsCode.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,31 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import de.griefed.generation.blocks.BlockDefinitionParser;
import de.griefed.generation.generator.CommonSubProjectGenerator;
import org.gradle.api.Plugin;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
* Generates Forge, Fabric and Quilt block-implementations from block-definitions in a JSON-file.<br>
* See <code>definitions/blocks.json</code><br>
* Take note that textures for blocks must be present in <code>definitions/blocks</code> and the name of the textures
* must match the ID of the block.
*/
public class BlockAndItemCodeGenerator implements Plugin<Project> {
public class GenerateBlocksAndItemsCode extends DefaultTask {

private final ObjectMapper objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature());

public void apply(Project project) {
project.getTasks().register("blockAndItemCodeGen", task -> task.doLast(s -> System.out.println("Hello from plugin " + this.getClass().getName())));
@TaskAction
public void generateCode() {
try {
Properties gradleProperties = new Properties();
gradleProperties.load(new FileInputStream(new File(project.getRootDir(), "gradle.properties")));
gradleProperties.load(new FileInputStream(new File(getProject().getRootDir(), "gradle.properties")));
String modName = gradleProperties.getProperty("mod_name");

BlockDefinitionParser parser = new BlockDefinitionParser(project, objectMapper);
CommonSubProjectGenerator common = new CommonSubProjectGenerator(project.findProject("Common"), modName, parser, objectMapper);
BlockDefinitionParser parser = new BlockDefinitionParser(getProject(), objectMapper);
CommonSubProjectGenerator common = new CommonSubProjectGenerator(getProject().findProject("Common"), modName, parser, objectMapper);
common.run();
} catch (IOException e) {
throw new RuntimeException("Error generating block and item code.", e);
Expand Down

0 comments on commit 592689e

Please sign in to comment.