Skip to content

Commit

Permalink
implement book and chat formatting (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 authored Jul 22, 2022
1 parent 48278b4 commit 0d89541
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ object ChattyConfig : IdofrontConfig<ChattyConfig.Data>(chattyPlugin, Data.seria
val playerHeadFont: String = "minecraft:chatty_heads",
val nicknames: Nickname = Nickname(),
val chat: Chat = Chat(),
val book: Book = Book(),
val sign: Sign = Sign(),
val ping: Ping = Ping(),
val join: Join = Join(),
val leave: Leave = Leave(),
Expand Down Expand Up @@ -101,4 +103,17 @@ object ChattyConfig : IdofrontConfig<ChattyConfig.Data>(chattyPlugin, Data.seria
val pingReceiveFormat: String = "<yellow><b>",
val pingSendFormat: String = "<i>"
)

@Serializable
data class Book(
val useDisplayNameForAuthor: Boolean = false,
val bypassFormatPermission: String = "chatty.book.bypassformat",
val allowedTags: List<ChattyTags> = emptyList(),
)

@Serializable
data class Sign(
val bypassFormatPermission: String = "chatty.sign.bypassformat",
val allowedTags: List<ChattyTags> = emptyList(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,23 @@ fun Component.fixLegacy() : Component =
// Splits <color> and <gradient:...> tags and checks if theyre allowed
fun String.verifyChatStyling(): String {
val finalString = this
this.getTags().filter { tag -> tag !in chattyConfig.nicknames.allowedTags }.forEach { tag ->
this.getTags().filter { tag -> tag !in chattyConfig.chat.allowedTags }.forEach { tag ->
finalString.replace(tag.toString().lowercase(), "\\<${tag.toString().lowercase()}")
}
return finalString
}

fun String.verifyBookStyling(): String {
val finalString = this
this.getTags().filter { tag -> tag !in chattyConfig.book.allowedTags }.forEach { tag ->
finalString.replace(tag.toString().lowercase(), "\\<${tag.toString().lowercase()}")
}
return finalString
}

fun String.verifySignStyling(): String {
val finalString = this
this.getTags().filter { tag -> tag !in chattyConfig.sign.allowedTags }.forEach { tag ->
finalString.replace(tag.toString().lowercase(), "\\<${tag.toString().lowercase()}")
}
return finalString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import com.mineinabyss.chatty.components.HideJoinLeave
import com.mineinabyss.chatty.components.PlayerData
import com.mineinabyss.chatty.helpers.*
import com.mineinabyss.geary.papermc.access.toGeary
import com.mineinabyss.idofront.messaging.miniMsg
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.block.SignChangeEvent
import org.bukkit.event.player.PlayerEditBookEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent

Expand Down Expand Up @@ -39,4 +43,45 @@ class PlayerListener : Listener {
// if (chattyConfig.leave.sendAcrossProxy)
// Bukkit.getServer().sendPluginMessage(chattyPlugin, chattyProxyChannel, PlaceholderAPI.setPlaceholders(player, messages.proxies.proxyLeave).toByteArray())
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun PlayerEditBookEvent.onBookSign() {
val meta = newBookMeta
val config = chattyConfig.book
if (player.hasPermission(chattyConfig.book.bypassFormatPermission)) {
if (meta.hasAuthor() && !config.useDisplayNameForAuthor)
meta.author(newBookMeta.author().fixLegacy())
else if (meta.hasAuthor() && config.useDisplayNameForAuthor)
meta.author(player.displayName())
if (meta.hasTitle())
meta.title(newBookMeta.title().fixLegacy())
if (meta.hasPages())
meta.pages(newBookMeta.pages().map { it.fixLegacy() })
}
else {
if (meta.hasAuthor() && !config.useDisplayNameForAuthor)
meta.author(newBookMeta.author().serialize().verifyBookStyling().miniMsg())
else if (meta.hasAuthor() && config.useDisplayNameForAuthor)
meta.author(player.displayName().serialize().verifyBookStyling().miniMsg())
if (meta.hasTitle())
meta.title(newBookMeta.title().serialize().verifyBookStyling().miniMsg())
if (meta.hasPages())
meta.pages(newBookMeta.pages().map { it.serialize().verifyBookStyling().miniMsg() })
}
newBookMeta = meta
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun SignChangeEvent.onSign() {
if (player.hasPermission(chattyConfig.sign.bypassFormatPermission)) {
lines().forEachIndexed { index, line ->
line(index, line.fixLegacy())
}
}
else {
lines().forEachIndexed { index, line ->
line(index, line.serialize().verifySignStyling().miniMsg())
}
}
}
}
29 changes: 29 additions & 0 deletions chatty-paper/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ chat:
- GRADIENT
- HOVER

book:
useDisplayNameForAuthor: false
bypassFormatPermission: "chatty.chat.bypassformat"
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
- UNDERLINE # https://docs.adventure.kyori.net/minimessage/format.html
- ITALIC
- OBFUSCATED
- TEXTCOLOR
- HEXCOLOR
- RAINBOW
- GRADIENT
- HOVER

sign:
bypassFormatPermission: "chatty.chat.bypassformat"
allowedTags: # Available formats are:
- BOLD # [TEXTCOLOR, HEXCOLOR, GRADIENT, RAINBOW, UNDERLINE, STRIKETHROUGH, BOLD, ITALIC, OBFUSCATED]
- STRIKETHROUGH # [HOVER, CLICK, FONT, SPACES, INSERTION, RESET, TRANSITION, KEYBIND, TRANSLATABLE]
- UNDERLINE # https://docs.adventure.kyori.net/minimessage/format.html
- ITALIC
- OBFUSCATED
- TEXTCOLOR
- HEXCOLOR
- RAINBOW
- GRADIENT
- HOVER

nicknames:
permission: "chatty.nickname"
nickOtherPermission: "chatty.nickname.other"
Expand Down

0 comments on commit 0d89541

Please sign in to comment.