Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up id mess and add tests #867

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package de.hysky.skyblocker.injected;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

public interface SkyblockerStack {
@Nullable
@NotNull
default String getSkyblockId() {
return "";
}

@Nullable
@NotNull
default String getSkyblockApiId() {
return "";
}

@Nullable
@NotNull
default String getNeuName() {
return "";
}
Expand Down
145 changes: 7 additions & 138 deletions src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package de.hysky.skyblocker.mixins;

import com.google.gson.JsonParser;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.serialization.JsonOps;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.injected.SkyblockerStack;
import de.hysky.skyblocker.skyblock.PetCache.PetInfo;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
Expand All @@ -17,10 +12,8 @@
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipAppender;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -29,9 +22,6 @@
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Locale;
import java.util.Optional;

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

Expand Down Expand Up @@ -129,144 +119,23 @@ public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack
}

@Override
@Nullable
@NotNull
public String getSkyblockId() {
if (skyblockId != null && !skyblockId.isEmpty()) return skyblockId;
return skyblockId = skyblocker$getSkyblockId(true);
return skyblockId = ItemUtils.getItemId(this);
}

@Override
@Nullable
@NotNull
public String getSkyblockApiId() {
if (skyblockApiId != null && !skyblockApiId.isEmpty()) return skyblockApiId;
return skyblockApiId = skyblocker$getSkyblockId(false);
return skyblockApiId = ItemUtils.getSkyblockApiId(this);
}

@Override
@Nullable
@NotNull
public String getNeuName() {
if (neuName != null && !neuName.isEmpty()) return neuName;
String apiId = getSkyblockApiId();
String id = getSkyblockId();
if (apiId == null || id == null) return null;

if (apiId.startsWith("ISSHINY_")) apiId = id;

return neuName = ItemTooltip.getNeuName(id, apiId);
}

@Unique
private String skyblocker$getSkyblockId(boolean internalIDOnly) {
NbtCompound customData = ItemUtils.getCustomData((ItemStack) (Object) this);

if (customData == null || !customData.contains(ItemUtils.ID, NbtElement.STRING_TYPE)) {
return null;
}
String customDataString = customData.getString(ItemUtils.ID);

if (internalIDOnly) {
return customDataString;
}

// Transformation to API format.
//TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow
if (customData.contains("is_shiny")) {
return "ISSHINY_" + customDataString;
}

switch (customDataString) {
case "ENCHANTED_BOOK" -> {
if (customData.contains("enchantments")) {
NbtCompound enchants = customData.getCompound("enchantments");
Optional<String> firstEnchant = enchants.getKeys().stream().findFirst();
String enchant = firstEnchant.orElse("");
return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ENGLISH) + "_" + enchants.getInt(enchant);
}
}

case "PET" -> {
if (customData.contains("petInfo")) {
PetInfo petInfo = PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow();
return "LVL_1_" + petInfo.tier() + "_" + petInfo.type();
}
}

case "POTION" -> {
String enhanced = customData.contains("enhanced") ? "_ENHANCED" : "";
String extended = customData.contains("extended") ? "_EXTENDED" : "";
String splash = customData.contains("splash") ? "_SPLASH" : "";
if (customData.contains("potion") && customData.contains("potion_level")) {
return (customData.getString("potion") + "_" + customDataString + "_" + customData.getInt("potion_level")
+ enhanced + extended + splash).toUpperCase(Locale.ENGLISH);
}
}

case "RUNE" -> {
if (customData.contains("runes")) {
NbtCompound runes = customData.getCompound("runes");
Optional<String> firstRunes = runes.getKeys().stream().findFirst();
String rune = firstRunes.orElse("");
return rune.toUpperCase(Locale.ENGLISH) + "_RUNE_" + runes.getInt(rune);
}
}

case "ATTRIBUTE_SHARD" -> {
if (customData.contains("attributes")) {
NbtCompound shards = customData.getCompound("attributes");
Optional<String> firstShards = shards.getKeys().stream().findFirst();
String shard = firstShards.orElse("");
return customDataString + "-" + shard.toUpperCase(Locale.ENGLISH) + "_" + shards.getInt(shard);
}
}

case "NEW_YEAR_CAKE" -> {
return customDataString + "_" + customData.getInt("new_years_cake");
}

case "PARTY_HAT_CRAB", "PARTY_HAT_CRAB_ANIMATED", "BALLOON_HAT_2024" -> {
return customDataString + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ENGLISH);
}

case "PARTY_HAT_SLOTH" -> {
return customDataString + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ENGLISH);
}

case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS" -> {
NbtCompound attributes = customData.getCompound("attributes");

if (attributes.contains("magic_find") && attributes.contains("veteran")) {
return customDataString + "-MAGIC_FIND-VETERAN";
}
}

case "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS" -> {
NbtCompound attributes = customData.getCompound("attributes");

if (attributes.contains("mana_pool") && attributes.contains("mana_regeneration")) {
return customDataString + "-MANA_POOL-MANA_REGENERATION";
}
}

case "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> {
NbtCompound attributes = customData.getCompound("attributes");

if (attributes.contains("lifeline") && attributes.contains("mana_pool")) {
return customDataString + "-LIFELINE-MANA_POOL";
}
}

case "MIDAS_SWORD" -> {
if (customData.getInt("winning_bid") >= 50000000) {
return customDataString + "_50M";
}
}

case "MIDAS_STAFF" -> {
if (customData.getInt("winning_bid") >= 100000000) {
return customDataString + "_100M";
}
}
}
return customDataString;
return neuName = ItemUtils.getNeuId((ItemStack) (Object) this);
}
}
10 changes: 4 additions & 6 deletions src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.hysky.skyblocker.skyblock;

import com.google.gson.JsonObject;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.config.configs.DungeonsConfig;
import de.hysky.skyblocker.config.configs.UIAndVisualsConfig;
import de.hysky.skyblocker.mixins.accessors.HandledScreenAccessor;
import de.hysky.skyblocker.mixins.accessors.ScreenAccessor;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair;
Expand Down Expand Up @@ -79,11 +77,11 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler,
}

String name = stack.getName().getString();
String id = stack.getSkyblockApiId();
String skyblockApiId = stack.getSkyblockApiId();

//Regular item price
if (id != null) {
DoubleBooleanPair priceData = ItemUtils.getItemPrice(id);
if (!skyblockApiId.isEmpty()) {
DoubleBooleanPair priceData = ItemUtils.getItemPrice(skyblockApiId);

if (!priceData.rightBoolean()) hasIncompleteData = true;

Expand Down Expand Up @@ -160,7 +158,7 @@ private static Text getChestValue(GenericContainerScreenHandler handler, Text ti

String id = stack.getSkyblockApiId();

if (id != null) {
if (!id.isEmpty()) {
DoubleBooleanPair priceData = ItemUtils.getItemPrice(id);

if (!priceData.rightBoolean()) hasIncompleteData = true;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/de/hysky/skyblocker/skyblock/PetCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.slot.Slot;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

Expand All @@ -27,7 +28,7 @@

/**
* Doesn't work with auto pet right now because thats complicated.
*
* <p>
* Want support? Ask the Admins for a Mod API event or open your pets menu.
*/
public class PetCache {
Expand Down Expand Up @@ -134,16 +135,23 @@ public static PetInfo getCurrentPet() {
}

public record PetInfo(String type, double exp, String tier, Optional<String> uuid, Optional<String> item, Optional<String> skin) {
// TODO: Combine with SkyblockItemRarity
private static final String[] TIER_INDEX = {"COMMON", "UNCOMMON", "RARE", "EPIC", "LEGENDARY", "MYTHIC"};

public static final Codec<PetInfo> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("type").forGetter(PetInfo::type),
Codec.DOUBLE.fieldOf("exp").forGetter(PetInfo::exp),
Codec.STRING.fieldOf("tier").forGetter(PetInfo::tier),
Codec.STRING.optionalFieldOf("uuid").forGetter(PetInfo::uuid),
Codec.STRING.optionalFieldOf("heldItem").forGetter(PetInfo::item),
Codec.STRING.optionalFieldOf("skin").forGetter(PetInfo::skin))
.apply(instance, PetInfo::new));
Codec.STRING.optionalFieldOf("skin").forGetter(PetInfo::skin)
).apply(instance, PetInfo::new));
private static final Codec<Object2ObjectOpenHashMap<String, Object2ObjectOpenHashMap<String, PetInfo>>> SERIALIZATION_CODEC = Codec.unboundedMap(Codec.STRING,
Codec.unboundedMap(Codec.STRING, CODEC).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new)
).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new);
).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new);

public int tierIndex() {
return ArrayUtils.indexOf(TIER_INDEX, tier);
}
}
}
55 changes: 26 additions & 29 deletions src/main/java/de/hysky/skyblocker/skyblock/TeleportOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,37 @@ private static void render(WorldRenderContext wrc) {
String itemId = heldItem.getSkyblockId();
NbtCompound customData = ItemUtils.getCustomData(heldItem);

if (itemId != null) {
switch (itemId) {
case "ASPECT_OF_THE_LEECH_1" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWeirdTransmission) {
render(wrc, 3);
}
switch (itemId) {
case "ASPECT_OF_THE_LEECH_1" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWeirdTransmission) {
render(wrc, 3);
}
case "ASPECT_OF_THE_LEECH_2" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWeirdTransmission) {
render(wrc, 4);
}
}
case "ASPECT_OF_THE_LEECH_2" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWeirdTransmission) {
render(wrc, 4);
}
case "ASPECT_OF_THE_END", "ASPECT_OF_THE_VOID" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableEtherTransmission && client.options.sneakKey.isPressed() && customData != null && customData.getInt("ethermerge") == 1) {
render(wrc, customData, 57);
} else if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableInstantTransmission) {
render(wrc, customData, 8);
}
}
case "ASPECT_OF_THE_END", "ASPECT_OF_THE_VOID" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableEtherTransmission && client.options.sneakKey.isPressed() && customData.getInt("ethermerge") == 1) {
render(wrc, customData, 57);
} else if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableInstantTransmission) {
render(wrc, customData, 8);
}
case "ETHERWARP_CONDUIT" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableEtherTransmission) {
render(wrc, customData, 57);
}
}
case "ETHERWARP_CONDUIT" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableEtherTransmission) {
render(wrc, customData, 57);
}
case "SINSEEKER_SCYTHE" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableSinrecallTransmission) {
render(wrc, customData, 4);
}
}
case "SINSEEKER_SCYTHE" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableSinrecallTransmission) {
render(wrc, customData, 4);
}
case "NECRON_BLADE", "ASTRAEA", "HYPERION", "SCYLLA", "VALKYRIE" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWitherImpact) {
render(wrc, 10);
}
}
case "NECRON_BLADE", "ASTRAEA", "HYPERION", "SCYLLA", "VALKYRIE" -> {
if (SkyblockerConfigManager.get().uiAndVisuals.teleportOverlay.enableWitherImpact) {
render(wrc, 10);
}
}
}
Expand All @@ -84,7 +82,6 @@ private static void render(WorldRenderContext wrc, int range) {
if (client.crosshairTarget != null && client.crosshairTarget.getType() == HitResult.Type.BLOCK && client.crosshairTarget instanceof BlockHitResult blockHitResult && client.crosshairTarget.squaredDistanceTo(client.player) < range * range) {
render(wrc, blockHitResult);
} else if (client.interactionManager != null && range > client.player.getAttributeInstance(EntityAttributes.PLAYER_BLOCK_INTERACTION_RANGE).getValue()) {
@SuppressWarnings("DataFlowIssue")
HitResult result = client.player.raycast(range, wrc.tickCounter().getTickDelta(true), false);
if (result.getType() == HitResult.Type.BLOCK && result instanceof BlockHitResult blockHitResult) {
render(wrc, blockHitResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import de.hysky.skyblocker.skyblock.auction.widgets.CategoryTabWidget;
import de.hysky.skyblocker.skyblock.auction.widgets.RarityWidget;
import de.hysky.skyblocker.skyblock.auction.widgets.SortWidget;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.render.gui.AbstractCustomHypixelGUI;
Expand Down Expand Up @@ -295,14 +294,7 @@ public void onSlotChange(AuctionHouseScreenHandler handler, int slotId, ItemStac
String coins = split[1].replace(",", "").replace("coins", "").trim();
try {
long parsed = Long.parseLong(coins);
String name = stack.getSkyblockApiId();
String internalID = stack.getSkyblockId();
String neuName = name;
if (name == null || internalID == null) break;
if (name.startsWith("ISSHINY_")) {
neuName = internalID;
}
JsonElement jsonElement = TooltipInfoType.THREE_DAY_AVERAGE.getData().get(ItemTooltip.getNeuName(internalID, neuName));
JsonElement jsonElement = TooltipInfoType.THREE_DAY_AVERAGE.getData().get(stack.getNeuName());
if (jsonElement == null) break;
else {
isSlotHighlighted.put(slotId, jsonElement.getAsDouble() > parsed);
Expand Down
Loading