diff options
Diffstat (limited to 'src/main/java/com/anthonyhilyard')
8 files changed, 388 insertions, 301 deletions
diff --git a/src/main/java/com/anthonyhilyard/iceberg/events/RenderTooltipEvents.java b/src/main/java/com/anthonyhilyard/iceberg/events/RenderTooltipEvents.java index c5a10a8..4b4584e 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/events/RenderTooltipEvents.java +++ b/src/main/java/com/anthonyhilyard/iceberg/events/RenderTooltipEvents.java @@ -2,13 +2,14 @@ package com.anthonyhilyard.iceberg.events; import java.util.List; -import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.datafixers.util.Either; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; import net.minecraft.network.chat.FormattedText; import net.minecraft.world.InteractionResult; import net.minecraft.world.inventory.tooltip.TooltipComponent; @@ -34,11 +35,11 @@ public final class RenderTooltipEvents }); public static final Event<RenderTooltipEvents.PreExt> PREEXT = EventFactory.createArrayBacked(RenderTooltipEvents.PreExt.class, - callbacks -> (stack, components, poseStack, x, y, screenWidth, screenHeight, font, comparison, index) -> { + callbacks -> (stack, graphics, x, y, screenWidth, screenHeight, font, components, positioner, comparison, index) -> { PreExtResult result = new PreExtResult(InteractionResult.PASS, x, y, screenWidth, screenHeight, font); for (RenderTooltipEvents.PreExt callback : callbacks) { - result = callback.onPre(stack, components, poseStack, result.x, result.y, screenWidth, screenHeight, result.font, comparison, index); + result = callback.onPre(stack, graphics, result.x, result.y, result.screenWidth, result.screenHeight, result.font, components, positioner, comparison, index); if (result.result != InteractionResult.PASS) { @@ -48,55 +49,21 @@ public final class RenderTooltipEvents return result; }); - @Deprecated - public static final Event<RenderTooltipEvents.Pre> PRE = EventFactory.createArrayBacked(RenderTooltipEvents.Pre.class, - callbacks -> (stack, components, poseStack, x, y, screenWidth, screenHeight, maxWidth, font, comparison) -> { - for (RenderTooltipEvents.Pre callback : callbacks) - { - InteractionResult result = callback.onPre(stack, components, poseStack, x, y, screenWidth, screenHeight, maxWidth, font, comparison); - - if (result != InteractionResult.PASS) - { - return result; - } - } - return InteractionResult.PASS; - }); - public static final Event<RenderTooltipEvents.ColorExt> COLOREXT = EventFactory.createArrayBacked(RenderTooltipEvents.ColorExt.class, - callbacks -> (stack, components, poseStack, x, y, font, backgroundStart, backgroundEnd, borderStart, borderEnd, comparison, index) -> { + callbacks -> (stack, graphics, x, y, font, backgroundStart, backgroundEnd, borderStart, borderEnd, components, comparison, index) -> { ColorExtResult result = new ColorExtResult(backgroundStart, backgroundEnd, borderStart, borderEnd); for (RenderTooltipEvents.ColorExt callback : callbacks) { - result = callback.onColor(stack, components, poseStack, x, y, font, result.backgroundStart, result.backgroundEnd, result.borderStart, result.borderEnd, comparison, index); + result = callback.onColor(stack, graphics, x, y, font, result.backgroundStart, result.backgroundEnd, result.borderStart, result.borderEnd, components, comparison, index); } return result; }); - @Deprecated - public static final Event<RenderTooltipEvents.Color> COLOR = EventFactory.createArrayBacked(RenderTooltipEvents.Color.class, - callbacks -> (stack, components, poseStack, x, y, font, background, borderStart, borderEnd, comparison) -> { - ColorResult result = new ColorResult(background, borderStart, borderEnd); - for (RenderTooltipEvents.Color callback : callbacks) - { - result = callback.onColor(stack, components, poseStack, x, y, font, result.background, result.borderStart, result.borderEnd, comparison); - } - return result; - }); - public static final Event<RenderTooltipEvents.PostExt> POSTEXT = EventFactory.createArrayBacked(RenderTooltipEvents.PostExt.class, - callbacks -> (stack, components, poseStack, x, y, font, width, height, comparison, index) -> { + callbacks -> (stack, graphics, x, y, font, width, height, components, comparison, index) -> { for (RenderTooltipEvents.PostExt callback : callbacks) { - callback.onPost(stack, components, poseStack, x, y, font, width, height, comparison, index); - } - }); - - public static final Event<RenderTooltipEvents.Post> POST = EventFactory.createArrayBacked(RenderTooltipEvents.Post.class, - callbacks -> (stack, components, poseStack, x, y, font, width, height, comparison) -> { - for (RenderTooltipEvents.Post callback : callbacks) - { - callback.onPost(stack, components, poseStack, x, y, font, width, height, comparison); + callback.onPost(stack, graphics, x, y, font, width, height, components, comparison, index); } }); @@ -109,44 +76,22 @@ public final class RenderTooltipEvents @FunctionalInterface public interface PreExt { - PreExtResult onPre(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, int screenWidth, int screenHeight, Font font, boolean comparison, int index); - } - - @Deprecated - @FunctionalInterface - public interface Pre - { - InteractionResult onPre(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, int screenWidth, int screenHeight, int maxWidth, Font font, boolean comparison); + PreExtResult onPre(ItemStack stack, GuiGraphics graphics, int x, int y, int screenWidth, int screenHeight, Font font, List<ClientTooltipComponent> components, ClientTooltipPositioner positioner, boolean comparison, int index); } @FunctionalInterface public interface ColorExt { - ColorExtResult onColor(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, Font font, int backgroundStart, int backgroundEnd, int borderStart, int borderEnd, boolean comparison, int index); - } - - @Deprecated - @FunctionalInterface - public interface Color - { - ColorResult onColor(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, Font font, int background, int borderStart, int borderEnd, boolean comparison); + ColorExtResult onColor(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, int backgroundStart, int backgroundEnd, int borderStart, int borderEnd, List<ClientTooltipComponent> components, boolean comparison, int index); } @FunctionalInterface public interface PostExt { - void onPost(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, Font font, int width, int height, boolean comparison, int index); - } - - @Deprecated - @FunctionalInterface - public interface Post - { - void onPost(ItemStack stack, List<ClientTooltipComponent> components, PoseStack poseStack, int x, int y, Font font, int width, int height, boolean comparison); + void onPost(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, int width, int height, List<ClientTooltipComponent> components, boolean comparison, int index); } public record GatherResult(InteractionResult result, int maxWidth, List<Either<FormattedText, TooltipComponent>> tooltipElements) {} public record PreExtResult(InteractionResult result, int x, int y, int screenWidth, int screenHeight, Font font) {} public record ColorExtResult(int backgroundStart, int backgroundEnd, int borderStart, int borderEnd) {} - public record ColorResult(int background, int borderStart, int borderEnd) {} } diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/GuiGraphicsMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/GuiGraphicsMixin.java new file mode 100644 index 0000000..30babf4 --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/GuiGraphicsMixin.java @@ -0,0 +1,257 @@ +package com.anthonyhilyard.iceberg.mixin; + +import java.util.List; +import java.util.Optional; + +import com.anthonyhilyard.iceberg.events.RenderTooltipEvents; +import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.ColorExtResult; +import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.PreExtResult; +import com.anthonyhilyard.iceberg.util.Tooltips; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import org.joml.Vector2ic; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Group; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; +import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil; +import net.minecraft.client.renderer.Rect2i; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextColor; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraft.world.item.ItemStack; + +@Mixin(GuiGraphics.class) +public class GuiGraphicsMixin +{ + @Shadow + @Final + private Minecraft minecraft; + + private static ItemStack tooltipStack = ItemStack.EMPTY; + + private int storedTooltipWidth, storedTooltipHeight; + private Vector2ic storedPostPos; + + @Shadow + private void renderTooltipInternal(Font font, List<ClientTooltipComponent> list, int i, int j, ClientTooltipPositioner clientTooltipPositioner) {} + + @Inject(method = "renderTooltip(Lnet/minecraft/client/gui/Font;Lnet/minecraft/world/item/ItemStack;II)V", at = @At(value = "HEAD")) + protected void renderTooltipHead(Font font, ItemStack itemStack, int x, int y, CallbackInfo info) + { + tooltipStack = itemStack; + } + + @Inject(method = "renderTooltip(Lnet/minecraft/client/gui/Font;Lnet/minecraft/world/item/ItemStack;II)V", at = @At(value = "TAIL")) + protected void renderTooltipTail(Font font, ItemStack itemStack, int x, int y, CallbackInfo info) + { + tooltipStack = ItemStack.EMPTY; + } + + @Inject(method = "renderTooltip(Lnet/minecraft/client/gui/Font;Ljava/util/List;Ljava/util/Optional;II)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;renderTooltipInternal(Lnet/minecraft/client/gui/Font;Ljava/util/List;IILnet/minecraft/client/gui/screens/inventory/tooltip/ClientTooltipPositioner;)V", + shift = Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILEXCEPTION) + public void renderTooltip(Font font, List<Component> textComponents, Optional<TooltipComponent> itemComponent, int x, int y, CallbackInfo info, List<ClientTooltipComponent> components) + { + Screen currentScreen = minecraft.screen; + + if (currentScreen != null && currentScreen instanceof AbstractContainerScreen<?> containerScreen) + { + Slot hoveredSlot = containerScreen.hoveredSlot; + + // If the tooltip stack is empty, try to get the stack from the slot under the mouse. + // This is needed for the creative inventory screen, which doesn't set the tooltip stack. + if (tooltipStack.isEmpty() && hoveredSlot != null) + { + tooltipStack = hoveredSlot.getItem(); + } + } + + List<ClientTooltipComponent> newComponents = Tooltips.gatherTooltipComponents(tooltipStack, textComponents, itemComponent, x, currentScreen.width, currentScreen.height, null, font, -1); + if (newComponents != null && !newComponents.isEmpty()) + { + components.clear(); + components.addAll(newComponents); + } + } + + private int xChange = 0, yChange = 0; + + @Inject(method = "renderTooltipInternal", at = @At(value = "HEAD"), cancellable = true) + private void preRenderTooltipInternal(Font font, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info) + { + GuiGraphics self = (GuiGraphics)(Object)this; + Screen currentScreen = minecraft.screen; + + int width = minecraft.getWindow().getGuiScaledWidth(); + int height = minecraft.getWindow().getGuiScaledHeight(); + + if (currentScreen != null) + { + width = currentScreen.width; + height = currentScreen.height; + } + else + { + if (FabricLoader.getInstance().isModLoaded("andromeda") && tooltipStack != null && !tooltipStack.isEmpty()) + { + List<ClientTooltipComponent> newComponents = Tooltips.gatherTooltipComponents(tooltipStack, Screen.getTooltipFromItem(minecraft, tooltipStack), tooltipStack.getTooltipImage(), x, width, height, null, font, -1); + if (newComponents != null && !newComponents.isEmpty()) + { + int oldWidth = 0, oldHeight = 0; + + for (ClientTooltipComponent tooltipComponent : components) + { + oldHeight += tooltipComponent.getHeight(); + int thisWidth = tooltipComponent.getWidth(font); + if (thisWidth > oldWidth) + { + oldWidth = thisWidth; + } + } + + components.clear(); + components.addAll(newComponents); + Rect2i newRect = Tooltips.calculateRect(tooltipStack, self, positioner, components, x, y, width, height, width, font, 0, true); + + if (tooltipStack == minecraft.player.getMainHandItem()) + { + PoseStack poseStack = RenderSystem.getModelViewStack(); + xChange = (oldWidth - newRect.getWidth()) / 2; + yChange = oldHeight - newRect.getHeight(); + + poseStack.translate(xChange, yChange, 0); + RenderSystem.applyModelViewMatrix(); + } + } + } + } + + if (!components.isEmpty()) + { + PreExtResult eventResult = null; + InteractionResult result = InteractionResult.PASS; + + eventResult = RenderTooltipEvents.PREEXT.invoker().onPre(tooltipStack, self, x, y, width, height, font, components, positioner, false, 0); + result = eventResult.result(); + + if (result != InteractionResult.PASS) + { + info.cancel(); + } + } + } + + @Inject(method = "renderTooltipInternal", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;drawManaged(Ljava/lang/Runnable;)V", + ordinal = 0, shift = Shift.BEFORE)) + private void preFillGradient(Font font, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info) + { + GuiGraphics self = (GuiGraphics)(Object)this; + Screen currentScreen = minecraft.screen; + ItemStack containerStack = ItemStack.EMPTY; + if (currentScreen != null && currentScreen instanceof AbstractContainerScreen<?> containerScreen) + { + Slot hoveredSlot = containerScreen.hoveredSlot; + if (hoveredSlot != null) + { + containerStack = hoveredSlot.getItem(); + } + } + + if (containerStack.isEmpty()) + { + containerStack = tooltipStack; + } + + if (!containerStack.isEmpty()) + { + int background = TooltipRenderUtil.BACKGROUND_COLOR; + int backgroundEnd = background; + int borderStart = TooltipRenderUtil.BORDER_COLOR_TOP; + int borderEnd = TooltipRenderUtil.BORDER_COLOR_BOTTOM; + + // Do colors now, sure why not. + ColorExtResult result = RenderTooltipEvents.COLOREXT.invoker().onColor(containerStack, self, x, y, font, background, backgroundEnd, borderStart, borderEnd, components, false, 0); + if (result != null) + { + background = result.backgroundStart(); + backgroundEnd = result.backgroundEnd(); + borderStart = result.borderStart(); + borderEnd = result.borderEnd(); + } + + Tooltips.currentColors = new Tooltips.TooltipColors(TextColor.fromRgb(background), TextColor.fromRgb(backgroundEnd), TextColor.fromRgb(borderStart), TextColor.fromRgb(borderEnd)); + } + } + + @Group(name = "storeLocals", min = 1, max = 1) + @Inject(method = "renderTooltipInternal", at = @At(value = "INVOKE", target = "Lorg/joml/Vector2ic;x()I", shift = Shift.BEFORE, remap = false), locals = LocalCapture.CAPTURE_FAILSOFT) + private void storeLocals(Font font, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info, int tooltipWidth, int tooltipHeight, int tooltipWidth2, int tooltipHeight2, Vector2ic postPos) + { + storedTooltipWidth = tooltipWidth2; + storedTooltipHeight = tooltipHeight2; + storedPostPos = postPos; + } + + @Group(name = "storeLocals", min = 1, max = 1) + @Inject(method = "renderTooltipInternal", at = @At(value = "INVOKE", target = "Lorg/joml/Vector2ic;x()I", shift = Shift.BEFORE, remap = false), locals = LocalCapture.CAPTURE_FAILSOFT) + private void storeLocalsOptifine(Font font, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info, Object preEvent, int tooltipWidth, int tooltipHeight, int tooltipWidth2, int tooltipHeight2, Vector2ic postPos) + { + storeLocals(font, components, x, y, positioner, info, tooltipWidth, tooltipHeight, tooltipWidth2, tooltipHeight2, postPos); + } + + @Inject(method = "renderTooltipInternal", at = @At(value = "TAIL")) + private void renderTooltipInternalTail(Font font, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info) + { + GuiGraphics self = (GuiGraphics)(Object)this; + Screen currentScreen = minecraft.screen; + ItemStack containerStack = ItemStack.EMPTY; + if (currentScreen != null && currentScreen instanceof AbstractContainerScreen<?> containerScreen) + { + Slot hoveredSlot = containerScreen.hoveredSlot; + if (hoveredSlot != null) + { + containerStack = hoveredSlot.getItem(); + } + } + + if (containerStack.isEmpty()) + { + containerStack = tooltipStack; + } + + if (!containerStack.isEmpty() && !components.isEmpty()) + { + RenderTooltipEvents.POSTEXT.invoker().onPost(containerStack, self, storedPostPos.x(), storedPostPos.y(), font, storedTooltipWidth, storedTooltipHeight, components, false, 0); + } + + if (FabricLoader.getInstance().isModLoaded("andromeda") && tooltipStack == minecraft.player.getMainHandItem()) + { + PoseStack poseStack = RenderSystem.getModelViewStack(); + poseStack.translate(-xChange, -yChange, 0); + RenderSystem.applyModelViewMatrix(); + } + + tooltipStack = ItemStack.EMPTY; + xChange = 0; + yChange = 0; + } +} diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java index 3ce65ac..211903a 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java @@ -1,205 +1,42 @@ package com.anthonyhilyard.iceberg.mixin; +import java.lang.reflect.Field; import java.util.List; -import java.util.Optional; -import com.anthonyhilyard.iceberg.events.RenderTooltipEvents; -import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.ColorExtResult; -import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.ColorResult; -import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.PreExtResult; -import com.anthonyhilyard.iceberg.util.Tooltips; -import com.google.common.collect.Lists; -import com.mojang.blaze3d.vertex.PoseStack; - -import org.joml.Vector2ic; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.At.Shift; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import com.anthonyhilyard.iceberg.Loader; -import net.minecraft.client.gui.Font; -import net.minecraft.client.gui.components.events.AbstractContainerEventHandler; -import net.minecraft.client.gui.components.events.GuiEventListener; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.Screen; -import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; -import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; -import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; -import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil; -import net.minecraft.network.chat.TextColor; -import net.minecraft.network.chat.Component; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; +import net.minecraft.network.chat.Component; @Mixin(Screen.class) -public class ScreenMixin extends AbstractContainerEventHandler +public class ScreenMixin { - @Shadow - protected Font font = null; - - @Shadow - private List<GuiEventListener> children = Lists.newArrayList(); - - private ItemStack tooltipStack = ItemStack.EMPTY; - - @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/world/item/ItemStack;II)V", at = @At(value = "HEAD")) - protected void renderTooltipHead(PoseStack poseStack, ItemStack itemStack, int x, int y, CallbackInfo info) - { - tooltipStack = itemStack; - } - - @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/world/item/ItemStack;II)V", at = @At(value = "TAIL")) - protected void renderTooltipTail(PoseStack poseStack, ItemStack itemStack, int x, int y, CallbackInfo info) - { - tooltipStack = ItemStack.EMPTY; - } - - @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Ljava/util/List;Ljava/util/Optional;II)V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/Screen;renderTooltipInternal(Lcom/mojang/blaze3d/vertex/PoseStack;Ljava/util/List;IILnet/minecraft/client/gui/screens/inventory/tooltip/ClientTooltipPositioner;)V", - shift = Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILEXCEPTION) - public void renderTooltip(PoseStack poseStack, List<Component> textComponents, Optional<TooltipComponent> itemComponent, int x, int y, CallbackInfo info, List<ClientTooltipComponent> components) - { - Screen self = (Screen)(Object)this; - - if (self instanceof AbstractContainerScreen<?> containerScreen) - { - Slot hoveredSlot = containerScreen.hoveredSlot; - - // If the tooltip stack is empty, try to get the stack from the slot under the mouse. - // This is needed for the creative inventory screen, which doesn't set the tooltip stack. - if (tooltipStack.isEmpty() && hoveredSlot != null) - { - tooltipStack = hoveredSlot.getItem(); - } - } - - List<ClientTooltipComponent> newComponents = Tooltips.gatherTooltipComponents(tooltipStack, textComponents, itemComponent, x, self.width, self.height, null, font, -1); - if (newComponents != null && !newComponents.isEmpty()) - { - components.clear(); - components.addAll(newComponents); - } - } - - @SuppressWarnings("deprecation") - @Inject(method = "renderTooltipInternal", at = @At(value = "HEAD"), cancellable = true) - private void preRenderTooltipInternal(PoseStack poseStack, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info) + @Inject(method = "getTooltipFromItem", at = @At(value = "HEAD")) + private static List<Component> getTooltipFromItem(Minecraft minecraft, ItemStack itemStack, CallbackInfoReturnable<List<Component>> info) { - Screen self = (Screen)(Object)this; - - if (!components.isEmpty()) + if (FabricLoader.getInstance().isModLoaded("andromeda")) { - PreExtResult eventResult = null; - InteractionResult result = InteractionResult.PASS; - - eventResult = RenderTooltipEvents.PREEXT.invoker().onPre(tooltipStack, components, poseStack, x, y, self.width, self.height, font, false, 0); - result = eventResult.result(); - - if (result != InteractionResult.PASS) + try { - info.cancel(); + Field tooltipStackField = GuiGraphics.class.getDeclaredField("tooltipStack"); + tooltipStackField.setAccessible(true); + tooltipStackField.set(null, itemStack); } - - // Fire a pre event as well for compatibility. - result = RenderTooltipEvents.PRE.invoker().onPre(tooltipStack, components, poseStack, x, y, self.width, self.height, -1, font, false); - if (result != InteractionResult.PASS) + catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { - info.cancel(); + Loader.LOGGER.error(ExceptionUtils.getStackTrace(e)); } } - } - - @SuppressWarnings("deprecation") - @Inject(method = "renderTooltipInternal", - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderTooltipBackground(Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil$BlitPainter;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/vertex/BufferBuilder;IIIII)V", - ordinal = 0, shift = Shift.BEFORE)) - private void preFillGradient(PoseStack poseStack, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info) - { - Screen self = (Screen)(Object)this; - ItemStack containerStack = ItemStack.EMPTY; - if (self instanceof AbstractContainerScreen<?> containerScreen) - { - Slot hoveredSlot = containerScreen.hoveredSlot; - if (hoveredSlot != null) - { - containerStack = hoveredSlot.getItem(); - } - } - - if (containerStack.isEmpty()) - { - containerStack = tooltipStack; - } - - if (!containerStack.isEmpty()) - { - int background = TooltipRenderUtil.BACKGROUND_COLOR; - int backgroundEnd = background; - int borderStart = TooltipRenderUtil.BORDER_COLOR_TOP; - int borderEnd = TooltipRenderUtil.BORDER_COLOR_BOTTOM; - - // Do colors now, sure why not. - ColorExtResult result = RenderTooltipEvents.COLOREXT.invoker().onColor(containerStack, components, poseStack, x, y, font, background, backgroundEnd, borderStart, borderEnd, false, 0); - if (result != null) - { - background = result.backgroundStart(); - backgroundEnd = result.backgroundEnd(); - borderStart = result.borderStart(); - borderEnd = result.borderEnd(); - } - - // Fire a colors event as well for compatibility. - ColorResult colorResult = RenderTooltipEvents.COLOR.invoker().onColor(containerStack, components, poseStack, x, y, font, background, borderStart, borderEnd, false); - if (colorResult != null) - { - background = colorResult.background(); - borderStart = colorResult.borderStart(); - borderEnd = colorResult.borderEnd(); - } - - Tooltips.currentColors = new Tooltips.TooltipColors(TextColor.fromRgb(background), TextColor.fromRgb(backgroundEnd), TextColor.fromRgb(borderStart), TextColor.fromRgb(borderEnd)); - } - } - - @SuppressWarnings("deprecation") - @Inject(method = "renderTooltipInternal", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;popPose()V", shift = Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILEXCEPTION, cancellable = true) - private void renderTooltipInternal(PoseStack poseStack, List<ClientTooltipComponent> components, int x, int y, ClientTooltipPositioner positioner, CallbackInfo info, int tooltipWidth, int tooltipHeight, int tooltipWidth2, int tooltipHeight2, Vector2ic postPos) - { - Screen self = (Screen)(Object)this; - ItemStack containerStack = ItemStack.EMPTY; - if (self instanceof AbstractContainerScreen<?> containerScreen) - { - Slot hoveredSlot = containerScreen.hoveredSlot; - if (hoveredSlot != null) - { - containerStack = hoveredSlot.getItem(); - } - } - - if (containerStack.isEmpty()) - { - containerStack = tooltipStack; - } - - poseStack.popPose(); - - if (!containerStack.isEmpty() && !components.isEmpty()) - { - RenderTooltipEvents.POSTEXT.invoker().onPost(containerStack, components, poseStack, postPos.x(), postPos.y(), font, tooltipWidth2, tooltipHeight2, false, 0); - RenderTooltipEvents.POST.invoker().onPost(containerStack, components, poseStack, postPos.x(), postPos.y(), font, tooltipWidth2, tooltipHeight2, false); - } - - tooltipStack = ItemStack.EMPTY; - info.cancel(); - } - - @Override - public List<? extends GuiEventListener> children() - { - return children; + return info.getReturnValue(); } } diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/TooltipRenderUtilMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/TooltipRenderUtilMixin.java index 18ef9e4..6007283 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/mixin/TooltipRenderUtilMixin.java +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/TooltipRenderUtilMixin.java @@ -1,6 +1,5 @@ package com.anthonyhilyard.iceberg.mixin; -import org.joml.Matrix4f; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -10,8 +9,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.anthonyhilyard.iceberg.util.Tooltips; -import com.mojang.blaze3d.vertex.BufferBuilder; +import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil; import net.minecraft.network.chat.TextColor; @@ -33,32 +32,32 @@ public class TooltipRenderUtilMixin @Final private static int BORDER_COLOR_BOTTOM; - @Inject(method = "renderTooltipBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil$BlitPainter;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/vertex/BufferBuilder;IIIII)V", shift = At.Shift.BEFORE, ordinal = 0)) - private static void icebergRenderTooltipBackgroundOne(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int height, int z, CallbackInfo info) + @Inject(method = "renderTooltipBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/GuiGraphics;IIIII)V", shift = At.Shift.BEFORE, ordinal = 0)) + private static void icebergRenderTooltipBackgroundOne(GuiGraphics painter, int x, int y, int width, int height, int z, CallbackInfo info) { horizontalLineColor = Tooltips.currentColors.backgroundColorStart(); } - @Inject(method = "renderTooltipBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil$BlitPainter;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/vertex/BufferBuilder;IIIII)V", shift = At.Shift.BEFORE, ordinal = 1)) - private static void icebergRenderTooltipBackgroundTwo(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int height, int z, CallbackInfo info) + @Inject(method = "renderTooltipBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/GuiGraphics;IIIII)V", shift = At.Shift.BEFORE, ordinal = 1)) + private static void icebergRenderTooltipBackgroundTwo(GuiGraphics painter, int x, int y, int width, int height, int z, CallbackInfo info) { horizontalLineColor = Tooltips.currentColors.backgroundColorEnd(); } - @Inject(method = "renderFrameGradient", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil$BlitPainter;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/vertex/BufferBuilder;IIIII)V", shift = At.Shift.BEFORE, ordinal = 0)) - private static void icebergRenderFrameGradientOne(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int height, int z, int color1, int color2, CallbackInfo info) + @Inject(method = "renderFrameGradient", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/GuiGraphics;IIIII)V", shift = At.Shift.BEFORE, ordinal = 0)) + private static void icebergRenderFrameGradientOne(GuiGraphics painter, int x, int y, int width, int height, int z, int color1, int color2, CallbackInfo info) { horizontalLineColor = Tooltips.currentColors.borderColorStart(); } - @Inject(method = "renderFrameGradient", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil$BlitPainter;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/vertex/BufferBuilder;IIIII)V", shift = At.Shift.BEFORE, ordinal = 1)) - private static void icebergRenderFrameGradientTwo(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int height, int z, int color1, int color2, CallbackInfo info) + @Inject(method = "renderFrameGradient", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/tooltip/TooltipRenderUtil;renderHorizontalLine(Lnet/minecraft/client/gui/GuiGraphics;IIIII)V", shift = At.Shift.BEFORE, ordinal = 1)) + private static void icebergRenderFrameGradientTwo(GuiGraphics painter, int x, int y, int width, int height, int z, int color1, int color2, CallbackInfo info) { horizontalLineColor = Tooltips.currentColors.borderColorEnd(); } @Inject(method = "renderHorizontalLine", at = @At(value = "HEAD"), cancellable = true) - private static void icebergRenderHorizontalLine(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int z, int color, CallbackInfo info) + private static void icebergRenderHorizontalLine(GuiGraphics painter, int x, int y, int width, int z, int color, CallbackInfo info) { if (color != BACKGROUND_COLOR && color != BORDER_COLOR_TOP && color != BORDER_COLOR_BOTTOM) { @@ -68,13 +67,13 @@ public class TooltipRenderUtilMixin { // Replace the rendered colors with the ones previously stored. int renderColor = horizontalLineColor.getValue(); - painter.blit(matrix, bufferbuilder, x, y, x + width, y + 1, z, renderColor, renderColor); + painter.fillGradient(x, y, x + width, y + 1, z, renderColor, renderColor); info.cancel(); } } @Inject(method = "renderRectangle", at = @At(value = "HEAD"), cancellable = true) - private static void icebergRenderRectangle(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int width, int height, int z, int color, CallbackInfo info) + private static void icebergRenderRectangle(GuiGraphics painter, int x, int y, int width, int height, int z, int color, CallbackInfo info) { if (color != BACKGROUND_COLOR) { @@ -83,13 +82,13 @@ public class TooltipRenderUtilMixin else { // Replace the rendered colors with the ones previously stored. - painter.blit(matrix, bufferbuilder, x, y, x + width, y + height, z, Tooltips.currentColors.backgroundColorStart().getValue(), Tooltips.currentColors.backgroundColorEnd().getValue()); + painter.fillGradient(x, y, x + width, y + height, z, Tooltips.currentColors.backgroundColorStart().getValue(), Tooltips.currentColors.backgroundColorEnd().getValue()); info.cancel(); } } @Inject(method = "renderVerticalLine", at = @At(value = "HEAD"), cancellable = true) - private static void icebergRenderVerticalLine(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int height, int z, int color, CallbackInfo info) + private static void icebergRenderVerticalLine(GuiGraphics painter, int x, int y, int height, int z, int color, CallbackInfo info) { if (color != BACKGROUND_COLOR) { @@ -98,13 +97,13 @@ public class TooltipRenderUtilMixin else { // Replace the rendered colors with the ones previously stored. - painter.blit(matrix, bufferbuilder, x, y, x + 1, y + height, z, Tooltips.currentColors.backgroundColorStart().getValue(), Tooltips.currentColors.backgroundColorEnd().getValue()); + painter.fillGradient(x, y, x + 1, y + height, z, Tooltips.currentColors.backgroundColorStart().getValue(), Tooltips.currentColors.backgroundColorEnd().getValue()); info.cancel(); } } @Inject(method = "renderVerticalLineGradient", at = @At(value = "HEAD"), cancellable = true) - private static void icebergRenderVerticalLineGradient(TooltipRenderUtil.BlitPainter painter, Matrix4f matrix, BufferBuilder bufferbuilder, int x, int y, int height, int z, int startColor, int endColor, CallbackInfo info) + private static void icebergRenderVerticalLineGradient(GuiGraphics painter, int x, int y, int height, int z, int startColor, int endColor, CallbackInfo info) { if (startColor != BORDER_COLOR_TOP || endColor != BORDER_COLOR_BOTTOM) { @@ -113,7 +112,7 @@ public class TooltipRenderUtilMixin else { // Replace the rendered colors with the ones previously stored. - painter.blit(matrix, bufferbuilder, x, y, x + 1, y + height, z, Tooltips.currentColors.borderColorStart().getValue(), Tooltips.currentColors.borderColorEnd().getValue()); + painter.fillGradient(x, y, x + 1, y + height, z, Tooltips.currentColors.borderColorStart().getValue(), Tooltips.currentColors.borderColorEnd().getValue()); info.cancel(); } } diff --git a/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java b/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java index 88b3b6d..90d1278 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java +++ b/src/main/java/com/anthonyhilyard/iceberg/renderer/CustomItemRenderer.java @@ -8,6 +8,7 @@ import java.util.Set; import java.util.function.Predicate; import com.anthonyhilyard.iceberg.util.EntityCollector; +import com.anthonyhilyard.iceberg.util.GuiHelper; import com.google.common.collect.Maps; import org.joml.Matrix4f; @@ -18,6 +19,7 @@ import org.lwjgl.opengl.GL11; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.blaze3d.vertex.VertexSorting; import com.mojang.datafixers.util.Pair; import com.mojang.math.Axis; import com.mojang.math.MatrixUtil; @@ -37,7 +39,7 @@ import net.minecraft.client.renderer.entity.ItemRenderer; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.color.item.ItemColors; -import net.minecraft.client.gui.GuiComponent; +import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.client.renderer.ItemBlockRenderTypes; import net.minecraft.client.renderer.LightTexture; @@ -97,6 +99,7 @@ public class CustomItemRenderer extends ItemRenderer private static Map<Pair<Item, CompoundTag>, ModelBounds> modelBoundsCache = Maps.newHashMap(); private static final List<Direction> quadDirections; + static { quadDirections = new ArrayList<>(Arrays.asList(Direction.values())); @@ -106,7 +109,6 @@ public class CustomItemRenderer extends ItemRenderer private Minecraft minecraft; private final ModelManager modelManager; private final BlockEntityWithoutLevelRenderer blockEntityRenderer; - private float blitOffset = 100.0f; public CustomItemRenderer(TextureManager textureManagerIn, ModelManager modelManagerIn, ItemColors itemColorsIn, BlockEntityWithoutLevelRenderer blockEntityRendererIn, Minecraft mcIn) { @@ -129,7 +131,7 @@ public class CustomItemRenderer extends ItemRenderer } } - private void renderGuiModel(ItemStack itemStack, int x, int y, Quaternionf rotation, BakedModel bakedModel) + private void renderGuiModel(ItemStack itemStack, int x, int y, Quaternionf rotation, BakedModel bakedModel, GuiGraphics graphics) { minecraft.getTextureManager().getTexture(InventoryMenu.BLOCK_ATLAS).setFilter(false, false); RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS); @@ -139,10 +141,8 @@ public class CustomItemRenderer extends ItemRenderer PoseStack modelViewStack = RenderSystem.getModelViewStack(); modelViewStack.pushPose(); - modelViewStack.translate(x, y, 100.0f + blitOffset); - - modelViewStack.translate(8.0f, 8.0f, 0.0f); - modelViewStack.scale(1.0f, -1.0f, 1.0f); + modelViewStack.translate(x + 8.0f, y + 8.0f, 150.0f); + modelViewStack.mulPoseMatrix((new Matrix4f()).scaling(1.0f, -1.0f, 1.0f)); modelViewStack.scale(16.0f, 16.0f, 16.0f); RenderSystem.applyModelViewMatrix(); @@ -153,9 +153,10 @@ public class CustomItemRenderer extends ItemRenderer PoseStack poseStack = new PoseStack(); renderModel(itemStack, ItemDisplayContext.GUI, false, poseStack, rotation, bufferSource, LightTexture.FULL_BRIGHT, OverlayTexture.NO_OVERLAY, bakedModel); - poseStack.popPose(); + RenderSystem.disableDepthTest(); bufferSource.endBatch(); RenderSystem.enableDepthTest(); + if (flatLighting) { Lighting.setupFor3DItems(); } modelViewStack.popPose(); @@ -349,7 +350,6 @@ public class CustomItemRenderer extends ItemRenderer { // Apply the standard block rotation so block entities match other blocks. poseStack.mulPose(new Quaternionf().rotationXYZ((float)Math.toRadians(30.0f), (float)Math.toRadians(225.0f), 0.0f)); - } else { @@ -407,7 +407,6 @@ public class CustomItemRenderer extends ItemRenderer } poseStack.translate(-0.5f, -0.5f, -0.5f); - CheckedBufferSource checkedBufferSource = CheckedBufferSource.create(bufferSource); renderModelInternal(itemStack, previewContext, leftHanded, poseStack, rotation, checkedBufferSource, packedLight, packedOverlay, bakedModel, b -> !b.hasRendered()); @@ -624,16 +623,14 @@ public class CustomItemRenderer extends ItemRenderer return modelBoundsCache.get(key); } - public void renderDetailModelIntoGUI(ItemStack stack, int x, int y, Quaternionf rotation) + public void renderDetailModelIntoGUI(ItemStack stack, int x, int y, Quaternionf rotation, GuiGraphics graphics) { Minecraft minecraft = Minecraft.getInstance(); BakedModel bakedModel = minecraft.getItemRenderer().getModel(stack, minecraft.level, minecraft.player, 0); - blitOffset += 50.0f; - try { - renderGuiModel(stack, x, y, rotation, bakedModel); + renderGuiModel(stack, x, y, rotation, bakedModel, graphics); } catch (Throwable throwable) { @@ -654,8 +651,6 @@ public class CustomItemRenderer extends ItemRenderer }); throw new ReportedException(crashreport); } - - blitOffset -= 50.0f; } public void renderItemModelIntoGUIWithAlpha(PoseStack poseStack, ItemStack stack, int x, int y, float alpha) @@ -672,7 +667,7 @@ public class CustomItemRenderer extends ItemRenderer RenderSystem.clear(GL11.GL_DEPTH_BUFFER_BIT, Minecraft.ON_OSX); RenderSystem.backupProjectionMatrix(); - RenderSystem.setProjectionMatrix(matrix); + RenderSystem.setProjectionMatrix(matrix, VertexSorting.ORTHOGRAPHIC_Z); minecraft.getTextureManager().getTexture(InventoryMenu.BLOCK_ATLAS).setFilter(false, false); RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS); @@ -720,7 +715,7 @@ public class CustomItemRenderer extends ItemRenderer RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, alpha); RenderSystem.setShaderTexture(0, iconFrameBuffer.getColorTextureId()); - GuiComponent.blit(poseStack, x, y, 16, 16, 0, 0, iconFrameBuffer.width, iconFrameBuffer.height, iconFrameBuffer.width, iconFrameBuffer.height); + GuiHelper.blit(poseStack, x, y, 16, 16, 0, 0, iconFrameBuffer.width, iconFrameBuffer.height, iconFrameBuffer.width, iconFrameBuffer.height); RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); @@ -740,4 +735,5 @@ public class CustomItemRenderer extends ItemRenderer // Clear the model bounds cache. modelBoundsCache.clear(); } + }
\ No newline at end of file diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java b/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java index 6e3b079..90acfe4 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/EntityCollector.java @@ -108,14 +108,14 @@ public class EntityCollector extends Level try { - Player dummyPlayer = new Player(minecraft.player.level, BlockPos.ZERO, 0.0f, new GameProfile(null, "_dummy")) { + Player dummyPlayer = new Player(minecraft.player.level(), BlockPos.ZERO, 0.0f, new GameProfile(null, "_dummy")) { @Override public boolean isSpectator() { return false; } @Override public boolean isCreative() { return false; } }; dummyPlayer.setItemInHand(InteractionHand.MAIN_HAND, dummyStack); - EntityCollector levelWrapper = EntityCollector.of(dummyPlayer.level); + EntityCollector levelWrapper = EntityCollector.of(dummyPlayer.level()); if (item instanceof SpawnEggItem spawnEggItem) { diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/GuiHelper.java b/src/main/java/com/anthonyhilyard/iceberg/util/GuiHelper.java index eb91073..0b7da1f 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/GuiHelper.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/GuiHelper.java @@ -1,7 +1,5 @@ package com.anthonyhilyard.iceberg.util; -import org.joml.Matrix4f; - import net.minecraft.client.renderer.GameRenderer; import com.mojang.blaze3d.systems.RenderSystem; @@ -9,8 +7,9 @@ import com.mojang.blaze3d.vertex.Tesselator; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferUploader; import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexFormat; - +import org.joml.Matrix4f; public class GuiHelper { @@ -74,4 +73,24 @@ public class GuiHelper RenderSystem.disableBlend(); } + + public static void blit(PoseStack poseStack, int x, int y, int width, int height, float texX, float texY, int texWidth, int texHeight, int fullWidth, int fullHeight) { + blit(poseStack, x, x + width, y, y + height, 0, texWidth, texHeight, texX, texY, fullWidth, fullHeight); + } + public static void blit(PoseStack poseStack, int x0, int x1, int y0, int y1, int z, int texWidth, int texHeight, float texX, float texY, int fullWidth, int fullHeight) { + innerBlit(poseStack, x0, x1, y0, y1, z, (texX + 0.0F) / (float)fullWidth, (texX + (float)texWidth) / (float)fullWidth, (texY + 0.0F) / (float)fullHeight, (texY + (float)texHeight) / (float)fullHeight); + } + + private static void innerBlit(PoseStack poseStack, int x0, int x1, int y0, int y1, int z, float u0, float u1, float v0, float v1) + { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + Matrix4f matrix4f = poseStack.last().pose(); + BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder(); + bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX); + bufferbuilder.vertex(matrix4f, (float)x0, (float)y0, (float)z).uv(u0, v0).endVertex(); + bufferbuilder.vertex(matrix4f, (float)x0, (float)y1, (float)z).uv(u0, v1).endVertex(); + bufferbuilder.vertex(matrix4f, (float)x1, (float)y1, (float)z).uv(u1, v1).endVertex(); + bufferbuilder.vertex(matrix4f, (float)x1, (float)y0, (float)z).uv(u1, v0).endVertex(); + BufferUploader.drawWithShader(bufferbuilder.end()); + } } diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java index 29cc39b..f1fe7c9 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java @@ -24,8 +24,11 @@ import com.mojang.datafixers.util.Either; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTextTooltip; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; +import net.minecraft.client.gui.screens.inventory.tooltip.DefaultTooltipPositioner; import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.renderer.MultiBufferSource; @@ -41,7 +44,6 @@ import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.InteractionResult; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; - import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -161,6 +163,7 @@ public class Tooltips return titleLines; } + @Deprecated public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, Rect2i rect, int screenWidth, int screenHeight, int backgroundColor, int borderColorStart, int borderColorEnd) @@ -168,6 +171,7 @@ public class Tooltips renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, borderColorStart, borderColorEnd, false); } + @Deprecated public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, Rect2i rect, int screenWidth, int screenHeight, int backgroundColor, int borderColorStart, int borderColorEnd, boolean comparison) @@ -175,18 +179,34 @@ public class Tooltips renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, borderColorStart, borderColorEnd, comparison, false); } + @Deprecated public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, Rect2i rect, int screenWidth, int screenHeight, int backgroundColor, int borderColorStart, int borderColorEnd, boolean comparison, boolean constrain) { - renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, backgroundColor, borderColorStart, borderColorEnd, comparison, constrain, false, 0); + renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, backgroundColor, borderColorStart, borderColorEnd, + comparison, constrain, false, 0); } + @Deprecated public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, Rect2i rect, int screenWidth, int screenHeight, int backgroundColorStart, int backgroundColorEnd, int borderColorStart, int borderColorEnd, boolean comparison, boolean constrain, boolean centeredTitle, int index) { + Minecraft minecraft = Minecraft.getInstance(); + GuiGraphics graphics = new GuiGraphics(minecraft, poseStack, minecraft.renderBuffers().bufferSource()); + renderItemTooltip(stack, info, rect, screenWidth, screenHeight, backgroundColorStart, backgroundColorEnd, borderColorStart, borderColorEnd, + graphics, DefaultTooltipPositioner.INSTANCE, comparison, constrain, centeredTitle, index); + } + + @SuppressWarnings("deprecation") + public static void renderItemTooltip(@Nonnull final ItemStack stack, TooltipInfo info, + Rect2i rect, int screenWidth, int screenHeight, + int backgroundColorStart, int backgroundColorEnd, int borderColorStart, int borderColorEnd, + GuiGraphics graphics, ClientTooltipPositioner positioner, + boolean comparison, boolean constrain, boolean centeredTitle, int index) + { if (info.getComponents().isEmpty()) { return; @@ -207,7 +227,7 @@ public class Tooltips int rectX = rect.getX() + 4; int rectY = rect.getY() + 4; - PreExtResult preResult = RenderTooltipEvents.PREEXT.invoker().onPre(stack, info.getComponents(), poseStack, rectX, rectY, screenWidth, screenHeight, info.getFont(), comparison, index); + PreExtResult preResult = RenderTooltipEvents.PREEXT.invoker().onPre(stack, graphics, rectX, rectY, screenWidth, screenHeight, info.getFont(), info.getComponents(), positioner, comparison, index); if (preResult.result() != InteractionResult.PASS) { return; @@ -219,8 +239,8 @@ public class Tooltips screenHeight = preResult.screenHeight(); info.setFont(preResult.font()); + PoseStack poseStack = graphics.pose(); poseStack.pushPose(); - final int zLevel = 400; Tesselator tesselator = Tesselator.getInstance(); @@ -229,7 +249,7 @@ public class Tooltips bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); Matrix4f matrix4f = poseStack.last().pose(); - ColorExtResult colors = RenderTooltipEvents.COLOREXT.invoker().onColor(stack, info.components, poseStack, rectX, rectY, info.getFont(), backgroundColorStart, backgroundColorEnd, borderColorStart, borderColorEnd, comparison, index); + ColorExtResult colors = RenderTooltipEvents.COLOREXT.invoker().onColor(stack, graphics, rectX, rectY, info.getFont(), backgroundColorStart, backgroundColorEnd, borderColorStart, borderColorEnd, info.getComponents(), comparison, index); backgroundColorStart = colors.backgroundStart(); backgroundColorEnd = colors.backgroundEnd(); @@ -238,9 +258,12 @@ public class Tooltips currentColors = new TooltipColors(TextColor.fromRgb(backgroundColorStart), TextColor.fromRgb(backgroundColorEnd), TextColor.fromRgb(borderColorStart), TextColor.fromRgb(borderColorEnd)); - TooltipRenderUtil.renderTooltipBackground((matrix, bufferBuilder, left, top, right, bottom, z, startColor, endColor) -> { - GuiHelper.drawGradientRect(matrix, bufferBuilder, left, top, right, bottom, z, startColor, endColor); - }, matrix4f, bufferbuilder, rectX, rectY, rect.getWidth(), rect.getHeight(), zLevel); + final int finalRectX = rectX; + final int finalRectY = rectY; + + graphics.drawManaged(() -> { + TooltipRenderUtil.renderTooltipBackground(graphics, finalRectX, finalRectY, rect.getWidth(), rect.getHeight(), zLevel); + }); RenderSystem.enableDepthTest(); RenderSystem.enableBlend(); @@ -273,13 +296,13 @@ public class Tooltips for (int componentNumber = 0; componentNumber < info.getComponents().size(); ++componentNumber) { ClientTooltipComponent imageComponent = (ClientTooltipComponent)info.getComponents().get(componentNumber); - imageComponent.renderImage(info.getFont(), rectX, tooltipTop, poseStack, itemRenderer); + imageComponent.renderImage(info.getFont(), rectX, tooltipTop, graphics); tooltipTop += imageComponent.getHeight() + (componentNumber == 0 ? 2 : 0); } poseStack.popPose(); - RenderTooltipEvents.POSTEXT.invoker().onPost(stack, info.getComponents(), poseStack, rectX, rectY, info.getFont(), rect.getWidth(), rect.getHeight(), comparison, index); + RenderTooltipEvents.POSTEXT.invoker().onPost(stack, graphics, rectX, rectY, info.getFont(), rect.getWidth(), rect.getHeight(), info.getComponents(), comparison, index); } private static ClientTooltipComponent getClientComponent(TooltipComponent componentData) @@ -392,15 +415,26 @@ public class Tooltips Tooltips::getClientComponent)).toList(); } + @Deprecated public static Rect2i calculateRect(final ItemStack stack, PoseStack poseStack, List<ClientTooltipComponent> components, int mouseX, int mouseY,int screenWidth, int screenHeight, int maxTextWidth, Font font) { return calculateRect(stack, poseStack, components, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font, 0, false); } + @Deprecated public static Rect2i calculateRect(final ItemStack stack, PoseStack poseStack, List<ClientTooltipComponent> components, int mouseX, int mouseY,int screenWidth, int screenHeight, int maxTextWidth, Font font, int minWidth, boolean centeredTitle) { + Minecraft minecraft = Minecraft.getInstance(); + GuiGraphics graphics = new GuiGraphics(minecraft, poseStack, minecraft.renderBuffers().bufferSource()); + return calculateRect(stack, graphics, DefaultTooltipPositioner.INSTANCE, components, mouseX, mouseY, screenWidth, screenHeight, + maxTextWidth, font, minWidth, centeredTitle); + } + + public static Rect2i calculateRect(final ItemStack stack, GuiGraphics graphics, ClientTooltipPositioner positioner, List<ClientTooltipComponent> components, + int mouseX, int mouseY,int screenWidth, int screenHeight, int maxTextWidth, Font font, int minWidth, boolean centeredTitle) + { Rect2i rect = new Rect2i(0, 0, 0, 0); if (components == null || components.isEmpty() || stack == null) { @@ -408,7 +442,7 @@ public class Tooltips } // Generate a tooltip event even though we aren't rendering anything in case event handlers are modifying the input values. - PreExtResult preResult = RenderTooltipEvents.PREEXT.invoker().onPre(stack, components, poseStack, mouseX, mouseY, screenWidth, screenHeight, font, false, 0); + PreExtResult preResult = RenderTooltipEvents.PREEXT.invoker().onPre(stack, graphics, mouseX, mouseY, screenWidth, screenHeight, font, components, positioner, false, 0); if (preResult.result() != InteractionResult.PASS) { return rect; |