From c037fe3d442702885d9c6bfa65621fb624210c02 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 28 May 2017 11:00:02 +1000 Subject: [PATCH] Add color API for Teams --- nms-patches/EnumChatFormat.patch | 11 +++++++ .../craftbukkit/scoreboard/CraftTeam.java | 17 ++++++++++ .../craftbukkit/util/CraftChatMessage.java | 32 ++++++++++++------- src/test/java/org/bukkit/ChatTest.java | 22 +++++++++++++ 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 nms-patches/EnumChatFormat.patch create mode 100644 src/test/java/org/bukkit/ChatTest.java diff --git a/nms-patches/EnumChatFormat.patch b/nms-patches/EnumChatFormat.patch new file mode 100644 index 0000000000..7c87673d94 --- /dev/null +++ b/nms-patches/EnumChatFormat.patch @@ -0,0 +1,11 @@ +--- a/net/minecraft/server/EnumChatFormat.java ++++ b/net/minecraft/server/EnumChatFormat.java +@@ -16,7 +16,7 @@ + private static final Map w = Maps.newHashMap(); + private static final Pattern x = Pattern.compile("(?i)\u00a7[0-9A-FK-OR]"); + private final String y; +- private final char z; ++ public final char z; // PAIL + private final boolean A; + private final String B; + private final int C; diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java index e7688340e7..a2496e79d4 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 560f95ccb6..598d088f5a 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); - static { - Builder builder = ImmutableMap.builder(); - for (EnumChatFormat format : EnumChatFormat.values()) { - builder.put(Character.toLowerCase(format.toString().charAt(1)), format); - } - formatMap = builder.build(); + 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 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(); + } + + 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 0000000000..444ce6eb89 --- /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))); + } + } +}