Skip to content

Commit

Permalink
greatly improve playerheads
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jul 30, 2022
1 parent c900221 commit 68865e1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions chatty-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
compileOnly(chattyLibs.discordsrv)

// Shaded
implementation(chattyLibs.imageloader)
implementation(libs.idofront.core)
// compileOnly(libs.idofront.nms)
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -124,34 +123,25 @@ fun translatePlaceholders(player: Player, message: String): Component {
}

//TODO Convert to using BLHE
val playerHeadMapCache = mutableMapOf<Player, Component>()
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<Audience> {
Expand All @@ -163,18 +153,22 @@ fun setAudienceForChannelType(player: Player): Set<Audience> {
ChannelType.GLOBAL -> {
audiences.addAll(onlinePlayers)
}

ChannelType.RADIUS -> {
if (channel.channelRadius <= 0) audiences.addAll(onlinePlayers)
else audiences.addAll(onlinePlayers.filter { p ->
p.toGeary().has<SpyOnChannels>() ||
(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 {
Expand All @@ -186,7 +180,7 @@ fun setAudienceForChannelType(player: Player): Set<Audience> {

fun String.serializeLegacy() = LegacyComponentSerializer.legacy('§').deserialize(this).fixLegacy()

fun Component.fixLegacy() : Component =
fun Component.fixLegacy(): Component =
this.serialize().replace("\\<", "<").replace("\\>", ">").miniMsg()

// Splits <color> and <gradient:...> tags and checks if theyre allowed
Expand Down Expand Up @@ -224,7 +218,7 @@ fun String.getTags(): List<ChattyTags> {
val tags = mutableListOf<ChattyTags>()
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)
Expand All @@ -245,7 +239,7 @@ fun String.getTags(): List<ChattyTags> {
tag.startsWith("key") -> tags.add(ChattyTags.KEYBIND)
tag.startsWith("lang") -> tags.add(ChattyTags.TRANSLATABLE)
}
}
}
return tags.toList()
}

Expand Down
1 change: 1 addition & 0 deletions gradle/chattyLibs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 68865e1

Please sign in to comment.