diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
6 files changed, 273 insertions, 13 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt new file mode 100644 index 000000000..a4dbede9d --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt @@ -0,0 +1,208 @@ +package at.hannibal2.skyhanni.utils + +import at.hannibal2.skyhanni.features.garden.fortuneguide.FFGuideGUI +import at.hannibal2.skyhanni.utils.LorenzUtils.round +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.FontRenderer +import net.minecraft.client.gui.GuiScreen +import net.minecraft.client.renderer.GlStateManager +import net.minecraft.client.renderer.RenderHelper +import net.minecraft.item.ItemStack +import org.lwjgl.opengl.GL11 +import java.awt.Color +import java.text.DecimalFormat +import kotlin.math.roundToInt + +/** + * Taken from NotEnoughUpdates + */ +object GuiRenderUtils { + + fun drawStringCentered(str: String?, fr: FontRenderer, x: Float, y: Float, shadow: Boolean, colour: Int) { + val strLen = fr.getStringWidth(str) + val x2 = x - strLen / 2f + val y2 = y - fr.FONT_HEIGHT / 2f + GL11.glTranslatef(x2, y2, 0f) + fr.drawString(str, 0f, 0f, colour, shadow) + GL11.glTranslatef(-x2, -y2, 0f) + } + + fun drawString(str: String, x: Float, y: Float) { + Minecraft.getMinecraft().fontRendererObj.drawString(str, x, y, 0xffffff, true) + } + + fun drawStringCentered(str: String?, x: Int, y: Int) { + drawStringCentered( + str, + Minecraft.getMinecraft().fontRendererObj, + x.toFloat(), + y.toFloat(), + true, + 0xffffff + ) + } + + fun renderItemStack(item: ItemStack, x: Int, y: Int) { + val itemRender = Minecraft.getMinecraft().renderItem + RenderHelper.enableGUIStandardItemLighting() + itemRender.zLevel = -145f + itemRender.renderItemAndEffectIntoGUI(item, x, y) + itemRender.zLevel = 0f + RenderHelper.disableStandardItemLighting() + } + + // Code taken and edited from NEU + private fun drawTooltip( + textLines: List<String>, + mouseX: Int, + mouseY: Int, + screenHeight: Int, + fr: FontRenderer + ) { + if (textLines.isNotEmpty()) { + val borderColor = StringUtils.getColor(textLines[0], 0x505000FF) + + GlStateManager.disableRescaleNormal() + RenderHelper.disableStandardItemLighting() + GlStateManager.disableLighting() + GlStateManager.enableDepth() + var tooltipTextWidth = 0 + + for (textLine in textLines) { + val textLineWidth: Int = fr.getStringWidth(textLine) + if (textLineWidth > tooltipTextWidth) { + tooltipTextWidth = textLineWidth + } + } + + val tooltipX = mouseX + 12 + var tooltipY = mouseY - 12 + var tooltipHeight = 8 + + if (textLines.size > 1) tooltipHeight += (textLines.size - 1) * 10 + 2 + GlStateManager.translate(0f, 0f, 100f) + if (tooltipY + tooltipHeight + 6 > screenHeight) tooltipY = screenHeight - tooltipHeight - 6 + // main background + GuiScreen.drawRect(tooltipX - 3, tooltipY - 3, + tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, -0xfeffff0) + + // borders + GuiScreen.drawRect(tooltipX - 3, tooltipY - 3 + 1, + tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColor) + + GuiScreen.drawRect(tooltipX + tooltipTextWidth + 2, tooltipY - 3 + 1, + tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3 - 1, borderColor) + + GuiScreen.drawRect(tooltipX - 3, tooltipY - 3, + tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColor) + + GuiScreen.drawRect(tooltipX - 3, tooltipY + tooltipHeight + 2, + tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, borderColor) + GlStateManager.translate(0f, 0f, -100f) + GlStateManager.disableDepth() + + for (line in textLines) { + fr.drawString(line, tooltipX.toFloat(), tooltipY.toFloat(), 0xffffff, true) + + tooltipY += if (line == textLines[0]) 12 else 10 + } + + GlStateManager.enableDepth() + GlStateManager.enableLighting() + GlStateManager.enableRescaleNormal() + RenderHelper.enableStandardItemLighting() + } + GlStateManager.disableLighting() + } + + fun drawTooltip(textLines: List<String>, mouseX: Int, mouseY: Int, screenHeight: Int) { + drawTooltip(textLines, mouseX, mouseY, screenHeight, Minecraft.getMinecraft().fontRendererObj) + } + + fun isPointInRect(x: Int, y: Int, left: Int, top: Int, width: Int, height: Int): Boolean { + return left <= x && x < left + width && top <= y && y < top + height + } + + fun drawProgressBar(x: Int, y: Int, barWidth: Int, progress: Float) { + GuiScreen.drawRect(x, y, x + barWidth, y + 6, 0xFF43464B.toInt()) + val width = barWidth * progress + GuiScreen.drawRect(x + 1, y + 1, (x + width).toInt() + 1, y + 5, 0xFF00FF00.toInt()) + if (progress != 1f) GuiScreen.drawRect((x + width).toInt() + 1, y + 1, x + barWidth - 1, y + 5, 0xFF013220.toInt()) + } + + fun renderItemAndTip(item: ItemStack?, x: Int, y: Int, mouseX: Int, mouseY: Int, color: Int = 0xFF43464B.toInt()) { + GuiScreen.drawRect(x, y, x + 16, y + 16, color) + if (item != null) { + renderItemStack(item, x, y) + if (isPointInRect(mouseX, mouseY, x, y, 16, 16)) { + val tt: List<String> = item.getTooltip(Minecraft.getMinecraft().thePlayer, false) + FFGuideGUI.tooltipToDisplay.addAll(tt) + } + } + } + + // assuming 70% font size + fun drawFarmingBar( + label: String, + tooltip: String, + currentValue: Number, + maxValue: Number, + xPos: Int, + yPos: Int, + width: Int, + mouseX: Int, + mouseY: Int, + output: MutableList<String>, + textScale: Float = .7f + ) { + var currentVal = currentValue.toDouble() + currentVal = if (currentVal < 0) 0.0 else currentVal + + var barProgress = currentVal / maxValue.toFloat() + if (maxValue == 0) barProgress = 1.0 + barProgress = when { + barProgress > 1 -> 1.0 + barProgress < 0 -> 0.0 + else -> barProgress + } + + val filledWidth = (width * barProgress).toInt() + val current = DecimalFormat("0.##").format(currentVal.round(2)) + val progressPercentage = (barProgress * 10000).roundToInt() / 100 + val inverseScale = 1 / textScale + val textWidth: Int = Minecraft.getMinecraft().fontRendererObj.getStringWidth("$progressPercentage%") + val barColor = barColorGradient(barProgress) + + GlStateManager.scale(textScale, textScale, textScale) + drawString(label, xPos * inverseScale, yPos * inverseScale) + drawString("§2$current / $maxValue☘", xPos * inverseScale, (yPos + 8) * inverseScale) + drawString("§2$progressPercentage%", (xPos + width - textWidth * textScale) * inverseScale, (yPos + 8) * inverseScale) + GlStateManager.scale(inverseScale, inverseScale, inverseScale) + + GuiScreen.drawRect(xPos, yPos + 16, xPos + width, yPos + 20, 0xFF43464B.toInt()) + GuiScreen.drawRect(xPos + 1, yPos + 17, xPos + width - 1, yPos + 19, barColor.darkenColor()) + GuiScreen.drawRect(xPos + 1, yPos + 17, + if (filledWidth < 2) xPos + 1 else xPos + filledWidth - 1, yPos + 19, barColor) + + if (tooltip != "") { + if (isPointInRect(mouseX, mouseY, xPos - 2, yPos - 2, width + 4, 20 + 4)) { + val split = tooltip.split("\n") + for (line in split) { + output.add(line) + } + } + } + } + + private fun barColorGradient(double: Double): Int { + var newDouble = (double - .5) * 2 + if (newDouble < 0) newDouble = 0.0 + return Color((255 * (1 - newDouble)).toInt(), (255 * newDouble).toInt(), 0).rgb + } + + fun Int.darkenColor(): Int { + val color = Color(this) + return Color(color.red / 5, color.green / 5, color.blue / 5).rgb + } + +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 69687535b..b78a23c0d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -1,10 +1,13 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.utils.ItemBlink.checkBlinkItem import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import com.google.gson.JsonObject +import com.google.gson.JsonPrimitive import io.github.moulberry.notenoughupdates.NEUManager import io.github.moulberry.notenoughupdates.NEUOverlay import io.github.moulberry.notenoughupdates.NotEnoughUpdates @@ -268,4 +271,23 @@ object NEUItems { + ";" + group("level").romanToDecimal()) } } + + //Uses NEU + fun saveNBTData(item: ItemStack, removeLore: Boolean = true): String { + val jsonObject = manager.getJsonForItem(item) + if (!jsonObject.has("internalname")) { + jsonObject.add("internalname", JsonPrimitive("_")) + } + if (removeLore) { + if (jsonObject.has("lore")) jsonObject.remove("lore") + } + val jsonString = jsonObject.toString() + return StringUtils.encodeBase64(jsonString) + } + + fun loadNBTData(encoded: String): ItemStack { + val jsonString = StringUtils.decodeBase64(encoded) + val jsonObject = ConfigManager.gson.fromJson(jsonString, JsonObject::class.java) + return manager.jsonToStack(jsonObject, false) + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index bab653f68..7e56be09e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -791,15 +791,5 @@ object RenderUtils { GlStateManager.enableDepth() } - /** - * Taken from NotEnoughUpdates - */ - fun drawStringCentered(str: String?, fr: FontRenderer, x: Float, y: Float, shadow: Boolean, colour: Int) { - val strLen = fr.getStringWidth(str) - val x2 = x - strLen / 2f - val y2 = y - fr.FONT_HEIGHT / 2f - GL11.glTranslatef(x2, y2, 0f) - fr.drawString(str, 0f, 0f, colour, shadow) - GL11.glTranslatef(-x2, -y2, 0f) - } -}
\ No newline at end of file + +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt index fa86f5a18..83c28c099 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SkyBlockItemModifierUtils.kt @@ -3,11 +3,13 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import com.google.gson.JsonObject import net.minecraft.item.ItemStack object SkyBlockItemModifierUtils { private val drillPartTypes = listOf("drill_part_upgrade_module", "drill_part_engine", "drill_part_fuel_tank") + private val petLevelPattern = "§7\\[Lvl (?<level>.*)\\] .*".toPattern() fun ItemStack.getHotPotatoCount() = getAttributeInt("hot_potato_count") @@ -32,7 +34,19 @@ object SkyBlockItemModifierUtils { fun ItemStack.getManaDisintegrators() = getAttributeInt("mana_disintegrator_count") - fun ItemStack.getPetCandyUsed() = ConfigManager.gson.fromJson(getExtraAttributes()?.getString("petInfo"), JsonObject::class.java)?.get("candyUsed")?.asInt + fun ItemStack.getPetCandyUsed() = getPetInfo()?.get("candyUsed")?.asInt + + fun ItemStack.getPetItem() = getPetInfo()?.get("heldItem")?.asString + + private fun ItemStack.getPetInfo() = + ConfigManager.gson.fromJson(getExtraAttributes()?.getString("petInfo"), JsonObject::class.java) + + fun ItemStack.getPetLevel(): Int { + petLevelPattern.matchMatcher(this.displayName) { + return group("level").toInt() + } + return 0 + } fun ItemStack.getMasterStars(): Int { val stars = mapOf( diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt index 95406b1bf..325e87211 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt @@ -8,6 +8,7 @@ import net.minecraft.util.ResourceLocation object SoundUtils { private val beepSound by lazy { createSound("random.orb", 1f) } + private val clickSound by lazy { createSound("gui.button.press", 1f) } fun ISound.playSound() { Minecraft.getMinecraft().addScheduledTask { @@ -48,4 +49,8 @@ object SoundUtils { fun playBeepSound() { beepSound.playSound() } + + fun playClickSound() { + clickSound.playSound() + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 62ab4330d..8ae1fe288 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -1,5 +1,7 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.utils.GuiRenderUtils.darkenColor +import net.minecraft.client.Minecraft import org.intellij.lang.annotations.Language import java.util.* import java.util.regex.Matcher @@ -72,4 +74,23 @@ object StringUtils { split[0].removeColor() } } + + fun getColor(string: String, default: Int, darker: Boolean = true): Int { + val stringPattern = "§[0123456789abcdef].*".toPattern() + + val matcher = stringPattern.matcher(string) + if (matcher.matches()) { + val colorInt = Minecraft.getMinecraft().fontRendererObj.getColorCode(string[1]) + return if (darker) { + colorInt.darkenColor() + } else { + "ff${Integer.toHexString(colorInt)}".toLong(radix = 16).toInt() + } + } + return default + } + + fun encodeBase64(input: String) = Base64.getEncoder().encodeToString(input.toByteArray()) + + fun decodeBase64(input: String) = Base64.getDecoder().decode(input).decodeToString() } |