From 602112724d8236c1ec6671e1893128862c9f5815 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 22 Feb 2024 00:30:05 +0100 Subject: Add custom model predicates Add regex support Add and and or predicates --- .../custommodels/BakedOverrideDataHolder.java | 33 +++++++++++++ .../custommodels/ModelOverrideDataHolder.java | 33 +++++++++++++ .../custommodels/PatchOverrideDeserializer.java | 55 ++++++++++++++++++++++ .../TestForFirmamentOverridePredicatesPatch.java | 52 ++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 src/main/java/moe/nea/firmament/mixins/custommodels/BakedOverrideDataHolder.java create mode 100644 src/main/java/moe/nea/firmament/mixins/custommodels/ModelOverrideDataHolder.java create mode 100644 src/main/java/moe/nea/firmament/mixins/custommodels/PatchOverrideDeserializer.java create mode 100644 src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java (limited to 'src/main/java/moe/nea/firmament/mixins') diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/BakedOverrideDataHolder.java b/src/main/java/moe/nea/firmament/mixins/custommodels/BakedOverrideDataHolder.java new file mode 100644 index 0000000..5e5f863 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custommodels/BakedOverrideDataHolder.java @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custommodels; + +import moe.nea.firmament.features.texturepack.BakedOverrideData; +import moe.nea.firmament.features.texturepack.FirmamentModelPredicate; +import net.minecraft.client.render.model.json.ModelOverrideList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +@Mixin(ModelOverrideList.BakedOverride.class) +public class BakedOverrideDataHolder implements BakedOverrideData { + + @Unique + private FirmamentModelPredicate[] firmamentOverrides; + + @Nullable + @Override + public FirmamentModelPredicate[] getFirmamentOverrides() { + return firmamentOverrides; + } + + @Override + public void setFirmamentOverrides(@NotNull FirmamentModelPredicate[] overrides) { + this.firmamentOverrides = overrides; + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/ModelOverrideDataHolder.java b/src/main/java/moe/nea/firmament/mixins/custommodels/ModelOverrideDataHolder.java new file mode 100644 index 0000000..8a476db --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custommodels/ModelOverrideDataHolder.java @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custommodels; + +import moe.nea.firmament.features.texturepack.FirmamentModelPredicate; +import moe.nea.firmament.features.texturepack.ModelOverrideData; +import net.minecraft.client.render.model.json.ModelOverride; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +@Mixin(ModelOverride.class) +public class ModelOverrideDataHolder implements ModelOverrideData { + + @Unique + private FirmamentModelPredicate[] overrides; + + @Nullable + @Override + public FirmamentModelPredicate[] getFirmamentOverrides() { + return overrides; + } + + @Override + public void setFirmamentOverrides(@NotNull FirmamentModelPredicate[] overrides) { + this.overrides = overrides; + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/PatchOverrideDeserializer.java b/src/main/java/moe/nea/firmament/mixins/custommodels/PatchOverrideDeserializer.java new file mode 100644 index 0000000..e85bff0 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custommodels/PatchOverrideDeserializer.java @@ -0,0 +1,55 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custommodels; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.sugar.Local; +import moe.nea.firmament.features.texturepack.CustomModelOverrideParser; +import moe.nea.firmament.features.texturepack.ModelOverrideData; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.List; +import java.util.Map; + +@Mixin(ModelOverride.Deserializer.class) +public class PatchOverrideDeserializer { + + @ModifyReturnValue( + method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/client/render/model/json/ModelOverride;", + at = @At(value = "RETURN")) + private ModelOverride addCustomOverrides(ModelOverride original, @Local JsonObject jsonObject) { + var originalData = (ModelOverrideData) original; + originalData.setFirmamentOverrides(CustomModelOverrideParser.parseCustomModelOverrides(jsonObject)); + return original; + } + + @ModifyExpressionValue( + method = "deserializeMinPropertyValues(Lcom/google/gson/JsonObject;)Ljava/util/List;", + at = @At(value = "INVOKE", target = "Ljava/util/Map$Entry;getValue()Ljava/lang/Object;")) + private Object removeFirmamentPredicatesFromJsonIteration(Object original, @Local Map.Entry entry) { + if (entry.getKey().startsWith("firmament:")) return new JsonPrimitive(0F); + return original; + } + + @Inject( + method = "deserializeMinPropertyValues", + at = @At(value = "INVOKE", target = "Ljava/util/Map;entrySet()Ljava/util/Set;") + ) + private void whatever(JsonObject object, CallbackInfoReturnable> cir, + @Local Map maps) { + maps.entrySet().removeIf(it -> it.getKey().getNamespace().equals("firmament")); + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java b/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java new file mode 100644 index 0000000..4db9fc0 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java @@ -0,0 +1,52 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custommodels; + +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; +import moe.nea.firmament.features.texturepack.BakedOverrideData; +import moe.nea.firmament.features.texturepack.FirmamentModelPredicate; +import moe.nea.firmament.features.texturepack.ModelOverrideData; +import net.minecraft.client.render.model.json.ModelOverride; +import net.minecraft.client.render.model.json.ModelOverrideList; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +@Mixin(ModelOverrideList.class) +public class TestForFirmamentOverridePredicatesPatch { + + @ModifyArg(method = "(Lnet/minecraft/client/render/model/Baker;Lnet/minecraft/client/render/model/json/JsonUnbakedModel;Ljava/util/List;)V", + at = @At( + value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + )) + public Object onInit( + Object element, + @Local ModelOverride modelOverride + ) { + var bakedOverride = (ModelOverrideList.BakedOverride) element; + ((BakedOverrideData) bakedOverride) + .setFirmamentOverrides(((ModelOverrideData) modelOverride).getFirmamentOverrides()); + return element; + } + + @ModifyExpressionValue(method = "apply", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/json/ModelOverrideList$BakedOverride;test([F)Z")) + public boolean testFirmamentOverrides(boolean originalValue, + @Local ModelOverrideList.BakedOverride bakedOverride, + @Local ItemStack stack) { + if (!originalValue) return false; + var overrideData = (BakedOverrideData) bakedOverride; + var overrides = overrideData.getFirmamentOverrides(); + if (overrides == null) return true; + for (FirmamentModelPredicate firmamentOverride : overrides) { + if (!firmamentOverride.test(stack)) + return false; + } + return true; + } +} -- cgit