From c037fe3d442702885d9c6bfa65621fb624210c02 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 28 May 2017 11:00:02 +1000 Subject: Add color API for Teams --- .../bukkit/craftbukkit/scoreboard/CraftTeam.java | 17 ++++++++++++ .../bukkit/craftbukkit/util/CraftChatMessage.java | 30 ++++++++++++++-------- src/test/java/org/bukkit/ChatTest.java | 22 ++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/bukkit/ChatTest.java (limited to 'src') diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java index e7688340..a2496e79 100644 --- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java +++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java @@ -13,6 +13,8 @@ import com.google.common.collect.ImmutableSet; import net.minecraft.server.ScoreboardTeam; import net.minecraft.server.ScoreboardTeamBase; +import org.bukkit.ChatColor; +import org.bukkit.craftbukkit.util.CraftChatMessage; final class CraftTeam extends CraftScoreboardComponent implements Team { private final ScoreboardTeam team; @@ -70,6 +72,21 @@ final class CraftTeam extends CraftScoreboardComponent implements Team { team.setSuffix(suffix); } + @Override + public ChatColor getColor() throws IllegalStateException { + CraftScoreboard scoreboard = checkState(); + + return CraftChatMessage.getColor(team.m()); // PAIL: rename + } + + @Override + public void setColor(ChatColor color) { + Validate.notNull(color, "Color cannot be null"); + CraftScoreboard scoreboard = checkState(); + + team.a(CraftChatMessage.getColor(color)); // PAIL: rename + } + public boolean allowFriendlyFire() throws IllegalStateException { CraftScoreboard scoreboard = checkState(); diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java index 560f95cc..598d088f 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java @@ -16,21 +16,31 @@ import net.minecraft.server.IChatBaseComponent; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import net.minecraft.server.ChatMessage; +import org.bukkit.ChatColor; public final class CraftChatMessage { - + private static final Pattern LINK_PATTERN = Pattern.compile("((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))"); - private static class StringMessage { - private static final Map formatMap; - private static final Pattern INCREMENTAL_PATTERN = Pattern.compile("(" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + "[0-9a-fk-or])|(\\n)|((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))", Pattern.CASE_INSENSITIVE); + private static final Map formatMap; - static { - Builder builder = ImmutableMap.builder(); - for (EnumChatFormat format : EnumChatFormat.values()) { - builder.put(Character.toLowerCase(format.toString().charAt(1)), format); - } - formatMap = builder.build(); + static { + Builder builder = ImmutableMap.builder(); + for (EnumChatFormat format : EnumChatFormat.values()) { + builder.put(Character.toLowerCase(format.toString().charAt(1)), format); } + formatMap = builder.build(); + } + + public static EnumChatFormat getColor(ChatColor color) { + return formatMap.get(color.getChar()); + } + + public static ChatColor getColor(EnumChatFormat format) { + return ChatColor.getByChar(format.z); + } + + private static class StringMessage { + private static final Pattern INCREMENTAL_PATTERN = Pattern.compile("(" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + "[0-9a-fk-or])|(\\n)|((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))", Pattern.CASE_INSENSITIVE); private final List list = new ArrayList(); private IChatBaseComponent currentChatComponent = new ChatComponentText(""); diff --git a/src/test/java/org/bukkit/ChatTest.java b/src/test/java/org/bukkit/ChatTest.java new file mode 100644 index 00000000..444ce6eb --- /dev/null +++ b/src/test/java/org/bukkit/ChatTest.java @@ -0,0 +1,22 @@ +package org.bukkit; + +import net.minecraft.server.EnumChatFormat; +import org.bukkit.craftbukkit.util.CraftChatMessage; +import org.junit.Assert; +import org.junit.Test; + +public class ChatTest { + + @Test + public void testColors() { + for (ChatColor color : ChatColor.values()) { + Assert.assertNotNull(CraftChatMessage.getColor(color)); + Assert.assertEquals(color, CraftChatMessage.getColor(CraftChatMessage.getColor(color))); + } + + for (EnumChatFormat format : EnumChatFormat.values()) { + Assert.assertNotNull(CraftChatMessage.getColor(format)); + Assert.assertEquals(format, CraftChatMessage.getColor(CraftChatMessage.getColor(format))); + } + } +} -- cgit v1.2.3