Skip to content

Commit

Permalink
Nice things
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Sep 9, 2024
1 parent eab377b commit 71c2bc7
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 27 deletions.
19 changes: 19 additions & 0 deletions docs/resourcepacks.md
Original file line number Diff line number Diff line change
@@ -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": [
...
]
}
```
29 changes: 7 additions & 22 deletions src/main/java/dev/enjarai/blahajtotem/BlahajFlags.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 0 additions & 1 deletion src/main/java/dev/enjarai/blahajtotem/BlahajTotem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +20,7 @@ public class PlayerEntityRendererMixin {
)
private static void cuddleBlahaj(AbstractClientPlayerEntity player, Hand hand, CallbackInfoReturnable<BipedEntityModel.ArmPose> ci) {
ItemStack lv = player.getStackInHand(hand);
if (BlahajFlags.isBlahaj(lv)) {
if (BlahajFlags.isHuggable(lv, player)) {
ci.setReturnValue(BipedEntityModel.ArmPose.CROSSBOW_HOLD);
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.enjarai.blahajtotem.pond;

public interface BakedHuggableModel {
void blahaj_totem$setHuggable(boolean huggable);
boolean blahaj_totem$isHuggable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.enjarai.blahajtotem.pond;

public interface UnbakedHuggableModel {
void blahaj_totem$setHuggable(boolean huggable);
boolean blahaj_totem$isHuggable();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"huggable": true,
"credit": "Made by Crephan",
"texture_size": [32, 32],
"textures": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"huggable": true,
"credit": "Made by Crephan",
"texture_size": [32, 32],
"textures": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"huggable": true,
"credit": "Made by Crephan",
"texture_size": [32, 32],
"textures": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"huggable": true,
"texture_size": [32, 32],
"textures": {
"0": "blahaj_totem:item/blahaj_skins/shark_shark",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"huggable": true,
"credit": "Made with Blockbench",
"texture_size": [32, 32],
"textures": {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/blahaj-totem.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"defaultRequire": 1
},
"client": [
"BasicBakedModelMixin",
"BipedEntityModelMixin",
"ClientPlayNetworkHandlerMixin",
"JsonUnbakedModel$DeserializerMixin",
"JsonUnbakedModelMixin",
"PlayerEntityRendererMixin"
]
}
Empty file.

0 comments on commit 71c2bc7

Please sign in to comment.