From 57a069630faceb27e9adc7a4ffdc76730a3ada2e Mon Sep 17 00:00:00 2001 From: joserobjr Date: Fri, 21 Oct 2016 06:10:44 -0300 Subject: [PATCH] Adds conversion functions between bukkit's and bungee's ChatColor --- .../com/gamemods/kotlinfun/spigot/spigot.kt | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/SpigotPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/spigot/spigot.kt b/SpigotPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/spigot/spigot.kt index ebda4b8..7f762a2 100644 --- a/SpigotPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/spigot/spigot.kt +++ b/SpigotPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/spigot/spigot.kt @@ -3,11 +3,14 @@ package br.com.gamemods.kotlinfun.spigot import br.com.gamemods.kotlinfun.bukkit.Colorized import br.com.gamemods.kotlinfun.bungee.ComponentArray +import br.com.gamemods.kotlinfun.bungee.* import net.md_5.bungee.api.ChatColor import net.md_5.bungee.api.chat.* +import java.util.* fun Colorized.builder() = ComponentArray(create()) fun Colorized.color(color: ChatColor) = builder().color(color) +fun Colorized.color(color: org.bukkit.ChatColor) = builder().color(color.asBungee()) fun Colorized.obfuscated(magic: Boolean) = builder().obfuscated(magic) fun Colorized.bold(bold: Boolean) = builder().bold(bold) fun Colorized.strikethrough(strike: Boolean) = builder().strikethrough(strike) @@ -26,3 +29,98 @@ operator fun Colorized.plus(o: BaseComponent) = builder() + o operator fun Colorized.plus(o: ComponentBuilder) = builder() + o operator fun Colorized.plus(o: ComponentArray) = builder() + o operator fun Colorized.plus(o: Array) = builder() + o + +fun br.com.gamemods.kotlinfun.bungee.Colorized.color(color: org.bukkit.ChatColor) = color(color.asBungee()) + +fun BaseComponent.format(style: org.bukkit.ChatColor?) = format(style?.asBungee()) +fun BaseComponent.setColor(color: org.bukkit.ChatColor?) = setColor(color?.asBungee()) +fun BaseComponent.format(style: Iterable) : BaseComponent{ + for (st in style) + format(st) + return this +} +operator fun Array.plus(o: org.bukkit.ChatColor) = this + o.asBungee() +operator fun BaseComponent.plus(o: org.bukkit.ChatColor) = ComponentArray(this) + o.asBungee() + +operator fun org.bukkit.ChatColor.plus(o: BaseComponent) = component() + o +operator fun org.bukkit.ChatColor.plus(o: ComponentBuilder) = component() + o +operator fun org.bukkit.ChatColor.plus(o: ComponentArray) = component() + o +operator fun org.bukkit.ChatColor.plus(o: Array) = component() + o +fun org.bukkit.ChatColor.component() = asBungee().component() + +fun ComponentBuilder.format(style: org.bukkit.ChatColor?) = format(style?.asBungee()) +fun ComponentBuilder.format(style: Iterable) : ComponentBuilder{ + for (st in style) + format(st) + return this +} + +fun Iterable.asBungeeNullable() : Iterable { + return object : Iterable { + override fun iterator(): Iterator { + val host = this@asBungeeNullable.iterator() + return object : Iterator { + override fun hasNext() = host.hasNext() + + override fun next() = host.next()?.asBungee() + } + } + } +} + +fun Iterable.asBungee() : Iterable { + return object : Iterable { + override fun iterator(): Iterator { + val host = this@asBungee.iterator() + return object : Iterator { + override fun hasNext() = host.hasNext() + + override fun next() = host.next().asBungee() + } + } + } +} + +private val bungeeToBukkit = EnumMap(ChatColor::class.java) + get() { + if(field.isEmpty()) { + val bungee = ChatColor.values() + val bukkit = org.bukkit.ChatColor.values() + for (color in bungee) + field[color] = bukkit[color.ordinal] + } + return field + } + +fun ChatColor.asBukkit() = bungeeToBukkit[this]!! + +fun Iterable.asBukkitNullable() : Iterable { + return object : Iterable { + override fun iterator(): Iterator { + val host = this@asBukkitNullable.iterator() + return object : Iterator { + override fun hasNext() = host.hasNext() + + override fun next() = host.next()?.asBukkit() + } + } + } +} + +fun Iterable.asBukkit() : Iterable { + return object : Iterable { + override fun iterator(): Iterator { + val host = this@asBukkit.iterator() + return object : Iterator { + override fun hasNext() = host.hasNext() + + override fun next() = host.next().asBukkit() + } + } + } +} + +fun ComponentArray.color(color: org.bukkit.ChatColor) = color(color.asBungee()) +fun ComponentArray.format(format: org.bukkit.ChatColor) = format(format.asBungee()) +fun ComponentArray.format(formats: Iterable) = builder.format(formats) +operator fun ComponentArray.plus(o: org.bukkit.ChatColor) = plus(o.asBungee())