Skip to content

Commit

Permalink
Swallow pride, re-implement Forge datagen
Browse files Browse the repository at this point in the history
TODO: Absorb AppMek-specific datagen when the add-on sees a 1.20 update
  • Loading branch information
62832 committed Jul 28, 2023
1 parent d3e55b5 commit 7afe92b
Show file tree
Hide file tree
Showing 17 changed files with 884 additions and 122 deletions.
20 changes: 11 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

import com.diffplug.gradle.spotless.SpotlessExtension
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

import net.fabricmc.loom.api.LoomGradleExtensionAPI
Expand All @@ -11,7 +11,6 @@ import net.fabricmc.loom.task.RemapJarTask
import dev.architectury.plugin.ArchitectPluginExtension

import me.shedaniel.unifiedpublishing.UnifiedPublishingExtension
import java.util.*

plugins {
java
Expand Down Expand Up @@ -224,15 +223,18 @@ for (platform in platforms) {
shadowCommon(project(path = ":common", configuration = "transformProduction${capitalise(platform)}")) { isTransitive = false }
}

sourceSets {
main {
resources {
srcDir(file("src/generated/resources"))
exclude("**/.cache")
}
}
}

tasks {
processResources {
extra["commonProps"] = mapOf("version" to project.version) + project.properties

from(fileTree(project(":common").file("src/generated/resources"))) {
val conventionTags = ObjectMapper().readValue(file("convention_tags.json"), object: TypeReference<Map<String, String>>() {})
expand(conventionTags)
exclude("**/.cache")
}
}

withType<ShadowJar> {
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ loom {

property("fabric-api.datagen")
property("fabric-api.datagen.modid", rootProject.property("modId").toString())
property("fabric-api.datagen.output-dir", project(":common").file("src/generated/resources").absolutePath)
property("fabric-api.datagen.output-dir", file("src/generated/resources").absolutePath)
property("fabric-api.datagen.strict-validation")
}
}
Expand Down
4 changes: 0 additions & 4 deletions fabric/convention_tags.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package gripe._90.megacells.datagen;

import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;

import gripe._90.megacells.definition.MEGABlocks;
import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.definition.MEGATranslations;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;

class LocalisationProvider extends FabricLanguageProvider {
protected LocalisationProvider(FabricDataOutput output, String locale) {
super(output, locale);
protected LocalisationProvider(FabricDataOutput output) {
super(output, "en_us");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gripe._90.megacells.datagen;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
Expand All @@ -15,17 +14,12 @@ public void onInitializeDataGenerator(FabricDataGenerator generator) {
var pack = generator.createPack();
var registries = CompletableFuture.supplyAsync(VanillaRegistries::createLookup, Util.backgroundExecutor());

var blockTagsProvider = pack
.addProvider((FabricDataOutput packOutput) -> new TagProvider.Blocks(packOutput, registries));
pack.addProvider(
(FabricDataOutput packOutput) -> new TagProvider.Items(packOutput, registries, blockTagsProvider));
var blockTags = pack.addProvider((FabricDataOutput output) -> new TagProvider.Blocks(output, registries));
pack.addProvider((FabricDataOutput output) -> new TagProvider.Items(output, registries, blockTags));

pack.addProvider(LootTableProvider::new);
pack.addProvider(ModelProvider::new);
pack.addProvider(RecipeProvider::new);

for (var en : List.of("en_us", "en_gb", "en_ca", "en_au", "en_nz")) {
pack.addProvider((FabricDataOutput packOutput) -> new LocalisationProvider(packOutput, en));
}
pack.addProvider(LootTableProvider::new);
pack.addProvider(LocalisationProvider::new);
}
}
86 changes: 61 additions & 25 deletions fabric/src/data/java/gripe/_90/megacells/datagen/ModelProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.ibm.icu.impl.Pair;

import org.jetbrains.annotations.NotNull;
Expand All @@ -19,13 +20,15 @@
import net.minecraft.data.models.blockstates.PropertyDispatch;
import net.minecraft.data.models.blockstates.Variant;
import net.minecraft.data.models.blockstates.VariantProperties;
import net.minecraft.data.models.blockstates.VariantProperty;
import net.minecraft.data.models.model.DelegatedModel;
import net.minecraft.data.models.model.ModelTemplate;
import net.minecraft.data.models.model.ModelTemplates;
import net.minecraft.data.models.model.TextureMapping;
import net.minecraft.data.models.model.TextureSlot;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;

import appeng.api.orientation.BlockOrientation;
import appeng.block.crafting.AbstractCraftingUnitBlock;
Expand Down Expand Up @@ -64,6 +67,9 @@ class ModelProvider extends FabricModelProvider {
Optional.of(AppEng.makeId("item/cable_interface")), Optional.empty(),
SIDES, TextureSlot.BACK, TextureSlot.FRONT);

private static final VariantProperty<VariantProperties.Rotation> Z_ROT = new VariantProperty<>("ae2:z",
r -> new JsonPrimitive(r.ordinal() * 90));

ModelProvider(FabricDataOutput output) {
super(output);
}
Expand Down Expand Up @@ -91,8 +97,6 @@ public void generateBlockStateModels(BlockModelGenerators generator) {

@Override
public void generateItemModels(ItemModelGenerators generator) {
generatePartModels(generator);

generator.generateFlatItem(MEGAItems.MEGA_ITEM_CELL_HOUSING.asItem(), ModelTemplates.FLAT_ITEM);
generator.generateFlatItem(MEGAItems.MEGA_FLUID_CELL_HOUSING.asItem(), ModelTemplates.FLAT_ITEM);

Expand Down Expand Up @@ -141,6 +145,8 @@ public void generateItemModels(ItemModelGenerators generator) {
driveCell("mega_item_cell", generator);
driveCell("mega_fluid_cell", generator);
driveCell("bulk_item_cell", generator);

generatePartModels(generator);
}

private void generatePartModels(ItemModelGenerators generator) {
Expand All @@ -153,8 +159,8 @@ private void cellModel(ItemDefinition<?> cell, ItemModelGenerators generator) {
.put(TextureSlot.LAYER1, AppEng.makeId("item/storage_cell_led")), generator.output);
}

private void portableModel(ItemDefinition<?> portable, String screenType,
ResourceLocation housingTexture, ItemModelGenerators generator) {
private void portableModel(ItemDefinition<?> portable, String screenType, ResourceLocation housingTexture,
ItemModelGenerators generator) {
var path = portable.id().getPath();
var tierSuffix = path.substring(path.lastIndexOf('_') + 1);
PORTABLE.create(Utils.makeId("item/" + portable.id().getPath()), new TextureMapping()
Expand Down Expand Up @@ -187,22 +193,30 @@ private void craftingUnit(Block block, String texture, BlockModelGenerators gene
}

private void craftingMonitor(BlockModelGenerators generator) {
var formed = Utils.makeId("block/crafting/monitor_formed");
var unformed = Utils.makeId("block/crafting/monitor");
var unit = Utils.makeId("block/crafting/unit");
var unformedModel = ModelTemplates.CUBE.create(unformed, new TextureMapping()
.put(TextureSlot.NORTH, unformed)
.put(TextureSlot.EAST, unit)
.put(TextureSlot.SOUTH, unit)
.put(TextureSlot.WEST, unit)
.put(TextureSlot.DOWN, unit)
.put(TextureSlot.UP, unit)
.put(TextureSlot.PARTICLE, unformed), generator.modelOutput);

var formedModel = Utils.makeId("block/crafting/monitor_formed");
generator.blockStateOutput.accept(MultiVariantGenerator.multiVariant(MEGABlocks.CRAFTING_MONITOR.block())
.with(PropertyDispatch.property(AbstractCraftingUnitBlock.FORMED)
.select(false, Variant.variant().with(VariantProperties.MODEL,
ModelTemplates.CUBE.create(unformed, new TextureMapping()
.put(TextureSlot.NORTH, unformed)
.put(TextureSlot.EAST, unit)
.put(TextureSlot.SOUTH, unit)
.put(TextureSlot.WEST, unit)
.put(TextureSlot.DOWN, unit)
.put(TextureSlot.UP, unit)
.put(TextureSlot.PARTICLE, unformed), generator.modelOutput)))
.select(true, Variant.variant().with(VariantProperties.MODEL, formed))));
generator.modelOutput.accept(formed, () -> customModelLoader(formed));
.with(PropertyDispatch.properties(AbstractCraftingUnitBlock.FORMED, BlockStateProperties.FACING)
.generate((formed, facing) -> {
if (formed) {
return Variant.variant().with(VariantProperties.MODEL, formedModel);
} else {
return applyOrientation(
Variant.variant().with(VariantProperties.MODEL, unformedModel),
BlockOrientation.get(facing));
}
})));
generator.modelOutput.accept(formedModel, () -> customModelLoader(formedModel));
generator.delegateItemModel(MEGABlocks.CRAFTING_MONITOR.block(), unformed);
}

Expand Down Expand Up @@ -234,20 +248,42 @@ private void patternProviderBlock(BlockModelGenerators generator) {
return Variant.variant().with(VariantProperties.MODEL, normal);
} else {
var orientation = BlockOrientation.get(forward);

// + 90 because the default model is oriented UP, while block orientation assumes NORTH
var angleX = normalizeAngle(orientation.getAngleX() + 90);
var angleY = normalizeAngle(orientation.getAngleY());

return Variant.variant().with(VariantProperties.MODEL, oriented)
.with(VariantProperties.X_ROT, rotationByAngle(angleX))
.with(VariantProperties.Y_ROT, rotationByAngle(angleY));
return applyRotation(
Variant.variant().with(VariantProperties.MODEL, oriented),
// + 90 because the default model is oriented UP, while block orientation assumes NORTH
orientation.getAngleX() + 90,
orientation.getAngleY(),
0);
}
})));

generator.delegateItemModel(MEGABlocks.MEGA_PATTERN_PROVIDER.block(), normal);
}

protected Variant applyOrientation(Variant variant, BlockOrientation orientation) {
return applyRotation(variant,
orientation.getAngleX(),
orientation.getAngleY(),
orientation.getAngleZ());
}

protected Variant applyRotation(Variant variant, int angleX, int angleY, int angleZ) {
angleX = normalizeAngle(angleX);
angleY = normalizeAngle(angleY);
angleZ = normalizeAngle(angleZ);

if (angleX != 0) {
variant = variant.with(VariantProperties.X_ROT, rotationByAngle(angleX));
}
if (angleY != 0) {
variant = variant.with(VariantProperties.Y_ROT, rotationByAngle(angleY));
}
if (angleZ != 0) {
variant = variant.with(Z_ROT, rotationByAngle(angleZ));
}
return variant;
}

private int normalizeAngle(int angle) {
return angle - (angle / 360) * 360;
}
Expand Down
Loading

0 comments on commit 7afe92b

Please sign in to comment.