From 68865e147b7beb3d4760a0b27d5d27bbd6811554 Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 30 Jul 2022 19:11:14 +0200 Subject: [PATCH] greatly improve playerheads --- chatty-paper/build.gradle.kts | 1 + .../mineinabyss/chatty/helpers/ChatHelpers.kt | 70 +++++++++---------- gradle/chattyLibs.versions.toml | 1 + 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/chatty-paper/build.gradle.kts b/chatty-paper/build.gradle.kts index f1f192c..7d7148d 100644 --- a/chatty-paper/build.gradle.kts +++ b/chatty-paper/build.gradle.kts @@ -30,6 +30,7 @@ dependencies { compileOnly(chattyLibs.discordsrv) // Shaded + implementation(chattyLibs.imageloader) implementation(libs.idofront.core) // compileOnly(libs.idofront.nms) } diff --git a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/ChatHelpers.kt b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/ChatHelpers.kt index 06486df..5946441 100644 --- a/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/ChatHelpers.kt +++ b/chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/ChatHelpers.kt @@ -1,18 +1,17 @@ package com.mineinabyss.chatty.helpers -import com.destroystokyo.paper.ClientOption +import com.combimagnetron.imageloader.Image import com.mineinabyss.chatty.components.ChannelType import com.mineinabyss.chatty.components.SpyOnChannels import com.mineinabyss.chatty.components.chattyData import com.mineinabyss.geary.papermc.access.toGeary -import com.mineinabyss.idofront.font.Space import com.mineinabyss.idofront.messaging.miniMsg import me.clip.placeholderapi.PlaceholderAPI import net.kyori.adventure.audience.Audience import net.kyori.adventure.key.Key import net.kyori.adventure.text.Component import net.kyori.adventure.text.TextReplacementConfig -import net.kyori.adventure.text.format.TextColor +import net.kyori.adventure.text.format.Style import net.kyori.adventure.text.minimessage.MiniMessage import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer @@ -21,8 +20,6 @@ import org.bukkit.ChatColor import org.bukkit.Sound import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import java.awt.Color -import javax.imageio.ImageIO val ZERO_WIDTH = "\u200B" val ping = chattyConfig.ping @@ -54,7 +51,8 @@ fun Component.handlePlayerPings(player: Player, pingedPlayer: Player) { val pingMessage = this.replaceText( TextReplacementConfig.builder() .match(ping.pingPrefix + player.chattyData.displayName) - .replacement((ping.pingReceiveFormat + clickToReply + ping.pingPrefix + player.chattyData.displayName).miniMsg()).build() + .replacement((ping.pingReceiveFormat + clickToReply + ping.pingPrefix + player.chattyData.displayName).miniMsg()) + .build() ) if (!pingedPlayer.chattyData.disablePingSound) @@ -64,7 +62,8 @@ fun Component.handlePlayerPings(player: Player, pingedPlayer: Player) { val pingerMessage = this.replaceText( TextReplacementConfig.builder() .match(ping.pingPrefix + player.chattyData.displayName) - .replacement((ping.pingSendFormat + clickToReply + ping.pingPrefix + player.chattyData.displayName).miniMsg()).build() + .replacement((ping.pingSendFormat + clickToReply + ping.pingPrefix + player.chattyData.displayName).miniMsg()) + .build() ) player.sendMessage(pingerMessage) } @@ -124,34 +123,25 @@ fun translatePlaceholders(player: Player, message: String): Component { } //TODO Convert to using BLHE +val playerHeadMapCache = mutableMapOf() fun Player.translatePlayerHeadComponent(): Component { - val message = Component.text() - val url = playerProfile.textures.skin ?: return message.build() - val hasHat = getClientOption(ClientOption.SKIN_PARTS).hasHatsEnabled() - val c = ImageIO.read(url) - var pixel = '#' - - (0..9).forEach { x -> - val component = Component.text("$pixel!").font(Key.key("minecraft:chatty_heads")) - (0..7).forEach { y -> - val isTransparent = Color(c.getRGB(40 + y, 8 + x - 2), true).alpha <= 254 - val color = if (isTransparent || !hasHat) c.getRGB(8 + y, 8 + x - 2) else c.getRGB(40 + y, 8 + x - 2) - message.append(component.color(TextColor.color(color))) - } - - when (x) { - 1 -> pixel = '#' - 2 -> pixel = '$' - 3 -> pixel = '%' - 4 -> pixel = '&' - 5 -> pixel = '\'' - 6 -> pixel = '(' - 7 -> pixel = ')' - 8 -> pixel = '*' - } - message.append(Space.of(-12).miniMsg()) + if (this !in playerHeadMapCache) { + val image = convertURLToImageString("https://api.mineatar.io/face/$name?scale=1") + playerHeadMapCache[this] = + convertToImageComponent(image, Key.key(chattyConfig.playerHeadFont)) + .append(Component.text("").font(Key.key("minecraft:default"))) } - return message.append(Space.of(8).miniMsg()).build() + return playerHeadMapCache[this]!! +} + +private fun convertToImageComponent(image: String, font: Key): Component { + return MiniMessage.builder().build().deserialize(image).style(Style.style().font(font).build()) +} + +private fun convertURLToImageString( + url: String, ascent: Int = 4, colorType: Image.ColorType = Image.ColorType.MINIMESSAGE +): String { + return Image.builder().image(url).colorType(colorType).ascent(ascent).build().generate() } fun setAudienceForChannelType(player: Player): Set { @@ -163,18 +153,22 @@ fun setAudienceForChannelType(player: Player): Set { ChannelType.GLOBAL -> { audiences.addAll(onlinePlayers) } + ChannelType.RADIUS -> { if (channel.channelRadius <= 0) audiences.addAll(onlinePlayers) else audiences.addAll(onlinePlayers.filter { p -> p.toGeary().has() || - (player.world == p.world && p.location.distanceSquared(player.location) <= channel.channelRadius) + (player.world == p.world && p.location.distanceSquared(player.location) <= channel.channelRadius) }) } + ChannelType.PERMISSION -> { audiences.addAll(onlinePlayers.filter { p -> p.checkPermission(channel.permission) }) } // Intended for Guilds etc., want to consider finding a non-permission way for this - ChannelType.PRIVATE -> { audiences.add(player) } + ChannelType.PRIVATE -> { + audiences.add(player) + } } audiences.addAll(onlinePlayers.filter { @@ -186,7 +180,7 @@ fun setAudienceForChannelType(player: Player): Set { fun String.serializeLegacy() = LegacyComponentSerializer.legacy('ยง').deserialize(this).fixLegacy() -fun Component.fixLegacy() : Component = +fun Component.fixLegacy(): Component = this.serialize().replace("\\<", "<").replace("\\>", ">").miniMsg() // Splits and tags and checks if theyre allowed @@ -224,7 +218,7 @@ fun String.getTags(): List { val tags = mutableListOf() if (" " in this) tags.add(ChattyTags.SPACES) MiniMessage.builder().build().deserializeToTree(this).toString() - .split("TagNode(",") {").filter { "Node" !in it && it.isNotBlank() }.toList().forEach { + .split("TagNode(", ") {").filter { "Node" !in it && it.isNotBlank() }.toList().forEach { val tag = it.replace("'", "").replace(",", "") when { tag in ChatColor.values().toString().lowercase() -> tags.add(ChattyTags.TEXTCOLOR) @@ -245,7 +239,7 @@ fun String.getTags(): List { tag.startsWith("key") -> tags.add(ChattyTags.KEYBIND) tag.startsWith("lang") -> tags.add(ChattyTags.TRANSLATABLE) } - } + } return tags.toList() } diff --git a/gradle/chattyLibs.versions.toml b/gradle/chattyLibs.versions.toml index 2d6af56..e874d63 100644 --- a/gradle/chattyLibs.versions.toml +++ b/gradle/chattyLibs.versions.toml @@ -13,3 +13,4 @@ bondrewdlikeshisemotes = { module = "io.github.bananapuncher714:bondrewdlikeshis velocity = { module = "com.velocitypowered:velocity-api", version.ref = "velocity" } discordsrv = { module = "com.discordsrv:discordsrv", version.ref = "discordsrv" } placeholderapi = "me.clip:placeholderapi:2.11.1" +imageloader = "com.combimagnetron:imageloader:1.0-SNAPSHOT"