From 99773d6a593c444151503de315f127bea6f74d49 Mon Sep 17 00:00:00 2001 From: Lorenz Date: Thu, 7 Jul 2022 00:31:50 +0200 Subject: init lorenz mod --- src/main/java/at/lorenz/mod/bazaar/BazaarApi.kt | 59 +++++++++++ src/main/java/at/lorenz/mod/bazaar/BazaarData.kt | 3 + .../java/at/lorenz/mod/bazaar/BazaarDataGrabber.kt | 116 +++++++++++++++++++++ .../java/at/lorenz/mod/bazaar/BazaarOrderHelper.kt | 87 ++++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 src/main/java/at/lorenz/mod/bazaar/BazaarApi.kt create mode 100644 src/main/java/at/lorenz/mod/bazaar/BazaarData.kt create mode 100644 src/main/java/at/lorenz/mod/bazaar/BazaarDataGrabber.kt create mode 100644 src/main/java/at/lorenz/mod/bazaar/BazaarOrderHelper.kt (limited to 'src/main/java/at/lorenz/mod/bazaar') diff --git a/src/main/java/at/lorenz/mod/bazaar/BazaarApi.kt b/src/main/java/at/lorenz/mod/bazaar/BazaarApi.kt new file mode 100644 index 000000000..28ce228e2 --- /dev/null +++ b/src/main/java/at/lorenz/mod/bazaar/BazaarApi.kt @@ -0,0 +1,59 @@ +package at.lorenz.mod.bazaar + +import at.lorenz.mod.utils.LorenzUtils + +class BazaarApi { + + companion object { + private val bazaarMap = mutableMapOf() + + fun isBazaarInventory(inventoryName: String): Boolean { + if (inventoryName.contains(" ➜ ") && !inventoryName.contains("Museum")) return true + if (BazaarOrderHelper.isBazaarOrderInventory(inventoryName)) return true + + return when (inventoryName) { + "Your Bazaar Orders" -> true + "How many do you want?" -> true + "How much do you want to pay?" -> true + "Confirm Buy Order" -> true + "Confirm Instant Buy" -> true + "At what price are you selling?" -> true + "Confirm Sell Offer" -> true + "Order options" -> true + + else -> false + } + } + + fun getCleanBazaarName(name: String): String { + if (name.endsWith(" Gemstone")) { + return name.substring(6) + } + if (name.startsWith("§")) { + return name.substring(2) + } + + return name + } + + fun getBazaarDataForName(name: String): BazaarData { + if (bazaarMap.containsKey(name)) { + val bazaarData = bazaarMap[name] + if (bazaarData != null) { + return bazaarData + } + LorenzUtils.error("Bazaar data is null for item '$name'") + } + throw Error("no bz data found for name '$name'") + } + + fun isBazaarItem(name: String): Boolean { + val bazaarName = getCleanBazaarName(name) + return bazaarMap.containsKey(bazaarName) + } + } + + init { + BazaarDataGrabber(bazaarMap).start() + } +} \ No newline at end of file diff --git a/src/main/java/at/lorenz/mod/bazaar/BazaarData.kt b/src/main/java/at/lorenz/mod/bazaar/BazaarData.kt new file mode 100644 index 000000000..a9f75370c --- /dev/null +++ b/src/main/java/at/lorenz/mod/bazaar/BazaarData.kt @@ -0,0 +1,3 @@ +package at.lorenz.mod.bazaar + +data class BazaarData(val apiName: String, val itemName: String, val sellPrice: Double, val buyPrice: Double) \ No newline at end of file diff --git a/src/main/java/at/lorenz/mod/bazaar/BazaarDataGrabber.kt b/src/main/java/at/lorenz/mod/bazaar/BazaarDataGrabber.kt new file mode 100644 index 000000000..78341e05c --- /dev/null +++ b/src/main/java/at/lorenz/mod/bazaar/BazaarDataGrabber.kt @@ -0,0 +1,116 @@ +package at.lorenz.mod.bazaar + +import at.lorenz.mod.utils.APIUtil +import at.lorenz.mod.utils.LorenzUtils +import at.lorenz.mod.utils.LorenzUtils.Companion.round +import kotlin.concurrent.fixedRateTimer + +internal class BazaarDataGrabber(private var bazaarMap: MutableMap) { + + companion object { + private val itemNames = mutableMapOf() + + private var lastData = "" + var lastTime = 0L + var blockNoChange = false + var currentlyUpdating = false + } + + private fun loadItemNames(): Boolean { + currentlyUpdating = true + try { + val itemsData = APIUtil.getJSONResponse("https://api.hypixel.net/resources/skyblock/items") + for (element in itemsData["items"].asJsonArray) { + val jsonObject = element.asJsonObject + val name = jsonObject["name"].asString + val id = jsonObject["id"].asString + itemNames[id] = name + } + currentlyUpdating = false + return true + } catch (e: Throwable) { + e.printStackTrace() + LorenzUtils.error("Error while trying to read bazaar item list from api: " + e.message) + currentlyUpdating = false + return false + } + } + + fun start() { + fixedRateTimer(name = "lorenz-bazaar-update", period = 1000L) { + //TODO add +// if (!LorenzUtils.inSkyBlock) { +// return@fixedRateTimer +// } + + if (currentlyUpdating) { + LorenzUtils.error("Bazaar update took too long! Error?") + return@fixedRateTimer + } + + if (itemNames.isEmpty()) { + if (!loadItemNames()) { + return@fixedRateTimer + } + } + checkIfUpdateNeeded() + } + } + + private fun checkIfUpdateNeeded() { + if (lastData != "") { + if (System.currentTimeMillis() - lastTime > 9_000) { + blockNoChange = true + } else { + if (blockNoChange) { + return + } + } + } + + currentlyUpdating = true + updateBazaarData() + currentlyUpdating = false + } + + private fun updateBazaarData() { + val bazaarData = APIUtil.getJSONResponse("https://api.hypixel.net/skyblock/bazaar") + if (bazaarData.toString() != lastData) { + lastData = bazaarData.toString() + lastTime = System.currentTimeMillis() + } + + val products = bazaarData["products"].asJsonObject + + for (entry in products.entrySet()) { + val apiName = entry.key + + if (apiName == "ENCHANTED_CARROT_ON_A_STICK") continue + if (apiName == "BAZAAR_COOKIE") continue + + val itemData = entry.value.asJsonObject + + val itemName = itemNames.getOrDefault(apiName, null) + if (itemName == null) { + LorenzUtils.error("Bazaar item name is null for '$apiName'! Restart to fix this problem!") + continue + } + + val sellPrice: Double = try { + itemData["sell_summary"].asJsonArray[0].asJsonObject["pricePerUnit"].asDouble.round(1) + } catch (e: Exception) { +// LorenzUtils.warning("Bazaar buy order for $itemName not found!") + 0.0 + } + val buyPrice: Double = try { + itemData["buy_summary"].asJsonArray[0].asJsonObject["pricePerUnit"].asDouble.round(1) + } catch (e: Exception) { +// LorenzUtils.warning("Bazaar sell offers for $itemName not found!") + 0.0 + } + + val data = BazaarData(apiName, itemName, sellPrice, buyPrice) + bazaarMap[itemName] = data + } + } +} \ No newline at end of file diff --git a/src/main/java/at/lorenz/mod/bazaar/BazaarOrderHelper.kt b/src/main/java/at/lorenz/mod/bazaar/BazaarOrderHelper.kt new file mode 100644 index 000000000..0daa12b7d --- /dev/null +++ b/src/main/java/at/lorenz/mod/bazaar/BazaarOrderHelper.kt @@ -0,0 +1,87 @@ +package at.lorenz.mod.bazaar + +import at.lorenz.mod.config.LorenzConfig +import at.lorenz.mod.events.GuiContainerEvent +import at.lorenz.mod.utils.ItemUtils.Companion.getLore +import at.lorenz.mod.utils.LorenzColor +import at.lorenz.mod.utils.RenderUtil.Companion.highlight +import net.minecraft.client.gui.inventory.GuiChest +import net.minecraft.client.renderer.GlStateManager +import net.minecraft.inventory.ContainerChest +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.lwjgl.opengl.GL11 + +class BazaarOrderHelper { + + companion object { + fun isBazaarOrderInventory(inventoryName: String): Boolean = when (inventoryName) { + "Your Bazaar Orders" -> true + "Co-op Bazaar Orders" -> true + else -> false + } + } + + @SubscribeEvent + fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) { + if (!LorenzConfig.lorenzBazaarOrderHelper) return + if (event.gui !is GuiChest) return + val guiChest = event.gui + val chest = guiChest.inventorySlots as ContainerChest + val inventoryName = chest.lowerChestInventory.displayName.unformattedText.trim() + + if (!isBazaarOrderInventory(inventoryName)) return + val lightingState = GL11.glIsEnabled(GL11.GL_LIGHTING) + GlStateManager.disableLighting() + GlStateManager.color(1f, 1f, 1f, 1f) + + out@ for (slot in chest.inventorySlots) { + if (slot == null) continue + if (slot.slotNumber != slot.slotIndex) continue + if (slot.stack == null) continue + + val stack = slot.stack + val displayName = stack.displayName + val isSelling = displayName.startsWith("§6§lSELL§7: ") + val isBuying = displayName.startsWith("§a§lBUY§7: ") + if (!isSelling && !isBuying) continue + + val text = displayName.split("§7: ")[1] + val name = BazaarApi.getCleanBazaarName(text) + val data = BazaarApi.getBazaarDataForName(name) + val buyPrice = data.buyPrice + val sellPrice = data.sellPrice + + val itemLore = stack.getLore() + for (line in itemLore) { + if (line.startsWith("§7Filled:")) { + if (line.endsWith(" §a§l100%!")) { + slot highlight LorenzColor.GREEN + continue@out + } + } + } + for (line in itemLore) { + if (line.startsWith("§7Price per unit:")) { + var text = line.split(": §6")[1] + text = text.substring(0, text.length - 6) + text = text.replace(",", "") + val price = text.toDouble() + if (isSelling) { + if (buyPrice < price) { + slot highlight LorenzColor.GOLD + continue@out + } + } else { + if (sellPrice > price) { + slot highlight LorenzColor.GOLD + continue@out + } + } + + } + } + } + + if (lightingState) GlStateManager.enableLighting() + } +} -- cgit