-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TheChilliPL/dev
Version 0.1, merge dev to master
- Loading branch information
Showing
9 changed files
with
295 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,5 @@ gradle-app.setting | |
.gradletasknamecache | ||
|
||
# IntelliJ IDEA | ||
/.idea/ | ||
/.idea/* | ||
!/.idea/codeStyles |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.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,29 @@ | ||
package me.patrykanuszczyk.textcomponentserialization | ||
|
||
import net.md_5.bungee.api.chat.TextComponent | ||
import org.bukkit.configuration.ConfigurationSection | ||
import org.bukkit.inventory.ItemStack | ||
|
||
fun ConfigurationSection.getTextComponent(path: String): TextComponent? { | ||
return deserializeTextComponent(this.get(path)) | ||
} | ||
|
||
fun ConfigurationSection.setTextComponent(path: String, value: TextComponent?) { | ||
this.set(path, value.serialize()) | ||
} | ||
|
||
fun ConfigurationSection.getTextComponentList(path: String): List<TextComponent?> { | ||
return this.getList(path).map { deserializeTextComponent(it) } | ||
} | ||
|
||
fun ConfigurationSection.setTextComponentList(path: String, value: List<TextComponent?>) { | ||
this.set(path, value.map { it.serialize() }) | ||
} | ||
|
||
fun ConfigurationSection.getBook(path: String): ItemStack? { | ||
return deserializeBook(this.get(path)) | ||
} | ||
|
||
fun ConfigurationSection.setBook(path: String, value: ItemStack?) { | ||
this.set(path, serializeBook(value)) | ||
} |
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
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.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,55 @@ | ||
package me.patrykanuszczyk.textcomponentserialization | ||
|
||
import net.md_5.bungee.api.chat.ClickEvent | ||
import net.md_5.bungee.api.chat.HoverEvent | ||
import net.md_5.bungee.api.chat.TextComponent | ||
import org.bukkit.ChatColor | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.BookMeta | ||
|
||
fun formatString(string: String, placeholders: Map<String, String>? = null): String { | ||
var stringFormatted = string | ||
placeholders?.forEach { (key, value) -> | ||
stringFormatted = stringFormatted.replace("{$key}", value, true) | ||
} | ||
return ChatColor.translateAlternateColorCodes('&', stringFormatted) | ||
} | ||
|
||
fun TextComponent.format(placeholders: Map<String, String>? = null): TextComponent { | ||
val newComponent = TextComponent(this) | ||
if (text != null) newComponent.text = formatString(text, placeholders) | ||
//extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } | ||
if(extra != null) | ||
newComponent.extra = extra.map { (it as TextComponent).format(placeholders) } | ||
if (insertion != null) newComponent.insertion = formatString(insertion, placeholders) | ||
if (clickEvent != null) | ||
newComponent.clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) | ||
if (hoverEvent != null) | ||
newComponent.hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { | ||
(it as TextComponent).format(placeholders) | ||
}.toTypedArray()) | ||
return newComponent | ||
} | ||
|
||
fun formatBook(book: ItemStack?, placeholders: Map<String, String>? = null): ItemStack? { | ||
if (book == null) return null | ||
|
||
val oldMeta = book.itemMeta as BookMeta | ||
|
||
val newBook = ItemStack(book) | ||
|
||
val meta = newBook.itemMeta as BookMeta | ||
meta.title = formatString(oldMeta.title, placeholders) | ||
meta.author = formatString(oldMeta.author, placeholders) | ||
meta.generation = oldMeta.generation | ||
|
||
meta.spigot().addPage(*oldMeta.spigot().pages.map { page -> | ||
page.map { | ||
(it as TextComponent).format(placeholders) | ||
}.toTypedArray() | ||
}.toTypedArray()) | ||
|
||
newBook.itemMeta = meta | ||
|
||
return newBook | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.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,12 @@ | ||
package me.patrykanuszczyk.textcomponentserialization | ||
|
||
import net.md_5.bungee.api.chat.TextComponent | ||
|
||
val TextComponent.isEmpty: Boolean | ||
get() { | ||
if(!text.isNullOrEmpty()) return false | ||
|
||
return extra.all { it is TextComponent && it.isEmpty } | ||
} | ||
|
||
val TextComponent.isNotEmpty: Boolean get() = !isEmpty |
3 changes: 1 addition & 2 deletions
3
src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/mapConvert.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
Oops, something went wrong.