Skip to content

Commit

Permalink
Refactor ItemRarityBackgrounds
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Oct 7, 2023
1 parent 05d86bf commit 238ed9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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 String EMPTY = "";
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
Expand All @@ -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<SkyblockItemRarity> 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) -> {
Expand All @@ -64,7 +63,7 @@ public static void tryDraw(ItemStack stack, DrawContext context, int x, int y) {
if (itemRarity != null) draw(context, x, y, itemRarity);
}
}

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

Expand All @@ -73,23 +72,23 @@ private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEnt

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<Text> 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 238ed9a

Please sign in to comment.