Remove TeamColor

This commit is contained in:
Kieran Wallbanks 2021-03-24 16:50:02 +00:00
parent 1fe159636b
commit 9f87912d02
8 changed files with 59 additions and 129 deletions

View File

@ -1,8 +1,11 @@
package net.minestom.server.adventure;
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
@ -11,6 +14,25 @@ import java.util.Collection;
* Utility methods to convert adventure enums to their packet values.
*/
public class AdventurePacketConvertor {
private static final Object2IntMap<NamedTextColor> NAMED_TEXT_COLOR_ID_MAP = new Object2IntArrayMap<>(16);
static {
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.BLACK, 0);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_BLUE, 1);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_GREEN, 2);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_AQUA, 3);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_RED, 4);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_PURPLE, 5);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GOLD, 6);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GRAY, 7);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_GRAY, 8);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.BLUE, 9);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GREEN, 10);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.AQUA, 11);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.RED, 12);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.LIGHT_PURPLE, 13);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.YELLOW, 14);
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.WHITE, 15);
}
public static int getBossBarOverlayValue(@NotNull BossBar.Overlay overlay) {
return overlay.ordinal();
@ -40,4 +62,8 @@ public class AdventurePacketConvertor {
throw new IllegalArgumentException("Cannot get message type");
}
public static int getNamedTextColorValue(@NotNull NamedTextColor color) {
return NAMED_TEXT_COLOR_ID_MAP.getInt(color);
}
}

View File

@ -6,7 +6,6 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.color.Color;
import net.minestom.server.color.TeamColor;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -288,19 +287,6 @@ public final class ChatColor {
return new Color(red, green, blue);
}
/**
* Gets the TeamColor of this chat color.
*
* @return the team color, or null if there is no team color for this chat color
*/
public @Nullable TeamColor asTeamColor() {
try {
return TeamColor.values()[this.getId()];
} catch (ArrayIndexOutOfBoundsException ignored) {
return null;
}
}
@NotNull
@Override
public String toString() {

View File

@ -1,79 +0,0 @@
package net.minestom.server.color;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.util.RGBLike;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The format used for teams. Note that this is often referred to as "team color". This
* is misleading as teams can also use text decoration like bold, italics, etc.
*/
public enum TeamColor implements RGBLike {
BLACK(NamedTextColor.BLACK),
DARK_BLUE(NamedTextColor.DARK_BLUE),
DARK_GREEN(NamedTextColor.DARK_GREEN),
DARK_AQUA(NamedTextColor.DARK_AQUA),
DARK_RED(NamedTextColor.DARK_RED),
DARK_PURPLE(NamedTextColor.DARK_PURPLE),
GOLD(NamedTextColor.GOLD),
GRAY(NamedTextColor.GRAY),
DARK_GRAY(NamedTextColor.DARK_GRAY),
BLUE(NamedTextColor.BLUE),
GREEN(NamedTextColor.GREEN),
AQUA(NamedTextColor.AQUA),
RED(NamedTextColor.RED),
LIGHT_PURPLE(NamedTextColor.LIGHT_PURPLE),
YELLOW(NamedTextColor.YELLOW),
WHITE(NamedTextColor.WHITE);
private final TextColor color;
TeamColor(TextColor color) {
this.color = color;
}
/**
* Gets the text color equivalent to this team color.
*
* @return the text color
*/
public @NotNull TextColor getTextColor() {
return this.color;
}
/**
* Gets the color equivalent to this team color.
*
* @return the color
*/
public @NotNull Color getColor() {
return new Color(this.color);
}
/**
* Gets the ID number of this team color.
*
* @return the id number
*/
public int getId() {
return this.ordinal();
}
@Override
public int red() {
return this.color.red();
}
@Override
public int green() {
return this.color.green();
}
@Override
public int blue() {
return this.color.blue();
}
}

View File

@ -1,7 +1,8 @@
package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.text.Component;
import net.minestom.server.color.TeamColor;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.adventure.AdventurePacketConvertor;
import net.minestom.server.network.packet.server.ComponentHoldingServerPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
@ -46,7 +47,7 @@ public class TeamsPacket implements ComponentHoldingServerPacket {
/**
* The color of the team
*/
public TeamColor teamColor;
public NamedTextColor teamColor;
/**
* The prefix of the team
*/
@ -77,7 +78,7 @@ public class TeamsPacket implements ComponentHoldingServerPacket {
writer.writeByte(this.friendlyFlags);
writer.writeSizedString(this.nameTagVisibility.getIdentifier());
writer.writeSizedString(this.collisionRule.getIdentifier());
writer.writeVarInt(this.teamColor.getId());
writer.writeVarInt(AdventurePacketConvertor.getNamedTextColorValue(this.teamColor));
writer.writeComponent(this.teamPrefix);
writer.writeComponent(this.teamSuffix);
break;

View File

@ -2,8 +2,8 @@ package net.minestom.server.scoreboard;
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.color.TeamColor;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.DisplayScoreboardPacket;
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
@ -439,7 +439,7 @@ public class Sidebar implements Scoreboard {
private final byte friendlyFlags = 0x00;
private final TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.NEVER;
private final TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER;
private final TeamColor teamColor = TeamColor.DARK_GREEN;
private final NamedTextColor teamColor = NamedTextColor.DARK_GREEN;
/**

View File

@ -4,10 +4,11 @@ import com.google.common.collect.MapMaker;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.AdventurePacketConvertor;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.color.TeamColor;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.Player;
import net.minestom.server.network.ConnectionManager;
@ -15,7 +16,6 @@ import net.minestom.server.network.packet.server.play.TeamsPacket;
import net.minestom.server.network.packet.server.play.TeamsPacket.CollisionRule;
import net.minestom.server.network.packet.server.play.TeamsPacket.NameTagVisibility;
import net.minestom.server.utils.PacketUtils;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
@ -59,7 +59,7 @@ public class Team implements ForwardingAudience {
* Used to color the name of players on the team <br>
* The color of a team defines how the names of the team members are visualized.
*/
private TeamColor teamColor;
private NamedTextColor teamColor;
/**
* Shown before the names of the players who belong to this team.
@ -86,7 +86,7 @@ public class Team implements ForwardingAudience {
this.nameTagVisibility = NameTagVisibility.ALWAYS;
this.collisionRule = CollisionRule.ALWAYS;
this.teamColor = TeamColor.WHITE;
this.teamColor = NamedTextColor.WHITE;
this.prefix = Component.empty();
this.suffix = Component.empty();
@ -235,13 +235,11 @@ public class Team implements ForwardingAudience {
*
* @param color The new team color
* @see #updateTeamColor(ChatColor)
* @deprecated Use {@link #setTeamColor(TeamColor)}
* @deprecated Use {@link #setTeamColor(NamedTextColor)}
*/
@Deprecated
public void setTeamColor(@NotNull ChatColor color) {
TeamColor teamColor = color.asTeamColor();
Validate.notNull(teamColor, "Cannot set team color to non-color.");
this.setTeamColor(teamColor);
this.setTeamColor(NamedTextColor.nearestTo(color.asTextColor()));
}
/**
@ -250,9 +248,9 @@ public class Team implements ForwardingAudience {
* <b>Warning:</b> This is only changed on the <b>server side</b>.
*
* @param color The new team color
* @see #setTeamColor(TeamColor)
* @see #updateTeamColor(NamedTextColor)
*/
public void setTeamColor(@NotNull TeamColor color) {
public void setTeamColor(@NotNull NamedTextColor color) {
this.teamColor = color;
}
@ -260,13 +258,11 @@ public class Team implements ForwardingAudience {
* Changes the color of the team and sends an update packet.
*
* @param chatColor The new team color
* @deprecated Use {@link #updateTeamColor(TeamColor)}
* @deprecated Use {@link #updateTeamColor(NamedTextColor)}
*/
@Deprecated
public void updateTeamColor(@NotNull ChatColor chatColor) {
TeamColor teamColor = chatColor.asTeamColor();
Validate.notNull(teamColor, "Cannot set team color to non-color.");
this.updateTeamColor(teamColor);
this.updateTeamColor(NamedTextColor.nearestTo(chatColor.asTextColor()));
}
/**
@ -274,7 +270,7 @@ public class Team implements ForwardingAudience {
*
* @param color The new team color
*/
public void updateTeamColor(@NotNull TeamColor color) {
public void updateTeamColor(@NotNull NamedTextColor color) {
this.setTeamColor(color);
sendUpdatePacket();
}
@ -502,7 +498,7 @@ public class Team implements ForwardingAudience {
@Deprecated
@NotNull
public ChatColor getTeamColorOld() {
return ChatColor.fromName(teamColor.name());
return ChatColor.fromId(AdventurePacketConvertor.getNamedTextColorValue(teamColor));
}
/**
@ -511,7 +507,7 @@ public class Team implements ForwardingAudience {
* @return the team color
*/
@NotNull
public TeamColor getTeamColor() {
public NamedTextColor getTeamColor() {
return teamColor;
}

View File

@ -1,9 +1,9 @@
package net.minestom.server.scoreboard;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.color.TeamColor;
import net.minestom.server.network.packet.server.play.TeamsPacket.CollisionRule;
import net.minestom.server.network.packet.server.play.TeamsPacket.NameTagVisibility;
@ -87,11 +87,11 @@ public class TeamBuilder {
*
* @param color The new color
* @return this builder, for chaining
* @deprecated Use {@link #updateTeamColor(TeamColor)}
* @deprecated Use {@link #updateTeamColor(NamedTextColor)}
*/
@Deprecated
public TeamBuilder updateTeamColor(ChatColor color) {
return this.updateTeamColor(color.asTeamColor());
return this.updateTeamColor(NamedTextColor.nearestTo(color.asTextColor()));
}
/**
@ -100,7 +100,7 @@ public class TeamBuilder {
* @param color The new color
* @return this builder, for chaining
*/
public TeamBuilder updateTeamColor(TeamColor color) {
public TeamBuilder updateTeamColor(NamedTextColor color) {
this.team.updateTeamColor(color);
return this;
}
@ -315,11 +315,11 @@ public class TeamBuilder {
*
* @param color The new team color
* @return this builder, for chaining
* @deprecated Use {@link #teamColor(TeamColor)}
* @deprecated Use {@link #teamColor(NamedTextColor)}
*/
@Deprecated
public TeamBuilder teamColor(ChatColor color) {
return this.teamColor(color.asTeamColor());
return this.teamColor(NamedTextColor.nearestTo(color.asTextColor()));
}
/**
@ -330,7 +330,7 @@ public class TeamBuilder {
* @param color The new team color
* @return this builder, for chaining
*/
public TeamBuilder teamColor(TeamColor color) {
public TeamBuilder teamColor(NamedTextColor color) {
this.team.setTeamColor(color);
return this;
}

View File

@ -1,10 +1,10 @@
package net.minestom.server.scoreboard;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.color.TeamColor;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.Player;
import net.minestom.server.network.ConnectionManager;
@ -99,11 +99,11 @@ public final class TeamManager {
* @param teamColor The team color
* @param suffix The team suffix
* @return the created {@link Team} with a prefix, teamColor and suffix
* @deprecated Use {@link #createTeam(String, Component, TeamColor, Component)}
* @deprecated Use {@link #createTeam(String, Component, NamedTextColor, Component)}
*/
@Deprecated
public Team createTeam(String name, JsonMessage prefix, ChatColor teamColor, JsonMessage suffix) {
return this.createTeam(name, prefix.asComponent(), teamColor.asTeamColor(), suffix.asComponent());
return this.createTeam(name, prefix.asComponent(), NamedTextColor.nearestTo(teamColor.asTextColor()), suffix.asComponent());
}
/**
@ -115,7 +115,7 @@ public final class TeamManager {
* @param suffix The team suffix
* @return the created {@link Team} with a prefix, teamColor and suffix
*/
public Team createTeam(String name, Component prefix, TeamColor teamColor, Component suffix) {
public Team createTeam(String name, Component prefix, NamedTextColor teamColor, Component suffix) {
return this.createBuilder(name).prefix(prefix).teamColor(teamColor).suffix(suffix).updateTeamPacket().build();
}
@ -128,11 +128,11 @@ public final class TeamManager {
* @param teamColor The team color
* @param suffix The team suffix
* @return the created {@link Team} with a prefix, teamColor, suffix and the display name
* @deprecated Use {@link #createTeam(String, Component, Component, TeamColor, Component)}
* @deprecated Use {@link #createTeam(String, Component, Component, NamedTextColor, Component)}
*/
@Deprecated
public Team createTeam(String name, JsonMessage displayName, JsonMessage prefix, ChatColor teamColor, JsonMessage suffix) {
return this.createTeam(name, displayName.asComponent(), prefix.asComponent(), teamColor.asTeamColor(), suffix.asComponent());
return this.createTeam(name, displayName.asComponent(), prefix.asComponent(), NamedTextColor.nearestTo(teamColor.asTextColor()), suffix.asComponent());
}
/**
@ -145,7 +145,7 @@ public final class TeamManager {
* @param suffix The team suffix
* @return the created {@link Team} with a prefix, teamColor, suffix and the display name
*/
public Team createTeam(String name, Component displayName, Component prefix, TeamColor teamColor, Component suffix) {
public Team createTeam(String name, Component displayName, Component prefix, NamedTextColor teamColor, Component suffix) {
return this.createBuilder(name).teamDisplayName(displayName).prefix(prefix).teamColor(teamColor).suffix(suffix).updateTeamPacket().build();
}