From 71c2bc72d447e44969933c103e1aa7f0eb0b6dcc Mon Sep 17 00:00:00 2001 From: enjarai Date: Mon, 9 Sep 2024 19:20:41 +0200 Subject: [PATCH] Nice things --- docs/resourcepacks.md | 19 ++++++++ .../dev/enjarai/blahajtotem/BlahajFlags.java | 29 +++--------- .../dev/enjarai/blahajtotem/BlahajTotem.java | 1 - .../mixin/BasicBakedModelMixin.java | 22 +++++++++ .../mixin/BipedEntityModelMixin.java | 3 +- .../JsonUnbakedModel$DeserializerMixin.java | 25 ++++++++++ .../mixin/JsonUnbakedModelMixin.java | 46 +++++++++++++++++++ .../mixin/PlayerEntityRendererMixin.java | 3 +- .../blahajtotem/pond/BakedHuggableModel.java | 6 +++ .../pond/UnbakedHuggableModel.java | 6 +++ .../models/item/astolfo_bean.json | 1 + .../blahaj_totem/models/item/basket.json | 1 + .../blahaj_totem/models/item/mahiro.json | 1 + .../blahaj_totem/models/item/shork.json | 1 + .../assets/blahaj_totem/models/item/whal.json | 1 + src/main/resources/blahaj-totem.mixins.json | 3 ++ .../assets/blahaj_totem/default_to_totem | 0 17 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 docs/resourcepacks.md create mode 100644 src/main/java/dev/enjarai/blahajtotem/mixin/BasicBakedModelMixin.java create mode 100644 src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModel$DeserializerMixin.java create mode 100644 src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModelMixin.java create mode 100644 src/main/java/dev/enjarai/blahajtotem/pond/BakedHuggableModel.java create mode 100644 src/main/java/dev/enjarai/blahajtotem/pond/UnbakedHuggableModel.java delete mode 100644 src/main/resources/resourcepacks/default_to_totem/assets/blahaj_totem/default_to_totem diff --git a/docs/resourcepacks.md b/docs/resourcepacks.md new file mode 100644 index 0000000..90866e9 --- /dev/null +++ b/docs/resourcepacks.md @@ -0,0 +1,19 @@ +# Resource Packs + +The cuddling animation this mod uses is available for +external resource packs to make use of in their own models. +You can add the `huggable` property to any item model definition to +make it use this custom arm position. + +```json +{ + "huggable": true, + "parent": ..., + "textures": { + ... + }, + "elements": [ + ... + ] +} +``` diff --git a/src/main/java/dev/enjarai/blahajtotem/BlahajFlags.java b/src/main/java/dev/enjarai/blahajtotem/BlahajFlags.java index 90a88ba..92f97fa 100644 --- a/src/main/java/dev/enjarai/blahajtotem/BlahajFlags.java +++ b/src/main/java/dev/enjarai/blahajtotem/BlahajFlags.java @@ -1,29 +1,14 @@ package dev.enjarai.blahajtotem; -import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; -import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; +import dev.enjarai.blahajtotem.pond.BakedHuggableModel; +import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.resource.ResourceManager; -import net.minecraft.util.Identifier; -public class BlahajFlags implements IdentifiableResourceReloadListener, SimpleSynchronousResourceReloadListener { - public static boolean defaultToTotem = false; - - public static boolean isBlahaj(ItemStack itemStack) { - if (itemStack.isOf(Items.TOTEM_OF_UNDYING)) { - return !BlahajFlags.defaultToTotem || BlahajTotem.getShorkType(itemStack) != null; - } - return false; - } - - @Override - public Identifier getFabricId() { - return BlahajTotem.id("flags"); - } - - @Override - public void reload(ResourceManager manager) { - defaultToTotem = manager.getResource(BlahajTotem.id("default_to_totem")).isPresent(); +public class BlahajFlags { + public static boolean isHuggable(ItemStack itemStack, LivingEntity entity) { + var model = MinecraftClient.getInstance().getItemRenderer().getModel(itemStack, entity.getWorld(), entity, 0); + return model instanceof BakedHuggableModel huggableModel && huggableModel.blahaj_totem$isHuggable(); } } diff --git a/src/main/java/dev/enjarai/blahajtotem/BlahajTotem.java b/src/main/java/dev/enjarai/blahajtotem/BlahajTotem.java index 4ab7ad7..625c08a 100644 --- a/src/main/java/dev/enjarai/blahajtotem/BlahajTotem.java +++ b/src/main/java/dev/enjarai/blahajtotem/BlahajTotem.java @@ -102,7 +102,6 @@ public void onInitializeClient() { return 0f; }); - ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new BlahajFlags()); ResourceManagerHelper.registerBuiltinResourcePack( BlahajTotem.id("default_to_totem"), FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow(), Text.translatable("blahaj_totem.resourcepack.default_to_totem"), ResourcePackActivationType.NORMAL diff --git a/src/main/java/dev/enjarai/blahajtotem/mixin/BasicBakedModelMixin.java b/src/main/java/dev/enjarai/blahajtotem/mixin/BasicBakedModelMixin.java new file mode 100644 index 0000000..9e82cb7 --- /dev/null +++ b/src/main/java/dev/enjarai/blahajtotem/mixin/BasicBakedModelMixin.java @@ -0,0 +1,22 @@ +package dev.enjarai.blahajtotem.mixin; + +import dev.enjarai.blahajtotem.pond.BakedHuggableModel; +import net.minecraft.client.render.model.BasicBakedModel; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +@Mixin(BasicBakedModel.class) +public class BasicBakedModelMixin implements BakedHuggableModel { + @Unique + private boolean huggable; + + @Override + public void blahaj_totem$setHuggable(boolean huggable) { + this.huggable = huggable; + } + + @Override + public boolean blahaj_totem$isHuggable() { + return huggable; + } +} diff --git a/src/main/java/dev/enjarai/blahajtotem/mixin/BipedEntityModelMixin.java b/src/main/java/dev/enjarai/blahajtotem/mixin/BipedEntityModelMixin.java index 893130c..0b051f9 100644 --- a/src/main/java/dev/enjarai/blahajtotem/mixin/BipedEntityModelMixin.java +++ b/src/main/java/dev/enjarai/blahajtotem/mixin/BipedEntityModelMixin.java @@ -4,7 +4,6 @@ import net.minecraft.client.model.ModelPart; import net.minecraft.client.render.entity.model.BipedEntityModel; import net.minecraft.entity.LivingEntity; -import net.minecraft.item.Items; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -31,7 +30,7 @@ public class BipedEntityModelMixin { cancellable = true ) public void poseArms(LivingEntity entity, CallbackInfo ci) { - if(BlahajFlags.isBlahaj(entity.getMainHandStack()) || BlahajFlags.isBlahaj(entity.getOffHandStack())) { + if (BlahajFlags.isHuggable(entity.getMainHandStack(), entity) || BlahajFlags.isHuggable(entity.getOffHandStack(), entity)) { this.rightArm.pitch = -0.95F; this.rightArm.yaw = (float) (-Math.PI / 8); this.leftArm.pitch = -0.90F; diff --git a/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModel$DeserializerMixin.java b/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModel$DeserializerMixin.java new file mode 100644 index 0000000..7b1d586 --- /dev/null +++ b/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModel$DeserializerMixin.java @@ -0,0 +1,25 @@ +package dev.enjarai.blahajtotem.mixin; + +import com.google.gson.JsonElement; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.sugar.Local; +import dev.enjarai.blahajtotem.pond.UnbakedHuggableModel; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.util.JsonHelper; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(JsonUnbakedModel.Deserializer.class) +public class JsonUnbakedModel$DeserializerMixin { + @ModifyReturnValue( + method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/client/render/model/json/JsonUnbakedModel;", + at = @At("RETURN") + ) + private JsonUnbakedModel deserializeAdditionalField(JsonUnbakedModel original, @Local(argsOnly = true) JsonElement element) { + if (element.getAsJsonObject().has("huggable")) { + ((UnbakedHuggableModel) original).blahaj_totem$setHuggable( + JsonHelper.getBoolean(element.getAsJsonObject(), "huggable")); + } + return original; + } +} diff --git a/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModelMixin.java b/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModelMixin.java new file mode 100644 index 0000000..f846254 --- /dev/null +++ b/src/main/java/dev/enjarai/blahajtotem/mixin/JsonUnbakedModelMixin.java @@ -0,0 +1,46 @@ +package dev.enjarai.blahajtotem.mixin; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import dev.enjarai.blahajtotem.pond.BakedHuggableModel; +import dev.enjarai.blahajtotem.pond.UnbakedHuggableModel; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(JsonUnbakedModel.class) +public class JsonUnbakedModelMixin implements UnbakedHuggableModel { + @Shadow @Nullable protected JsonUnbakedModel parent; + @Unique @Nullable + private Boolean huggable; + + @Override + public void blahaj_totem$setHuggable(boolean huggable) { + this.huggable = huggable; + } + + @Override + public boolean blahaj_totem$isHuggable() { + if (huggable != null) { + return huggable; + } else if (parent != null) { + return ((UnbakedHuggableModel) parent).blahaj_totem$isHuggable(); + } else { + return false; + } + } + + @ModifyReturnValue( + method = "bake(Lnet/minecraft/client/render/model/Baker;Lnet/minecraft/client/render/model/json/JsonUnbakedModel;Ljava/util/function/Function;Lnet/minecraft/client/render/model/ModelBakeSettings;Z)Lnet/minecraft/client/render/model/BakedModel;", + at = @At("RETURN") + ) + private BakedModel addFieldToBakedModel(BakedModel original) { + if (original instanceof BakedHuggableModel huggableModel) { + huggableModel.blahaj_totem$setHuggable(blahaj_totem$isHuggable()); + } + return original; + } +} diff --git a/src/main/java/dev/enjarai/blahajtotem/mixin/PlayerEntityRendererMixin.java b/src/main/java/dev/enjarai/blahajtotem/mixin/PlayerEntityRendererMixin.java index f0e9af1..095b315 100644 --- a/src/main/java/dev/enjarai/blahajtotem/mixin/PlayerEntityRendererMixin.java +++ b/src/main/java/dev/enjarai/blahajtotem/mixin/PlayerEntityRendererMixin.java @@ -5,7 +5,6 @@ import net.minecraft.client.render.entity.PlayerEntityRenderer; import net.minecraft.client.render.entity.model.BipedEntityModel; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; import net.minecraft.util.Hand; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -21,7 +20,7 @@ public class PlayerEntityRendererMixin { ) private static void cuddleBlahaj(AbstractClientPlayerEntity player, Hand hand, CallbackInfoReturnable ci) { ItemStack lv = player.getStackInHand(hand); - if (BlahajFlags.isBlahaj(lv)) { + if (BlahajFlags.isHuggable(lv, player)) { ci.setReturnValue(BipedEntityModel.ArmPose.CROSSBOW_HOLD); ci.cancel(); } diff --git a/src/main/java/dev/enjarai/blahajtotem/pond/BakedHuggableModel.java b/src/main/java/dev/enjarai/blahajtotem/pond/BakedHuggableModel.java new file mode 100644 index 0000000..fdb4011 --- /dev/null +++ b/src/main/java/dev/enjarai/blahajtotem/pond/BakedHuggableModel.java @@ -0,0 +1,6 @@ +package dev.enjarai.blahajtotem.pond; + +public interface BakedHuggableModel { + void blahaj_totem$setHuggable(boolean huggable); + boolean blahaj_totem$isHuggable(); +} diff --git a/src/main/java/dev/enjarai/blahajtotem/pond/UnbakedHuggableModel.java b/src/main/java/dev/enjarai/blahajtotem/pond/UnbakedHuggableModel.java new file mode 100644 index 0000000..a4c9a5c --- /dev/null +++ b/src/main/java/dev/enjarai/blahajtotem/pond/UnbakedHuggableModel.java @@ -0,0 +1,6 @@ +package dev.enjarai.blahajtotem.pond; + +public interface UnbakedHuggableModel { + void blahaj_totem$setHuggable(boolean huggable); + boolean blahaj_totem$isHuggable(); +} diff --git a/src/main/resources/assets/blahaj_totem/models/item/astolfo_bean.json b/src/main/resources/assets/blahaj_totem/models/item/astolfo_bean.json index 9f3203c..21b73f0 100644 --- a/src/main/resources/assets/blahaj_totem/models/item/astolfo_bean.json +++ b/src/main/resources/assets/blahaj_totem/models/item/astolfo_bean.json @@ -1,4 +1,5 @@ { + "huggable": true, "credit": "Made by Crephan", "texture_size": [32, 32], "textures": { diff --git a/src/main/resources/assets/blahaj_totem/models/item/basket.json b/src/main/resources/assets/blahaj_totem/models/item/basket.json index 7342c5f..f293bfe 100644 --- a/src/main/resources/assets/blahaj_totem/models/item/basket.json +++ b/src/main/resources/assets/blahaj_totem/models/item/basket.json @@ -1,4 +1,5 @@ { + "huggable": true, "credit": "Made by Crephan", "texture_size": [32, 32], "textures": { diff --git a/src/main/resources/assets/blahaj_totem/models/item/mahiro.json b/src/main/resources/assets/blahaj_totem/models/item/mahiro.json index 9d4831b..a91a680 100644 --- a/src/main/resources/assets/blahaj_totem/models/item/mahiro.json +++ b/src/main/resources/assets/blahaj_totem/models/item/mahiro.json @@ -1,4 +1,5 @@ { + "huggable": true, "credit": "Made by Crephan", "texture_size": [32, 32], "textures": { diff --git a/src/main/resources/assets/blahaj_totem/models/item/shork.json b/src/main/resources/assets/blahaj_totem/models/item/shork.json index 588d0c6..8aaf423 100644 --- a/src/main/resources/assets/blahaj_totem/models/item/shork.json +++ b/src/main/resources/assets/blahaj_totem/models/item/shork.json @@ -1,4 +1,5 @@ { + "huggable": true, "texture_size": [32, 32], "textures": { "0": "blahaj_totem:item/blahaj_skins/shark_shark", diff --git a/src/main/resources/assets/blahaj_totem/models/item/whal.json b/src/main/resources/assets/blahaj_totem/models/item/whal.json index 3976127..cdfbb3d 100644 --- a/src/main/resources/assets/blahaj_totem/models/item/whal.json +++ b/src/main/resources/assets/blahaj_totem/models/item/whal.json @@ -1,4 +1,5 @@ { + "huggable": true, "credit": "Made with Blockbench", "texture_size": [32, 32], "textures": { diff --git a/src/main/resources/blahaj-totem.mixins.json b/src/main/resources/blahaj-totem.mixins.json index 80427e7..f641702 100644 --- a/src/main/resources/blahaj-totem.mixins.json +++ b/src/main/resources/blahaj-totem.mixins.json @@ -8,8 +8,11 @@ "defaultRequire": 1 }, "client": [ + "BasicBakedModelMixin", "BipedEntityModelMixin", "ClientPlayNetworkHandlerMixin", + "JsonUnbakedModel$DeserializerMixin", + "JsonUnbakedModelMixin", "PlayerEntityRendererMixin" ] } \ No newline at end of file diff --git a/src/main/resources/resourcepacks/default_to_totem/assets/blahaj_totem/default_to_totem b/src/main/resources/resourcepacks/default_to_totem/assets/blahaj_totem/default_to_totem deleted file mode 100644 index e69de29..0000000