From 764b70586e1853ff1c5596b998c1b9c0855a945c Mon Sep 17 00:00:00 2001 From: ipz233_vvv Date: Sun, 5 Nov 2023 07:58:28 +0200 Subject: [PATCH 01/10] MultiTooltipApi --- .../rendering/v1/TooltipDataCallback.java | 29 ++++++++ .../tooltip/MultiTooltipComponent.java | 74 +++++++++++++++++++ .../MultiTooltipComponentRegister.java | 16 ++++ .../rendering/tooltip/MultiTooltipData.java | 15 ++++ .../client/rendering/HandledScreenMixin.java | 30 ++++++++ .../resources/fabric-rendering-v1.mixins.json | 1 + .../src/client/resources/fabric.mod.json | 5 ++ .../src/testmod/resources/fabric.mod.json | 5 +- .../tooltip/BundleFullnessTooltipTest.java | 66 +++++++++++++++++ .../client/tooltip/DurabilityTooltipTest.java | 62 ++++++++++++++++ .../client/tooltip/FoodTooltipTest.java | 69 +++++++++++++++++ 11 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java create mode 100644 fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java create mode 100644 fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java create mode 100644 fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java new file mode 100644 index 0000000000..4bc1b90e64 --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -0,0 +1,29 @@ +package net.fabricmc.fabric.api.client.rendering.v1; + +import java.util.List; + +import net.minecraft.client.item.TooltipData; +import net.minecraft.item.ItemStack; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +@FunctionalInterface +public interface TooltipDataCallback { + /** + * Allows registering custom TooltipData object for item. + * This allows you to add your own tooltips to existing items + * Tooltip data rendering should be registered using TooltipComponentCallback, + * otherwise game will crash when trying to map TooltipData to TooltipComponent + * If you don't need to add tooltip data to this specific itemStack you can return Optional.empty() + */ + public static final Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { + //MultiTooltipData tooltipData = new MultiTooltipData(callbacks.length); + for(TooltipDataCallback callback : callbacks){ + callback.getTooltipData(itemStack,tooltipDataList); + } + }); + void getTooltipData(ItemStack itemStack, List tooltipDataList); +} + + diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java new file mode 100644 index 0000000000..e7d9e53152 --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java @@ -0,0 +1,74 @@ +package net.fabricmc.fabric.impl.client.rendering.tooltip; + +import java.util.ArrayList; +import java.util.List; + +import org.joml.Matrix4f; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.render.VertexConsumerProvider; + +/** + * This class renders multiple tooltip components as one + */ +public class MultiTooltipComponent implements TooltipComponent { + private final int height; + private final int width; + private final List components; + + public static MultiTooltipComponent of(MultiTooltipData data){ + var l = new ArrayList(data.size()); + for(var d : data){ + l.add(TooltipComponent.of(d)); + } + return new MultiTooltipComponent(l); + } + + public MultiTooltipComponent(List components) { + this.components = components; + int height=0; + int width=0; + for (TooltipComponent component : components) { + height += component.getHeight(); + width = Math.max(width, component.getWidth(MinecraftClient.getInstance().textRenderer)); + } + this.height = height; + this.width = width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getWidth(TextRenderer textRenderer) { + return width; + } + + @Override + public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) { + int position = 0; + for(var c : components){ + matrix.translate(0,position,0); + c.drawText(textRenderer,x,y,matrix,vertexConsumers); + matrix.translate(0,-position,0); + position+=c.getHeight(); + } + } + + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + int position = 0; + for(var c : components) { + context.getMatrices().push(); + context.getMatrices().translate(0,position,0); + c.drawItems(textRenderer,x,y,context); + context.getMatrices().pop(); + position+=c.getHeight(); + } + } +} diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java new file mode 100644 index 0000000000..2b2af1684c --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java @@ -0,0 +1,16 @@ +package net.fabricmc.fabric.impl.client.rendering.tooltip; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; + +public class MultiTooltipComponentRegister implements ClientModInitializer { + @Override + public void onInitializeClient() { + TooltipComponentCallback.EVENT.register((tooltipData)->{ + if(tooltipData instanceof MultiTooltipData multiTooltipData){ + return MultiTooltipComponent.of(multiTooltipData); + } + return null; + }); + } +} diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java new file mode 100644 index 0000000000..a544167b28 --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java @@ -0,0 +1,15 @@ +package net.fabricmc.fabric.impl.client.rendering.tooltip; + +import net.minecraft.client.item.TooltipData; + +import java.util.ArrayList; +import java.util.Optional; + +/** + * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent + */ +public class MultiTooltipData extends ArrayList implements TooltipData { + public MultiTooltipData(int length) { + super(length); + } +} diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java new file mode 100644 index 0000000000..bdead9dcc6 --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -0,0 +1,30 @@ +package net.fabricmc.fabric.mixin.client.rendering; + + +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; +import net.fabricmc.fabric.impl.client.rendering.tooltip.MultiTooltipData; + +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.client.item.TooltipData; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.util.Optional; + +@Mixin(HandledScreen.class) +class HandledScreenMixin { + @Redirect(method = "drawMouseoverTooltip",at = @At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) + Optional addMultiData(ItemStack stack){ + var original = stack.getTooltipData(); + var mutlidata = new MultiTooltipData(1); + original.ifPresent(mutlidata::add); + TooltipDataCallback.EVENT.invoker().getTooltipData(stack,mutlidata); + if(mutlidata.size() == 0){ + return Optional.empty(); + } + return Optional.of(mutlidata); + } +} diff --git a/fabric-rendering-v1/src/client/resources/fabric-rendering-v1.mixins.json b/fabric-rendering-v1/src/client/resources/fabric-rendering-v1.mixins.json index 0951fa261d..cd2841d964 100644 --- a/fabric-rendering-v1/src/client/resources/fabric-rendering-v1.mixins.json +++ b/fabric-rendering-v1/src/client/resources/fabric-rendering-v1.mixins.json @@ -12,6 +12,7 @@ "EntityModelLayersAccessor", "EntityModelsMixin", "EntityRenderersMixin", + "HandledScreenMixin", "InGameHudMixin", "ItemColorsMixin", "LivingEntityRendererAccessor", diff --git a/fabric-rendering-v1/src/client/resources/fabric.mod.json b/fabric-rendering-v1/src/client/resources/fabric.mod.json index a17c8cd5cf..3223f6cd9c 100644 --- a/fabric-rendering-v1/src/client/resources/fabric.mod.json +++ b/fabric-rendering-v1/src/client/resources/fabric.mod.json @@ -19,6 +19,11 @@ "fabricloader": ">=0.4.0", "fabric-api-base": "*" }, + "entrypoints": { + "client": [ + "net.fabricmc.fabric.impl.client.rendering.tooltip.MultiTooltipComponentRegister" + ] + }, "description": "Hooks and registries for rendering-related things.", "mixins": [ "fabric-rendering-v1.mixins.json" diff --git a/fabric-rendering-v1/src/testmod/resources/fabric.mod.json b/fabric-rendering-v1/src/testmod/resources/fabric.mod.json index f51ecb36c6..be6842d09e 100644 --- a/fabric-rendering-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-rendering-v1/src/testmod/resources/fabric.mod.json @@ -15,7 +15,10 @@ "net.fabricmc.fabric.test.rendering.client.FeatureRendererTest", "net.fabricmc.fabric.test.rendering.client.TooltipComponentTests", "net.fabricmc.fabric.test.rendering.client.DimensionalRenderingTest", - "net.fabricmc.fabric.test.rendering.client.HudAndShaderTest" + "net.fabricmc.fabric.test.rendering.client.HudAndShaderTest", + "net.fabricmc.fabric.test.rendering.client.tooltip.BundleFullnessTooltipTest", + "net.fabricmc.fabric.test.rendering.client.tooltip.DurabilityTooltipTest", + "net.fabricmc.fabric.test.rendering.client.tooltip.FoodTooltipTest" ] } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java new file mode 100644 index 0000000000..5c6e99f445 --- /dev/null +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java @@ -0,0 +1,66 @@ +package net.fabricmc.fabric.test.rendering.client.tooltip; + + +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; + +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.item.TooltipData; +import net.minecraft.item.BundleItem; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; + +public class BundleFullnessTooltipTest implements ClientModInitializer { + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.getItem() instanceof BundleItem bundle) { + tooltipDataList.add(0,new BundleCustomTooltipData(BundleItem.getAmountFilled(itemStack))); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof BundleCustomTooltipData bundleCustomTooltipData) + return new BundleFullnessTooltipComponent(bundleCustomTooltipData.fullness); + return null; + }); + } + + private static class BundleCustomTooltipData implements TooltipData { + private final float fullness; + + public BundleCustomTooltipData(float fullness) { + this.fullness = fullness; + } + } + + private static class BundleFullnessTooltipComponent implements TooltipComponent { + private static final int BAR_WIDTH = 40; + private static final int BAR_HEIGHT = 10; + private static final int GAP = 2; + private final float fullness; + + public BundleFullnessTooltipComponent(float fullness) { + this.fullness = fullness; + } + + @Override + public int getHeight() { + return BAR_HEIGHT + GAP; + } + + @Override + public int getWidth(TextRenderer textRenderer) { + return BAR_WIDTH; + } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + context.getMatrices().push(); + context.getMatrices().translate(x,y,0); + context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFF3F007F); + context.fill(0,0, (int) (BAR_WIDTH*fullness),BAR_HEIGHT,0xFF7F00FF); + context.getMatrices().pop(); + } + } +} diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java new file mode 100644 index 0000000000..d5aeaf9e24 --- /dev/null +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java @@ -0,0 +1,62 @@ +package net.fabricmc.fabric.test.rendering.client.tooltip; + + +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; + +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.item.TooltipData; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; + + +public class DurabilityTooltipTest implements ClientModInitializer { + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.isDamageable()) { + tooltipDataList.add(new DamagedItemData(itemStack.getDamage(), itemStack.getMaxDamage())); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof DamagedItemData damagedItemData) + return new DurabilityModTooltipComponent(damagedItemData); + return null; + }); + } + + public record DamagedItemData(int durability, int maxDurability) implements TooltipData { + } + + private static class DurabilityModTooltipComponent implements TooltipComponent { + private static final int BAR_WIDTH = 40; + private static final int BAR_HEIGHT = 10; + private static final int GAP = 2; + private final DamagedItemData damage; + + public DurabilityModTooltipComponent(DamagedItemData data) { + this.damage = data; + } + + @Override + public int getHeight() { + return BAR_HEIGHT + GAP; + } + + @Override + public int getWidth(TextRenderer textRenderer) { + return BAR_WIDTH; + } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + context.getMatrices().push(); + context.getMatrices().translate(x,y,0); + float width = 1-(float) this.damage.durability / this.damage.maxDurability; + context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFFFF0000); + context.fill(0,0, (int) (BAR_WIDTH*width),BAR_HEIGHT,0xFF00FF00); + context.getMatrices().pop(); + } + } +} diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java new file mode 100644 index 0000000000..c2c716c8f5 --- /dev/null +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java @@ -0,0 +1,69 @@ +package net.fabricmc.fabric.test.rendering.client.tooltip; + + +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; + +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.item.TooltipData; +import net.minecraft.item.FoodComponent; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; + +public class FoodTooltipTest implements ClientModInitializer { + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.getItem().getFoodComponent() != null) { + var foodData = new FoodItemData(itemStack.getItem().getFoodComponent()); + tooltipDataList.add(foodData); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof FoodItemData foodItemData) + return new FoodModTooltip(foodItemData); + return null; + }); + } + + + private static class FoodItemData implements TooltipData { + public final int hunger; + + public FoodItemData(FoodComponent foodComponent) { + this.hunger = foodComponent.getHunger(); + } + } + + private static class FoodModTooltip implements TooltipComponent { + private final FoodItemData food; + private static final int SIZE=8; + private static final int GAP=2; + + public FoodModTooltip(FoodItemData foodItemData) { + this.food = foodItemData; + } + + @Override + public int getHeight() { + return (SIZE+GAP); + } + + @Override + public int getWidth(TextRenderer textRenderer) { + return (SIZE+GAP)*food.hunger-GAP; + } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + context.getMatrices().push(); + context.getMatrices().translate(x,y,0); + for(int i=0;i Date: Sun, 5 Nov 2023 08:05:13 +0200 Subject: [PATCH 02/10] updated javadocs --- .../client/rendering/v1/TooltipComponentCallback.java | 3 ++- .../api/client/rendering/v1/TooltipDataCallback.java | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java index f0a6b0c941..98b8cd7b9b 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java @@ -27,7 +27,8 @@ /** * Allows registering a mapping from {@link TooltipData} to {@link TooltipComponent}. - * This allows custom tooltips for items: first, override {@link Item#getTooltipData} and return a custom {@code TooltipData}. + * This allows custom tooltips for items: first, override {@link Item#getTooltipData} and return a custom {@code TooltipData}, or use + * {@link TooltipDataCallback} to add {@code TooltipData} to an existing item * Second, register a listener to this event and convert the data to your component implementation if it's an instance of your data class. * *

Note that failure to map some data to a component will throw an exception, diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index 4bc1b90e64..3336c4b433 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -11,11 +11,10 @@ @FunctionalInterface public interface TooltipDataCallback { /** - * Allows registering custom TooltipData object for item. - * This allows you to add your own tooltips to existing items - * Tooltip data rendering should be registered using TooltipComponentCallback, - * otherwise game will crash when trying to map TooltipData to TooltipComponent - * If you don't need to add tooltip data to this specific itemStack you can return Optional.empty() + * Allows registering custom {@link TooltipData} object for item. + * This allows you to add your own tooltips to existing items. + * Custom {@code TooltipData} should be registered using {@link TooltipComponentCallback}, + * otherwise game will crash when trying to map {@code TooltipData} to {@code TooltipComponent} */ public static final Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { //MultiTooltipData tooltipData = new MultiTooltipData(callbacks.length); From 53c4a12eb5298637212a3dfee173335bb8449217 Mon Sep 17 00:00:00 2001 From: ipz233_vvv Date: Sun, 5 Nov 2023 13:47:29 +0200 Subject: [PATCH 03/10] added license headers --- .../rendering/v1/TooltipDataCallback.java | 44 ++++--- .../tooltip/MultiTooltipComponent.java | 116 ++++++++++-------- .../MultiTooltipComponentRegister.java | 16 +++ .../rendering/tooltip/MultiTooltipData.java | 27 +++- .../client/rendering/HandledScreenMixin.java | 44 ++++--- .../tooltip/BundleFullnessTooltipTest.java | 107 +++++++++------- .../client/tooltip/DurabilityTooltipTest.java | 111 +++++++++-------- .../client/tooltip/FoodTooltipTest.java | 111 +++++++++-------- 8 files changed, 349 insertions(+), 227 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index 3336c4b433..9398e68171 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.fabricmc.fabric.api.client.rendering.v1; import java.util.List; @@ -10,19 +26,17 @@ @FunctionalInterface public interface TooltipDataCallback { - /** - * Allows registering custom {@link TooltipData} object for item. - * This allows you to add your own tooltips to existing items. - * Custom {@code TooltipData} should be registered using {@link TooltipComponentCallback}, - * otherwise game will crash when trying to map {@code TooltipData} to {@code TooltipComponent} - */ - public static final Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { - //MultiTooltipData tooltipData = new MultiTooltipData(callbacks.length); - for(TooltipDataCallback callback : callbacks){ - callback.getTooltipData(itemStack,tooltipDataList); - } - }); - void getTooltipData(ItemStack itemStack, List tooltipDataList); + /** + * Allows registering custom {@link TooltipData} object for item. + * This allows you to add your own tooltips to existing items. + * Custom {@code TooltipData} should be registered using {@link TooltipComponentCallback}, + * otherwise game will crash when trying to map {@code TooltipData} to {@code TooltipComponent} + */ + public static final Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { + //MultiTooltipData tooltipData = new MultiTooltipData(callbacks.length); + for(TooltipDataCallback callback : callbacks){ + callback.getTooltipData(itemStack,tooltipDataList); + } + }); + void getTooltipData(ItemStack itemStack, List tooltipDataList); } - - diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java index e7d9e53152..d3b8d2117b 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.fabricmc.fabric.impl.client.rendering.tooltip; import java.util.ArrayList; @@ -15,60 +31,60 @@ * This class renders multiple tooltip components as one */ public class MultiTooltipComponent implements TooltipComponent { - private final int height; - private final int width; - private final List components; + private final int height; + private final int width; + private final List components; - public static MultiTooltipComponent of(MultiTooltipData data){ - var l = new ArrayList(data.size()); - for(var d : data){ - l.add(TooltipComponent.of(d)); - } - return new MultiTooltipComponent(l); - } + public static MultiTooltipComponent of(MultiTooltipData data){ + var l = new ArrayList(data.size()); + for(var d : data){ + l.add(TooltipComponent.of(d)); + } + return new MultiTooltipComponent(l); + } - public MultiTooltipComponent(List components) { - this.components = components; - int height=0; - int width=0; - for (TooltipComponent component : components) { - height += component.getHeight(); - width = Math.max(width, component.getWidth(MinecraftClient.getInstance().textRenderer)); - } - this.height = height; - this.width = width; - } + public MultiTooltipComponent(List components) { + this.components = components; + int height=0; + int width=0; + for (TooltipComponent component : components) { + height += component.getHeight(); + width = Math.max(width, component.getWidth(MinecraftClient.getInstance().textRenderer)); + } + this.height = height; + this.width = width; + } - @Override - public int getHeight() { - return height; - } + @Override + public int getHeight() { + return height; + } - @Override - public int getWidth(TextRenderer textRenderer) { - return width; - } + @Override + public int getWidth(TextRenderer textRenderer) { + return width; + } - @Override - public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) { - int position = 0; - for(var c : components){ - matrix.translate(0,position,0); - c.drawText(textRenderer,x,y,matrix,vertexConsumers); - matrix.translate(0,-position,0); - position+=c.getHeight(); - } - } + @Override + public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) { + int position = 0; + for(var c : components){ + matrix.translate(0,position,0); + c.drawText(textRenderer,x,y,matrix,vertexConsumers); + matrix.translate(0,-position,0); + position+=c.getHeight(); + } + } - @Override - public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { - int position = 0; - for(var c : components) { - context.getMatrices().push(); - context.getMatrices().translate(0,position,0); - c.drawItems(textRenderer,x,y,context); - context.getMatrices().pop(); - position+=c.getHeight(); - } - } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + int position = 0; + for(var c : components) { + context.getMatrices().push(); + context.getMatrices().translate(0,position,0); + c.drawItems(textRenderer,x,y,context); + context.getMatrices().pop(); + position+=c.getHeight(); + } + } } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java index 2b2af1684c..61713239d7 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.fabricmc.fabric.impl.client.rendering.tooltip; import net.fabricmc.api.ClientModInitializer; diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java index a544167b28..faf43c41e3 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java @@ -1,15 +1,30 @@ -package net.fabricmc.fabric.impl.client.rendering.tooltip; +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import net.minecraft.client.item.TooltipData; +package net.fabricmc.fabric.impl.client.rendering.tooltip; import java.util.ArrayList; -import java.util.Optional; + +import net.minecraft.client.item.TooltipData; /** * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent */ public class MultiTooltipData extends ArrayList implements TooltipData { - public MultiTooltipData(int length) { - super(length); - } + public MultiTooltipData(int length) { + super(length); + } } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index bdead9dcc6..b1ba687df3 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -1,30 +1,46 @@ -package net.fabricmc.fabric.mixin.client.rendering; +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.fabricmc.fabric.mixin.client.rendering; -import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; -import net.fabricmc.fabric.impl.client.rendering.tooltip.MultiTooltipData; -import net.minecraft.client.gui.screen.ingame.HandledScreen; -import net.minecraft.client.item.TooltipData; -import net.minecraft.item.ItemStack; +import java.util.Optional; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; -import java.util.Optional; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.client.item.TooltipData; +import net.minecraft.item.ItemStack; + +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; +import net.fabricmc.fabric.impl.client.rendering.tooltip.MultiTooltipData; @Mixin(HandledScreen.class) class HandledScreenMixin { - @Redirect(method = "drawMouseoverTooltip",at = @At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) - Optional addMultiData(ItemStack stack){ - var original = stack.getTooltipData(); - var mutlidata = new MultiTooltipData(1); - original.ifPresent(mutlidata::add); - TooltipDataCallback.EVENT.invoker().getTooltipData(stack,mutlidata); + @Redirect(method = "drawMouseoverTooltip",at = @At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) + Optional addMultiData(ItemStack stack){ + var original = stack.getTooltipData(); + var mutlidata = new MultiTooltipData(1); + original.ifPresent(mutlidata::add); + TooltipDataCallback.EVENT.invoker().getTooltipData(stack,mutlidata); if(mutlidata.size() == 0){ return Optional.empty(); } return Optional.of(mutlidata); - } + } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java index 5c6e99f445..8e8f827462 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java @@ -1,7 +1,21 @@ -package net.fabricmc.fabric.test.rendering.client.tooltip; +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.fabricmc.fabric.test.rendering.client.tooltip; -import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; @@ -11,56 +25,57 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; public class BundleFullnessTooltipTest implements ClientModInitializer { - @Override - public void onInitializeClient() { - TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { - if (itemStack.getItem() instanceof BundleItem bundle) { - tooltipDataList.add(0,new BundleCustomTooltipData(BundleItem.getAmountFilled(itemStack))); - } - }); - TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof BundleCustomTooltipData bundleCustomTooltipData) - return new BundleFullnessTooltipComponent(bundleCustomTooltipData.fullness); - return null; - }); - } + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.getItem() instanceof BundleItem bundle) { + tooltipDataList.add(0,new BundleCustomTooltipData(BundleItem.getAmountFilled(itemStack))); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof BundleCustomTooltipData bundleCustomTooltipData) + return new BundleFullnessTooltipComponent(bundleCustomTooltipData.fullness); + return null; + }); + } - private static class BundleCustomTooltipData implements TooltipData { - private final float fullness; + private static class BundleCustomTooltipData implements TooltipData { + private final float fullness; - public BundleCustomTooltipData(float fullness) { - this.fullness = fullness; - } - } + public BundleCustomTooltipData(float fullness) { + this.fullness = fullness; + } + } - private static class BundleFullnessTooltipComponent implements TooltipComponent { - private static final int BAR_WIDTH = 40; - private static final int BAR_HEIGHT = 10; - private static final int GAP = 2; - private final float fullness; + private static class BundleFullnessTooltipComponent implements TooltipComponent { + private static final int BAR_WIDTH = 40; + private static final int BAR_HEIGHT = 10; + private static final int GAP = 2; + private final float fullness; - public BundleFullnessTooltipComponent(float fullness) { - this.fullness = fullness; - } + public BundleFullnessTooltipComponent(float fullness) { + this.fullness = fullness; + } - @Override - public int getHeight() { - return BAR_HEIGHT + GAP; - } + @Override + public int getHeight() { + return BAR_HEIGHT + GAP; + } - @Override - public int getWidth(TextRenderer textRenderer) { - return BAR_WIDTH; - } - @Override - public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { - context.getMatrices().push(); - context.getMatrices().translate(x,y,0); - context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFF3F007F); - context.fill(0,0, (int) (BAR_WIDTH*fullness),BAR_HEIGHT,0xFF7F00FF); - context.getMatrices().pop(); - } - } + @Override + public int getWidth(TextRenderer textRenderer) { + return BAR_WIDTH; + } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + context.getMatrices().push(); + context.getMatrices().translate(x,y,0); + context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFF3F007F); + context.fill(0,0, (int) (BAR_WIDTH*fullness),BAR_HEIGHT,0xFF7F00FF); + context.getMatrices().pop(); + } + } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java index d5aeaf9e24..cd26885f34 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java @@ -1,7 +1,21 @@ -package net.fabricmc.fabric.test.rendering.client.tooltip; +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.fabricmc.fabric.test.rendering.client.tooltip; -import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; @@ -10,53 +24,54 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; public class DurabilityTooltipTest implements ClientModInitializer { - @Override - public void onInitializeClient() { - TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { - if (itemStack.isDamageable()) { - tooltipDataList.add(new DamagedItemData(itemStack.getDamage(), itemStack.getMaxDamage())); - } - }); - TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof DamagedItemData damagedItemData) - return new DurabilityModTooltipComponent(damagedItemData); - return null; - }); - } - - public record DamagedItemData(int durability, int maxDurability) implements TooltipData { - } - - private static class DurabilityModTooltipComponent implements TooltipComponent { - private static final int BAR_WIDTH = 40; - private static final int BAR_HEIGHT = 10; - private static final int GAP = 2; - private final DamagedItemData damage; - - public DurabilityModTooltipComponent(DamagedItemData data) { - this.damage = data; - } - - @Override - public int getHeight() { - return BAR_HEIGHT + GAP; - } - - @Override - public int getWidth(TextRenderer textRenderer) { - return BAR_WIDTH; - } - @Override - public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { - context.getMatrices().push(); - context.getMatrices().translate(x,y,0); - float width = 1-(float) this.damage.durability / this.damage.maxDurability; - context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFFFF0000); - context.fill(0,0, (int) (BAR_WIDTH*width),BAR_HEIGHT,0xFF00FF00); - context.getMatrices().pop(); - } - } + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.isDamageable()) { + tooltipDataList.add(new DamagedItemData(itemStack.getDamage(), itemStack.getMaxDamage())); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof DamagedItemData damagedItemData) + return new DurabilityModTooltipComponent(damagedItemData); + return null; + }); + } + + public record DamagedItemData(int durability, int maxDurability) implements TooltipData { + } + + private static class DurabilityModTooltipComponent implements TooltipComponent { + private static final int BAR_WIDTH = 40; + private static final int BAR_HEIGHT = 10; + private static final int GAP = 2; + private final DamagedItemData damage; + + public DurabilityModTooltipComponent(DamagedItemData data) { + this.damage = data; + } + + @Override + public int getHeight() { + return BAR_HEIGHT + GAP; + } + + @Override + public int getWidth(TextRenderer textRenderer) { + return BAR_WIDTH; + } + @Override + public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { + context.getMatrices().push(); + context.getMatrices().translate(x,y,0); + float width = 1-(float) this.damage.durability / this.damage.maxDurability; + context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFFFF0000); + context.fill(0,0, (int) (BAR_WIDTH*width),BAR_HEIGHT,0xFF00FF00); + context.getMatrices().pop(); + } + } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java index c2c716c8f5..6f8e449899 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java @@ -1,7 +1,21 @@ -package net.fabricmc.fabric.test.rendering.client.tooltip; +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.fabricmc.fabric.test.rendering.client.tooltip; -import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; @@ -11,59 +25,60 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; public class FoodTooltipTest implements ClientModInitializer { - @Override - public void onInitializeClient() { - TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { - if (itemStack.getItem().getFoodComponent() != null) { - var foodData = new FoodItemData(itemStack.getItem().getFoodComponent()); - tooltipDataList.add(foodData); - } - }); - TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof FoodItemData foodItemData) - return new FoodModTooltip(foodItemData); - return null; - }); - } + @Override + public void onInitializeClient() { + TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { + if (itemStack.getItem().getFoodComponent() != null) { + var foodData = new FoodItemData(itemStack.getItem().getFoodComponent()); + tooltipDataList.add(foodData); + } + }); + TooltipComponentCallback.EVENT.register(data -> { + if(data instanceof FoodItemData foodItemData) + return new FoodModTooltip(foodItemData); + return null; + }); + } - private static class FoodItemData implements TooltipData { - public final int hunger; + private static class FoodItemData implements TooltipData { + public final int hunger; - public FoodItemData(FoodComponent foodComponent) { - this.hunger = foodComponent.getHunger(); - } - } + public FoodItemData(FoodComponent foodComponent) { + this.hunger = foodComponent.getHunger(); + } + } - private static class FoodModTooltip implements TooltipComponent { - private final FoodItemData food; - private static final int SIZE=8; - private static final int GAP=2; + private static class FoodModTooltip implements TooltipComponent { + private final FoodItemData food; + private static final int SIZE=8; + private static final int GAP=2; - public FoodModTooltip(FoodItemData foodItemData) { - this.food = foodItemData; - } + public FoodModTooltip(FoodItemData foodItemData) { + this.food = foodItemData; + } - @Override - public int getHeight() { - return (SIZE+GAP); - } + @Override + public int getHeight() { + return (SIZE+GAP); + } - @Override - public int getWidth(TextRenderer textRenderer) { - return (SIZE+GAP)*food.hunger-GAP; - } - @Override - public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { - context.getMatrices().push(); - context.getMatrices().translate(x,y,0); - for(int i=0;i Date: Sun, 5 Nov 2023 14:53:05 +0200 Subject: [PATCH 04/10] fixed code style --- .../rendering/v1/TooltipDataCallback.java | 7 ++-- .../tooltip/MultiTooltipComponent.java | 37 +++++++++++-------- .../MultiTooltipComponentRegister.java | 5 ++- .../rendering/tooltip/MultiTooltipData.java | 2 +- .../client/rendering/HandledScreenMixin.java | 13 ++++--- .../tooltip/BundleFullnessTooltipTest.java | 19 +++++----- .../client/tooltip/DurabilityTooltipTest.java | 16 ++++---- .../client/tooltip/FoodTooltipTest.java | 31 +++++++++------- 8 files changed, 72 insertions(+), 58 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index 9398e68171..d6fd52f891 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -32,10 +32,9 @@ public interface TooltipDataCallback { * Custom {@code TooltipData} should be registered using {@link TooltipComponentCallback}, * otherwise game will crash when trying to map {@code TooltipData} to {@code TooltipComponent} */ - public static final Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { - //MultiTooltipData tooltipData = new MultiTooltipData(callbacks.length); - for(TooltipDataCallback callback : callbacks){ - callback.getTooltipData(itemStack,tooltipDataList); + Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { + for (TooltipDataCallback callback : callbacks) { + callback.getTooltipData(itemStack, tooltipDataList); } }); void getTooltipData(ItemStack itemStack, List tooltipDataList); diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java index d3b8d2117b..e0ddb55518 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java @@ -25,32 +25,37 @@ import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.item.TooltipData; import net.minecraft.client.render.VertexConsumerProvider; /** - * This class renders multiple tooltip components as one + * This class renders multiple tooltip components as one. */ public class MultiTooltipComponent implements TooltipComponent { private final int height; private final int width; private final List components; - public static MultiTooltipComponent of(MultiTooltipData data){ + public static MultiTooltipComponent of(MultiTooltipData data) { var l = new ArrayList(data.size()); - for(var d : data){ + + for (TooltipData d : data) { l.add(TooltipComponent.of(d)); } + return new MultiTooltipComponent(l); } public MultiTooltipComponent(List components) { this.components = components; - int height=0; - int width=0; + int height = 0; + int width = 0; + for (TooltipComponent component : components) { - height += component.getHeight(); + height += component.getHeight(); width = Math.max(width, component.getWidth(MinecraftClient.getInstance().textRenderer)); } + this.height = height; this.width = width; } @@ -68,23 +73,25 @@ public int getWidth(TextRenderer textRenderer) { @Override public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) { int position = 0; - for(var c : components){ - matrix.translate(0,position,0); - c.drawText(textRenderer,x,y,matrix,vertexConsumers); - matrix.translate(0,-position,0); - position+=c.getHeight(); + + for (TooltipComponent c : components) { + matrix.translate(0, position, 0); + c.drawText(textRenderer, x, y, matrix, vertexConsumers); + matrix.translate(0, -position, 0); + position += c.getHeight(); } } @Override public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { int position = 0; - for(var c : components) { + + for (TooltipComponent c : components) { context.getMatrices().push(); - context.getMatrices().translate(0,position,0); - c.drawItems(textRenderer,x,y,context); + context.getMatrices().translate(0, position, 0); + c.drawItems(textRenderer, x, y, context); context.getMatrices().pop(); - position+=c.getHeight(); + position += c.getHeight(); } } } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java index 61713239d7..c6816fa3cc 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java @@ -22,10 +22,11 @@ public class MultiTooltipComponentRegister implements ClientModInitializer { @Override public void onInitializeClient() { - TooltipComponentCallback.EVENT.register((tooltipData)->{ - if(tooltipData instanceof MultiTooltipData multiTooltipData){ + TooltipComponentCallback.EVENT.register((tooltipData) -> { + if (tooltipData instanceof MultiTooltipData multiTooltipData) { return MultiTooltipComponent.of(multiTooltipData); } + return null; }); } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java index faf43c41e3..8c8ec09a91 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java @@ -21,7 +21,7 @@ import net.minecraft.client.item.TooltipData; /** - * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent + * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent. */ public class MultiTooltipData extends ArrayList implements TooltipData { public MultiTooltipData(int length) { diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index b1ba687df3..e85ae93c83 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -16,7 +16,6 @@ package net.fabricmc.fabric.mixin.client.rendering; - import java.util.Optional; import org.spongepowered.asm.mixin.Mixin; @@ -32,15 +31,17 @@ @Mixin(HandledScreen.class) class HandledScreenMixin { - @Redirect(method = "drawMouseoverTooltip",at = @At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) - Optional addMultiData(ItemStack stack){ - var original = stack.getTooltipData(); + @Redirect(method = "drawMouseoverTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) + Optional addMultiData(ItemStack stack) { + Optional original = stack.getTooltipData(); var mutlidata = new MultiTooltipData(1); original.ifPresent(mutlidata::add); - TooltipDataCallback.EVENT.invoker().getTooltipData(stack,mutlidata); - if(mutlidata.size() == 0){ + TooltipDataCallback.EVENT.invoker().getTooltipData(stack, mutlidata); + + if (mutlidata.size() == 0) { return Optional.empty(); } + return Optional.of(mutlidata); } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java index 8e8f827462..5c406f2608 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java @@ -16,7 +16,6 @@ package net.fabricmc.fabric.test.rendering.client.tooltip; - import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; @@ -32,20 +31,21 @@ public class BundleFullnessTooltipTest implements ClientModInitializer { public void onInitializeClient() { TooltipDataCallback.EVENT.register((itemStack, tooltipDataList) -> { if (itemStack.getItem() instanceof BundleItem bundle) { - tooltipDataList.add(0,new BundleCustomTooltipData(BundleItem.getAmountFilled(itemStack))); + tooltipDataList.add(0, new BundleCustomTooltipData(BundleItem.getAmountFilled(itemStack))); } }); TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof BundleCustomTooltipData bundleCustomTooltipData) + if (data instanceof BundleCustomTooltipData bundleCustomTooltipData) { return new BundleFullnessTooltipComponent(bundleCustomTooltipData.fullness); + } + return null; }); } private static class BundleCustomTooltipData implements TooltipData { private final float fullness; - - public BundleCustomTooltipData(float fullness) { + BundleCustomTooltipData(float fullness) { this.fullness = fullness; } } @@ -56,7 +56,7 @@ private static class BundleFullnessTooltipComponent implements TooltipComponent private static final int GAP = 2; private final float fullness; - public BundleFullnessTooltipComponent(float fullness) { + BundleFullnessTooltipComponent(float fullness) { this.fullness = fullness; } @@ -69,12 +69,13 @@ public int getHeight() { public int getWidth(TextRenderer textRenderer) { return BAR_WIDTH; } + @Override public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { context.getMatrices().push(); - context.getMatrices().translate(x,y,0); - context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFF3F007F); - context.fill(0,0, (int) (BAR_WIDTH*fullness),BAR_HEIGHT,0xFF7F00FF); + context.getMatrices().translate(x, y, 0); + context.fill(0, 0, BAR_WIDTH, BAR_HEIGHT, 0xFF3F007F); + context.fill(0, 0, (int) (BAR_WIDTH * fullness), BAR_HEIGHT, 0xFF7F00FF); context.getMatrices().pop(); } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java index cd26885f34..dde717c9ad 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/DurabilityTooltipTest.java @@ -16,7 +16,6 @@ package net.fabricmc.fabric.test.rendering.client.tooltip; - import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; @@ -26,7 +25,6 @@ import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; import net.fabricmc.fabric.api.client.rendering.v1.TooltipDataCallback; - public class DurabilityTooltipTest implements ClientModInitializer { @Override public void onInitializeClient() { @@ -35,9 +33,12 @@ public void onInitializeClient() { tooltipDataList.add(new DamagedItemData(itemStack.getDamage(), itemStack.getMaxDamage())); } }); + TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof DamagedItemData damagedItemData) + if (data instanceof DamagedItemData damagedItemData) { return new DurabilityModTooltipComponent(damagedItemData); + } + return null; }); } @@ -51,7 +52,7 @@ private static class DurabilityModTooltipComponent implements TooltipComponent { private static final int GAP = 2; private final DamagedItemData damage; - public DurabilityModTooltipComponent(DamagedItemData data) { + DurabilityModTooltipComponent(DamagedItemData data) { this.damage = data; } @@ -64,13 +65,14 @@ public int getHeight() { public int getWidth(TextRenderer textRenderer) { return BAR_WIDTH; } + @Override public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { context.getMatrices().push(); - context.getMatrices().translate(x,y,0); + context.getMatrices().translate(x, y, 0); float width = 1-(float) this.damage.durability / this.damage.maxDurability; - context.fill(0,0,BAR_WIDTH,BAR_HEIGHT,0xFFFF0000); - context.fill(0,0, (int) (BAR_WIDTH*width),BAR_HEIGHT,0xFF00FF00); + context.fill(0, 0, BAR_WIDTH, BAR_HEIGHT, 0xFFFF0000); + context.fill(0, 0, (int) (BAR_WIDTH * width), BAR_HEIGHT, 0xFF00FF00); context.getMatrices().pop(); } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java index 6f8e449899..6f07aeeb79 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/tooltip/FoodTooltipTest.java @@ -16,7 +16,6 @@ package net.fabricmc.fabric.test.rendering.client.tooltip; - import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; @@ -36,48 +35,52 @@ public void onInitializeClient() { tooltipDataList.add(foodData); } }); + TooltipComponentCallback.EVENT.register(data -> { - if(data instanceof FoodItemData foodItemData) + if (data instanceof FoodItemData foodItemData) { return new FoodModTooltip(foodItemData); + } + return null; }); } - private static class FoodItemData implements TooltipData { public final int hunger; - - public FoodItemData(FoodComponent foodComponent) { + FoodItemData(FoodComponent foodComponent) { this.hunger = foodComponent.getHunger(); } } private static class FoodModTooltip implements TooltipComponent { private final FoodItemData food; - private static final int SIZE=8; - private static final int GAP=2; + private static final int SIZE = 8; + private static final int GAP = 2; - public FoodModTooltip(FoodItemData foodItemData) { + FoodModTooltip(FoodItemData foodItemData) { this.food = foodItemData; } @Override public int getHeight() { - return (SIZE+GAP); + return (SIZE + GAP); } @Override public int getWidth(TextRenderer textRenderer) { - return (SIZE+GAP)*food.hunger-GAP; + return (SIZE + GAP) * food.hunger - GAP; } + @Override public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { context.getMatrices().push(); - context.getMatrices().translate(x,y,0); - for(int i=0;i Date: Mon, 6 Nov 2023 19:52:32 +0200 Subject: [PATCH 05/10] Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com> --- .../api/client/rendering/v1/TooltipComponentCallback.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java index 98b8cd7b9b..f02a599d0d 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java @@ -28,7 +28,7 @@ /** * Allows registering a mapping from {@link TooltipData} to {@link TooltipComponent}. * This allows custom tooltips for items: first, override {@link Item#getTooltipData} and return a custom {@code TooltipData}, or use - * {@link TooltipDataCallback} to add {@code TooltipData} to an existing item + * {@link TooltipDataCallback} to add {@code TooltipData} to an existing item. * Second, register a listener to this event and convert the data to your component implementation if it's an instance of your data class. * *

Note that failure to map some data to a component will throw an exception, From 24ede0ab399582c6430f5f9dc1722cd2422070c3 Mon Sep 17 00:00:00 2001 From: JumperOnJava <59884606+JumperOnJava@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:52:49 +0200 Subject: [PATCH 06/10] Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com> --- .../fabric/api/client/rendering/v1/TooltipDataCallback.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index d6fd52f891..0b7943a5fd 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -29,8 +29,9 @@ public interface TooltipDataCallback { /** * Allows registering custom {@link TooltipData} object for item. * This allows you to add your own tooltips to existing items. - * Custom {@code TooltipData} should be registered using {@link TooltipComponentCallback}, - * otherwise game will crash when trying to map {@code TooltipData} to {@code TooltipComponent} + * + *

Custom {@link TooltipData} should be registered using {@link TooltipComponentCallback}, + * otherwise game will crash when trying to map {@link TooltipData} to {@link TooltipComponent}. */ Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { for (TooltipDataCallback callback : callbacks) { From 3d739ac435a175e98d19a724d10dea4a7379f069 Mon Sep 17 00:00:00 2001 From: ipz233_vvv Date: Mon, 6 Nov 2023 21:02:19 +0200 Subject: [PATCH 07/10] fixed issues in code found by @Juuxel --- .../v1/TooltipComponentCallback.java | 2 +- .../rendering/v1/TooltipDataCallback.java | 23 +++++++++++-------- .../tooltip/MultiTooltipComponent.java | 21 ++++++++--------- .../MultiTooltipComponentRegister.java | 2 +- .../rendering/tooltip/MultiTooltipData.java | 8 +++---- .../client/rendering/HandledScreenMixin.java | 14 ++++++----- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java index f02a599d0d..ee2786d205 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipComponentCallback.java @@ -28,7 +28,7 @@ /** * Allows registering a mapping from {@link TooltipData} to {@link TooltipComponent}. * This allows custom tooltips for items: first, override {@link Item#getTooltipData} and return a custom {@code TooltipData}, or use - * {@link TooltipDataCallback} to add {@code TooltipData} to an existing item. + * {@link TooltipDataCallback} to add {@link TooltipData} to an existing item. * Second, register a listener to this event and convert the data to your component implementation if it's an instance of your data class. * *

Note that failure to map some data to a component will throw an exception, diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index 0b7943a5fd..1e38aab633 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -18,25 +18,30 @@ import java.util.List; +import net.minecraft.client.gui.tooltip.TooltipComponent; import net.minecraft.client.item.TooltipData; import net.minecraft.item.ItemStack; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +/** + * Allows registering custom {@link TooltipData} object for item. + * This allows you to add your own tooltips to existing items. + * + *

Custom {@link TooltipData} should be registered using {@link TooltipComponentCallback}, + * otherwise game will crash when trying to map {@link TooltipData} to {@link TooltipComponent}. + */ @FunctionalInterface public interface TooltipDataCallback { - /** - * Allows registering custom {@link TooltipData} object for item. - * This allows you to add your own tooltips to existing items. - * - *

Custom {@link TooltipData} should be registered using {@link TooltipComponentCallback}, - * otherwise game will crash when trying to map {@link TooltipData} to {@link TooltipComponent}. - */ Event EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { for (TooltipDataCallback callback : callbacks) { - callback.getTooltipData(itemStack, tooltipDataList); + callback.appendTooltipData(itemStack, tooltipDataList); } }); - void getTooltipData(ItemStack itemStack, List tooltipDataList); + + /** + * Add your own {@link TooltipData} to passed list if itemStack matches your requirements + */ + void appendTooltipData(ItemStack itemStack, List tooltipDataList); } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java index e0ddb55518..7d37ed3298 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java @@ -3,7 +3,7 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * You may obtain a matrixCopy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -36,7 +36,7 @@ public class MultiTooltipComponent implements TooltipComponent { private final int width; private final List components; - public static MultiTooltipComponent of(MultiTooltipData data) { + public static MultiTooltipComponent of(List data) { var l = new ArrayList(data.size()); for (TooltipData d : data) { @@ -72,26 +72,23 @@ public int getWidth(TextRenderer textRenderer) { @Override public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) { - int position = 0; + Matrix4f matrixCopy = new Matrix4f(matrix); for (TooltipComponent c : components) { - matrix.translate(0, position, 0); - c.drawText(textRenderer, x, y, matrix, vertexConsumers); - matrix.translate(0, -position, 0); - position += c.getHeight(); + c.drawText(textRenderer, x, y, matrixCopy, vertexConsumers); + matrixCopy.translate(0, c.getHeight(), 0); } } @Override public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { - int position = 0; + context.getMatrices().push(); for (TooltipComponent c : components) { - context.getMatrices().push(); - context.getMatrices().translate(0, position, 0); c.drawItems(textRenderer, x, y, context); - context.getMatrices().pop(); - position += c.getHeight(); + context.getMatrices().translate(0, c.getHeight(), 0); } + + context.getMatrices().pop(); } } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java index c6816fa3cc..3828331423 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java @@ -24,7 +24,7 @@ public class MultiTooltipComponentRegister implements ClientModInitializer { public void onInitializeClient() { TooltipComponentCallback.EVENT.register((tooltipData) -> { if (tooltipData instanceof MultiTooltipData multiTooltipData) { - return MultiTooltipComponent.of(multiTooltipData); + return MultiTooltipComponent.of(multiTooltipData.tooltipData()); } return null; diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java index 8c8ec09a91..f544417abf 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java @@ -17,14 +17,14 @@ package net.fabricmc.fabric.impl.client.rendering.tooltip; import java.util.ArrayList; +import java.util.List; import net.minecraft.client.item.TooltipData; +import javax.tools.Tool; + /** * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent. */ -public class MultiTooltipData extends ArrayList implements TooltipData { - public MultiTooltipData(int length) { - super(length); - } +public record MultiTooltipData(List tooltipData) implements TooltipData { } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index e85ae93c83..739a64ad2e 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -16,10 +16,12 @@ package net.fabricmc.fabric.mixin.client.rendering; +import java.util.ArrayList; import java.util.Optional; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.Redirect; import net.minecraft.client.gui.screen.ingame.HandledScreen; @@ -34,14 +36,14 @@ class HandledScreenMixin { @Redirect(method = "drawMouseoverTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) Optional addMultiData(ItemStack stack) { Optional original = stack.getTooltipData(); - var mutlidata = new MultiTooltipData(1); - original.ifPresent(mutlidata::add); - TooltipDataCallback.EVENT.invoker().getTooltipData(stack, mutlidata); + var multidata = new MultiTooltipData(new ArrayList<>()); + original.ifPresent(multidata.tooltipData()::add); + TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multidata.tooltipData()); - if (mutlidata.size() == 0) { - return Optional.empty(); + if (multidata.tooltipData().size() <= 1) { + return original; } - return Optional.of(mutlidata); + return Optional.of(multidata); } } From 7f1f9bd862dcabb2127114a806067902871d7ee3 Mon Sep 17 00:00:00 2001 From: ipz233_vvv Date: Mon, 6 Nov 2023 21:07:10 +0200 Subject: [PATCH 08/10] Style fixes --- .../api/client/rendering/v1/TooltipDataCallback.java | 2 +- .../rendering/tooltip/MultiTooltipComponent.java | 2 +- .../client/rendering/tooltip/MultiTooltipData.java | 3 --- .../mixin/client/rendering/HandledScreenMixin.java | 11 +++++------ 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java index 1e38aab633..eef5298fbc 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java @@ -41,7 +41,7 @@ public interface TooltipDataCallback { }); /** - * Add your own {@link TooltipData} to passed list if itemStack matches your requirements + * Add your own {@link TooltipData} to passed list if itemStack matches your requirements. */ void appendTooltipData(ItemStack itemStack, List tooltipDataList); } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java index 7d37ed3298..aeef327f50 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java @@ -3,7 +3,7 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. - * You may obtain a matrixCopy of the License at + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java index f544417abf..9e98cea7bd 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java @@ -16,13 +16,10 @@ package net.fabricmc.fabric.impl.client.rendering.tooltip; -import java.util.ArrayList; import java.util.List; import net.minecraft.client.item.TooltipData; -import javax.tools.Tool; - /** * This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent. */ diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index 739a64ad2e..417b0d7cc4 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -21,7 +21,6 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.Redirect; import net.minecraft.client.gui.screen.ingame.HandledScreen; @@ -36,14 +35,14 @@ class HandledScreenMixin { @Redirect(method = "drawMouseoverTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) Optional addMultiData(ItemStack stack) { Optional original = stack.getTooltipData(); - var multidata = new MultiTooltipData(new ArrayList<>()); - original.ifPresent(multidata.tooltipData()::add); - TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multidata.tooltipData()); + var multiData = new MultiTooltipData(new ArrayList<>()); + original.ifPresent(multiData.tooltipData()::add); + TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multiData.tooltipData()); - if (multidata.tooltipData().size() <= 1) { + if (multiData.tooltipData().size() <= 1) { return original; } - return Optional.of(multidata); + return Optional.of(multiData); } } From 809fd25b837812c247d44de13ba78b8f8d35881e Mon Sep 17 00:00:00 2001 From: ipz233_vvv Date: Mon, 6 Nov 2023 21:20:29 +0200 Subject: [PATCH 09/10] fixed tooltip data not adding It happened when only one tooltip data is added by event handlers I for some reason assumed that there is always original data in item --- .../fabric/mixin/client/rendering/HandledScreenMixin.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index 417b0d7cc4..86901c7471 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -39,10 +39,14 @@ Optional addMultiData(ItemStack stack) { original.ifPresent(multiData.tooltipData()::add); TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multiData.tooltipData()); - if (multiData.tooltipData().size() <= 1) { + if (multiData.tooltipData().size() == 0) { return original; } + if (multiData.tooltipData().size() == 1){ + return Optional.of(multiData.tooltipData().get(0)); + } + return Optional.of(multiData); } } From 9414fb1fe2a0bcd7b9567882c063ebf758ac33fe Mon Sep 17 00:00:00 2001 From: JumperOnJava <59884606+JumperOnJava@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:37:03 +0200 Subject: [PATCH 10/10] Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com> --- .../fabric/mixin/client/rendering/HandledScreenMixin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java index 86901c7471..b2c9159ea8 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java @@ -39,7 +39,7 @@ Optional addMultiData(ItemStack stack) { original.ifPresent(multiData.tooltipData()::add); TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multiData.tooltipData()); - if (multiData.tooltipData().size() == 0) { + if (multiData.tooltipData().isEmpty()) { return original; }