Browse Source

[ci skip] Introduce item as an additional context (#189)

* Introduce item as an additional context

* Remove @Internal
shedaniel 3 years ago
parent
commit
6241b262e9

+ 15 - 0
common/src/main/java/me/shedaniel/architectury/event/events/TooltipEvent.java

@@ -22,6 +22,7 @@ package me.shedaniel.architectury.event.events;
 import com.mojang.blaze3d.vertex.PoseStack;
 import me.shedaniel.architectury.event.Event;
 import me.shedaniel.architectury.event.EventFactory;
+import me.shedaniel.architectury.impl.TooltipAdditionalContextsImpl;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.network.chat.Component;
@@ -30,6 +31,8 @@ import net.minecraft.util.FormattedCharSequence;
 import net.minecraft.world.InteractionResult;
 import net.minecraft.world.item.ItemStack;
 import net.minecraft.world.item.TooltipFlag;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Nullable;
 
 import java.util.List;
 
@@ -56,6 +59,18 @@ public interface TooltipEvent {
      */
     Event<RenderModifyColor> RENDER_MODIFY_COLOR = EventFactory.createLoop();
     
+    static AdditionalContexts additionalContexts() {
+        return TooltipAdditionalContextsImpl.get();
+    }
+    
+    @ApiStatus.NonExtendable
+    interface AdditionalContexts {
+        @Nullable
+        ItemStack getItem();
+        
+        void setItem(@Nullable ItemStack stack);
+    }
+    
     @Environment(EnvType.CLIENT)
     interface Item {
         /**

+ 46 - 0
common/src/main/java/me/shedaniel/architectury/impl/TooltipAdditionalContextsImpl.java

@@ -0,0 +1,46 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021, 2022 architectury
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.impl;
+
+import me.shedaniel.architectury.event.events.TooltipEvent;
+import net.minecraft.world.item.ItemStack;
+import org.jetbrains.annotations.Nullable;
+
+public class TooltipAdditionalContextsImpl implements TooltipEvent.AdditionalContexts {
+    private static final ThreadLocal<TooltipAdditionalContextsImpl> INSTANCE_LOCAL = ThreadLocal.withInitial(TooltipAdditionalContextsImpl::new);
+    
+    public static TooltipEvent.AdditionalContexts get() {
+        return INSTANCE_LOCAL.get();
+    }
+    
+    @Nullable
+    private ItemStack item;
+    
+    @Override
+    @Nullable
+    public ItemStack getItem() {
+        return item;
+    }
+    
+    @Override
+    public void setItem(@Nullable ItemStack item) {
+        this.item = item;
+    }
+}

+ 11 - 0
fabric/src/main/java/me/shedaniel/architectury/mixin/fabric/client/MixinScreen.java

@@ -33,6 +33,7 @@ import net.minecraft.client.gui.screens.Screen;
 import net.minecraft.util.FormattedCharSequence;
 import net.minecraft.world.InteractionResult;
 import net.minecraft.world.InteractionResultHolder;
+import net.minecraft.world.item.ItemStack;
 import org.spongepowered.asm.mixin.Final;
 import org.spongepowered.asm.mixin.Mixin;
 import org.spongepowered.asm.mixin.Shadow;
@@ -89,6 +90,16 @@ public abstract class MixinScreen implements ScreenInputDelegate {
         return message;
     }
     
+    @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/world/item/ItemStack;II)V", at = @At("HEAD"))
+    private void preRenderTooltipItem(PoseStack poseStack, ItemStack stack, int x, int y, CallbackInfo ci) {
+        TooltipEvent.additionalContexts().setItem(stack);
+    }
+    
+    @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/world/item/ItemStack;II)V", at = @At("RETURN"))
+    private void postRenderTooltipItem(PoseStack poseStack, ItemStack stack, int x, int y, CallbackInfo ci) {
+        TooltipEvent.additionalContexts().setItem(null);
+    }
+    
     @Inject(method = "renderTooltip(Lcom/mojang/blaze3d/vertex/PoseStack;Ljava/util/List;II)V", at = @At("HEAD"), cancellable = true)
     private void renderTooltip(PoseStack poseStack, List<? extends FormattedCharSequence> list, int x, int y, CallbackInfo ci) {
         if (!list.isEmpty()) {

+ 30 - 18
forge/src/main/java/me/shedaniel/architectury/event/forge/EventHandlerImplClient.java

@@ -171,29 +171,41 @@ public class EventHandlerImplClient {
     
     @SubscribeEvent(priority = EventPriority.HIGH)
     public static void event(RenderTooltipEvent.Pre event) {
-        if (TooltipEvent.RENDER_FORGE_PRE.invoker().renderTooltip(event.getMatrixStack(), event.getLines(), event.getX(), event.getY()) == InteractionResult.FAIL) {
-            event.setCanceled(true);
-            return;
-        }
+        TooltipEvent.additionalContexts().setItem(event.getStack());
         
-        TooltipEventPositionContextImpl positionContext = tooltipPositionContext.get();
-        positionContext.reset(event.getX(), event.getY());
-        TooltipEvent.RENDER_MODIFY_POSITION.invoker().renderTooltip(event.getMatrixStack(), positionContext);
-        event.setX(positionContext.getTooltipX());
-        event.setY(positionContext.getTooltipY());
+        try {
+            if (TooltipEvent.RENDER_FORGE_PRE.invoker().renderTooltip(event.getMatrixStack(), event.getLines(), event.getX(), event.getY()) == InteractionResult.FAIL) {
+                event.setCanceled(true);
+                return;
+            }
+            
+            TooltipEventPositionContextImpl positionContext = tooltipPositionContext.get();
+            positionContext.reset(event.getX(), event.getY());
+            TooltipEvent.RENDER_MODIFY_POSITION.invoker().renderTooltip(event.getMatrixStack(), positionContext);
+            event.setX(positionContext.getTooltipX());
+            event.setY(positionContext.getTooltipY());
+        } finally {
+            TooltipEvent.additionalContexts().setItem(null);
+        }
     }
     
     @SubscribeEvent(priority = EventPriority.HIGH)
     public static void event(RenderTooltipEvent.Color event) {
-        TooltipEventColorContextImpl colorContext = tooltipColorContext.get();
-        colorContext.reset();
-        colorContext.setBackgroundColor(event.getBackground());
-        colorContext.setOutlineGradientTopColor(event.getBorderStart());
-        colorContext.setOutlineGradientBottomColor(event.getBorderEnd());
-        TooltipEvent.RENDER_MODIFY_COLOR.invoker().renderTooltip(event.getMatrixStack(), event.getX(), event.getY(), colorContext);
-        event.setBackground(colorContext.getBackgroundColor());
-        event.setBorderEnd(colorContext.getOutlineGradientBottomColor());
-        event.setBorderStart(colorContext.getOutlineGradientTopColor());
+        TooltipEvent.additionalContexts().setItem(event.getStack());
+        
+        try {
+            TooltipEventColorContextImpl colorContext = tooltipColorContext.get();
+            colorContext.reset();
+            colorContext.setBackgroundColor(event.getBackground());
+            colorContext.setOutlineGradientTopColor(event.getBorderStart());
+            colorContext.setOutlineGradientBottomColor(event.getBorderEnd());
+            TooltipEvent.RENDER_MODIFY_COLOR.invoker().renderTooltip(event.getMatrixStack(), event.getX(), event.getY(), colorContext);
+            event.setBackground(colorContext.getBackgroundColor());
+            event.setBorderEnd(colorContext.getOutlineGradientBottomColor());
+            event.setBorderStart(colorContext.getOutlineGradientTopColor());
+        } finally {
+            TooltipEvent.additionalContexts().setItem(null);
+        }
     }
     
     @SubscribeEvent(priority = EventPriority.HIGH)

+ 1 - 1
forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinChunkSerializer.java

@@ -59,7 +59,7 @@ public class MixinChunkSerializer {
             ChunkDataEvent.Load load = (ChunkDataEvent.Load) event;
             ((EventHandlerImplCommon.WorldEventAttachment) load).architectury$attachLevel(levelRef.get());
         }
-        level.set(null);
+        level.remove();
         return event;
     }
 }