Skip to content

Commit

Permalink
Made api more intuitive
Browse files Browse the repository at this point in the history
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
  • Loading branch information
omoflop authored and UnlikePaladin committed Sep 17, 2023
1 parent 090054f commit 253971b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
62 changes: 51 additions & 11 deletions common/src/main/java/org/figuramc/figura/model/FiguraModelPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
6 changes: 4 additions & 2 deletions common/src/main/resources/assets/figura/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 253971b

Please sign in to comment.