Skip to content

Commit

Permalink
Shadow hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
gigabit101 committed Nov 18, 2023
1 parent 0e691f2 commit 049c7dd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
5 changes: 3 additions & 2 deletions common/src/main/java/net/gigabit101/shrink/Shrink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/net/gigabit101/shrink/ShrinkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions common/src/main/java/net/gigabit101/shrink/api/ShrinkAPI.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/shrink.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"defaultRequire": 1
},
"client": [
"EntityRenderDispatcherMixin",
"LivingEntityRendererMixin"
]
}

0 comments on commit 049c7dd

Please sign in to comment.