From 253971b2bc161dc9c025b76b7bdb542d7f9db690 Mon Sep 17 00:00:00 2001 From: omoflop <29988852+omoflop@users.noreply.github.com> Date: Sat, 19 Aug 2023 16:02:24 -0700 Subject: [PATCH] Made api more intuitive getPrimaryTexture and getSecondaryTexture now return two values, the first being the override type, and the second being the value, if any. getPrimaryDefinedTextures and getSecondaryDefinedTextures both return a table containing each texture on the specified face --- .../figura/model/FiguraModelPart.java | 62 +++++++++++++++---- .../rendering/texture/FiguraTextureSet.java | 19 +++++- .../resources/assets/figura/lang/en_us.json | 6 +- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/org/figuramc/figura/model/FiguraModelPart.java b/common/src/main/java/org/figuramc/figura/model/FiguraModelPart.java index 5777fa3b0..647a755f8 100644 --- a/common/src/main/java/org/figuramc/figura/model/FiguraModelPart.java +++ b/common/src/main/java/org/figuramc/figura/model/FiguraModelPart.java @@ -22,10 +22,7 @@ import org.figuramc.figura.model.rendertasks.*; import org.figuramc.figura.utils.LuaUtils; import org.figuramc.figura.utils.ui.UIHelper; -import org.luaj.vm2.LuaError; -import org.luaj.vm2.LuaFunction; -import org.luaj.vm2.LuaTable; -import org.luaj.vm2.LuaValue; +import org.luaj.vm2.*; import java.util.ArrayList; import java.util.HashMap; @@ -695,7 +692,34 @@ public FiguraModelPart secondaryRenderType(String type) { @LuaWhitelist @LuaMethodDoc("model_part.get_primary_texture") - public Object getPrimaryTexture(Integer value) { + public Varargs getPrimaryTexture() { + return LuaValue.varargsOf(getTextureType(customization.primaryTexture), getTextureValue(customization.primaryTexture)); + } + + @LuaWhitelist + @LuaMethodDoc("model_part.get_secondary_texture") + public Object getSecondaryTexture() { + return LuaValue.varargsOf(getTextureType(customization.secondaryTexture), getTextureValue(customization.secondaryTexture)); + } + + private LuaValue getTextureType(TextureCustomization tex) { + if (tex == null) return LuaValue.valueOf(FiguraTextureSet.OverrideType.PRIMARY.name()); + return LuaValue.valueOf(tex.getOverrideType().name()); + } + + private LuaValue getTextureValue(TextureCustomization tex) { + if (tex == null) return LuaValue.NIL; + Object val = tex.getValue(); + if (val == null) return LuaValue.NIL; + if (val instanceof String str) return LuaValue.valueOf(str); + return LuaValue.userdataOf(val); + } + + + + @LuaWhitelist + @LuaMethodDoc("model_part.get_primary_defined_textures") + public Object getPrimaryDefinedTextures(Integer value) { if (customization.primaryTexture == null) { LuaTable tbl = new LuaTable(); FiguraTexture[] arr = this.textures.get(value).textures; @@ -711,8 +735,8 @@ public Object getPrimaryTexture(Integer value) { } @LuaWhitelist - @LuaMethodDoc("model_part.get_secondary_texture") - public Object getSecondaryTexture(Integer value) { + @LuaMethodDoc("model_part.get_secondary_defined_textures") + public Object getSecondaryDefinedTextures(Integer value) { if (customization.secondaryTexture == null) { LuaTable tbl = new LuaTable(); FiguraTexture[] arr = this.textures.get(value).textures; @@ -748,9 +772,11 @@ public Object getSecondaryTexture(Integer value) { ) public FiguraModelPart setPrimaryTexture(String type, Object x) { try { - this.customization.primaryTexture = type == null ? null : new TextureCustomization(FiguraTextureSet.OverrideType.valueOf(type.toUpperCase()), x); + FiguraTextureSet.OverrideType overrideType = FiguraTextureSet.OverrideType.valueOf(type.toUpperCase()); + checkTexture(overrideType, x); + this.customization.primaryTexture = type == null ? null : new TextureCustomization(overrideType, x); return this; - } catch (Exception ignored) { + } catch (IllegalArgumentException ignored) { throw new LuaError("Invalid texture override type: " + type); } } @@ -776,13 +802,27 @@ public FiguraModelPart setPrimaryTexture(String type, Object x) { ) public FiguraModelPart setSecondaryTexture(String type, Object x) { try { - this.customization.secondaryTexture = type == null ? null : new TextureCustomization(FiguraTextureSet.OverrideType.valueOf(type.toUpperCase()), x); + FiguraTextureSet.OverrideType overrideType = FiguraTextureSet.OverrideType.valueOf(type.toUpperCase()); + checkTexture(overrideType, x); + this.customization.secondaryTexture = type == null ? null : new TextureCustomization(overrideType, x); return this; - } catch (Exception ignored) { + } catch (IllegalArgumentException ignored) { throw new LuaError("Invalid texture override type: " + type); } } + private void checkTexture(FiguraTextureSet.OverrideType type, Object value) { + if (type.argumentType == null && value == null) return; + + if (type.argumentType == null) { + throw new LuaError("\""+type.name()+"\n texture type requires no arguments!"); + } + + if (value == null || type.argumentType != value.getClass()) { + throw new LuaError("\""+type.name()+"\" texture type requires argument type: " + type.typeName); + } + } + @LuaWhitelist public FiguraModelPart primaryTexture(String type, Object x) { return setPrimaryTexture(type, x); diff --git a/common/src/main/java/org/figuramc/figura/model/rendering/texture/FiguraTextureSet.java b/common/src/main/java/org/figuramc/figura/model/rendering/texture/FiguraTextureSet.java index 4529bdec9..db9cae8ce 100644 --- a/common/src/main/java/org/figuramc/figura/model/rendering/texture/FiguraTextureSet.java +++ b/common/src/main/java/org/figuramc/figura/model/rendering/texture/FiguraTextureSet.java @@ -7,7 +7,9 @@ import net.minecraft.resources.ResourceLocation; import org.figuramc.figura.mixin.render.layers.elytra.ElytraLayerAccessor; import org.figuramc.figura.model.TextureCustomization; +import org.jetbrains.annotations.Nullable; +import java.lang.reflect.Type; import java.util.UUID; public class FiguraTextureSet { @@ -100,11 +102,24 @@ public enum OverrideType { SKIN, CAPE, ELYTRA, - RESOURCE, + RESOURCE(String.class, "String"), PRIMARY, SECONDARY, SPECULAR, NORMAL, - CUSTOM + CUSTOM(FiguraTexture.class, "Texture"); + + public final @Nullable Type argumentType; + public final @Nullable String typeName; + + OverrideType() { + argumentType = null; + typeName = null; + } + + OverrideType(@Nullable Type argumentType, String typeName) { + this.argumentType = argumentType; + this.typeName = typeName; + } } } diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index 81361d360..64bbce61f 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -1342,9 +1342,11 @@ "figura.docs.model_part.get_secondary_render_type": "Gets the current secondary render type of this model part\nNil by default, meaning the part copies the secondary render type of its parent", "figura.docs.model_part.set_primary_render_type": "Sets the current primary render type of this model part\nNil by default, meaning the part copies the primary render type of its parent during rendering\nCheck the docs enum command for all render types", "figura.docs.model_part.set_secondary_render_type": "Sets the current secondary render type of this model part\nNil by default, meaning the part copies the secondary render type of its parent during rendering\nCheck the docs enum command for all render types", - "figura.docs.model_part.get_primary_texture": "Gets the primary texture of this part\nReturns a table containing each texture", + "figura.docs.model_part.get_primary_texture": "Gets the primary texture of this part\nReturns two values, first being the override type, second being the value, if any.", + "figura.docs.model_part.get_primary_defined_textures": "Gets the primary textures of this part\nReturns a table of each texture for the specified face.", "figura.docs.model_part.set_primary_texture": "Sets the primary texture override of this part\nCheck the TextureType types in the list docs\nIf using \"resource\", the second parameter should indicate the path to the Minecraft texture you want to use\nIf using \"custom\", the second parameter should indicate a texture object", - "figura.docs.model_part.get_secondary_texture": "Gets the secondary texture of this part\nReturns a table containing each texture", + "figura.docs.model_part.get_secondary_texture": "Gets the secondary texture of this part\nReturns two values, first being the override type, second being the value, if any.", + "figura.docs.model_part.get_secondary_defined_textures": "Gets the secondary textures of this part\nReturns a table of each texture for the specified face.", "figura.docs.model_part.set_secondary_texture": "Sets the secondary texture override of this part\nCheck the TextureType types in the list docs\nIf using \"resource\", the second parameter should indicate the path to the Minecraft texture you want to use\nIf using \"custom\", the second parameter should indicate a texture object", "figura.docs.model_part.get_textures": "Returns a table with all textures used by this part\nDo not include children textures, so groups usually will return an empty table", "figura.docs.model_part.part_to_world_matrix": "Gets a matrix which transforms a point from this part's position to a world location\nRecommended to use this in POST_RENDER, as by then the matrix is updated\nIn RENDER it will be 1 frame behind the part's visual position for that frame\nAlso, if the model is not rendered in-world, the part's matrix will not be updated\nPaperdoll rendering and other UI rendering will not affect this matrix",