Skip to content

Commit

Permalink
Merge pull request #750 from kevinthegreat1/potion-level-slot-text
Browse files Browse the repository at this point in the history
Refactor Tooltips and Slot Texts
  • Loading branch information
AzureAaron authored Jun 13, 2024
2 parents ebd5ccd + 938ba92 commit 8d804b9
Show file tree
Hide file tree
Showing 29 changed files with 209 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.general.itemInfoDisplay"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.general.itemInfoDisplay.slotText"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.general.itemInfoDisplay.slotText.@Tooltip")))
.binding(defaults.general.itemInfoDisplay.slotText,
() -> config.general.itemInfoDisplay.slotText,
newValue -> config.general.itemInfoDisplay.slotText = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.general.itemInfoDisplay.attributeShardInfo"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.general.itemInfoDisplay.attributeShardInfo.@Tooltip")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public String toString() {
}

public static class ItemInfoDisplay {
@SerialEntry
public boolean slotText = true;

@SerialEntry
public boolean attributeShardInfo = true;

Expand Down
13 changes: 4 additions & 9 deletions src/main/java/de/hysky/skyblocker/mixins/HandledScreenMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.hysky.skyblocker.mixins;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.blaze3d.systems.RenderSystem;
import de.hysky.skyblocker.SkyblockerMod;
Expand All @@ -17,7 +16,6 @@
import de.hysky.skyblocker.skyblock.item.slottext.SlotTextManager;
import de.hysky.skyblocker.skyblock.item.tooltip.BackpackPreview;
import de.hysky.skyblocker.skyblock.item.tooltip.CompactorDeletorPreview;
import de.hysky.skyblocker.skyblock.item.tooltip.TooltipManager;
import de.hysky.skyblocker.skyblock.quicknav.QuickNav;
import de.hysky.skyblocker.skyblock.quicknav.QuickNavButton;
import de.hysky.skyblocker.utils.ItemUtils;
Expand All @@ -38,7 +36,10 @@
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand Down Expand Up @@ -189,12 +190,6 @@ protected HandledScreenMixin(Text title) {
return skyblocker$experimentSolvers$getStack(focusedSlot, stack);
}

@SuppressWarnings("deprecation")
@ModifyReturnValue(method = "getTooltipFromItem", at = @At("RETURN"))
private List<Text> skyblocker$tooltips$addToTooltip(List<Text> tooltip) {
return TooltipManager.addToTooltip(tooltip, focusedSlot);
}

@ModifyVariable(method = "drawSlot", at = @At(value = "LOAD", ordinal = 3), ordinal = 0)
private ItemStack skyblocker$experimentSolvers$replaceDisplayStack(ItemStack stack, DrawContext context, Slot slot) {
return skyblocker$experimentSolvers$getStack(slot, stack);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import it.unimi.dsi.fastutil.ints.IntIntPair;
import net.minecraft.component.ComponentHolder;
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TooltipAppender;
Expand All @@ -28,7 +29,7 @@
import java.util.Optional;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements SkyblockerStack {
public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack {

@Shadow
public abstract int getDamage();
Expand All @@ -51,7 +52,7 @@ public abstract class ItemStackMixin implements SkyblockerStack {
@ModifyReturnValue(method = "getName", at = @At("RETURN"))
private Text skyblocker$customItemNames(Text original) {
if (Utils.isOnSkyblock()) {
return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(ItemUtils.getItemUuid((ItemStack) (Object) this), original);
return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(ItemUtils.getItemUuid(this), original);
}

return original;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package de.hysky.skyblocker.mixins.accessors;

import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.util.List;

@Mixin(HandledScreen.class)
public interface HandledScreenAccessor {
Expand All @@ -32,7 +27,4 @@ public interface HandledScreenAccessor {

@Accessor("focusedSlot")
Slot getFocusedSlot();

@Invoker
List<Text> invokeGetTooltipFromItem(ItemStack stack);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.util.Formatting;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
Expand Down Expand Up @@ -255,8 +256,8 @@ public Tooltip() {
}

@Override
public void addToTooltip(List<Text> lines, Slot focusedSlot) {
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.enableChocolateFactoryHelper) return;
public void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines) {
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.enableChocolateFactoryHelper || focusedSlot == null) return;

int lineIndex = lines.size();
//This boolean is used to determine if we should add a smooth line to separate the added information from the rest of the tooltip.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package de.hysky.skyblocker.skyblock.item.slottext;

import de.hysky.skyblocker.skyblock.ChestValue;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.render.gui.AbstractContainerMatcher;
import net.minecraft.screen.slot.Slot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.regex.Pattern;

/**
* Extend this class and add it to {@link SlotTextManager#adders} to add text to any arbitrary slot.
*/
public abstract class SlotTextAdder {
/**
* The title of the screen must match this pattern for this adder to be applied. Null means it will be applied to all screens.
* @implNote Don't end your regex with a {@code $} as {@link ChestValue} appends text to the end of the title,
* so the regex will stop matching if the player uses it.
*/
public final @Nullable Pattern titlePattern;

public abstract class SlotTextAdder extends AbstractContainerMatcher {
/**
* Utility constructor that will compile the given string into a pattern.
*
* @see #SlotTextAdder(Pattern)
*/
protected SlotTextAdder(@NotNull String titlePattern) {
this(Pattern.compile(titlePattern));
super(titlePattern);
}

/**
Expand All @@ -34,14 +27,14 @@ protected SlotTextAdder(@NotNull String titlePattern) {
* @param titlePattern The pattern to match the screen title against.
*/
protected SlotTextAdder(@NotNull Pattern titlePattern) {
this.titlePattern = titlePattern;
super(titlePattern);
}

/**
* Creates a SlotTextAdder that will be applied to all screens.
*/
protected SlotTextAdder() {
this.titlePattern = null;
super();
}

/**
Expand All @@ -59,6 +52,6 @@ protected SlotTextAdder() {
* @implNote The slot text adders only work while in skyblock, so no need to check for that again.
*/
public boolean isEnabled() {
return true;
return SkyblockerConfigManager.get().general.itemInfoDisplay.slotText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class SlotTextManager {
new CatacombsLevelAdder.ReadyUp(),
new RancherBootsSpeedAdder(),
new AttributeShardAdder(),
new PrehistoricEggAdder()
new PrehistoricEggAdder(),
new PotionLevelAdder(),
new CollectionAdder()
};
private static final ArrayList<SlotTextAdder> currentScreenAdders = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.hysky.skyblocker.skyblock.item.slottext.adders;

import de.hysky.skyblocker.skyblock.item.slottext.SlotText;
import de.hysky.skyblocker.skyblock.item.slottext.SlotTextAdder;
import de.hysky.skyblocker.utils.RomanNumerals;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CollectionAdder extends SlotTextAdder {
private static final Pattern COLLECTION = Pattern.compile("^[\\w ]+ (?<level>[IVXLCDM]+)$");

public CollectionAdder() {
super("^\\w+ Collections");
}

@Override
public @NotNull List<SlotText> getText(Slot slot) {
final ItemStack stack = slot.getStack();
Matcher matcher = COLLECTION.matcher(stack.getName().getString());
if (matcher.matches()) {
int level = RomanNumerals.romanToDecimal(matcher.group("level"));
return List.of(SlotText.bottomRight(Text.literal(String.valueOf(level)).formatted(Formatting.YELLOW)));
}
return List.of();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.hysky.skyblocker.skyblock.item.slottext.adders;

import de.hysky.skyblocker.skyblock.item.slottext.SlotText;
import de.hysky.skyblocker.skyblock.item.slottext.SlotTextAdder;
import de.hysky.skyblocker.utils.ItemUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class PotionLevelAdder extends SlotTextAdder {
@Override
public @NotNull List<SlotText> getText(Slot slot) {
final ItemStack stack = slot.getStack();
NbtCompound customData = ItemUtils.getCustomData(stack);
if (customData.contains("potion_level", NbtElement.INT_TYPE)) {
int level = customData.getInt("potion_level");
return List.of(SlotText.bottomRight(Text.literal(String.valueOf(level)).formatted(Formatting.AQUA)));
}
return List.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
package de.hysky.skyblocker.skyblock.item.tooltip;

import de.hysky.skyblocker.skyblock.ChestValue;
import de.hysky.skyblocker.utils.render.gui.AbstractContainerMatcher;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.regex.Pattern;

/**
* Extend this class and add it to {@link TooltipManager#adders} to add additional text to tooltips.
*/
public abstract class TooltipAdder {
/**
* The title of the screen must match this pattern for this adder to be applied. Null means it will be applied to all screens.
* @implNote Don't end your regex with a {@code $} as {@link ChestValue} appends text to the end of the title,
* so the regex will stop matching if the player uses it.
*/
public final Pattern titlePattern;
public abstract class TooltipAdder extends AbstractContainerMatcher {
/**
* The priority of this adder. Lower priority means it will be applied first.
* @apiNote Consider taking this value on your class' constructor and setting it from {@link TooltipManager#adders} to make it easy to read and maintain.
*/
public final int priority;

protected TooltipAdder(String titlePattern, int priority) {
this(Pattern.compile(titlePattern), priority);
super(titlePattern);
this.priority = priority;
}

protected TooltipAdder(Pattern titlePattern, int priority) {
this.titlePattern = titlePattern;
super(titlePattern);
this.priority = priority;
}

/**
* Creates a TooltipAdder that will be applied to all screens.
*/
protected TooltipAdder(int priority) {
this.titlePattern = null;
super();
this.priority = priority;
}

/**
* @implNote The first element of the lines list holds the item's display name,
* as it's a list of all lines that will be displayed in the tooltip.
*/
public abstract void addToTooltip(List<Text> lines, Slot focusedSlot);
public abstract void addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package de.hysky.skyblocker.skyblock.item.tooltip;

import de.hysky.skyblocker.mixins.accessors.HandledScreenAccessor;
import de.hysky.skyblocker.skyblock.chocolatefactory.ChocolateFactorySolver;
import de.hysky.skyblocker.skyblock.item.tooltip.adders.*;
import de.hysky.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -34,6 +40,13 @@ private TooltipManager() {
}

public static void init() {
ItemTooltipCallback.EVENT.register((stack, tooltipContext, tooltipType, lines) -> {
if (MinecraftClient.getInstance().currentScreen instanceof HandledScreen<?> handledScreen) {
addToTooltip(((HandledScreenAccessor) handledScreen).getFocusedSlot(), stack, lines);
} else {
addToTooltip(null, stack, lines);
}
});
ScreenEvents.AFTER_INIT.register((client, screen, width, height) -> {
onScreenChange(screen);
ScreenEvents.remove(screen).register(ignored -> currentScreenAdders.clear());
Expand All @@ -42,6 +55,7 @@ public static void init() {

private static void onScreenChange(Screen screen) {
final String title = screen.getTitle().getString();
currentScreenAdders.clear();
for (TooltipAdder adder : adders) {
if (adder.titlePattern == null || adder.titlePattern.matcher(title).find()) {
currentScreenAdders.add(adder);
Expand All @@ -56,16 +70,17 @@ private static void onScreenChange(Screen screen) {
*
* <p>If you want to add info to the tooltips of multiple items, consider using a switch statement with {@code focusedSlot.getIndex()}</p>
*
* @param lines The tooltip lines of the focused item. This includes the display name, as it's a part of the tooltip (at index 0).
* @param focusedSlot The slot that is currently focused by the cursor.
* @param stack The stack to render the tooltip for.
* @param lines The tooltip lines of the focused item. This includes the display name, as it's a part of the tooltip (at index 0).
* @return The lines list itself after all adders have added their text.
* @deprecated This method is public only for the sake of the mixin. Don't call directly, not that there is any point to it.
*/
@Deprecated
public static List<Text> addToTooltip(List<Text> lines, Slot focusedSlot) {
public static List<Text> addToTooltip(@Nullable Slot focusedSlot, ItemStack stack, List<Text> lines) {
if (!Utils.isOnSkyblock()) return lines;
for (TooltipAdder adder : currentScreenAdders) {
adder.addToTooltip(lines, focusedSlot);
adder.addToTooltip(focusedSlot, stack, lines);
}
return lines;
}
Expand Down
Loading

0 comments on commit 8d804b9

Please sign in to comment.