|
| 1 | +package codechicken.lib.datagen; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import net.minecraft.client.resources.model.BakedModel; |
| 5 | +import net.minecraft.resources.ResourceLocation; |
| 6 | +import net.neoforged.neoforge.client.model.generators.CustomLoaderBuilder; |
| 7 | +import net.neoforged.neoforge.client.model.generators.ModelBuilder; |
| 8 | +import net.neoforged.neoforge.common.data.ExistingFileHelper; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +import java.lang.reflect.Constructor; |
| 12 | +import java.lang.reflect.Modifier; |
| 13 | + |
| 14 | +import static codechicken.lib.CodeChickenLib.MOD_ID; |
| 15 | +import static java.util.Objects.requireNonNull; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by covers1624 on 1/21/25. |
| 19 | + */ |
| 20 | +public class ClassModelLoaderBuilder<T extends ModelBuilder<T>> extends CustomLoaderBuilder<T> { |
| 21 | + |
| 22 | + private @Nullable Class<? extends BakedModel> clazz; |
| 23 | + |
| 24 | + public ClassModelLoaderBuilder(T parent, ExistingFileHelper existingFileHelper) { |
| 25 | + super(ResourceLocation.fromNamespaceAndPath(MOD_ID, "class"), parent, existingFileHelper, false); |
| 26 | + } |
| 27 | + |
| 28 | + public ClassModelLoaderBuilder<T> clazz(Class<? extends BakedModel> clazz) { |
| 29 | + try { |
| 30 | + Constructor<?> ctor = clazz.getConstructor(); |
| 31 | + if (!Modifier.isPublic(ctor.getModifiers())) { |
| 32 | + throw new IllegalArgumentException("Expected single no-args public constructor."); |
| 33 | + } |
| 34 | + } catch (NoSuchMethodException ex) { |
| 35 | + throw new IllegalStateException("Expected single no-args public constructor.", ex); |
| 36 | + } |
| 37 | + this.clazz = clazz; |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public JsonObject toJson(JsonObject json) { |
| 43 | + super.toJson(json); |
| 44 | + json.addProperty("class", requireNonNull(clazz).getName()); |
| 45 | + return json; |
| 46 | + } |
| 47 | +} |
0 commit comments