From 049c7dd5390c120d521d8c6d8d83f98634dc7a96 Mon Sep 17 00:00:00 2001 From: Gigabit101 Date: Sat, 18 Nov 2023 05:29:16 +0000 Subject: [PATCH] Shadow hacks --- .../java/net/gigabit101/shrink/Shrink.java | 5 ++- .../net/gigabit101/shrink/ShrinkClient.java | 4 +- .../net/gigabit101/shrink/api/ShrinkAPI.java | 19 ++++++++ .../mixins/EntityRenderDispatcherMixin.java | 43 +++++++++++++++++++ common/src/main/resources/shrink.mixins.json | 1 + 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/net/gigabit101/shrink/mixins/EntityRenderDispatcherMixin.java diff --git a/common/src/main/java/net/gigabit101/shrink/Shrink.java b/common/src/main/java/net/gigabit101/shrink/Shrink.java index 88e9e6e..2b19910 100644 --- a/common/src/main/java/net/gigabit101/shrink/Shrink.java +++ b/common/src/main/java/net/gigabit101/shrink/Shrink.java @@ -8,6 +8,7 @@ import net.gigabit101.shrink.init.ModItems; import net.gigabit101.shrink.polylib.EntitySizeEvents; import net.minecraft.core.registries.Registries; +import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.player.Player; @@ -36,13 +37,13 @@ public static void init() { if(entity == null) return new EntitySizeEvents.UpdatedSize(size, eyeHeight, size, eyeHeight); - if(entity instanceof Player livingEntity) + if(entity instanceof LivingEntity livingEntity) { if (livingEntity.getAttributes() != null) { if (livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE) == null) return new EntitySizeEvents.UpdatedSize(size, eyeHeight, size, eyeHeight); - boolean canShrink = livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue() != 1.0D; + boolean canShrink = ShrinkAPI.canEntityShrink(livingEntity); System.out.println("SIDE: " + (entity.level().isClientSide() ? "CLIENT " : " SERVER ") + "canShrink:" + canShrink + " " + livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue()); diff --git a/common/src/main/java/net/gigabit101/shrink/ShrinkClient.java b/common/src/main/java/net/gigabit101/shrink/ShrinkClient.java index dfef3cf..9074ad3 100644 --- a/common/src/main/java/net/gigabit101/shrink/ShrinkClient.java +++ b/common/src/main/java/net/gigabit101/shrink/ShrinkClient.java @@ -10,7 +10,7 @@ public static void init() { LivingEntityRenderEvents.PRE.register((livingEntity, f, g, poseStack, multiBufferSource, i) -> { - boolean shrunk = livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue() != 1.0D; + boolean shrunk = ShrinkAPI.isEntityShrunk(livingEntity);//livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue() != 1.0D; if(shrunk) { poseStack.pushPose(); @@ -21,7 +21,7 @@ public static void init() LivingEntityRenderEvents.POST.register((livingEntity, f, g, poseStack, multiBufferSource, i) -> { - boolean shrunk = livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).hasModifier(ItemShrinkDevice.createModifier(1.0F)); + boolean shrunk = ShrinkAPI.isEntityShrunk(livingEntity);//livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).hasModifier(ItemShrinkDevice.createModifier(1.0F)); if(shrunk) { poseStack.popPose(); diff --git a/common/src/main/java/net/gigabit101/shrink/api/ShrinkAPI.java b/common/src/main/java/net/gigabit101/shrink/api/ShrinkAPI.java index 301005e..681f61b 100644 --- a/common/src/main/java/net/gigabit101/shrink/api/ShrinkAPI.java +++ b/common/src/main/java/net/gigabit101/shrink/api/ShrinkAPI.java @@ -1,9 +1,28 @@ package net.gigabit101.shrink.api; +import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.RangedAttribute; public class ShrinkAPI { public static final Attribute SCALE_ATTRIBUTE = new RangedAttribute("shrink_scale", 1.0D, 0.25D, 10).setSyncable(true); + + public static boolean canEntityShrink(LivingEntity livingEntity) + { + if(livingEntity == null) return false; + if(livingEntity.getAttributes() == null) return false; + if(livingEntity.getAttribute(SCALE_ATTRIBUTE) == null) return false; + + return livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue() != 1.0D; + } + + public static boolean isEntityShrunk(LivingEntity livingEntity) + { + if(livingEntity == null) return false; + if(livingEntity.getAttributes() == null) return false; + if(livingEntity.getAttribute(SCALE_ATTRIBUTE) == null) return false; + + return !livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getModifiers().isEmpty(); + } } diff --git a/common/src/main/java/net/gigabit101/shrink/mixins/EntityRenderDispatcherMixin.java b/common/src/main/java/net/gigabit101/shrink/mixins/EntityRenderDispatcherMixin.java new file mode 100644 index 0000000..170e0c7 --- /dev/null +++ b/common/src/main/java/net/gigabit101/shrink/mixins/EntityRenderDispatcherMixin.java @@ -0,0 +1,43 @@ +package net.gigabit101.shrink.mixins; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.gigabit101.shrink.api.ShrinkAPI; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.entity.EntityRenderDispatcher; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.level.LevelReader; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyVariable; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(EntityRenderDispatcher.class) +public class EntityRenderDispatcherMixin +{ + @Unique + private static float shrink$value = 0; + @Inject(method = "renderShadow", at = @At("HEAD")) + private static void renderShadow(PoseStack poseStack, MultiBufferSource multiBufferSource, Entity entity, float f, float g, LevelReader levelReader, float h, CallbackInfo ci) + { + if(entity instanceof LivingEntity livingEntity) + { + if(ShrinkAPI.isEntityShrunk(livingEntity)) + { + shrink$value = (float) (h * livingEntity.getAttribute(ShrinkAPI.SCALE_ATTRIBUTE).getValue()); + } + else + { + shrink$value = h; + } + } + } + + @ModifyVariable(method = "renderShadow", at = @At("HEAD"), ordinal = 2) + private static float injected(float h) + { + return shrink$value; + } +} diff --git a/common/src/main/resources/shrink.mixins.json b/common/src/main/resources/shrink.mixins.json index 8d61d16..591395a 100644 --- a/common/src/main/resources/shrink.mixins.json +++ b/common/src/main/resources/shrink.mixins.json @@ -11,6 +11,7 @@ "defaultRequire": 1 }, "client": [ + "EntityRenderDispatcherMixin", "LivingEntityRendererMixin" ] }