Skip to content

Commit

Permalink
Merge pull request #340 from AzureAaron/item-rarity-background
Browse files Browse the repository at this point in the history
Item Rarity Backgrounds
  • Loading branch information
AzureAaron authored Oct 7, 2023
2 parents 692059c + 0103dca commit 9553f2a
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void onInitializeClient() {
QuiverWarning.init();
SpecialEffects.init();
ItemProtection.init();
ItemRarityBackgrounds.init();
containerSolverManager.init();
statusBarTracker.init();
Scheduler.INSTANCE.scheduleCyclic(Utils::update, 20);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/xmrvizzy/skyblocker/config/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.controller.BooleanControllerBuilder;
import dev.isxander.yacl3.api.controller.EnumControllerBuilder;
import dev.isxander.yacl3.api.controller.ValueFormatter;
import me.xmrvizzy.skyblocker.config.controllers.EnumDropdownControllerBuilder;
import net.minecraft.text.Text;

public class ConfigUtils {
public static final ValueFormatter<Float> FLOAT_TWO_FORMATTER = value -> Text.literal(String.format("%,.2f", value).replaceAll("[\u00a0\u202F]", " "));

public static BooleanControllerBuilder createBooleanController(Option<Boolean> opt) {
return BooleanControllerBuilder.create(opt).yesNoFormatter().coloured(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ public static class ItemTooltip {
public static class ItemInfoDisplay {
@SerialEntry
public boolean attributeShardInfo = true;

@SerialEntry
public boolean itemRarityBackgrounds = false;

@SerialEntry
public float itemRarityBackgroundsOpacity = 1f;
}

public static class SpecialEffects {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package me.xmrvizzy.skyblocker.config.categories;

import dev.isxander.yacl3.api.ButtonOption;
import dev.isxander.yacl3.api.ConfigCategory;
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.OptionDescription;
import dev.isxander.yacl3.api.OptionGroup;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.FloatFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder;
import me.xmrvizzy.skyblocker.config.SkyblockerConfig;
import me.xmrvizzy.skyblocker.config.ConfigUtils;
import me.xmrvizzy.skyblocker.config.SkyblockerConfig;
import me.xmrvizzy.skyblocker.skyblock.shortcut.ShortcutsConfigScreen;
import me.xmrvizzy.skyblocker.utils.render.title.TitleContainerConfigScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;

public class GeneralCategory {

public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig config) {
public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig config) {
return ConfigCategory.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.category.general"))

//Ungrouped Options
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.acceptReparty"))
Expand Down Expand Up @@ -57,7 +54,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
newValue -> config.general.hideStatusEffectOverlay = newValue)
.controller(ConfigUtils::createBooleanController)
.build())

//Tab Hud
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.tabHud"))
Expand Down Expand Up @@ -94,7 +91,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createEnumCyclingListController)
.build())
.build())

//Fancy Bars
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.bars"))
Expand Down Expand Up @@ -135,7 +132,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createEnumCyclingListController)
.build())
.build())

//Experiments Solver
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.experiments"))
Expand All @@ -162,7 +159,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Fishing Helper
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.fishing"))
Expand All @@ -175,7 +172,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Fairy Souls Helper
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.fairySouls"))
Expand Down Expand Up @@ -203,7 +200,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Shortcuts
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.shortcuts"))
Expand Down Expand Up @@ -238,7 +235,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.action((screen, opt) -> MinecraftClient.getInstance().setScreen(new ShortcutsConfigScreen(screen)))
.build())
.build())

//Quiver Warning
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.quiverWarning"))
Expand All @@ -265,7 +262,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Item List
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.itemList"))
Expand All @@ -278,7 +275,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Item Tooltip
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.itemTooltip"))
Expand Down Expand Up @@ -335,7 +332,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Item Info Display
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.itemInfoDisplay"))
Expand All @@ -348,8 +345,23 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
newValue -> config.general.itemInfoDisplay.attributeShardInfo = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.itemInfoDisplay.itemRarityBackgrounds"))
.description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.general.itemInfoDisplay.itemRarityBackgrounds.@Tooltip")))
.binding(defaults.general.itemInfoDisplay.itemRarityBackgrounds,
() -> config.general.itemInfoDisplay.itemRarityBackgrounds,
newValue -> config.general.itemInfoDisplay.itemRarityBackgrounds = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Float>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.itemInfoDisplay.itemRarityBackgroundsOpacity"))
.binding(defaults.general.itemInfoDisplay.itemRarityBackgroundsOpacity,
() -> config.general.itemInfoDisplay.itemRarityBackgroundsOpacity,
newValue -> config.general.itemInfoDisplay.itemRarityBackgroundsOpacity = newValue)
.controller(opt -> FloatSliderControllerBuilder.create(opt).range(0f, 1f).step(0.05f).formatValue(ConfigUtils.FLOAT_TWO_FORMATTER))
.build())
.build())

//Special Effects
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.specialEffects"))
Expand All @@ -363,7 +375,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Hitboxes
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.hitbox"))
Expand All @@ -383,7 +395,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Title Container
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.titleContainer"))
Expand Down Expand Up @@ -430,7 +442,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.action((screen, opt) -> MinecraftClient.getInstance().setScreen(new TitleContainerConfigScreen(screen)))
.build())
.build())

//Teleport Overlays
.group(OptionGroup.createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.teleportOverlay"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.xmrvizzy.skyblocker.skyblock.item.BackpackPreview;
import me.xmrvizzy.skyblocker.skyblock.item.CompactorDeletorPreview;
import me.xmrvizzy.skyblocker.skyblock.item.ItemProtection;
import me.xmrvizzy.skyblocker.skyblock.item.ItemRarityBackgrounds;
import me.xmrvizzy.skyblocker.skyblock.item.WikiLookup;
import me.xmrvizzy.skyblocker.skyblock.itemlist.ItemRegistry;
import me.xmrvizzy.skyblocker.utils.Utils;
Expand Down Expand Up @@ -184,4 +185,9 @@ protected HandledScreenMixin(Text title) {
private static boolean skyblocker$doesLoreContain(ItemStack stack, MinecraftClient client, String searchString) {
return stack.getTooltip(client.player, TooltipContext.BASIC).stream().map(Text::getString).anyMatch(line -> line.contains(searchString));
}

@Inject(method = "drawSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawItem(Lnet/minecraft/item/ItemStack;III)V"))
private void skyblocker$drawItemRarityBackground(DrawContext context, Slot slot, CallbackInfo ci) {
if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgrounds) ItemRarityBackgrounds.tryDraw(slot.getStack(), context, slot.x, slot.y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import me.xmrvizzy.skyblocker.skyblock.FancyStatusBars;
import me.xmrvizzy.skyblocker.skyblock.HotbarSlotLock;
import me.xmrvizzy.skyblocker.skyblock.dungeon.DungeonMap;
import me.xmrvizzy.skyblocker.skyblock.item.ItemRarityBackgrounds;
import me.xmrvizzy.skyblocker.utils.Utils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -33,9 +35,10 @@ public abstract class InGameHudMixin {
private int scaledWidth;

@Inject(method = "renderHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/DrawContext;IIFLnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V", ordinal = 0))
public void skyblocker$renderHotbarItemLock(float tickDelta, DrawContext context, CallbackInfo ci, @Local(ordinal = 4, name = "m") int index, @Local(ordinal = 5, name = "n") int x, @Local(ordinal = 6, name = "o") int y) {
if (Utils.isOnSkyblock() && HotbarSlotLock.isLocked(index)) {
context.drawTexture(SLOT_LOCK, x, y, 0, 0, 16, 16);
public void skyblocker$renderHotbarItemLockOrRarityBg(float tickDelta, DrawContext context, CallbackInfo ci, @Local(ordinal = 4, name = "m") int index, @Local(ordinal = 5, name = "n") int x, @Local(ordinal = 6, name = "o") int y, @Local PlayerEntity player) {
if (Utils.isOnSkyblock()) {
if (SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgrounds) ItemRarityBackgrounds.tryDraw(player.getInventory().main.get(index), context, x, y);
if (HotbarSlotLock.isLocked(index)) context.drawTexture(SLOT_LOCK, x, y, 0, 0, 16, 16);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package me.xmrvizzy.skyblocker.skyblock.item;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableMap;
import com.mojang.blaze3d.systems.RenderSystem;

import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap;
import me.xmrvizzy.skyblocker.SkyblockerMod;
import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager;
import me.xmrvizzy.skyblocker.utils.Utils;
import me.xmrvizzy.skyblocker.utils.scheduler.Scheduler;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.texture.Sprite;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class ItemRarityBackgrounds {
private static final Identifier RARITY_BG_TEX = new Identifier(SkyblockerMod.NAMESPACE, "item_rarity_background");
private static final Supplier<Sprite> SPRITE = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(RARITY_BG_TEX);
private static final ImmutableMap<String, SkyblockItemRarity> LORE_RARITIES = ImmutableMap.ofEntries(
Map.entry("ADMIN", SkyblockItemRarity.ADMIN),
Map.entry("SPECIAL", SkyblockItemRarity.SPECIAL), //Very special is the same color so this will cover it
Map.entry("DIVINE", SkyblockItemRarity.DIVINE),
Map.entry("MYTHIC", SkyblockItemRarity.MYTHIC),
Map.entry("LEGENDARY", SkyblockItemRarity.LEGENDARY),
Map.entry("LEGENJERRY", SkyblockItemRarity.LEGENDARY),
Map.entry("EPIC", SkyblockItemRarity.EPIC),
Map.entry("RARE", SkyblockItemRarity.RARE),
Map.entry("UNCOMMON", SkyblockItemRarity.UNCOMMON),
Map.entry("COMMON", SkyblockItemRarity.COMMON)
);
private static final Int2ReferenceOpenHashMap<SkyblockItemRarity> CACHE = new Int2ReferenceOpenHashMap<>();

public static void init() {
//Clear the cache every 5 minutes, ints are very compact!
Scheduler.INSTANCE.scheduleCyclic(CACHE::clear, 4800);

//Clear cache after a screen where items can be upgraded in rarity closes
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
String title = screen.getTitle().getString();

if (Utils.isOnSkyblock() && (title.equals("The Hex") || title.equals("Craft Item") || title.equals("Anvil") || title.equals("Reforge Anvil"))) {
ScreenEvents.remove(screen).register(screen1 -> CACHE.clear());
}
});
}

public static void tryDraw(ItemStack stack, DrawContext context, int x, int y) {
MinecraftClient client = MinecraftClient.getInstance();

if (client.player != null) {
SkyblockItemRarity itemRarity = getItemRarity(stack, client.player);

if (itemRarity != null) draw(context, x, y, itemRarity);
}
}

private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEntity player) {
if (stack == null || stack.isEmpty()) return null;

int hashCode = 0;
NbtCompound nbt = stack.getNbt();

if (nbt != null && nbt.contains("ExtraAttributes")) {
NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes");
String itemUuid = extraAttributes.getString("uuid");

//If the item has an uuid, then use the hash code of the uuid otherwise use the identity hash code of the stack
hashCode = itemUuid.isEmpty() ? System.identityHashCode(stack) : itemUuid.hashCode();
}

if (CACHE.containsKey(hashCode)) return CACHE.get(hashCode);

List<Text> tooltip = stack.getTooltip(player, TooltipContext.BASIC);
String[] stringifiedTooltip = tooltip.stream().map(Text::getString).toArray(String[]::new);

for (String rarityString : LORE_RARITIES.keySet()) {
if (Arrays.stream(stringifiedTooltip).anyMatch(line -> line.contains(rarityString))) {
SkyblockItemRarity rarity = LORE_RARITIES.get(rarityString);

CACHE.put(hashCode, rarity);
return rarity;
}
}

CACHE.put(hashCode, null);
return null;
}

private static void draw(DrawContext context, int x, int y, SkyblockItemRarity rarity) {
//Enable blending to handle HUD translucency
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();

context.drawSprite(x, y, 0, 16, 16, SPRITE.get(), rarity.r, rarity.g, rarity.b, SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgroundsOpacity);

RenderSystem.disableBlend();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.xmrvizzy.skyblocker.skyblock.item;

import net.minecraft.util.Formatting;

public enum SkyblockItemRarity {
ADMIN(Formatting.DARK_RED),
VERY_SPECIAL(Formatting.RED),
SPECIAL(Formatting.RED),
DIVINE(Formatting.AQUA),
MYTHIC(Formatting.LIGHT_PURPLE),
LEGENDARY(Formatting.GOLD),
EPIC(Formatting.DARK_PURPLE),
RARE(Formatting.BLUE),
UNCOMMON(Formatting.GREEN),
COMMON(Formatting.WHITE);

public final float r;
public final float g;
public final float b;

SkyblockItemRarity(Formatting formatting) {
@SuppressWarnings("DataFlowIssue")
int rgb = formatting.getColorValue();

this.r = ((rgb >> 16) & 0xFF) / 255f;
this.g = ((rgb >> 8) & 0xFF) / 255f;
this.b = (rgb & 0xFF) / 255f;
}
}
Loading

0 comments on commit 9553f2a

Please sign in to comment.