generated from MineInAbyss/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure codebase, serialize stuff as little as possible
- Loading branch information
Showing
7 changed files
with
96 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/SkinHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.mineinabyss.chatty.helpers | ||
|
||
import com.combimagnetron.imageloader.Avatar | ||
import com.combimagnetron.imageloader.Image | ||
import com.combimagnetron.imageloader.ImageUtils | ||
import com.mineinabyss.chatty.chatty | ||
import com.mineinabyss.idofront.textcomponents.miniMsg | ||
import net.kyori.adventure.key.Key | ||
import net.kyori.adventure.text.Component | ||
import org.bukkit.OfflinePlayer | ||
import org.bukkit.entity.Player | ||
import org.bukkit.profile.PlayerTextures | ||
|
||
val playerHeadMapCache = mutableMapOf<OfflinePlayer, Component>() | ||
fun OfflinePlayer.translatePlayerHeadComponent(): Component { | ||
if (this !in playerHeadMapCache || playerHeadMapCache[this]!!.font() != Key.key(chatty.config.playerHeadFont)) { | ||
playerHeadMapCache[this] = runCatching { getPlayerHeadTexture(ascent = -5) }.getOrDefault(Component.empty()) | ||
} | ||
return playerHeadMapCache[this] ?: Component.empty() | ||
} | ||
|
||
val playerBodyMapCache = mutableMapOf<OfflinePlayer, Component>() | ||
fun Player.refreshSkinInCaches() { | ||
playerBodyMapCache -= this | ||
playerHeadMapCache -= this | ||
} | ||
fun OfflinePlayer.translateFullPlayerSkinComponent(): Component { | ||
if (this !in playerBodyMapCache || playerBodyMapCache[this]!!.font() != Key.key(chatty.config.playerHeadFont)) { | ||
playerBodyMapCache[this] = runCatching { getFullPlayerBodyTexture(ascent = -5) }.getOrDefault(Component.empty()) | ||
} | ||
return playerBodyMapCache[this] ?: Component.empty() | ||
} | ||
|
||
fun OfflinePlayer.getPlayerHeadTexture( | ||
scale: Int = 1, | ||
ascent: Int = 0, | ||
colorType: Image.ColorType = Image.ColorType.MINIMESSAGE, | ||
font: Key = Key.key(chatty.config.playerHeadFont) | ||
): Component { | ||
val image = avatarBuilder(this, scale, ascent, colorType).getBodyBufferedImage(scale).getSubimage(4, 0, 8, 8) | ||
return "<font:$font>${ImageUtils.generateStringFromImage(image, colorType, ascent)}</font>".miniMsg() | ||
} | ||
|
||
fun OfflinePlayer.getFullPlayerBodyTexture( | ||
scale: Int = 1, | ||
ascent: Int = 0, | ||
colorType: Image.ColorType = Image.ColorType.MINIMESSAGE, | ||
font: Key = Key.key(chatty.config.playerHeadFont) | ||
): Component { | ||
val image = avatarBuilder(this, scale, ascent, colorType).getBodyBufferedImage(scale) | ||
return "<font:$font>${ImageUtils.generateStringFromImage(image, colorType, ascent)}</font>".miniMsg() | ||
} | ||
|
||
private fun avatarBuilder( | ||
player: OfflinePlayer, | ||
scale: Int = 1, | ||
ascent: Int = 0, | ||
colorType: Image.ColorType = Image.ColorType.MINIMESSAGE | ||
): Avatar { | ||
return Avatar.builder().isSlim(player.playerProfile.apply { this.update() }.textures.skinModel == PlayerTextures.SkinModel.SLIM) | ||
.playerName(player.name) | ||
.ascent(ascent).colorType(colorType).scale(scale).build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters