From 58109ad63a5e2db7c71a98a960c67e47261cf8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=82=A6=E8=A7=A3=E8=AF=B4?= Date: Fri, 20 Sep 2024 23:58:47 +0800 Subject: [PATCH] HoldShiftTooltip --- .../mod/block/BlockMetalDetectionDoor.java | 5 + .../ziyue/tjmetro/mod/data/IGuiExtension.java | 103 +++++++++++++++++- .../resources/assets/tjmetro/lang/en_us.json | 2 +- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/fabric/src/main/java/ziyue/tjmetro/mod/block/BlockMetalDetectionDoor.java b/fabric/src/main/java/ziyue/tjmetro/mod/block/BlockMetalDetectionDoor.java index 45e23c6..7ddc254 100644 --- a/fabric/src/main/java/ziyue/tjmetro/mod/block/BlockMetalDetectionDoor.java +++ b/fabric/src/main/java/ziyue/tjmetro/mod/block/BlockMetalDetectionDoor.java @@ -8,6 +8,7 @@ import org.mtr.mod.block.IBlock; import ziyue.tjmetro.mod.BlockList; import ziyue.tjmetro.mod.TianjinMetro; +import ziyue.tjmetro.mod.data.IGuiExtension; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -90,6 +91,10 @@ public void onBreak2(World world, BlockPos pos, BlockState state, PlayerEntity p super.onBreak2(world, pos, state, player); } + @Override + public void addTooltips(ItemStack stack, @Nullable BlockView world, List tooltip, TooltipContext options) { + IGuiExtension.addHoldShiftTooltip(tooltip, TextHelper.translatable("tooltip.tjmetro.metal_detection_door")); + } @Override public void addBlockProperties(List> properties) { properties.add(FACING); diff --git a/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java b/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java index a80e00a..6abebd9 100644 --- a/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java +++ b/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java @@ -1,9 +1,14 @@ package ziyue.tjmetro.mod.data; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.mtr.mapping.holder.MutableText; +import org.mtr.mapping.holder.Screen; +import org.mtr.mapping.holder.TextFormatting; +import org.mtr.mapping.mapper.GraphicsHolder; import org.mtr.mapping.mapper.TextHelper; import org.mtr.mod.config.Config; import org.mtr.mod.data.IGui; +import org.mtr.mod.generated.lang.TranslationProvider; import javax.annotation.Nullable; import java.util.ArrayList; @@ -80,10 +85,13 @@ static ImmutablePair splitTranslation(String text) { return new ImmutablePair<>(text.substring(0, separatorIndex), text.substring(separatorIndex + 1)); } - static String insertTranslation(String keyCJK, String key, int expectedArguments, String... arguments) { - return insertTranslation(keyCJK, key, null, expectedArguments, arguments); - } - + /** + * Formatting the string. + * + * @author ZiYueCommentary + * @see IGui#insertTranslation(TranslationProvider.TranslationHolder, TranslationProvider.TranslationHolder, String, int, String...) + * @since 1.0.0-beta-2 + */ static String insertTranslation(String keyCJK, String key, @Nullable String overrideFirst, int expectedArguments, String... arguments) { if (arguments.length < expectedArguments) { return ""; @@ -147,4 +155,91 @@ static String insertTranslation(String keyCJK, String key, @Nullable String over return ""; } } + + /** + * @author ZiYueCommentary + * @see #insertTranslation(String, String, String, int, String...) + * @since 1.0.0-beta-2 + */ + static String insertTranslation(String keyCJK, String key, int expectedArguments, String... arguments) { + return insertTranslation(keyCJK, key, null, expectedArguments, arguments); + } + + /** + * Adding a hold-shift-tooltip to the tooltips. + * + * @param list hover Text List, just like pointer in C/C++ + * @param component a component that waits for split + * @param regex the delimiting regular expression + * @param limit the result threshold, as described above + * @return hover text lists + * @author ZiYueCommentary + * @since 1.0.0-beta-2 + */ + static List addHoldShiftTooltip(List list, MutableText component, boolean wordWarp, String regex, int limit) { + if (Screen.hasShiftDown()) { + // Adding a space in the end is the simplest way to fix the bug. Do not ask me why. + String[] texts = (component.getString() + " ").split(regex, limit); + if (wordWarp) { + for (String text : texts) { + int start = 0; + while (start < text.length()) { + int end = start; + int currentWidth = 0; + int lastSpace = -1; + + while (end < text.length() && currentWidth + GraphicsHolder.getTextWidth(text.substring(start, end + 1)) <= 380) { // 380 is the maximum width + if (text.charAt(end) == ' ') { + lastSpace = end; + } + currentWidth += GraphicsHolder.getTextWidth(text.substring(end, end + 1)); + end++; + } + + if (lastSpace != -1 && lastSpace > start) { + list.add(TextHelper.literal(text.substring(start, lastSpace))); + start = lastSpace + 1; + } else { + list.add(TextHelper.literal(text.substring(start, end))); + start = end; + } + } + } + } else { + for (String text : texts) { + list.add(TextHelper.literal(text)); + } + } + } else { + list.add(TextHelper.translatable("tooltip.tjmetro.shift").formatted(TextFormatting.YELLOW)); + } + return list; + } + + /** + * @author ZiYueCommentary + * @see #addHoldShiftTooltip(List, MutableText, boolean, String, int) + * @since 1.0.0-beta-2 + */ + static List addHoldShiftTooltip(List list, MutableText component, boolean wordWarp, String regex) { + return addHoldShiftTooltip(list, component, wordWarp, regex, 0); + } + + /** + * @author ZiYueCommentary + * @see #addHoldShiftTooltip(List, MutableText, boolean, String, int) + * @since 1.0.0-beta-2 + */ + static List addHoldShiftTooltip(List list, MutableText component, boolean wordWarp) { + return addHoldShiftTooltip(list, component, wordWarp, "\n", 0); + } + + /** + * @author ZiYueCommentary + * @see #addHoldShiftTooltip(List, MutableText, boolean, String, int) + * @since 1.0.0-beta-2 + */ + static List addHoldShiftTooltip(List list, MutableText component) { + return addHoldShiftTooltip(list, component, true, "\n", 0); + } } diff --git a/fabric/src/main/resources/assets/tjmetro/lang/en_us.json b/fabric/src/main/resources/assets/tjmetro/lang/en_us.json index 73e0385..c7ec5ec 100644 --- a/fabric/src/main/resources/assets/tjmetro/lang/en_us.json +++ b/fabric/src/main/resources/assets/tjmetro/lang/en_us.json @@ -106,7 +106,7 @@ "tooltip.tjmetro.custom_content": "Support custom display content", "tooltip.tjmetro.custom_color": "Support custom display color", "tooltip.tjmetro.station_name_sign": "A small station name sign, you can change display content with wrench.", - "tooltip.tjmetro.metal_detection_door": "A device for clearing items from players' inventory. Note this device will deleting item directly, instead of taking out item.", + "tooltip.tjmetro.metal_detection_door": "A device for clearing items from players' inventory. Note this device will delete item directly, instead of taking out items.", "tooltip.tjmetro.use_tianjin_metro_font": "Render texts with mod font in some blocks.", "tooltip.tjmetro.shift": "Hold SHIFT for more info",