Skip to content

Commit

Permalink
Merge pull request #867 from kevinthegreat1/id-cleanup
Browse files Browse the repository at this point in the history
Clean up id mess and add tests

(cherry picked from commit 4326f83)
  • Loading branch information
kevinthegreat1 committed Aug 10, 2024
1 parent 154a1b0 commit c40b6f9
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 219 deletions.
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
90 changes: 7 additions & 83 deletions src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package de.hysky.skyblocker.mixins;

import com.google.gson.JsonObject;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.injected.SkyblockerStack;
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.ints.IntIntPair;
import net.minecraft.component.ComponentHolder;
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.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 @@ -25,9 +20,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 @@ -125,91 +117,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.
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")) {
JsonObject petInfo = SkyblockerMod.GSON.fromJson(customData.getString("petInfo"), JsonObject.class);
return "LVL_1_" + petInfo.get("tier").getAsString() + "_" + petInfo.get("type").getAsString();
}
}
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);
}
}
}
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
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.tickDelta(), 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
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ private double valueChest(@NotNull ItemStack chest) {


private double getItemPrice(String itemDisplayName) {
return ItemUtils.getItemPrice(dungeonDropsNameToId.get(itemDisplayName)).leftDouble();
return ItemUtils.getItemPrice(dungeonDropsNameToApiId.get(itemDisplayName)).leftDouble();
}


// I did a thing :(
private final Map<String, String> dungeonDropsNameToId = Util.make(new HashMap<>(), map -> {
private final Map<String, String> dungeonDropsNameToApiId = Util.make(new HashMap<>(), map -> {
map.put("Enchanted Book (Ultimate Jerry I)", "ENCHANTMENT_ULTIMATE_JERRY_1"); // ultimate books start
map.put("Enchanted Book (Ultimate Jerry II)", "ENCHANTMENT_ULTIMATE_JERRY_2");
map.put("Enchanted Book (Ultimate Jerry III)", "ENCHANTMENT_ULTIMATE_JERRY_3");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.hysky.skyblocker.skyblock.garden;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType;
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
import de.hysky.skyblocker.skyblock.tabhud.util.Ico;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void updateContent() {
ItemStack farmingToolStack = client.player.getMainHandStack();
if (farmingToolStack == null) return;
String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack));
ItemStack cropStack = ItemRepository.getItemStack(ItemTooltip.getNeuName(cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops.
ItemStack cropStack = ItemRepository.getItemStack(cropItemId.replace(":", "-")); // Hacky conversion to neu id since ItemUtils.getNeuId requires an item stack.

String counterText = FarmingHud.counterText();
String counterNumber = FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import net.minecraft.nbt.NbtElement;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -19,7 +17,7 @@ public class PrehistoricEggAdder extends SlotTextAdder {
@Override
public @NotNull List<SlotText> getText(Slot slot) {
final ItemStack stack = slot.getStack();
if (!stack.isOf(Items.PLAYER_HEAD) || !StringUtils.equals(stack.getSkyblockId(), "PREHISTORIC_EGG")) return List.of();
if (!stack.isOf(Items.PLAYER_HEAD) || !stack.getSkyblockId().equals("PREHISTORIC_EGG")) return List.of();
NbtCompound nbt = ItemUtils.getCustomData(stack);
if (!nbt.contains("blocks_walked", NbtElement.INT_TYPE)) return List.of();
int walked = nbt.getInt("blocks_walked");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import net.minecraft.item.Items;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -25,8 +23,7 @@ public RancherBootsSpeedAdder() {
@Override
public @NotNull List<SlotText> getText(Slot slot) {
final ItemStack itemStack = slot.getStack();
// V null-safe equals.
if (!itemStack.isOf(Items.LEATHER_BOOTS) && !StringUtils.equals(itemStack.getSkyblockId(), "RANCHERS_BOOTS")) return List.of();
if (!itemStack.isOf(Items.LEATHER_BOOTS) && !slot.getStack().getSkyblockId().equals("RANCHERS_BOOTS")) return List.of();
Matcher matcher = ItemUtils.getLoreLineIfMatch(slot.getStack(), SPEED_PATTERN);
if (matcher == null) return List.of();
String speed = matcher.group(2);
Expand Down
Loading

0 comments on commit c40b6f9

Please sign in to comment.