-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'ClassModelLoader' - Instantiates a specific class for the model.
- Loading branch information
1 parent
fa868eb
commit 0589d18
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package codechicken.lib.model; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import net.covers1624.quack.gson.JsonUtils; | ||
import net.minecraft.client.renderer.block.model.ItemOverrides; | ||
import net.minecraft.client.renderer.texture.TextureAtlasSprite; | ||
import net.minecraft.client.resources.model.BakedModel; | ||
import net.minecraft.client.resources.model.Material; | ||
import net.minecraft.client.resources.model.ModelBaker; | ||
import net.minecraft.client.resources.model.ModelState; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraftforge.client.model.geometry.IGeometryBakingContext; | ||
import net.minecraftforge.client.model.geometry.IGeometryLoader; | ||
import net.minecraftforge.client.model.geometry.IUnbakedGeometry; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.function.Function; | ||
|
||
import static net.covers1624.quack.util.SneakyUtils.unsafeCast; | ||
|
||
/** | ||
* Created by covers1624 on 13/11/23. | ||
*/ | ||
public class ClassModelLoader implements IGeometryLoader<ClassModelLoader.Geometry> { | ||
|
||
@Override | ||
public Geometry read(JsonObject jsonObject, JsonDeserializationContext deserializationContext) throws JsonParseException { | ||
try { | ||
Class<?> clazz = Class.forName(JsonUtils.getString(jsonObject, "class")); | ||
Constructor<? extends BakedModel> ctor = unsafeCast(clazz.getConstructor()); | ||
return new Geometry(ctor); | ||
} catch (ClassNotFoundException | NoSuchMethodException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
public record Geometry(Constructor<? extends BakedModel> ctor) implements IUnbakedGeometry<Geometry> { | ||
|
||
@Override | ||
public BakedModel bake(IGeometryBakingContext context, ModelBaker baker, Function<Material, TextureAtlasSprite> spriteGetter, ModelState modelState, ItemOverrides overrides, ResourceLocation modelLocation) { | ||
try { | ||
return ctor.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { | ||
throw new RuntimeException("Failed to construct class.", ex); | ||
} | ||
} | ||
} | ||
} |