Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;
import java.util.Locale;
import org.jspecify.annotations.Nullable;

/**
* Item icon set folder under {@code textures/item/material_icons/}, matching GregTech 6 texture sets.
Expand Down Expand Up @@ -100,6 +101,33 @@ public enum MaterialTextureSet {
*/
public static List<MaterialTextureSet> modeled() { return MODELED_SETS; }

/**
* Set whose icon folder this one borrows the missing art from, or {@code null} for the two
* roots. Child sets only override the icons whose art actually differs.
*/
@Nullable
public MaterialTextureSet parent() {
return switch (this) {
case DULL, STONE -> null;
case NONE, FINE, METALLIC -> DULL;
case SAND, FOOD, FIERY, PAPER, LEAF, ROUGH -> FINE;
case POWDER -> FOOD;
case LIGNITE, LAPIS -> ROUGH;
case FLINT -> LIGNITE;
case REDSTONE, CUBE -> LAPIS;
case COPPER, MAGNETIC, SHINY -> METALLIC;
case RUBBER, GLASS, RUBY -> SHINY;
case OPAL, DIAMOND -> RUBY;
case EMERALD, GEM_HORIZONTAL, GEM_VERTICAL, SHARDS, CUBE_SHINY -> DIAMOND;
case BRICK, QUARTZ -> STONE;
case SPACE -> BRICK;
case WOOD, PRISMARINE -> QUARTZ;
case FLUID, GAS, HEX, NETHERSTAR, PLASMA, RAD -> DULL;
};
}

public boolean isRoot() { return parent() == null; }

/** Lowercase folder name used in resource locations (MC normalizes paths to lowercase). */
public String folder() {
return name().toLowerCase(Locale.ROOT);
Expand Down
10 changes: 10 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repositories {

dependencies {
api project(':api')
compileOnly 'org.jspecify:jspecify:1.0.0'
testImplementation platform('org.junit:junit-bom:5.11.4')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand All @@ -17,6 +18,15 @@ tasks.named('test').configure {
useJUnitPlatform()
systemProperty 'gt6ce.recipeRoot',
layout.projectDirectory.dir('src/main/resources/data/gt6ce/recipes').asFile.absolutePath
// Asset roots for the material icon and machine model integrity checks.
systemProperty 'gt6ce.coreAssets',
layout.projectDirectory.dir('src/main/resources/assets/gt6ce').asFile.absolutePath
systemProperty 'gt6ce.minecraftAssets',
layout.projectDirectory.dir('../minecraft/src/main/resources/assets/gt6ce').asFile.absolutePath
systemProperty 'gt6ce.forgeAssets',
layout.projectDirectory.dir('../minecraft/versions/1.20.1-forge/src/main/resources/assets/gt6ce').asFile.absolutePath
systemProperty 'gt6ce.neoforgeAssets',
layout.projectDirectory.dir('../minecraft/versions/1.21.1-neoforge/src/main/resources/assets/gt6ce').asFile.absolutePath
}

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package gregtech.engine.machine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.Nullable;

/**
* Compact face table for the orthogonally baked machine models.
*
* <p>Rows live in {@code assets/gt6ce/machine_models.tsv}; the client bake reads them instead of
* shipping one static model JSON per machine, per lit/running state and per facing. The six face
* columns are fixed to {@code DOWN, UP, NORTH, SOUTH, WEST, EAST}, the same numbers the vanilla
* direction enum uses, so a row is a cube's worth of textures rather than a JSON tree.
*
* <p>This class stays free of Minecraft types: it only resolves texture paths to strings.
*/
public final class OrthogonalMachineModels {
private static final String RESOURCE = "/assets/gt6ce/machine_models.tsv";
private static final String TEXTURE_NAMESPACE = "gt6ce:";
private static final String MACHINE_TEXTURE_DIRECTORY = "block/machines/";
/** {@code modelPath, solidBase, particle, base x 6, overlay x 6}. */
private static final int COLUMNS = 15;

private static final int BASE_COLUMN = 3;
private static final int OVERLAY_COLUMN = 9;
/** A face the machine does not draw, and a particle the row leaves empty. */
private static final String ABSENT = "-";

private static final Map<String, Entry> MODELS = load();

private OrthogonalMachineModels() {}

/** Every row, keyed by model path; empty when the table has not been generated yet. */
public static Map<String, Entry> all() {
return MODELS;
}

public static @Nullable Entry get(String modelPath) {
return modelPath == null ? null : MODELS.get(modelPath);
}

public static boolean contains(String modelPath) {
return modelPath != null && MODELS.containsKey(modelPath);
}

/** Rows name machine textures relative to {@code gt6ce:block/machines/}; other packs name their own. */
public static String fullTexturePath(String specTexture) {
return specTexture.indexOf(':') >= 0
? specTexture
: TEXTURE_NAMESPACE + MACHINE_TEXTURE_DIRECTORY + specTexture;
}

/**
* One table row. {@code base} carries the tinted shell faces, {@code overlay} the untinted
* detail pass; an entry may hold either pass alone but never neither.
*/
public record Entry(
String modelPath,
boolean solidBase,
@Nullable String particle,
@Nullable String[] base,
@Nullable String[] overlay) {
public Entry {
base = normalize(base);
overlay = normalize(overlay);
}

/** Texture of one base face, or null when the shell does not draw it. */
public @Nullable String baseFace(int face) {
return face(base, face);
}

/** Texture of one overlay face, or null when the machine has no detail on it. */
public @Nullable String overlayFace(int face) {
return face(overlay, face);
}

public boolean hasOverlay() {
return overlay != null;
}
}

/** Parses a whole table, skipping blank lines and {@code #} comments. */
public static List<Entry> parse(Reader reader) throws IOException {
BufferedReader lines = reader instanceof BufferedReader buffered ? buffered : new BufferedReader(reader);
List<Entry> entries = new ArrayList<>();
String line;
int number = 0;
while ((line = lines.readLine()) != null) {
number++;
String trimmed = line.strip();
if (trimmed.isEmpty() || trimmed.startsWith("#")) continue;
String[] columns = line.split("\t", -1);
if (columns.length != COLUMNS)
throw new IllegalStateException(
"Expected " + COLUMNS + " columns on line " + number + " of " + RESOURCE);
String modelPath = columns[0].strip();
if (modelPath.isEmpty())
throw new IllegalStateException("Empty model path on line " + number + " of " + RESOURCE);
entries.add(new Entry(
modelPath,
columns[1].strip().equals("1"),
absent(columns[2]),
faces(columns, BASE_COLUMN),
faces(columns, OVERLAY_COLUMN)));
}
return List.copyOf(entries);
}

private static Map<String, Entry> load() {
// The table ships separately from this class; an empty map lets missing rows be reported
// without aborting startup, rather than leaving deleted static block models in place.
try (InputStream stream = OrthogonalMachineModels.class.getResourceAsStream(RESOURCE)) {
if (stream == null) return Map.of();
Map<String, Entry> models = new LinkedHashMap<>();
for (Entry entry : parse(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
if (models.put(entry.modelPath(), entry) != null)
throw new IllegalStateException("Duplicate model " + entry.modelPath() + " in " + RESOURCE);
}
return Collections.unmodifiableMap(models);
} catch (IOException e) {
throw new IllegalStateException("Cannot read " + RESOURCE, e);
}
}

private static @Nullable String[] faces(String[] columns, int start) {
String[] faces = new String[6];
boolean any = false;
for (int face = 0; face < faces.length; face++) {
faces[face] = absent(columns[start + face]);
any |= faces[face] != null;
}
return any ? faces : null;
}

/** Fixed-length face array; callers index it by direction id, so it never stays ragged. */
private static @Nullable String[] normalize(@Nullable String[] faces) {
if (faces == null) return null;
String[] copy = new String[6];
for (int face = 0; face < copy.length; face++) copy[face] = face < faces.length ? absent(faces[face]) : null;
return copy;
}

private static @Nullable String absent(@Nullable String value) {
if (value == null) return null;
String stripped = value.strip();
return stripped.isEmpty() || stripped.equals(ABSENT) ? null : stripped;
}

private static @Nullable String face(@Nullable String[] faces, int face) {
return faces == null || face < 0 || face >= faces.length ? null : faces[face];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import gregtech.api.material.*;
import gregtech.content.data.MaterialPrefix;
import java.util.function.Predicate;
import org.jspecify.annotations.Nullable;

/** Shared original material icon selection and resource paths, without client classes. */
public final class MaterialIconDefinitions {
Expand Down Expand Up @@ -29,11 +31,6 @@ public static String modelPath(MaterialTextureSet set, MaterialPrefix prefix) {
return "item/material/" + set.folder() + "/" + prefix.getTextureFileName();
}

/** Item model for this material and prefix; {@code overlay} follows the icon art of its set. */
public static String modelJson(GTMaterial material, MaterialPrefix prefix, boolean overlay) {
return generatedModelJson(resolveTextureSet(material), prefix, overlay);
}

/** One generated item model. The texture lives in the addon namespace and is tinted by material colour. */
public static String addonItemModel(MaterialPrefix prefix) {
return """
Expand All @@ -46,26 +43,101 @@ public static String addonItemModel(MaterialPrefix prefix) {
""".formatted(prefix.textureNamespace(), prefix.addonTexturePath());
}

public static String sharedModelJson(MaterialTextureSet set, MaterialPrefix prefix, boolean overlay) {
return generatedModelJson(set, prefix, overlay);
}

/** Both platform geometry loaders delegate this reference to the original baked template cache. */
public static String sharedInventoryJson(String parent) {
return "{\"loader\":\"" + gregtech.engine.mod.GregTechIdentity.MOD_ID + ":shared_inventory\",\"parent\":\""
+ parent + "\"}";
}

private static String generatedModelJson(MaterialTextureSet set, MaterialPrefix prefix, boolean overlay) {
if (!overlay) {
/** Nearest set in the parent chain whose {@code hasOwnResource} probe passes, or {@code null}. */
@Nullable
private static MaterialTextureSet resolveChainOwner(
MaterialTextureSet set, Predicate<MaterialTextureSet> hasOwnResource) {
for (MaterialTextureSet current = set; current != null; current = current.parent()) {
if (hasOwnResource.test(current)) return current;
}
return null;
}

/**
* Nearest set in the inheritance chain that actually ships {@code fileName}, or {@code null}
* when no set in the chain does. {@code resourceDirectory} is relative to {@code textures/}.
*/
@Nullable
public static MaterialTextureSet resolveTextureOwner(
MaterialTextureSet set, String resourceDirectory, String fileName, Predicate<String> resourceExists) {
return resolveChainOwner(
set,
current -> resourceExists.test(
"textures/" + resourceDirectory + "/" + current.folder() + "/" + fileName + ".png"));
}

/** Icon set owning the item sprite for this prefix, following {@link MaterialTextureSet#parent()}. */
@Nullable
public static MaterialTextureSet resolveItemTextureOwner(
MaterialTextureSet set, MaterialPrefix prefix, boolean overlay, Predicate<String> resourceExists) {
return resolveTextureOwner(
set, "item/material_icons", prefix.getTextureFileName() + (overlay ? "_overlay" : ""), resourceExists);
}

/**
* Sprite id of one block icon, relative to the {@code gt6ce} namespace: the atlas key and the
* {@code textures/} resource path differ only by that prefix and the {@code .png} suffix.
*/
public static String blockIconPath(MaterialTextureSet set, String fileName) {
return "block/material_icons/" + set.folder() + "/" + fileName;
}

/**
* Icon set owning a block sprite, resolved from sprite ids alone, following
* {@link MaterialTextureSet#parent()}.
*/
@Nullable
public static MaterialTextureSet resolveBlockIconOwner(
MaterialTextureSet set, String fileName, Predicate<String> spritePathExists) {
return resolveChainOwner(set, current -> spritePathExists.test(blockIconPath(current, fileName)));
}

/** Icon set owning a block sprite file, following {@link MaterialTextureSet#parent()}. */
@Nullable
public static MaterialTextureSet resolveBlockTextureOwner(
MaterialTextureSet set, String fileName, Predicate<String> resourceExists) {
return resolveChainOwner(
set, current -> resourceExists.test("textures/" + blockIconPath(current, fileName) + ".png"));
}

/** Classpath probe for probes taken before the first client resource reload finishes. */
public static boolean classpathResourceExists(String relativePath) {
return MaterialIconDefinitions.class.getResource(
"/assets/" + gregtech.engine.mod.GregTechIdentity.REGISTRY_NAMESPACE + "/" + relativePath)
!= null;
}

/**
* Shared item model JSON for one set and prefix, or {@code null} when no set in the chain ships
* the base sprite. The overlay layer is emitted only when the chain ships a detail sprite.
*/
@Nullable
public static String sharedModelJson(
MaterialTextureSet set, MaterialPrefix prefix, Predicate<String> resourceExists) {
MaterialTextureSet baseOwner = resolveItemTextureOwner(set, prefix, false, resourceExists);
if (baseOwner == null) return null;
MaterialTextureSet overlayOwner = resolveItemTextureOwner(set, prefix, true, resourceExists);
return generatedModelJson(
"gt6ce:" + texturePath(baseOwner, prefix, false),
overlayOwner == null ? null : "gt6ce:" + texturePath(overlayOwner, prefix, true));
}

private static String generatedModelJson(String layer0, @Nullable String layer1) {
if (layer1 == null) {
return """
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "%s"
}
}
""".formatted("gt6ce:" + texturePath(set, prefix, false));
""".formatted(layer0);
}
return """
{
Expand All @@ -75,6 +147,6 @@ private static String generatedModelJson(MaterialTextureSet set, MaterialPrefix
"layer1": "%s"
}
}
""".formatted("gt6ce:" + texturePath(set, prefix, false), "gt6ce:" + texturePath(set, prefix, true));
""".formatted(layer0, layer1);
}
}
Loading
Loading