Skip to content

Commit

Permalink
Remove chainAlign and adjust all possible usages to use .align()
Browse files Browse the repository at this point in the history
  • Loading branch information
Emirlol committed Oct 22, 2024
1 parent b0429e8 commit 5a22f86
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 97 deletions.
25 changes: 23 additions & 2 deletions src/main/java/de/hysky/skyblocker/injected/AlignedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@

public interface AlignedText {
/**
* <h3>
* Aligned Text
* </h3>
* <p>
* This method is used to display text at a certain x offset after the current text in tooltips.
* This allows for aligned text when used on multiple rows.
* This allows for aligned text when used on multiple rows with the same offset.
* </p>
* <p>
* This method can be chained to achieve a grid-like layout in tooltips.
* </p>
* <h3>
* Styling
* </h3>
* <p>
* The way styling applies to aligned text is slightly different from normal text, where the styling of the parent text is applied to children as well
* (which causes almost all uses of text with formatting to be appended on an empty parent text when there is more than 1 style in the same line).
* </p>
* <p>
* For aligned text, each node has their own formatting and there is no style inheritance between them.
* </p>
* <p>
* However, each aligned text node can still have their own children elements like normal text,
* where the children will inherit the style of the parent and their text content will be appended to the parent.
* </p>
*
* @param text The text to render after this text
* @param xOffset The x offset to apply to the given {@code text},
* relative to the start of the text object this method is called upon.
* relative to the start of the text object this method is called upon.
* @return The {@code text} object passed in, for chaining purposes
* @see AlignedTooltipComponent
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public abstract class DrawContextMixin {
List<TooltipComponent> result = new ArrayList<>();

for (Text text : list) {
if (text instanceof MutableText mutableText && mutableText.getAlignedText() != null) result.add(new AlignedTooltipComponent(mutableText));
else result.add(new OrderedTextTooltipComponent(text.asOrderedText()));
if (text instanceof MutableText mutableText) {
MutableText firstOfChain = mutableText.getFirstOfChain();
if (firstOfChain != null) result.add(new AlignedTooltipComponent(firstOfChain));
else if (mutableText.getAlignedText() != null) result.add(new AlignedTooltipComponent(mutableText));
else result.add(new OrderedTextTooltipComponent(mutableText.asOrderedText()));
} else result.add(new OrderedTextTooltipComponent(text.asOrderedText()));
}

return (R) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.hysky.skyblocker.utils.scheduler.Scheduler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.slf4j.Logger;
Expand Down Expand Up @@ -43,11 +44,11 @@ public static void nullWarning() {
}
}

public static Text getCoinsMessage(double price, int count) {
public static MutableText getCoinsMessage(double price, int count) {
return getCoinsMessage(price, count, false);
}

public static Text getCoinsMessage(double price, int count, boolean preCounted) {
public static MutableText getCoinsMessage(double price, int count, boolean preCounted) {
// Format the price string once
String priceString = String.format(Locale.ENGLISH, "%1$,.1f", preCounted ? price / count : price);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import it.unimi.dsi.fastutil.Pair;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;
Expand All @@ -25,20 +24,18 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>
Pair<AccessoriesHelper.AccessoryReport, String> report = AccessoriesHelper.calculateReport4Accessory(internalID);

if (report.left() != AccessoriesHelper.AccessoryReport.INELIGIBLE) {
MutableText title = Text.literal(String.format("%-19s", "Accessory: ")).withColor(0xf57542);

Text stateText = switch (report.left()) {
case HAS_HIGHEST_TIER -> Text.literal("✔ Collected").formatted(Formatting.GREEN);
case IS_GREATER_TIER -> Text.literal("✦ Upgrade ").withColor(0x218bff).append(Text.literal(report.right()).withColor(0xf8f8ff));
case HAS_GREATER_TIER -> Text.literal("↑ Upgradable ").withColor(0xf8d048).append(Text.literal(report.right()).withColor(0xf8f8ff));
case OWNS_BETTER_TIER -> Text.literal("↓ Downgrade ").formatted(Formatting.GRAY).append(Text.literal(report.right()).withColor(0xf8f8ff));
case MISSING -> Text.literal("✖ Missing ").formatted(Formatting.RED).append(Text.literal(report.right()).withColor(0xf8f8ff));

//Should never be the case
default -> Text.literal("? Unknown").formatted(Formatting.GRAY);
};

lines.add(title.append(stateText));
lines.add(Text.literal("Accessory:").withColor(0xf57542).align(
switch (report.left()) {
case HAS_HIGHEST_TIER -> Text.literal("✔ Collected").formatted(Formatting.GREEN);
case IS_GREATER_TIER -> Text.literal("✦ Upgrade ").withColor(0x218bff).append(Text.literal(report.right()).withColor(0xf8f8ff));
case HAS_GREATER_TIER -> Text.literal("↑ Upgradable ").withColor(0xf8d048).append(Text.literal(report.right()).withColor(0xf8f8ff));
case OWNS_BETTER_TIER -> Text.literal("↓ Downgrade ").formatted(Formatting.GRAY).append(Text.literal(report.right()).withColor(0xf8f8ff));
case MISSING -> Text.literal("✖ Missing ").formatted(Formatting.RED).append(Text.literal(report.right()).withColor(0xf8f8ff));

//Should never be the case
default -> Text.literal("? Unknown").formatted(Formatting.GRAY);
}, 100));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>
String neuName = stack.getNeuName();
Average type = ItemTooltip.config.avg;

if ((TooltipInfoType.ONE_DAY_AVERAGE.getData() == null && type != Average.THREE_DAY) || (TooltipInfoType.THREE_DAY_AVERAGE.getData() == null && type != Average.ONE_DAY)) {
if ((TooltipInfoType.ONE_DAY_AVERAGE.getData() == null && type != Average.THREE_DAY) || (TooltipInfoType.THREE_DAY_AVERAGE.getData() == null && type != Average.ONE_DAY)) {
ItemTooltip.nullWarning();
} else {
/*
Expand All @@ -35,21 +35,24 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>
// "No data" line because of API not keeping old data, it causes NullPointerException
if (type == Average.ONE_DAY || type == Average.BOTH) {
lines.add(
Text.literal(String.format("%-19s", "1 Day Avg. Price:"))
Text.literal("1 Day Avg. Price:")
.formatted(Formatting.GOLD)
.append(!TooltipInfoType.ONE_DAY_AVERAGE.getData().containsKey(neuName)
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(TooltipInfoType.ONE_DAY_AVERAGE.getData().getDouble(neuName), stack.getCount())

.align(TooltipInfoType.ONE_DAY_AVERAGE.getData().containsKey(neuName)
? ItemTooltip.getCoinsMessage(TooltipInfoType.ONE_DAY_AVERAGE.getData().getDouble(neuName), stack.getCount())
: Text.literal("No data").formatted(Formatting.RED),
100
)
);
}
if (type == Average.THREE_DAY || type == Average.BOTH) {
lines.add(
Text.literal(String.format("%-19s", "3 Day Avg. Price:"))
.formatted(Formatting.GOLD)
.append(!TooltipInfoType.THREE_DAY_AVERAGE.getData().containsKey(neuName)
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(TooltipInfoType.THREE_DAY_AVERAGE.getData().getDouble(neuName), stack.getCount())
.align(TooltipInfoType.THREE_DAY_AVERAGE.getData().containsKey(neuName)
? ItemTooltip.getCoinsMessage(TooltipInfoType.THREE_DAY_AVERAGE.getData().getDouble(neuName), stack.getCount())
: Text.literal("No data").formatted(Formatting.RED),
100
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,39 @@
import java.util.List;

public class BazaarPriceTooltip extends SimpleTooltipAdder {
public BazaarPriceTooltip(int priority) {
public BazaarPriceTooltip(int priority) {
super(priority);
}

@Override
public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines) {
String skyblockApiId = stack.getSkyblockApiId();
String skyblockApiId = stack.getSkyblockApiId();
if (!TooltipInfoType.BAZAAR.hasOrNullWarning(skyblockApiId)) return;

if (TooltipInfoType.BAZAAR.hasOrNullWarning(skyblockApiId)) {
int count;
if (lines.size() >= 4 && lines.get(3).getSiblings().size() >= 2 && lines.get(1).getString().endsWith("Sack")) {
//The count is in the 2nd sibling of the 3rd line of the lore. here V
//Example line: empty[style={color=dark_purple,!italic}, siblings=[literal{Stored: }[style={color=gray}], literal{0}[style={color=dark_gray}], literal{/20k}[style={color=gray}]]
String line = lines.get(3).getSiblings().get(1).getString().replace(",", "");
count = NumberUtils.isParsable(line) && !line.equals("0") ? Integer.parseInt(line) : stack.getCount();
} else {
count = stack.getCount();
}
BazaarProduct product = TooltipInfoType.BAZAAR.getData().get(skyblockApiId);
lines.add(Text.literal(String.format("%-18s", "Bazaar buy Price:"))
.formatted(Formatting.GOLD)
.append(product.buyPrice().isEmpty()
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(product.buyPrice().getAsDouble(), count)));
lines.add(Text.literal(String.format("%-19s", "Bazaar sell Price:"))
.formatted(Formatting.GOLD)
.append(product.sellPrice().isEmpty()
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(product.sellPrice().getAsDouble(), count)));
int count;
if (lines.size() >= 4 && lines.get(3).getSiblings().size() >= 2 && lines.get(1).getString().endsWith("Sack")) {
//The count is in the 2nd sibling of the 3rd line of the lore. here V
//Example line: empty[style={color=dark_purple,!italic}, siblings=[literal{Stored: }[style={color=gray}], literal{0}[style={color=dark_gray}], literal{/20k}[style={color=gray}]]
String line = lines.get(3).getSiblings().get(1).getString().replace(",", "");
count = NumberUtils.isParsable(line) && !line.equals("0") ? Integer.parseInt(line) : stack.getCount();
} else {
count = stack.getCount();
}

@SuppressWarnings("DataFlowIssue") //The existence of the data is already checked via hasOrNullWarning, so the data is guaranteed to be present
BazaarProduct product = TooltipInfoType.BAZAAR.getData().get(skyblockApiId);
lines.add(Text.literal("Bazaar buy Price:")
.formatted(Formatting.GOLD)
.align(product.buyPrice().isEmpty()
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(product.buyPrice().getAsDouble(), count),
100));
lines.add(Text.literal("Bazaar sell Price:")
.formatted(Formatting.GOLD)
.align(product.sellPrice().isEmpty()
? Text.literal("No data").formatted(Formatting.RED)
: ItemTooltip.getCoinsMessage(product.sellPrice().getAsDouble(), count),
100));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public void addToTooltip(@Nullable Slot focusedSloFt, ItemStack stack, List<Text
} else amountInStack = stack.getCount();

neuRecipes.getFirst().getAllOutputs().stream().findFirst().ifPresent(outputIngredient ->
lines.add(Text.literal(String.format("%-20s", "Crafting Price:")).formatted(Formatting.GOLD)
.append(ItemTooltip.getCoinsMessage(totalCraftCost / outputIngredient.getAmount(), amountInStack))));
lines.add(Text.literal("Crafting Price:")
.formatted(Formatting.GOLD)
.align(ItemTooltip.getCoinsMessage(totalCraftCost / outputIngredient.getAmount(), amountInStack), 100)));

} catch (Exception e) {
LOGGER.error("[Skyblocker Craft Price] Error calculating craftprice tooltip for: " + stack.getNeuName(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ public DungeonQualityTooltip(int priority) {
@Override
public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines) {
NbtCompound customData = ItemUtils.getCustomData(stack);
if (customData == null || !customData.contains("baseStatBoostPercentage")) return;
if (customData.isEmpty() || !customData.contains("baseStatBoostPercentage")) return;
int baseStatBoostPercentage = customData.getInt("baseStatBoostPercentage");
boolean maxQuality = baseStatBoostPercentage == 50;
if (maxQuality) {
lines.add(Text.literal(String.format("%-17s", "Item Quality:") + baseStatBoostPercentage + "/50").formatted(Formatting.RED).formatted(Formatting.BOLD));
} else {
lines.add(Text.literal(String.format("%-21s", "Item Quality:") + baseStatBoostPercentage + "/50").formatted(Formatting.BLUE));
}
lines.add(Text.literal("Item Quality:").formatted(Formatting.BLUE)
.align(maxQuality
? Text.literal(baseStatBoostPercentage + "/50")
.formatted(Formatting.RED, Formatting.BOLD)
: Text.literal(baseStatBoostPercentage + "/50")
.formatted(Formatting.BLUE),
100));

if (customData.contains("item_tier")) { // sometimes it just isn't here?
int itemTier = customData.getInt("item_tier");
if (maxQuality) {
lines.add(Text.literal(String.format("%-17s", "Floor Tier:") + itemTier + " (" + getItemTierFloor(itemTier) + ")").formatted(Formatting.RED).formatted(Formatting.BOLD));
} else {
lines.add(Text.literal(String.format("%-21s", "Floor Tier:") + itemTier + " (" + getItemTierFloor(itemTier) + ")").formatted(Formatting.BLUE));
}
lines.add(Text.literal("Floor Tier:").formatted(Formatting.BLUE)
.align(maxQuality
? Text.literal(itemTier + " (" + getItemTierFloor(itemTier) + ")")
.formatted(Formatting.RED, Formatting.BOLD)
: Text.literal(itemTier + " (" + getItemTierFloor(itemTier) + ")")
.formatted(Formatting.BLUE),
100));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>
if (priceData == 0) return; //Default value for getLong is 0 if no value exists for that key

lines.add(Text.empty()
.append(Text.literal("Essence Cost: ").formatted(Formatting.AQUA))
.append(Text.literal(DECIMAL_FORMAT.format(priceData * cost.getAsLong()) + " coins").formatted(Formatting.DARK_AQUA))
.append(Text.literal("Essence Cost:").formatted(Formatting.AQUA))
.align(Text.literal(DECIMAL_FORMAT.format(priceData * cost.getAsLong()) + " coins").formatted(Formatting.DARK_AQUA), 100)
.append(Text.literal(" (").formatted(Formatting.GRAY))
.append(Text.literal(DECIMAL_FORMAT.format(priceData) + " each").formatted(Formatting.GRAY))
.append(Text.literal(")").formatted(Formatting.GRAY))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package de.hysky.skyblocker.skyblock.item.tooltip.adders;

import java.util.List;

import org.jetbrains.annotations.Nullable;

import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.item.tooltip.SimpleTooltipAdder;
import de.hysky.skyblocker.skyblock.item.tooltip.info.TooltipInfoType;
Expand All @@ -13,6 +9,9 @@
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class EstimatedItemValueTooltip extends SimpleTooltipAdder {

Expand All @@ -27,7 +26,7 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>
if (result.price() > 0) {
lines.add(Text.literal(String.format("%-20s", "Est. Item Value:"))
.formatted(Formatting.GOLD)
.append(ItemTooltip.getCoinsMessage(result.price(), stack.getCount(), true)));
.align(ItemTooltip.getCoinsMessage(result.price(), stack.getCount(), true), 100));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text>

// Check for whether the item exist in bazaar price data, because Skytils keeps some bazaar item data in lbin api
if (TooltipInfoType.LOWEST_BINS.hasOrNullWarning(skyblockApiId) && !TooltipInfoType.BAZAAR.hasOrNullWarning(skyblockApiId)) {
lines.add(Text.literal(String.format("%-19s", "Lowest BIN Price:"))
lines.add(Text.literal("Lowest BIN Price:")
.formatted(Formatting.GOLD)
.append(ItemTooltip.getCoinsMessage(TooltipInfoType.LOWEST_BINS.getData().getDouble(skyblockApiId), stack.getCount())));
.align(ItemTooltip.getCoinsMessage(TooltipInfoType.LOWEST_BINS.getData().getDouble(skyblockApiId), stack.getCount()),
100));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public MotesTooltip(int priority) {
public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines) {
final String internalID = stack.getSkyblockId();
if (TooltipInfoType.MOTES.hasOrNullWarning(internalID)) {
lines.add(Text.literal(String.format("%-20s", "Motes Price:"))
//noinspection DataFlowIssue --- The existence of the data is already checked via hasOrNullWarning, so the data is guaranteed to be present
lines.add(Text.literal("Motes Price:")
.formatted(Formatting.LIGHT_PURPLE)
.append(getMotesMessage(TooltipInfoType.MOTES.getData().getInt(internalID), stack.getCount())));
.align(getMotesMessage(TooltipInfoType.MOTES.getData().getInt(internalID), stack.getCount()), 100));
}
}

Expand All @@ -33,7 +34,7 @@ public boolean isEnabled() {
return TooltipInfoType.MOTES.isTooltipEnabled();
}

private static Text getMotesMessage(int price, int count) {
private static MutableText getMotesMessage(int price, int count) {
float motesMultiplier = SkyblockerConfigManager.get().otherLocations.rift.mcGrubberStacks * 0.05f + 1;

// Calculate the total price
Expand Down
Loading

0 comments on commit 5a22f86

Please sign in to comment.