From 238ed9ac50ecd4be790b0f30d54e962177fbb8d5 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:17:13 -0400 Subject: Refactor ItemRarityBackgrounds --- .../skyblock/item/ItemRarityBackgrounds.java | 27 +++++++++++----------- .../skyblock/item/SkyblockItemRarity.java | 7 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker') diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java index 87dac046..18a88539 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java @@ -26,7 +26,6 @@ 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 = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(RARITY_BG_TEX); - private static final String EMPTY = ""; private static final ImmutableMap 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 @@ -37,13 +36,13 @@ public class ItemRarityBackgrounds { Map.entry("EPIC", SkyblockItemRarity.EPIC), Map.entry("RARE", SkyblockItemRarity.RARE), Map.entry("UNCOMMON", SkyblockItemRarity.UNCOMMON), - Map.entry("COMMON", SkyblockItemRarity.COMMON)); - + Map.entry("COMMON", SkyblockItemRarity.COMMON) + ); private static final Int2ReferenceOpenHashMap CACHE = new Int2ReferenceOpenHashMap<>(); public static void init() { //Clear the cache every 5 minutes, ints are very compact! - Scheduler.INSTANCE.scheduleCyclic(() -> CACHE.clear(), 4800); + 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) -> { @@ -64,7 +63,7 @@ public class ItemRarityBackgrounds { if (itemRarity != null) draw(context, x, y, itemRarity); } } - + private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEntity player) { if (stack == null || stack.isEmpty()) return null; @@ -73,23 +72,23 @@ public class ItemRarityBackgrounds { if (nbt != null && nbt.contains("ExtraAttributes")) { NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : EMPTY; + String itemUuid = extraAttributes.getString("uuid"); - //If the item has a uuid, then use the hash code of the uuid otherwise use the identity hash code of the stack - hashCode = (itemUuid != EMPTY) ? itemUuid.hashCode() : System.identityHashCode(stack); + //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 tooltip = stack.getTooltip(player, TooltipContext.BASIC); String[] stringifiedTooltip = tooltip.stream().map(Text::getString).toArray(String[]::new); - for (String rarity : LORE_RARITIES.keySet()) { - if (Arrays.stream(stringifiedTooltip).anyMatch(line -> line.contains(rarity))) { - SkyblockItemRarity foundRarity = LORE_RARITIES.get(rarity); + for (String rarityString : LORE_RARITIES.keySet()) { + if (Arrays.stream(stringifiedTooltip).anyMatch(line -> line.contains(rarityString))) { + SkyblockItemRarity rarity = LORE_RARITIES.get(rarityString); - CACHE.put(hashCode, foundRarity); - return LORE_RARITIES.get(rarity); + CACHE.put(hashCode, rarity); + return rarity; } } diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java index da722196..f7ff1fb9 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java @@ -17,10 +17,11 @@ public enum SkyblockItemRarity { public final float r; public final float g; public final float b; - - private SkyblockItemRarity(Formatting formatting) { + + 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; -- cgit