-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TooltipDataCallback (copy of #3403 using Mixin Extras) #3486
Open
JumperOnJava
wants to merge
13
commits into
FabricMC:1.20.4
Choose a base branch
from
JumperOnJava:1.20.4
base: 1.20.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cb3c096
MultiTooltipApi
JumperOnJava bf0290d
updated javadocs
JumperOnJava 044d4d7
added license headers
JumperOnJava d9aa023
fixed code style
JumperOnJava ac5ec72
Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/cl…
JumperOnJava e2b54bb
Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/cl…
JumperOnJava aa3710c
fixed issues in code found by @Juuxel
JumperOnJava 98e0a4e
Style fixes
JumperOnJava 03818a1
fixed tooltip data not adding
JumperOnJava 5af4c67
Update fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/…
JumperOnJava f02dacc
ported to use ME's WrapOperation
JumperOnJava 92a62af
fixed style
JumperOnJava 219656f
Update build.gradle
JumperOnJava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...g-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/TooltipDataCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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; | ||
|
||
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. | ||
* | ||
* <p>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 { | ||
Event<TooltipDataCallback> EVENT = EventFactory.createArrayBacked(TooltipDataCallback.class, callbacks -> (itemStack, tooltipDataList) -> { | ||
for (TooltipDataCallback callback : callbacks) { | ||
callback.appendTooltipData(itemStack, tooltipDataList); | ||
} | ||
}); | ||
|
||
/** | ||
* Add your own {@link TooltipData} to passed list if itemStack matches your requirements. | ||
*/ | ||
void appendTooltipData(ItemStack itemStack, List<TooltipData> tooltipDataList); | ||
} |
94 changes: 94 additions & 0 deletions
94
.../client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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; | ||
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.item.TooltipData; | ||
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<TooltipComponent> components; | ||
|
||
public static MultiTooltipComponent of(List<TooltipData> data) { | ||
var l = new ArrayList<TooltipComponent>(data.size()); | ||
|
||
for (TooltipData d : data) { | ||
l.add(TooltipComponent.of(d)); | ||
} | ||
|
||
return new MultiTooltipComponent(l); | ||
} | ||
|
||
public MultiTooltipComponent(List<TooltipComponent> 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) { | ||
Matrix4f matrixCopy = new Matrix4f(matrix); | ||
|
||
for (TooltipComponent c : components) { | ||
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) { | ||
context.getMatrices().push(); | ||
|
||
for (TooltipComponent c : components) { | ||
c.drawItems(textRenderer, x, y, context); | ||
context.getMatrices().translate(0, c.getHeight(), 0); | ||
} | ||
|
||
context.getMatrices().pop(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipComponentRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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; | ||
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.tooltipData()); | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...1/src/client/java/net/fabricmc/fabric/impl/client/rendering/tooltip/MultiTooltipData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.List; | ||
|
||
import net.minecraft.client.item.TooltipData; | ||
|
||
/** | ||
* This class stores multiple TooltipData object to their further mapping to MultiTooltipComponent. | ||
*/ | ||
public record MultiTooltipData(List<TooltipData> tooltipData) implements TooltipData { | ||
} |
48 changes: 48 additions & 0 deletions
48
...ing-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/HandledScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
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 { | ||
@WrapOperation(method = "drawMouseoverTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getTooltipData()Ljava/util/Optional;")) | ||
private Optional<TooltipData> addMultiData(ItemStack stack, Operation<Optional<TooltipData>> original) { | ||
var multiData = new MultiTooltipData(new ArrayList<>()); | ||
TooltipDataCallback.EVENT.invoker().appendTooltipData(stack, multiData.tooltipData()); | ||
|
||
if (multiData.tooltipData().isEmpty()) { | ||
return original.call(stack); | ||
} | ||
|
||
original.call(stack).ifPresent(multiData.tooltipData()::add); | ||
return Optional.of(multiData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ent/java/net/fabricmc/fabric/test/rendering/client/tooltip/BundleFullnessTooltipTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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.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; | ||
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; | ||
}); | ||
} | ||
|
||
private static class BundleCustomTooltipData implements TooltipData { | ||
private final float fullness; | ||
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; | ||
|
||
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behaves diffrently to the other PR. Whats the expected behaviour?