Skip to content

Commit

Permalink
add "last Drawer" interface for InGameHud.
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Sep 24, 2024
1 parent faf3668 commit faea80d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/main/java/fi/dy/masa/malilib/event/RenderEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ public void registerWorldLastRenderer(IRenderer renderer)
}
}

@ApiStatus.Internal
public void onRenderGameOverlayLastDrawer(DrawContext drawContext, MinecraftClient mc, float partialTicks)
{
Profiler profiler = Profilers.get();

profiler.push("malilib_rendergameoverlaydrawer");

if (this.overlayRenderers.isEmpty() == false)
{
for (IRenderer renderer : this.overlayRenderers)
{
profiler.push(renderer.getProfilerSectionSupplier());
renderer.onRenderGameOverlayLastDrawer(drawContext, partialTicks, profiler, mc);
profiler.pop();
}
}

profiler.pop();
}

@ApiStatus.Internal
public void onRenderGameOverlayPost(DrawContext drawContext, MinecraftClient mc, float partialTicks)
{
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fi/dy/masa/malilib/interfaces/IRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

public interface IRenderer
{
/**
* Called after the vanilla "drawer" overlays have been rendered
*/
default void onRenderGameOverlayLastDrawer(DrawContext drawContext, float partialTicks, Profiler profiler, MinecraftClient mc) {}

/**
* Called after the vanilla overlays have been rendered, with advanced Parameters such as ticks, drawer, profiler
*/
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/fi/dy/masa/malilib/mixin/MixinInGameHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.LayeredDrawer;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.RenderTickCounter;
import org.spongepowered.asm.mixin.Final;
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;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -17,10 +19,23 @@
public abstract class MixinInGameHud
{
@Shadow @Final private MinecraftClient client;
@Shadow @Final private LayeredDrawer layeredDrawer;

@Inject(method = "<init>", at = @At("TAIL"))
private void onInit(CallbackInfo info)
{
this.layeredDrawer.addLayer(this::malilib_renderGameOverlayLastDrawer);
}

@Inject(method = "render", at = @At("TAIL"))
private void malilib_onGameOverlayPost(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci)
{
((RenderEventHandler) RenderEventHandler.getInstance()).onRenderGameOverlayPost(context, this.client, tickCounter.getTickDelta(false));
}

@Unique
private void malilib_renderGameOverlayLastDrawer(DrawContext context, RenderTickCounter tickCounter)
{
((RenderEventHandler) RenderEventHandler.getInstance()).onRenderGameOverlayLastDrawer(context, this.client, tickCounter.getTickDelta(false));
}
}
43 changes: 39 additions & 4 deletions src/main/java/fi/dy/masa/malilib/util/EntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import com.google.common.collect.Maps;
import com.llamalad7.mixinextras.lib.apache.commons.tuple.Pair;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Dynamic;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.*;
import net.minecraft.entity.attribute.AttributeContainer;
import net.minecraft.entity.attribute.DefaultAttributeRegistry;
import net.minecraft.entity.attribute.EntityAttribute;
Expand All @@ -24,6 +22,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.DynamicRegistryManager;
Expand All @@ -33,6 +32,7 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.village.TradeOfferList;
import net.minecraft.village.VillagerData;

Expand Down Expand Up @@ -405,4 +405,39 @@ public static RegistryEntry.Reference<EntityType<?>> getEntityTypeEntry(Identifi
}
}

/**
* Try to get the Leash Data from NBT using 'FakeLeashData' because LeashData is package-private
* @param nbt ()
* @return ()
*/
@SuppressWarnings("unchecked")
public static @Nullable FakeLeashData getLeashDataFromNbt(@Nonnull NbtCompound nbt)
{
FakeLeashData data = null;

if (nbt.contains("leash", Constants.NBT.TAG_COMPOUND))
{
data = new FakeLeashData(-1, null, Either.left(nbt.getCompound("leash").getUuid("UUID")));
}
else if (nbt.contains("leash", Constants.NBT.TAG_INT_ARRAY))
{
Either<UUID, BlockPos> either = (Either) NbtHelper.toBlockPos(nbt, "leash").map(Either::right).orElse(null);

if (either != null)
{
return new FakeLeashData(-1, null, either);
}
}

return data;
}

/**
* Fake "LeashData" record. To change the values, just make a new one.
*
* @param unresolvedLeashHolderId
* @param leashHolder
* @param unresolvedLeashData
*/
public record FakeLeashData(int unresolvedLeashHolderId, @Nullable Entity leashHolder, @Nullable Either<UUID, BlockPos> unresolvedLeashData) {}
}

0 comments on commit faea80d

Please sign in to comment.