diff --git a/uranos-api/src/main/kotlin/space/uranos/chat/ChatColor.kt b/uranos-api/src/main/kotlin/space/uranos/chat/ChatColor.kt index e2d177c..54e2848 100644 --- a/uranos-api/src/main/kotlin/space/uranos/chat/ChatColor.kt +++ b/uranos-api/src/main/kotlin/space/uranos/chat/ChatColor.kt @@ -7,6 +7,7 @@ package space.uranos.chat import com.squareup.moshi.FromJson import com.squareup.moshi.ToJson +import space.uranos.util.RGBColor @Suppress("ClassName") sealed class ChatColor(val stringRepresentation: String) { @@ -27,19 +28,12 @@ sealed class ChatColor(val stringRepresentation: String) { object YELLOW : ChatColor("yellow") object WHITE : ChatColor("white") - class Hex(code: String) : ChatColor("#" + code.removePrefix("#")) { - val code = toString() - - init { - if (!HEX_COLOR_REGEX.matches(code)) throw IllegalArgumentException("code is not a valid hexadecimal color") - } - } + class Custom(color: RGBColor) : + ChatColor("#${color.rgb.toString(16).padStart(6, '0')}") override fun toString(): String = stringRepresentation companion object { - private val HEX_COLOR_REGEX = Regex("^#(?:[0-9a-fA-F]{3}){1,2}\$") - val NAMED_COLORS by lazy { setOf( BLACK, @@ -62,12 +56,10 @@ sealed class ChatColor(val stringRepresentation: String) { } fun fromString(value: String): ChatColor = - if (value.startsWith("#")) Hex(value) - else try { - NAMED_COLORS.getValue(value) - } catch (e: NoSuchElementException) { - throw IllegalArgumentException("There is no color named '${value}'") - } + run { + if (value.startsWith("#")) RGBColor.fromString(value.removePrefix("#"))?.let { Custom(it) } + else NAMED_COLORS[value] + } ?: throw IllegalArgumentException("There is no color named '${value}'") } object JSONAdapter { @@ -78,5 +70,3 @@ sealed class ChatColor(val stringRepresentation: String) { fun fromJson(value: String) = fromString(value) } } - -val String.color get() = ChatColor.fromString(this) diff --git a/uranos-api/src/main/kotlin/space/uranos/util/RGBColor.kt b/uranos-api/src/main/kotlin/space/uranos/util/RGBColor.kt new file mode 100644 index 0000000..fa7e3e3 --- /dev/null +++ b/uranos-api/src/main/kotlin/space/uranos/util/RGBColor.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2020-2021 Moritz Ruth and Uranos contributors + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file + */ + +package space.uranos.util + +class RGBColor(rgb: Int) { + val rgb = rgb and 0xFFFFFF + + constructor( + red: UByte, + green: UByte, + blue: UByte + ) : this((red.toInt() shl 16) and (green.toInt() shl 8) and blue.toInt()) + + constructor(javaColor: java.awt.Color) : this(javaColor.rgb) + + val red: UByte by lazy { ((rgb and 0xFF0000) shr 16).toUByte() } + val green: UByte by lazy { ((rgb and 0x00FF00) shr 8).toUByte() } + val blue: UByte by lazy { (rgb and 0x0000FF).toUByte() } + + override fun toString() = "0x${rgb.toString(16).toUpperCase().padStart(6, '0')}" + + override fun equals(other: Any?): Boolean = (other == this) || (other is RGBColor && other.rgb == this.rgb) + override fun hashCode(): Int = rgb + + companion object { + fun fromString(string: String): RGBColor? = + runCatching { string.removePrefix("0x").toInt(16) }.getOrNull()?.let { RGBColor(it) } + } +} diff --git a/uranos-api/src/main/kotlin/space/uranos/world/Biome.kt b/uranos-api/src/main/kotlin/space/uranos/world/Biome.kt index fca10c9..bfc7cbf 100644 --- a/uranos-api/src/main/kotlin/space/uranos/world/Biome.kt +++ b/uranos-api/src/main/kotlin/space/uranos/world/Biome.kt @@ -6,15 +6,15 @@ package space.uranos.world import space.uranos.RegistryItem -import java.awt.Color +import space.uranos.util.RGBColor data class Biome( override val id: String, val precipitation: Precipitation, - val skyColor: Color, // TODO: Maybe replace with an own color class - val waterFogColor: Color, - val fogColor: Color, - val waterColor: Color, + val skyColor: RGBColor, + val waterFogColor: RGBColor, + val fogColor: RGBColor, + val waterColor: RGBColor, val moodSound: MoodSound, /** * Has an effect on grass and foliage color @@ -60,10 +60,10 @@ data class Biome( val PLAINS = Biome( "minecraft:plains", Precipitation.RAIN, - Color(7907327), - Color(329011), - Color(12638463), - Color(4159204), + RGBColor(7907327), + RGBColor(329011), + RGBColor(12638463), + RGBColor(4159204), MoodSound( 6000, 2.0, diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkLightDataPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkLightDataPacketCodec.kt index 3b99ec4..5dfbcda 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkLightDataPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkLightDataPacketCodec.kt @@ -11,11 +11,9 @@ import space.uranos.net.packet.OutgoingPacketCodec import space.uranos.util.checkBit import space.uranos.util.setBit -// TODO: Implement partial updates object ChunkLightDataPacketCodec : OutgoingPacketCodec(0x23, ChunkLightDataPacket::class) { private const val OUTSIDE_SECTIONS_MASK = 0b100000000000000001 - @OptIn(ExperimentalUnsignedTypes::class) override fun ChunkLightDataPacket.encode(dst: ByteBuf) { dst.writeVarInt(key.x) dst.writeVarInt(key.z) diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/DeclareRecipesPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/DeclareRecipesPacketCodec.kt index a6d5dcc..2b582af 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/DeclareRecipesPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/DeclareRecipesPacketCodec.kt @@ -12,8 +12,5 @@ import space.uranos.net.packet.OutgoingPacketCodec object DeclareRecipesPacketCodec : OutgoingPacketCodec(0x5A, DeclareRecipesPacket::class) { override fun DeclareRecipesPacket.encode(dst: ByteBuf) { dst.writeVarInt(recipes.size) - for (recipe in recipes) { - // TODO - } } }