diff --git a/.gitignore b/.gitignore index 96ef6c0..1f25516 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,13 @@ /target Cargo.lock +.vscode +.idea +.gradle +/extractor/build +/extractor/out +/extractor/classes +/extractor/run + +/extractor/gradlew +/extractor/gradlew.bat +/extractor/gradle/wrapper diff --git a/extractor/build.gradle b/extractor/build.gradle new file mode 100644 index 0000000..8712c2f --- /dev/null +++ b/extractor/build.gradle @@ -0,0 +1,37 @@ +plugins { + id 'fabric-loom' version '0.12-SNAPSHOT' +} + +sourceCompatibility = JavaVersion.VERSION_17 +targetCompatibility = JavaVersion.VERSION_17 + +archivesBaseName = project.archives_base_name +version = project.mod_version +group = project.maven_group + +dependencies { + implementation 'com.google.code.gson:gson:2.9.0' + + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 17 +} + +java { + withSourcesJar() +} diff --git a/extractor/gradle.properties b/extractor/gradle.properties new file mode 100644 index 0000000..2a1e67a --- /dev/null +++ b/extractor/gradle.properties @@ -0,0 +1,16 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G + +# Fabric Properties + # check these on https://fabricmc.net/develop + minecraft_version=1.19 + yarn_mappings=1.19+build.4 + loader_version=0.14.8 + +# Mod Properties + mod_version = 1.0.0 + maven_group = dev.00a + archives_base_name = valence-extractor + +# Dependencies + fabric_version=0.57.0+1.19 diff --git a/extractor/settings.gradle b/extractor/settings.gradle new file mode 100644 index 0000000..b02216b --- /dev/null +++ b/extractor/settings.gradle @@ -0,0 +1,10 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/Extractor.java b/extractor/src/main/java/dev/_00a/valence_extractor/Extractor.java new file mode 100644 index 0000000..48ecbdc --- /dev/null +++ b/extractor/src/main/java/dev/_00a/valence_extractor/Extractor.java @@ -0,0 +1,124 @@ +package dev._00a.valence_extractor; + +import com.google.gson.*; +import net.fabricmc.api.ModInitializer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.EmptyBlockView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Extractor implements ModInitializer { + public static final String MOD_ID = "valence_extractor"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + private Gson gson; + private Path outputDirectory; + + @Override + public void onInitialize() { + LOGGER.info("Starting extractor..."); + + try { + outputDirectory = Files.createDirectories(Paths.get("valence_extractor_output")); + gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); + + extractBlocks(); + extractEntities(); + } catch (Throwable e) { + LOGGER.error("Extraction failed", e); + System.exit(1); + } + + LOGGER.info("Extractor finished successfully"); + System.exit(0); + } + + void extractBlocks() throws IOException { + var blocksJson = new JsonArray(); + var stateIdCounter = 0; + + for (var block : Registry.BLOCK) { + var blockJson = new JsonObject(); +// blockJson.addProperty("id", Registry.BLOCK.getRawId(block)); + blockJson.addProperty("translation_key", block.getTranslationKey()); +// blockJson.addProperty("min_state_id", stateIdCounter); +// blockJson.addProperty("max_state_id", stateIdCounter + block.getStateManager().getStates().size() - 1); + + var propsJson = new JsonArray(); + for (var prop : block.getStateManager().getProperties()) { + var propJson = new JsonObject(); + + propJson.addProperty("name", prop.getName()); + + var valuesJson = new JsonArray(); + for (var value : prop.getValues()) { + valuesJson.add(value.toString()); + } + propJson.add("values", valuesJson); + + propsJson.add(propJson); + } + blockJson.add("properties", propsJson); + + var statesJson = new JsonArray(); + for (var state : block.getStateManager().getStates()) { + var stateJson = new JsonObject(); + var id = stateIdCounter++; + stateJson.addProperty("id", id); + stateJson.addProperty("luminance", state.getLuminance()); + stateJson.addProperty("opaque", state.isOpaque()); + + if (block.getDefaultState().equals(state)) { + blockJson.addProperty("default_state_id", id); + } + + var collisionShapesJson = new JsonArray(); + for (var box : state.getCollisionShape(EmptyBlockView.INSTANCE, BlockPos.ORIGIN).getBoundingBoxes()) { + var boxJson = new JsonObject(); + boxJson.addProperty("min_x", box.minX); + boxJson.addProperty("min_y", box.minY); + boxJson.addProperty("min_z", box.minZ); + boxJson.addProperty("max_x", box.maxX); + boxJson.addProperty("max_y", box.maxY); + boxJson.addProperty("max_z", box.maxZ); + collisionShapesJson.add(boxJson); + } + stateJson.add("collision_shapes", collisionShapesJson); + + statesJson.add(stateJson); + } + blockJson.add("states", statesJson); + + blocksJson.add(blockJson); + } + + writeJsonFile("blocks.json", blocksJson); + } + + void extractEntities() throws IOException { + var entitiesJson = new JsonArray(); + for (var entity : Registry.ENTITY_TYPE) { + var entityJson = new JsonObject(); + entityJson.addProperty("translation_key", entity.getTranslationKey()); + + entitiesJson.add(entityJson); + } + + writeJsonFile("entities.json", entitiesJson); + } + + void writeJsonFile(String fileName, JsonElement element) throws IOException { + var out = outputDirectory.resolve(fileName); + var fileWriter = new FileWriter(out.toFile(), StandardCharsets.UTF_8); + gson.toJson(element, fileWriter); + fileWriter.close(); + LOGGER.info("Wrote " + out.toAbsolutePath()); + } +} diff --git a/extractor/src/main/resources/fabric.mod.json b/extractor/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..cf1e291 --- /dev/null +++ b/extractor/src/main/resources/fabric.mod.json @@ -0,0 +1,29 @@ +{ + "schemaVersion": 1, + "id": "valence_extractor", + "version": "${version}", + "name": "Valence Extractor", + "description": "Extracts Minecraft data for use in Valence", + "authors": [ + "Valence Project Authors" + ], + "contact": { + "homepage": "https://github.com/rj00a/valence", + "sources": "https://github.com/rj00a/valence" + }, + "environment": "*", + "entrypoints": { + "main": [ + "dev._00a.valence_extractor.Extractor" + ] + }, + "mixins": [], + "depends": { + "fabricloader": ">=0.14.6", + "minecraft": "~1.19", + "java": ">=17" + }, + "suggests": { + "another-mod": "*" + } +}