aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ItemUtils.java58
1 files changed, 49 insertions, 9 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
index 1ac40d29..b1708404 100644
--- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
@@ -1,5 +1,7 @@
package de.hysky.skyblocker.utils;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
@@ -10,7 +12,10 @@ import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.hysky.skyblocker.skyblock.item.tooltip.adders.ObtainedDateTooltip;
import de.hysky.skyblocker.SkyblockerMod;
+import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType;
+import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair;
import it.unimi.dsi.fastutil.ints.IntIntPair;
+import it.unimi.dsi.fastutil.longs.LongBooleanPair;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentTypes;
@@ -57,7 +62,7 @@ public class ItemUtils {
}
@SuppressWarnings("deprecation")
- public static NbtCompound getCustomData(@NotNull ItemStack stack) {
+ public static NbtCompound getCustomData(@NotNull ItemStack stack) {
return stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).getNbt();
}
@@ -67,7 +72,7 @@ public class ItemUtils {
* @param stack the item stack to get the internal name from
* @return an optional containing the internal name of the item stack
*/
- public static Optional<String> getItemIdOptional(@NotNull ItemStack stack) {
+ public static Optional<String> getItemIdOptional(@NotNull ItemStack stack) {
NbtCompound customData = getCustomData(stack);
return customData.contains(ID) ? Optional.of(customData.getString(ID)) : Optional.empty();
}
@@ -78,7 +83,7 @@ public class ItemUtils {
* @param stack the item stack to get the internal name from
* @return the internal name of the item stack, or an empty string if the item stack is null or does not have an internal name
*/
- public static String getItemId(@NotNull ItemStack stack) {
+ public static String getItemId(@NotNull ItemStack stack) {
return getCustomData(stack).getString(ID);
}
@@ -88,7 +93,7 @@ public class ItemUtils {
* @param stack the item stack to get the UUID from
* @return an optional containing the UUID of the item stack
*/
- public static Optional<String> getItemUuidOptional(@NotNull ItemStack stack) {
+ public static Optional<String> getItemUuidOptional(@NotNull ItemStack stack) {
NbtCompound customData = getCustomData(stack);
return customData.contains(UUID) ? Optional.of(customData.getString(UUID)) : Optional.empty();
}
@@ -99,11 +104,46 @@ public class ItemUtils {
* @param stack the item stack to get the UUID from
* @return the UUID of the item stack, or an empty string if the item stack is null or does not have a UUID
*/
- public static String getItemUuid(@NotNull ItemStack stack) {
+ public static String getItemUuid(@NotNull ItemStack stack) {
return getCustomData(stack).getString(UUID);
}
/**
+ * Gets the bazaar sell price or the lowest bin based on the id of the item stack.
+ *
+ * @return An {@link LongBooleanPair} with the {@code left long} representing the item's price,
+ * and the {@code right boolean} indicating if the price was based on complete data.
+ */
+ public static DoubleBooleanPair getItemPrice(@NotNull ItemStack stack) {
+ return getItemPrice(getItemId(stack));
+ }
+
+ /**
+ * Gets the bazaar sell price or the lowest bin of the item with the specified id.
+ *
+ * @return An {@link LongBooleanPair} with the {@code left long} representing the item's price,
+ * and the {@code right boolean} indicating if the price was based on complete data.
+ */
+ public static DoubleBooleanPair getItemPrice(@Nullable String id) {
+ JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData();
+ JsonObject lowestBinPrices = TooltipInfoType.LOWEST_BINS.getData();
+
+ if (id == null || id.isEmpty() || bazaarPrices == null || lowestBinPrices == null) return DoubleBooleanPair.of(0, false);
+
+ if (bazaarPrices.has(id)) {
+ JsonElement sellPrice = bazaarPrices.get(id).getAsJsonObject().get("sellPrice");
+ boolean isPriceNull = sellPrice.isJsonNull();
+ return DoubleBooleanPair.of(isPriceNull ? 0 : sellPrice.getAsDouble(), !isPriceNull);
+ }
+
+ if (lowestBinPrices.has(id)) {
+ return DoubleBooleanPair.of(lowestBinPrices.get(id).getAsDouble(), true);
+ }
+
+ return DoubleBooleanPair.of(0, false);
+ }
+
+ /**
* This method converts the "timestamp" variable into the same date format as Hypixel represents it in the museum.
* Currently, there are two types of string timestamps the legacy which is built like this
* "dd/MM/yy hh:mm" ("25/04/20 16:38") and the current which is built like this
@@ -181,7 +221,7 @@ public class ItemUtils {
public static List<Text> getLore(ItemStack item) {
return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines();
}
-
+
public static PropertyMap propertyMapWithTexture(String textureValue) {
return Codecs.GAME_PROFILE_PROPERTY_MAP.parse(JsonOps.INSTANCE, JsonParser.parseString("[{\"name\":\"textures\",\"value\":\"" + textureValue + "\"}]")).getOrThrow();
}
@@ -190,12 +230,12 @@ public class ItemUtils {
if (!stack.isOf(Items.PLAYER_HEAD) || !stack.contains(DataComponentTypes.PROFILE)) return "";
ProfileComponent profile = stack.get(DataComponentTypes.PROFILE);
- String texture = profile.properties().get("textures").stream()
+ if (profile == null) return "";
+
+ return profile.properties().get("textures").stream()
.map(Property::value)
.findFirst()
.orElse("");
-
- return texture;
}
public static Optional<String> getHeadTextureOptional(ItemStack stack) {