mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 17:37:58 -04:00
convert ChatColors to kotlin and refactor
This commit is contained in:
parent
8ebbb81fcf
commit
d5f77b79f2
@ -15,16 +15,17 @@ package de.bixilon.minosoft.data.commands.parser
|
||||
import de.bixilon.minosoft.data.commands.CommandStringReader
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.ColorNotFoundCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.ChatCode
|
||||
import de.bixilon.minosoft.data.text.RGBColor
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
|
||||
object ColorParser : CommandParser() {
|
||||
|
||||
override fun parse(connection: PlayConnection, properties: ParserProperties?, stringReader: CommandStringReader): Any? {
|
||||
override fun parse(connection: PlayConnection, properties: ParserProperties?, stringReader: CommandStringReader): RGBColor {
|
||||
val color = stringReader.readUnquotedString()
|
||||
try {
|
||||
return ChatColors.getChatFormattingByName(color)
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
return ChatCode[color] as RGBColor
|
||||
} catch (exception: Exception) {
|
||||
throw ColorNotFoundCommandParseException(stringReader, color, exception)
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ import de.bixilon.minosoft.data.commands.CommandStringReader
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.ColorNotFoundCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.UnknownOperationCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.ChatCode
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
|
||||
object ScoreboardSlotParser : CommandParser() {
|
||||
@ -28,7 +28,7 @@ object ScoreboardSlotParser : CommandParser() {
|
||||
if (slot.startsWith("sidebar.team.")) {
|
||||
val color = slot.substring("sidebar.team.".length)
|
||||
try {
|
||||
return ChatColors.getChatFormattingByName(color)
|
||||
return ChatCode[color]
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
throw ColorNotFoundCommandParseException(stringReader, color)
|
||||
}
|
||||
|
@ -16,8 +16,6 @@ package de.bixilon.minosoft.data.entities.block
|
||||
import de.bixilon.minosoft.data.mappings.MultiResourceLocationAble
|
||||
import de.bixilon.minosoft.data.mappings.ResourceLocation
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.RGBColor
|
||||
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
import de.bixilon.minosoft.util.KUtil.toResourceLocationList
|
||||
|
||||
@ -27,37 +25,7 @@ class BedBlockEntity(connection: PlayConnection) : BlockEntity(connection) {
|
||||
|
||||
|
||||
override fun updateNBT(nbt: Map<String, Any>) {
|
||||
color = nbt["color"]?.let {
|
||||
when (it) {
|
||||
is String -> {
|
||||
it.asColor()
|
||||
}
|
||||
is Number -> {
|
||||
when (it.toInt()) {
|
||||
0 -> RGBColor(255, 255, 255) // white
|
||||
1 -> RGBColor(234, 103, 3) // orange
|
||||
2 -> RGBColor(199, 78, 189) // magenta
|
||||
3 -> RGBColor(47, 162, 212) // light blue
|
||||
4 -> RGBColor(251, 194, 32) // yellow
|
||||
5 -> RGBColor(101, 178, 24) // lime
|
||||
6 -> RGBColor(236, 126, 161) // pink
|
||||
7 -> RGBColor(76, 76, 76) // gray
|
||||
8 -> RGBColor(130, 130, 120) // light gray
|
||||
9 -> RGBColor(22, 128, 142) // cyan
|
||||
10 -> RGBColor(99, 30, 154) // purple
|
||||
11 -> RGBColor(44, 46, 143) // blue
|
||||
12 -> RGBColor(105, 65, 35) // brown
|
||||
13 -> RGBColor(77, 97, 34) // green
|
||||
14 -> RGBColor(139, 30, 31) // red
|
||||
15 -> RGBColor(15, 16, 19) // black
|
||||
else -> TODO("Can not find color!")
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
} ?: ChatColors.RED
|
||||
color = ChatColors.RED // ToDo
|
||||
}
|
||||
|
||||
companion object : BlockEntityFactory<BedBlockEntity>, MultiResourceLocationAble {
|
||||
|
@ -40,7 +40,7 @@ class Cat(connection: PlayConnection, entityType: EntityType, position: Vec3, ro
|
||||
|
||||
@get:EntityMetaDataFunction(name = "Collar color")
|
||||
val collarColor: RGBColor
|
||||
get() = ChatColors.getColorById(entityMetaData.sets.getInt(EntityMetaDataFields.CAT_GET_COLLAR_COLOR))
|
||||
get() = ChatColors[entityMetaData.sets.getInt(EntityMetaDataFields.CAT_GET_COLLAR_COLOR)]
|
||||
|
||||
enum class CatVariants {
|
||||
TABBY,
|
||||
|
@ -27,7 +27,7 @@ class Sheep(connection: PlayConnection, entityType: EntityType, position: Vec3,
|
||||
|
||||
@get:EntityMetaDataFunction(name = "Color")
|
||||
val color: RGBColor
|
||||
get() = ChatColors.getColorById(entityMetaData.sets.getByte(EntityMetaDataFields.SHEEP_FLAGS).toInt() and 0x0F)
|
||||
get() = ChatColors[entityMetaData.sets.getByte(EntityMetaDataFields.SHEEP_FLAGS).toInt() and 0x0F]
|
||||
|
||||
@get:EntityMetaDataFunction(name = "Is sheared")
|
||||
val isSheared: Boolean
|
||||
|
@ -33,7 +33,7 @@ class Wolf(connection: PlayConnection, entityType: EntityType, position: Vec3, r
|
||||
|
||||
@get:EntityMetaDataFunction(name = "Collar color")
|
||||
val collarColor: RGBColor
|
||||
get() = ChatColors.getColorById(entityMetaData.sets.getInt(EntityMetaDataFields.WOLF_COLLAR_COLOR))
|
||||
get() = ChatColors[entityMetaData.sets.getInt(EntityMetaDataFields.WOLF_COLLAR_COLOR)]
|
||||
|
||||
// ToDo
|
||||
@get:EntityMetaDataFunction(name = "Anger time")
|
||||
|
@ -42,7 +42,7 @@ class Shulker(connection: PlayConnection, entityType: EntityType, position: Vec3
|
||||
|
||||
@get:EntityMetaDataFunction(name = "Color")
|
||||
val color: RGBColor
|
||||
get() = ChatColors.getColorById(entityMetaData.sets.getByte(EntityMetaDataFields.SHULKER_COLOR).toInt())
|
||||
get() = ChatColors[entityMetaData.sets.getByte(EntityMetaDataFields.SHULKER_COLOR).toInt()]
|
||||
|
||||
|
||||
companion object : EntityFactory<Shulker> {
|
||||
|
@ -20,7 +20,6 @@ import de.bixilon.minosoft.data.entities.entities.EntityMetaDataFunction
|
||||
import de.bixilon.minosoft.data.entities.entities.LivingEntity
|
||||
import de.bixilon.minosoft.data.mappings.entities.EntityType
|
||||
import de.bixilon.minosoft.data.player.Hands
|
||||
import de.bixilon.minosoft.data.player.PlayerProperties
|
||||
import de.bixilon.minosoft.data.player.PlayerProperty
|
||||
import de.bixilon.minosoft.data.player.tab.TabListItem
|
||||
import de.bixilon.minosoft.data.world.World
|
||||
@ -35,7 +34,7 @@ abstract class PlayerEntity(
|
||||
position: Vec3 = Vec3(0, 0, 0),
|
||||
rotation: EntityRotation = EntityRotation(0.0, 0.0),
|
||||
name: String = "TBA",
|
||||
properties: Map<PlayerProperties, PlayerProperty> = mapOf(),
|
||||
properties: Map<String, PlayerProperty> = mapOf(),
|
||||
var tabListItem: TabListItem = TabListItem(name = name, gamemode = Gamemodes.SURVIVAL, properties = properties),
|
||||
) : LivingEntity(connection, entityType, position, rotation) {
|
||||
override val dimensions: Vec2
|
||||
|
@ -18,7 +18,6 @@ import de.bixilon.minosoft.data.entities.EntityRotation
|
||||
import de.bixilon.minosoft.data.mappings.ResourceLocation
|
||||
import de.bixilon.minosoft.data.mappings.entities.EntityFactory
|
||||
import de.bixilon.minosoft.data.mappings.entities.EntityType
|
||||
import de.bixilon.minosoft.data.player.PlayerProperties
|
||||
import de.bixilon.minosoft.data.player.PlayerProperty
|
||||
import de.bixilon.minosoft.data.player.tab.TabListItem
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
@ -30,7 +29,7 @@ class RemotePlayerEntity(
|
||||
position: Vec3 = Vec3(0, 0, 0),
|
||||
rotation: EntityRotation = EntityRotation(0.0, 0.0),
|
||||
name: String = "TBA",
|
||||
properties: Map<PlayerProperties, PlayerProperty> = mapOf(),
|
||||
properties: Map<String, PlayerProperty> = mapOf(),
|
||||
tabListItem: TabListItem = TabListItem(name = name, gamemode = Gamemodes.SURVIVAL, properties = properties),
|
||||
) : PlayerEntity(connection, entityType, position, rotation, name, properties, tabListItem) {
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
* Copyright (C) 2021 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
@ -11,13 +11,8 @@
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.data.text;
|
||||
package de.bixilon.minosoft.data.player
|
||||
|
||||
public interface ChatFormattingCode extends ChatCode {
|
||||
char getChar();
|
||||
|
||||
@Override
|
||||
String toString();
|
||||
|
||||
String getANSI();
|
||||
object DefaultPlayerProperties {
|
||||
const val TEXTURES = "textures"
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.data.player;
|
||||
|
||||
public class PlayerProperty {
|
||||
private final PlayerProperties property;
|
||||
private final String value;
|
||||
private final String signature;
|
||||
|
||||
public PlayerProperty(PlayerProperties property, String value, String signature) {
|
||||
this.property = property;
|
||||
this.value = value;
|
||||
this.signature = signature;
|
||||
}
|
||||
|
||||
public PlayerProperty(PlayerProperties property, String value) {
|
||||
this.property = property;
|
||||
this.value = value;
|
||||
this.signature = null;
|
||||
}
|
||||
|
||||
public PlayerProperties getProperty() {
|
||||
return this.property;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public boolean isSigned() {
|
||||
// ToDo check signature
|
||||
return this.signature != null;
|
||||
}
|
||||
|
||||
public String getSignature() {
|
||||
return this.signature;
|
||||
}
|
||||
}
|
@ -10,28 +10,13 @@
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.player
|
||||
|
||||
package de.bixilon.minosoft.data.player;
|
||||
|
||||
public enum PlayerProperties {
|
||||
TEXTURES("textures");
|
||||
// ToDo
|
||||
private final String name;
|
||||
|
||||
PlayerProperties(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static PlayerProperties byName(String name) {
|
||||
for (PlayerProperties property : values()) {
|
||||
if (property.getName().equals(name)) {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
class PlayerProperty(
|
||||
val key: String,
|
||||
val value: String,
|
||||
val signature: String? = null,
|
||||
) {
|
||||
val isSigned: Boolean
|
||||
get() = signature != null // ToDo check signature
|
||||
}
|
@ -14,7 +14,6 @@
|
||||
package de.bixilon.minosoft.data.player.tab
|
||||
|
||||
import de.bixilon.minosoft.data.abilities.Gamemodes
|
||||
import de.bixilon.minosoft.data.player.PlayerProperties
|
||||
import de.bixilon.minosoft.data.player.PlayerProperty
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
|
||||
@ -23,7 +22,7 @@ data class TabListItem(
|
||||
var ping: Int = -1,
|
||||
var gamemode: Gamemodes = Gamemodes.SURVIVAL,
|
||||
var displayName: ChatComponent = ChatComponent.of(name),
|
||||
var properties: Map<PlayerProperties, PlayerProperty> = mutableMapOf(),
|
||||
var properties: Map<String, PlayerProperty> = mutableMapOf(),
|
||||
) {
|
||||
|
||||
fun merge(data: TabListItemData) {
|
||||
|
@ -14,7 +14,6 @@
|
||||
package de.bixilon.minosoft.data.player.tab
|
||||
|
||||
import de.bixilon.minosoft.data.abilities.Gamemodes
|
||||
import de.bixilon.minosoft.data.player.PlayerProperties
|
||||
import de.bixilon.minosoft.data.player.PlayerProperty
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
|
||||
@ -24,6 +23,6 @@ data class TabListItemData(
|
||||
var gamemode: Gamemodes? = null,
|
||||
var hasDisplayName: Boolean? = null,
|
||||
var displayName: ChatComponent? = null,
|
||||
val properties: Map<PlayerProperties, PlayerProperty>? = null,
|
||||
val properties: Map<String, PlayerProperty>? = null,
|
||||
var remove: Boolean = false, // used for legacy tab list
|
||||
)
|
||||
|
@ -18,6 +18,8 @@ import com.google.gson.JsonObject
|
||||
import de.bixilon.minosoft.data.locale.minecraft.Translator
|
||||
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||
import de.bixilon.minosoft.util.KUtil.nullCast
|
||||
import glm_.vec2.Vec2i
|
||||
import javafx.collections.ObservableList
|
||||
import javafx.scene.Node
|
||||
import java.text.CharacterIterator
|
||||
|
59
src/main/java/de/bixilon/minosoft/data/text/ChatCode.kt
Normal file
59
src/main/java/de/bixilon/minosoft/data/text/ChatCode.kt
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.text
|
||||
|
||||
import de.bixilon.minosoft.util.KUtil.extend
|
||||
|
||||
interface ChatCode {
|
||||
|
||||
companion object {
|
||||
val FORMATTING_CODES: Map<String, ChatCode> = ChatColors.NAME_MAP.extend(
|
||||
"dark_grey" to ChatColors.DARK_GRAY,
|
||||
"obfuscated" to PreChatFormattingCodes.OBFUSCATED,
|
||||
"bold" to PreChatFormattingCodes.BOLD,
|
||||
"strikethrough" to PreChatFormattingCodes.STRIKETHROUGH,
|
||||
"underlined" to PreChatFormattingCodes.UNDERLINED,
|
||||
"italic" to PreChatFormattingCodes.ITALIC,
|
||||
"reset" to PostChatFormattingCodes.RESET,
|
||||
)
|
||||
val FORMATTING_CODES_ID: List<ChatCode> = ChatColors.VALUES.toList().extend(
|
||||
PreChatFormattingCodes.OBFUSCATED,
|
||||
PreChatFormattingCodes.BOLD,
|
||||
PreChatFormattingCodes.STRIKETHROUGH,
|
||||
PreChatFormattingCodes.UNDERLINED,
|
||||
PreChatFormattingCodes.UNDERLINED,
|
||||
PreChatFormattingCodes.ITALIC,
|
||||
PostChatFormattingCodes.RESET,
|
||||
)
|
||||
|
||||
operator fun get(name: String): ChatCode? {
|
||||
return FORMATTING_CODES[name]
|
||||
}
|
||||
|
||||
operator fun get(id: Int): ChatCode? {
|
||||
return FORMATTING_CODES_ID.getOrNull(id)
|
||||
}
|
||||
|
||||
operator fun get(char: Char): ChatCode? {
|
||||
return this[Character.digit(char, 16)]
|
||||
}
|
||||
|
||||
operator fun get(chatCode: ChatCode): String? {
|
||||
val index = FORMATTING_CODES_ID.indexOf(chatCode)
|
||||
if (index == -1) {
|
||||
return null
|
||||
}
|
||||
return "%x".format(index)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.data.text;
|
||||
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import org.checkerframework.common.value.qual.IntRange;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public final class ChatColors {
|
||||
public static final RGBColor BLACK = new RGBColor(0, 0, 0);
|
||||
public static final RGBColor DARK_BLUE = new RGBColor(0, 0, 170);
|
||||
public static final RGBColor DARK_GREEN = new RGBColor(0, 170, 0);
|
||||
public static final RGBColor DARK_AQUA = new RGBColor(0, 170, 170);
|
||||
public static final RGBColor DARK_RED = new RGBColor(170, 0, 0);
|
||||
public static final RGBColor DARK_PURPLE = new RGBColor(170, 0, 170);
|
||||
public static final RGBColor GOLD = new RGBColor(255, 170, 0);
|
||||
public static final RGBColor GRAY = new RGBColor(170, 170, 170);
|
||||
public static final RGBColor DARK_GRAY = new RGBColor(85, 85, 85);
|
||||
public static final RGBColor BLUE = new RGBColor(85, 85, 255);
|
||||
public static final RGBColor GREEN = new RGBColor(85, 255, 85);
|
||||
public static final RGBColor AQUA = new RGBColor(85, 255, 255);
|
||||
public static final RGBColor RED = new RGBColor(255, 85, 85);
|
||||
public static final RGBColor LIGHT_PURPLE = new RGBColor(255, 85, 255);
|
||||
public static final RGBColor YELLOW = new RGBColor(255, 255, 85);
|
||||
public static final RGBColor WHITE = new RGBColor(255, 255, 255);
|
||||
|
||||
private static final HashBiMap<RGBColor, Integer> COLOR_ID_MAP = HashBiMap.create();
|
||||
private static final RGBColor[] COLORS = {BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY, DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE};
|
||||
|
||||
static {
|
||||
for (int i = 0; i < COLORS.length; i++) {
|
||||
COLOR_ID_MAP.put(COLORS[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getANSIColorByFormattingChar(char c) {
|
||||
return getANSIColorByRGBColor(getColorByFormattingChar(c));
|
||||
}
|
||||
|
||||
public static String getANSIColorByRGBColor(RGBColor color) {
|
||||
return String.format("\033[38;2;%d;%d;%dm", color.getRed(), color.getGreen(), color.getBlue());
|
||||
}
|
||||
|
||||
public static RGBColor getColorByFormattingChar(char c) {
|
||||
return getColorById(Character.digit(c, 16));
|
||||
}
|
||||
|
||||
public static ChatCode getFormattingById(@IntRange(from = 0, to = 21) int id) {
|
||||
if (id <= 15) {
|
||||
return getColorById(id);
|
||||
}
|
||||
return switch (id) {
|
||||
case 16 -> PreChatFormattingCodes.OBFUSCATED;
|
||||
case 17 -> PreChatFormattingCodes.BOLD;
|
||||
case 18 -> PreChatFormattingCodes.STRIKETHROUGH;
|
||||
case 19 -> PreChatFormattingCodes.UNDERLINED;
|
||||
case 20 -> PreChatFormattingCodes.ITALIC;
|
||||
case 21 -> PostChatFormattingCodes.RESET;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
public static RGBColor getColorById(@IntRange(from = 0, to = 15) int id) {
|
||||
if (id < 0) {
|
||||
return null;
|
||||
}
|
||||
if (id <= 15) {
|
||||
return COLORS[id];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer getColorId(RGBColor color) {
|
||||
return COLOR_ID_MAP.get(color);
|
||||
}
|
||||
|
||||
public static String getColorChar(RGBColor color) {
|
||||
return String.format("%x", COLOR_ID_MAP.get(color));
|
||||
}
|
||||
|
||||
public static RGBColor getColorByName(String name) {
|
||||
return (RGBColor) getChatFormattingByName(name);
|
||||
}
|
||||
|
||||
public static ChatCode getChatFormattingByName(String name) {
|
||||
return switch (name.toLowerCase()) {
|
||||
case "black" -> BLACK;
|
||||
case "dark_blue" -> DARK_BLUE;
|
||||
case "dark_green" -> DARK_GREEN;
|
||||
case "dark_aqua" -> DARK_AQUA;
|
||||
case "dark_red" -> DARK_RED;
|
||||
case "dark_purple" -> DARK_PURPLE;
|
||||
case "gold" -> GOLD;
|
||||
case "gray", "grey" -> GRAY;
|
||||
case "dark_gray", "dark_grey" -> DARK_GRAY;
|
||||
case "blue" -> BLUE;
|
||||
case "green" -> GREEN;
|
||||
case "aqua" -> AQUA;
|
||||
case "red" -> RED;
|
||||
case "light_purple" -> LIGHT_PURPLE;
|
||||
case "yellow" -> YELLOW;
|
||||
case "white", "reset" -> WHITE;
|
||||
case "bold" -> PreChatFormattingCodes.BOLD;
|
||||
case "italic" -> PreChatFormattingCodes.ITALIC;
|
||||
case "underlined" -> PreChatFormattingCodes.UNDERLINED;
|
||||
case "strikethrough" -> PreChatFormattingCodes.STRIKETHROUGH;
|
||||
case "obfuscated" -> PreChatFormattingCodes.OBFUSCATED;
|
||||
default -> throw new IllegalArgumentException("Unexpected value: " + name);
|
||||
};
|
||||
}
|
||||
|
||||
public static RGBColor getRandomColor() {
|
||||
return getColorById(ThreadLocalRandom.current().nextInt(0, 15));
|
||||
}
|
||||
}
|
68
src/main/java/de/bixilon/minosoft/data/text/ChatColors.kt
Normal file
68
src/main/java/de/bixilon/minosoft/data/text/ChatColors.kt
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2021 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.data.text
|
||||
|
||||
object ChatColors {
|
||||
val BLACK = RGBColor(0, 0, 0)
|
||||
val DARK_BLUE = RGBColor(0, 0, 170)
|
||||
val DARK_GREEN = RGBColor(0, 170, 0)
|
||||
val DARK_AQUA = RGBColor(0, 170, 170)
|
||||
val DARK_RED = RGBColor(170, 0, 0)
|
||||
val DARK_PURPLE = RGBColor(170, 0, 170)
|
||||
val GOLD = RGBColor(255, 170, 0)
|
||||
val GRAY = RGBColor(170, 170, 170)
|
||||
val DARK_GRAY = RGBColor(85, 85, 85)
|
||||
val BLUE = RGBColor(85, 85, 255)
|
||||
val GREEN = RGBColor(85, 255, 85)
|
||||
val AQUA = RGBColor(85, 255, 255)
|
||||
|
||||
@JvmField
|
||||
val RED = RGBColor(255, 85, 85)
|
||||
val LIGHT_PURPLE = RGBColor(255, 85, 255)
|
||||
val YELLOW = RGBColor(255, 255, 85)
|
||||
|
||||
@JvmField
|
||||
val WHITE = RGBColor(255, 255, 255)
|
||||
|
||||
|
||||
val VALUES: List<RGBColor>
|
||||
val NAME_MAP: Map<String, RGBColor>
|
||||
|
||||
|
||||
init {
|
||||
val values: MutableList<RGBColor> = mutableListOf()
|
||||
val nameMap: MutableMap<String, RGBColor> = mutableMapOf()
|
||||
|
||||
|
||||
for (field in this::class.java.declaredFields) {
|
||||
val color = field.get(null)
|
||||
if (color !is RGBColor) {
|
||||
continue
|
||||
}
|
||||
values += color
|
||||
nameMap[field.name.lowercase()] = color
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
this.VALUES = values.toList()
|
||||
this.NAME_MAP = nameMap.toMap()
|
||||
}
|
||||
|
||||
operator fun get(id: Int): RGBColor {
|
||||
return VALUES[id]
|
||||
}
|
||||
}
|
@ -10,8 +10,9 @@
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.text
|
||||
|
||||
package de.bixilon.minosoft.data.text;
|
||||
|
||||
public interface ChatCode {
|
||||
interface ChatFormattingCode : ChatCode {
|
||||
val char: Char
|
||||
val ansi: String
|
||||
}
|
@ -10,30 +10,16 @@
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.text
|
||||
|
||||
package de.bixilon.minosoft.data.text;
|
||||
enum class PostChatFormattingCodes(
|
||||
override val char: Char,
|
||||
override val ansi: String,
|
||||
) : ChatFormattingCode {
|
||||
RESET('r', "\u001b[0m")
|
||||
;
|
||||
|
||||
public enum PostChatFormattingCodes implements ChatFormattingCode {
|
||||
RESET('r', "\u001b[0m");
|
||||
private final char c;
|
||||
private final String ansi;
|
||||
|
||||
PostChatFormattingCodes(char c, String ansi) {
|
||||
this.c = c;
|
||||
this.ansi = ansi;
|
||||
}
|
||||
|
||||
public char getChar() {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
public String getANSI() {
|
||||
return this.ansi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getANSI();
|
||||
override fun toString(): String {
|
||||
return ansi
|
||||
}
|
||||
}
|
||||
|
@ -10,34 +10,20 @@
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.text
|
||||
|
||||
package de.bixilon.minosoft.data.text;
|
||||
|
||||
public enum PreChatFormattingCodes implements ChatFormattingCode {
|
||||
enum class PreChatFormattingCodes(
|
||||
override val char: Char,
|
||||
override val ansi: String,
|
||||
) : ChatFormattingCode {
|
||||
OBFUSCATED('k', "\u001b[5m"),
|
||||
BOLD('l', "\u001b[1m"),
|
||||
STRIKETHROUGH('m', "\u001b[9m"),
|
||||
UNDERLINED('n', "\u001b[4m"),
|
||||
ITALIC('o', "\u001b[3m");
|
||||
private final char c;
|
||||
private final String ansi;
|
||||
ITALIC('o', "\u001b[3m")
|
||||
;
|
||||
|
||||
PreChatFormattingCodes(char c, String ansi) {
|
||||
this.c = c;
|
||||
this.ansi = ansi;
|
||||
}
|
||||
|
||||
public char getChar() {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
public String getANSI() {
|
||||
return this.ansi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getANSI();
|
||||
override fun toString(): String {
|
||||
return ansi
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import de.bixilon.minosoft.util.MMath
|
||||
import org.checkerframework.common.value.qual.IntRange
|
||||
|
||||
class RGBColor(val rgba: Int) : ChatCode {
|
||||
val ansi: String = "\u001b[38;2;$red;$green;${blue}m"
|
||||
|
||||
@JvmOverloads
|
||||
constructor(red: Int, green: Int, blue: Int, alpha: Int = 0xFF) : this(alpha or (blue shl 8) or (green shl 16) or (red shl 24))
|
||||
|
@ -75,7 +75,7 @@ open class TextComponent(
|
||||
get() {
|
||||
val stringBuilder = StringBuilder()
|
||||
this.color?.let {
|
||||
stringBuilder.append(ChatColors.getANSIColorByRGBColor(it))
|
||||
stringBuilder.append(it.ansi)
|
||||
}
|
||||
|
||||
for (formattingCode in this.formatting) {
|
||||
@ -90,8 +90,8 @@ open class TextComponent(
|
||||
get() {
|
||||
val stringBuilder = StringBuilder()
|
||||
color?.let {
|
||||
val colorChar = ChatColors.getColorId(it)
|
||||
if (colorChar != null) {
|
||||
val colorChar = ChatCode.FORMATTING_CODES_ID.indexOf(it)
|
||||
if (colorChar != -1) {
|
||||
stringBuilder.append(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR).append(Integer.toHexString(colorChar))
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ package de.bixilon.minosoft.protocol.packets.s2c.play
|
||||
|
||||
import de.bixilon.minosoft.data.abilities.Gamemodes
|
||||
import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity
|
||||
import de.bixilon.minosoft.data.player.PlayerProperties
|
||||
import de.bixilon.minosoft.data.player.PlayerProperty
|
||||
import de.bixilon.minosoft.data.player.tab.TabListItem
|
||||
import de.bixilon.minosoft.data.player.tab.TabListItemData
|
||||
@ -60,14 +59,14 @@ class TabListDataS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
|
||||
when (action) {
|
||||
PlayerListItemActions.ADD -> {
|
||||
val name = buffer.readString()
|
||||
val playerProperties: MutableMap<PlayerProperties, PlayerProperty> = mutableMapOf()
|
||||
val playerProperties: MutableMap<String, PlayerProperty> = mutableMapOf()
|
||||
for (index in 0 until buffer.readVarInt()) {
|
||||
val property = PlayerProperty(
|
||||
PlayerProperties.byName(buffer.readString()),
|
||||
buffer.readString(),
|
||||
buffer.readString(),
|
||||
buffer.readOptional { buffer.readString() },
|
||||
)
|
||||
playerProperties[property.property] = property
|
||||
playerProperties[property.key] = property
|
||||
}
|
||||
val gamemode = Gamemodes[buffer.readVarInt()]
|
||||
val ping = buffer.readVarInt()
|
||||
|
@ -17,7 +17,6 @@ import de.bixilon.minosoft.data.scoreboard.NameTagVisibilities
|
||||
import de.bixilon.minosoft.data.scoreboard.Team
|
||||
import de.bixilon.minosoft.data.scoreboard.TeamCollisionRules
|
||||
import de.bixilon.minosoft.data.text.ChatCode
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
|
||||
@ -68,9 +67,9 @@ class TeamCreateS2CP(val name: String, buffer: PlayInByteBuffer) : PlayS2CPacket
|
||||
this.collisionRule = TeamCollisionRules[buffer.readString()]
|
||||
}
|
||||
if (buffer.versionId < ProtocolVersions.V_18W01A) {
|
||||
this.formattingCode = ChatColors.getFormattingById(buffer.readByte().toInt())
|
||||
this.formattingCode = ChatCode[buffer.readUnsignedByte()]
|
||||
} else {
|
||||
this.formattingCode = ChatColors.getFormattingById(buffer.readVarInt())
|
||||
this.formattingCode = ChatCode[buffer.readVarInt()]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,6 @@ package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.teams
|
||||
import de.bixilon.minosoft.data.scoreboard.NameTagVisibilities
|
||||
import de.bixilon.minosoft.data.scoreboard.TeamCollisionRules
|
||||
import de.bixilon.minosoft.data.text.ChatCode
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
|
||||
@ -65,9 +64,9 @@ class TeamUpdateS2CP(val name: String, buffer: PlayInByteBuffer) : PlayS2CPacket
|
||||
this.collisionRule = TeamCollisionRules[buffer.readString()]
|
||||
}
|
||||
if (buffer.versionId < ProtocolVersions.V_18W01A) {
|
||||
this.formattingCode = ChatColors.getFormattingById(buffer.readByte().toInt())
|
||||
this.formattingCode = ChatCode[buffer.readUnsignedByte()]
|
||||
} else {
|
||||
this.formattingCode = ChatColors.getFormattingById(buffer.readVarInt())
|
||||
this.formattingCode = ChatCode[buffer.readVarInt()]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,8 +18,8 @@ import de.bixilon.minosoft.data.text.ChatColors;
|
||||
import de.bixilon.minosoft.data.text.PostChatFormattingCodes;
|
||||
|
||||
public abstract class Command {
|
||||
private static final String ERROR_MESSAGE_PREFIX = ChatColors.getANSIColorByRGBColor(ChatColors.RED);
|
||||
private static final String ERROR_MESSAGE_SUFFIX = PostChatFormattingCodes.RESET.getANSI();
|
||||
private static final String ERROR_MESSAGE_PREFIX = ChatColors.RED.getAnsi();
|
||||
private static final String ERROR_MESSAGE_SUFFIX = PostChatFormattingCodes.RESET.getAnsi();
|
||||
|
||||
public static void print(String string, Object... format) {
|
||||
if (format.length == 0) {
|
||||
|
@ -157,4 +157,30 @@ object KUtil {
|
||||
operator fun <T> List<T>.get(enum: Enum<*>): T {
|
||||
return this[enum.ordinal]
|
||||
}
|
||||
|
||||
fun <K, V> Map<K, Any>.extend(vararg pairs: Pair<K, Any>): Map<K, V> {
|
||||
val map: MutableMap<K, V> = mutableMapOf()
|
||||
|
||||
for ((key, value) in this) {
|
||||
map[key] = value as V
|
||||
}
|
||||
|
||||
for (pair in pairs) {
|
||||
map[pair.first] = pair.second as V
|
||||
}
|
||||
return map.toMap()
|
||||
}
|
||||
|
||||
fun <V> Collection<Any>.extend(vararg values: Any): List<V> {
|
||||
val list: MutableList<V> = mutableListOf()
|
||||
|
||||
for (value in this) {
|
||||
list += value as V
|
||||
}
|
||||
|
||||
for (value in values) {
|
||||
list += value as V
|
||||
}
|
||||
return list.toList()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user