From 8463042cc77d5f977f2c42564349e73fd49a1769 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 5 Jul 2016 13:32:17 +0200 Subject: [PATCH] =?UTF-8?q?PandacubeUtil=20n'est=20plus=20un=20plugin=20in?= =?UTF-8?q?d=C3=A9pendant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PandacubeUtil est maintenant intégré dans le packetage .jar des plugins Bungee et Spigot. - Les librairies importés sont réorganisés. PandacubeUtil ne dépend plus de BungeeCord et de Spigot - L'API chat de Bungee (couleur, mise en forme chat) est intégré à PandacubeUtil (code décompilé) --- .classpath | 3 +- make_jar.jardesc | 17 -- manifest | 1 - resources/bungee.yml | 4 - resources/plugin.yml | 4 - src/fr/pandacube/java/BungeeMain.java | 13 - src/fr/pandacube/java/SpigotMain.java | 13 - .../pandacube/java/util/db/DBConnection.java | 9 +- .../java/util/network/packet/Packet.java | 1 + src/net/md_5/bungee/api/ChatColor.java | 85 +++++++ src/net/md_5/bungee/api/ChatMessageType.java | 15 ++ .../md_5/bungee/api/chat/BaseComponent.java | 240 ++++++++++++++++++ src/net/md_5/bungee/api/chat/ClickEvent.java | 43 ++++ .../bungee/api/chat/ComponentBuilder.java | 132 ++++++++++ src/net/md_5/bungee/api/chat/HoverEvent.java | 44 ++++ .../md_5/bungee/api/chat/TextComponent.java | 170 +++++++++++++ .../api/chat/TranslatableComponent.java | 196 ++++++++++++++ 17 files changed, 930 insertions(+), 60 deletions(-) delete mode 100644 make_jar.jardesc delete mode 100644 manifest delete mode 100644 resources/bungee.yml delete mode 100644 resources/plugin.yml delete mode 100644 src/fr/pandacube/java/BungeeMain.java delete mode 100644 src/fr/pandacube/java/SpigotMain.java create mode 100644 src/net/md_5/bungee/api/ChatColor.java create mode 100644 src/net/md_5/bungee/api/ChatMessageType.java create mode 100644 src/net/md_5/bungee/api/chat/BaseComponent.java create mode 100644 src/net/md_5/bungee/api/chat/ClickEvent.java create mode 100644 src/net/md_5/bungee/api/chat/ComponentBuilder.java create mode 100644 src/net/md_5/bungee/api/chat/HoverEvent.java create mode 100644 src/net/md_5/bungee/api/chat/TextComponent.java create mode 100644 src/net/md_5/bungee/api/chat/TranslatableComponent.java diff --git a/.classpath b/.classpath index a2d0a31..7988882 100644 --- a/.classpath +++ b/.classpath @@ -4,7 +4,6 @@ - - + diff --git a/make_jar.jardesc b/make_jar.jardesc deleted file mode 100644 index 225a2a4..0000000 --- a/make_jar.jardesc +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/manifest b/manifest deleted file mode 100644 index 9d885be..0000000 --- a/manifest +++ /dev/null @@ -1 +0,0 @@ -Manifest-Version: 1.0 diff --git a/resources/bungee.yml b/resources/bungee.yml deleted file mode 100644 index d35517c..0000000 --- a/resources/bungee.yml +++ /dev/null @@ -1,4 +0,0 @@ -name: PandacubeUtil -main: fr.pandacube.java.BungeeMain -version: 1.0-beta4 -author: Marc Baloup (marcbal) \ No newline at end of file diff --git a/resources/plugin.yml b/resources/plugin.yml deleted file mode 100644 index 8c286e1..0000000 --- a/resources/plugin.yml +++ /dev/null @@ -1,4 +0,0 @@ -name: PandacubeUtil -main: fr.pandacube.java.SpigotMain -version: 1.0-beta4 -author: Marc Baloup (marcbal) \ No newline at end of file diff --git a/src/fr/pandacube/java/BungeeMain.java b/src/fr/pandacube/java/BungeeMain.java deleted file mode 100644 index fb30386..0000000 --- a/src/fr/pandacube/java/BungeeMain.java +++ /dev/null @@ -1,13 +0,0 @@ -package fr.pandacube.java; - -import net.md_5.bungee.api.plugin.Plugin; - -public class BungeeMain extends Plugin { - - @Override - public void onLoad() { - PandacubeUtil.setMasterLogger(getProxy().getLogger()); - PandacubeUtil.setPluginLogger(getLogger()); - } - -} diff --git a/src/fr/pandacube/java/SpigotMain.java b/src/fr/pandacube/java/SpigotMain.java deleted file mode 100644 index 3b9d8db..0000000 --- a/src/fr/pandacube/java/SpigotMain.java +++ /dev/null @@ -1,13 +0,0 @@ -package fr.pandacube.java; - -import org.bukkit.plugin.java.JavaPlugin; - -public class SpigotMain extends JavaPlugin { - - @Override - public void onLoad() { - PandacubeUtil.setMasterLogger(getServer().getLogger()); - PandacubeUtil.setPluginLogger(getLogger()); - } - -} diff --git a/src/fr/pandacube/java/util/db/DBConnection.java b/src/fr/pandacube/java/util/db/DBConnection.java index d103fa2..409c2b6 100644 --- a/src/fr/pandacube/java/util/db/DBConnection.java +++ b/src/fr/pandacube/java/util/db/DBConnection.java @@ -5,14 +5,11 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; -import org.bukkit.plugin.java.JavaPlugin; - public class DBConnection { - JavaPlugin plugin; Connection conn; - String url; - String login; - String pass; + private String url; + private String login; + private String pass; public DBConnection(String host, int port, String dbname, String l, String p) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); diff --git a/src/fr/pandacube/java/util/network/packet/Packet.java b/src/fr/pandacube/java/util/network/packet/Packet.java index 31e10ef..643c5d4 100644 --- a/src/fr/pandacube/java/util/network/packet/Packet.java +++ b/src/fr/pandacube/java/util/network/packet/Packet.java @@ -61,6 +61,7 @@ public abstract class Packet implements ByteSerializable { } } + @SuppressWarnings("unused") private static void addPacket(Class packetClass) { try { Packet p = (Packet)packetClass.newInstance(); diff --git a/src/net/md_5/bungee/api/ChatColor.java b/src/net/md_5/bungee/api/ChatColor.java new file mode 100644 index 0000000..2e83fb8 --- /dev/null +++ b/src/net/md_5/bungee/api/ChatColor.java @@ -0,0 +1,85 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public enum ChatColor { + BLACK('0', "black"), + DARK_BLUE('1', "dark_blue"), + DARK_GREEN('2', "dark_green"), + DARK_AQUA('3', "dark_aqua"), + DARK_RED('4', "dark_red"), + DARK_PURPLE('5', "dark_purple"), + GOLD('6', "gold"), + GRAY('7', "gray"), + DARK_GRAY('8', "dark_gray"), + BLUE('9', "blue"), + GREEN('a', "green"), + AQUA('b', "aqua"), + RED('c', "red"), + LIGHT_PURPLE('d', "light_purple"), + YELLOW('e', "yellow"), + WHITE('f', "white"), + MAGIC('k', "obfuscated"), + BOLD('l', "bold"), + STRIKETHROUGH('m', "strikethrough"), + UNDERLINE('n', "underline"), + ITALIC('o', "italic"), + RESET('r', "reset"); + + public static final char COLOR_CHAR = '\u00a7'; + public static final String ALL_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRr"; + public static final Pattern STRIP_COLOR_PATTERN; + private static final Map BY_CHAR; + private final char code; + private final String toString; + private final String name; + + private ChatColor(char code, String name) { + this.code = code; + this.name = name; + this.toString = new String(new char[]{'\u00a7', code}); + } + + public String toString() { + return this.toString; + } + + public static String stripColor(String input) { + if (input == null) { + return null; + } + return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); + } + + public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) { + char[] b2 = textToTranslate.toCharArray(); + for (int i = 0; i < b2.length - 1; ++i) { + if (b2[i] != altColorChar || "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b2[i + 1]) <= -1) continue; + b2[i] = 167; + b2[i + 1] = Character.toLowerCase(b2[i + 1]); + } + return new String(b2); + } + + public static ChatColor getByChar(char code) { + return BY_CHAR.get(Character.valueOf(code)); + } + + public String getName() { + return this.name; + } + + static { + STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf('\u00a7') + "[0-9A-FK-OR]"); + BY_CHAR = new HashMap(); + for (ChatColor colour : ChatColor.values()) { + BY_CHAR.put(Character.valueOf(colour.code), colour); + } + } +} + diff --git a/src/net/md_5/bungee/api/ChatMessageType.java b/src/net/md_5/bungee/api/ChatMessageType.java new file mode 100644 index 0000000..2c2d8bc --- /dev/null +++ b/src/net/md_5/bungee/api/ChatMessageType.java @@ -0,0 +1,15 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api; + +public enum ChatMessageType { + CHAT, + SYSTEM, + ACTION_BAR; + + + private ChatMessageType() { + } +} + diff --git a/src/net/md_5/bungee/api/chat/BaseComponent.java b/src/net/md_5/bungee/api/chat/BaseComponent.java new file mode 100644 index 0000000..51a95d6 --- /dev/null +++ b/src/net/md_5/bungee/api/chat/BaseComponent.java @@ -0,0 +1,240 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.util.ArrayList; +import java.util.List; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + +public abstract class BaseComponent { + BaseComponent parent; + private ChatColor color; + private Boolean bold; + private Boolean italic; + private Boolean underlined; + private Boolean strikethrough; + private Boolean obfuscated; + private String insertion; + private List extra; + private ClickEvent clickEvent; + private HoverEvent hoverEvent; + + BaseComponent(BaseComponent old) { + this.setColor(old.getColorRaw()); + this.setBold(old.isBoldRaw()); + this.setItalic(old.isItalicRaw()); + this.setUnderlined(old.isUnderlinedRaw()); + this.setStrikethrough(old.isStrikethroughRaw()); + this.setObfuscated(old.isObfuscatedRaw()); + this.setInsertion(old.getInsertion()); + this.setClickEvent(old.getClickEvent()); + this.setHoverEvent(old.getHoverEvent()); + if (old.getExtra() != null) { + for (BaseComponent component : old.getExtra()) { + this.addExtra(component.duplicate()); + } + } + } + + public abstract BaseComponent duplicate(); + + public static /* varargs */ String toLegacyText(BaseComponent ... components) { + StringBuilder builder = new StringBuilder(); + for (BaseComponent msg : components) { + builder.append(msg.toLegacyText()); + } + return builder.toString(); + } + + public static /* varargs */ String toPlainText(BaseComponent ... components) { + StringBuilder builder = new StringBuilder(); + for (BaseComponent msg : components) { + builder.append(msg.toPlainText()); + } + return builder.toString(); + } + + public ChatColor getColor() { + if (this.color == null) { + if (this.parent == null) { + return ChatColor.WHITE; + } + return this.parent.getColor(); + } + return this.color; + } + + public ChatColor getColorRaw() { + return this.color; + } + + public boolean isBold() { + if (this.bold == null) { + return this.parent != null && this.parent.isBold(); + } + return this.bold; + } + + public Boolean isBoldRaw() { + return this.bold; + } + + public boolean isItalic() { + if (this.italic == null) { + return this.parent != null && this.parent.isItalic(); + } + return this.italic; + } + + public Boolean isItalicRaw() { + return this.italic; + } + + public boolean isUnderlined() { + if (this.underlined == null) { + return this.parent != null && this.parent.isUnderlined(); + } + return this.underlined; + } + + public Boolean isUnderlinedRaw() { + return this.underlined; + } + + public boolean isStrikethrough() { + if (this.strikethrough == null) { + return this.parent != null && this.parent.isStrikethrough(); + } + return this.strikethrough; + } + + public Boolean isStrikethroughRaw() { + return this.strikethrough; + } + + public boolean isObfuscated() { + if (this.obfuscated == null) { + return this.parent != null && this.parent.isObfuscated(); + } + return this.obfuscated; + } + + public Boolean isObfuscatedRaw() { + return this.obfuscated; + } + + public void setExtra(List components) { + for (BaseComponent component : components) { + component.parent = this; + } + this.extra = components; + } + + public void addExtra(String text) { + this.addExtra(new TextComponent(text)); + } + + public void addExtra(BaseComponent component) { + if (this.extra == null) { + this.extra = new ArrayList(); + } + component.parent = this; + this.extra.add(component); + } + + public boolean hasFormatting() { + return this.color != null || this.bold != null || this.italic != null || this.underlined != null || this.strikethrough != null || this.obfuscated != null || this.hoverEvent != null || this.clickEvent != null; + } + + public String toPlainText() { + StringBuilder builder = new StringBuilder(); + this.toPlainText(builder); + return builder.toString(); + } + + void toPlainText(StringBuilder builder) { + if (this.extra != null) { + for (BaseComponent e2 : this.extra) { + e2.toPlainText(builder); + } + } + } + + public String toLegacyText() { + StringBuilder builder = new StringBuilder(); + this.toLegacyText(builder); + return builder.toString(); + } + + void toLegacyText(StringBuilder builder) { + if (this.extra != null) { + for (BaseComponent e2 : this.extra) { + e2.toLegacyText(builder); + } + } + } + + public void setColor(ChatColor color) { + this.color = color; + } + + public void setBold(Boolean bold) { + this.bold = bold; + } + + public void setItalic(Boolean italic) { + this.italic = italic; + } + + public void setUnderlined(Boolean underlined) { + this.underlined = underlined; + } + + public void setStrikethrough(Boolean strikethrough) { + this.strikethrough = strikethrough; + } + + public void setObfuscated(Boolean obfuscated) { + this.obfuscated = obfuscated; + } + + public void setInsertion(String insertion) { + this.insertion = insertion; + } + + public void setClickEvent(ClickEvent clickEvent) { + this.clickEvent = clickEvent; + } + + public void setHoverEvent(HoverEvent hoverEvent) { + this.hoverEvent = hoverEvent; + } + + public String toString() { + return "BaseComponent(color=" + (Object)((Object)this.getColor()) + ", bold=" + this.bold + ", italic=" + this.italic + ", underlined=" + this.underlined + ", strikethrough=" + this.strikethrough + ", obfuscated=" + this.obfuscated + ", insertion=" + this.getInsertion() + ", extra=" + this.getExtra() + ", clickEvent=" + this.getClickEvent() + ", hoverEvent=" + this.getHoverEvent() + ")"; + } + + public BaseComponent() { + } + + public String getInsertion() { + return this.insertion; + } + + public List getExtra() { + return this.extra; + } + + public ClickEvent getClickEvent() { + return this.clickEvent; + } + + public HoverEvent getHoverEvent() { + return this.hoverEvent; + } +} + diff --git a/src/net/md_5/bungee/api/chat/ClickEvent.java b/src/net/md_5/bungee/api/chat/ClickEvent.java new file mode 100644 index 0000000..cc8436c --- /dev/null +++ b/src/net/md_5/bungee/api/chat/ClickEvent.java @@ -0,0 +1,43 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.beans.ConstructorProperties; + +public final class ClickEvent { + private final Action action; + private final String value; + + public Action getAction() { + return this.action; + } + + public String getValue() { + return this.value; + } + + public String toString() { + return "ClickEvent(action=" + (Object)((Object)this.getAction()) + ", value=" + this.getValue() + ")"; + } + + @ConstructorProperties(value={"action", "value"}) + public ClickEvent(Action action, String value) { + this.action = action; + this.value = value; + } + + public static enum Action { + OPEN_URL, + OPEN_FILE, + RUN_COMMAND, + SUGGEST_COMMAND, + CHANGE_PAGE; + + + private Action() { + } + } + +} + diff --git a/src/net/md_5/bungee/api/chat/ComponentBuilder.java b/src/net/md_5/bungee/api/chat/ComponentBuilder.java new file mode 100644 index 0000000..20586f4 --- /dev/null +++ b/src/net/md_5/bungee/api/chat/ComponentBuilder.java @@ -0,0 +1,132 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.util.ArrayList; +import java.util.List; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + +public class ComponentBuilder { + private TextComponent current; + private final List parts = new ArrayList(); + + public ComponentBuilder(ComponentBuilder original) { + this.current = new TextComponent(original.current); + for (BaseComponent baseComponent : original.parts) { + this.parts.add(baseComponent.duplicate()); + } + } + + public ComponentBuilder(String text) { + this.current = new TextComponent(text); + } + + public ComponentBuilder append(String text) { + return this.append(text, FormatRetention.ALL); + } + + public ComponentBuilder append(String text, FormatRetention retention) { + this.parts.add(this.current); + this.current = new TextComponent(this.current); + this.current.setText(text); + this.retain(retention); + return this; + } + + public ComponentBuilder color(ChatColor color) { + this.current.setColor(color); + return this; + } + + public ComponentBuilder bold(boolean bold) { + this.current.setBold(bold); + return this; + } + + public ComponentBuilder italic(boolean italic) { + this.current.setItalic(italic); + return this; + } + + public ComponentBuilder underlined(boolean underlined) { + this.current.setUnderlined(underlined); + return this; + } + + public ComponentBuilder strikethrough(boolean strikethrough) { + this.current.setStrikethrough(strikethrough); + return this; + } + + public ComponentBuilder obfuscated(boolean obfuscated) { + this.current.setObfuscated(obfuscated); + return this; + } + + public ComponentBuilder insertion(String insertion) { + this.current.setInsertion(insertion); + return this; + } + + public ComponentBuilder event(ClickEvent clickEvent) { + this.current.setClickEvent(clickEvent); + return this; + } + + public ComponentBuilder event(HoverEvent hoverEvent) { + this.current.setHoverEvent(hoverEvent); + return this; + } + + public ComponentBuilder reset() { + return this.retain(FormatRetention.NONE); + } + + public ComponentBuilder retain(FormatRetention retention) { + TextComponent previous = this.current; + switch (retention) { + case NONE: { + this.current = new TextComponent(this.current.getText()); + break; + } + case ALL: { + break; + } + case EVENTS: { + this.current = new TextComponent(this.current.getText()); + this.current.setInsertion(previous.getInsertion()); + this.current.setClickEvent(previous.getClickEvent()); + this.current.setHoverEvent(previous.getHoverEvent()); + break; + } + case FORMATTING: { + this.current.setClickEvent(null); + this.current.setHoverEvent(null); + } + } + return this; + } + + public BaseComponent[] create() { + this.parts.add(this.current); + return this.parts.toArray(new BaseComponent[this.parts.size()]); + } + + public static enum FormatRetention { + NONE, + FORMATTING, + EVENTS, + ALL; + + + private FormatRetention() { + } + } + +} + diff --git a/src/net/md_5/bungee/api/chat/HoverEvent.java b/src/net/md_5/bungee/api/chat/HoverEvent.java new file mode 100644 index 0000000..5ff3b46 --- /dev/null +++ b/src/net/md_5/bungee/api/chat/HoverEvent.java @@ -0,0 +1,44 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.beans.ConstructorProperties; +import java.util.Arrays; +import net.md_5.bungee.api.chat.BaseComponent; + +public final class HoverEvent { + private final Action action; + private final BaseComponent[] value; + + public Action getAction() { + return this.action; + } + + public BaseComponent[] getValue() { + return this.value; + } + + public String toString() { + return "HoverEvent(action=" + (Object)((Object)this.getAction()) + ", value=" + Arrays.deepToString(this.getValue()) + ")"; + } + + @ConstructorProperties(value={"action", "value"}) + public HoverEvent(Action action, BaseComponent[] value) { + this.action = action; + this.value = value; + } + + public static enum Action { + SHOW_TEXT, + SHOW_ACHIEVEMENT, + SHOW_ITEM, + SHOW_ENTITY; + + + private Action() { + } + } + +} + diff --git a/src/net/md_5/bungee/api/chat/TextComponent.java b/src/net/md_5/bungee/api/chat/TextComponent.java new file mode 100644 index 0000000..67f4bdd --- /dev/null +++ b/src/net/md_5/bungee/api/chat/TextComponent.java @@ -0,0 +1,170 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.beans.ConstructorProperties; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; + +public class TextComponent +extends BaseComponent { + private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$"); + private String text; + + public static BaseComponent[] fromLegacyText(String message) { + ArrayList components = new ArrayList(); + StringBuilder builder = new StringBuilder(); + TextComponent component = new TextComponent(); + Matcher matcher = url.matcher(message); + block8 : for (int i = 0; i < message.length(); ++i) { + TextComponent old; + char c2 = message.charAt(i); + if (c2 == '\u00a7') { + ChatColor format; + if ((c2 = message.charAt(++i)) >= 'A' && c2 <= 'Z') { + c2 = (char)(c2 + 32); + } + if ((format = ChatColor.getByChar(c2)) == null) continue; + if (builder.length() > 0) { + old = component; + component = new TextComponent(old); + old.setText(builder.toString()); + builder = new StringBuilder(); + components.add(old); + } + switch (format) { + case BOLD: { + component.setBold(true); + continue block8; + } + case ITALIC: { + component.setItalic(true); + continue block8; + } + case UNDERLINE: { + component.setUnderlined(true); + continue block8; + } + case STRIKETHROUGH: { + component.setStrikethrough(true); + continue block8; + } + case MAGIC: { + component.setObfuscated(true); + continue block8; + } + case RESET: { + format = ChatColor.WHITE; + } + default: + } + component = new TextComponent(); + component.setColor(format); + continue; + } + int pos = message.indexOf(32, i); + if (pos == -1) { + pos = message.length(); + } + if (matcher.region(i, pos).find()) { + if (builder.length() > 0) { + old = component; + component = new TextComponent(old); + old.setText(builder.toString()); + builder = new StringBuilder(); + components.add(old); + } + old = component; + component = new TextComponent(old); + String urlString = message.substring(i, pos); + component.setText(urlString); + component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, urlString.startsWith("http") ? urlString : "http://" + urlString)); + components.add(component); + i += pos - i - 1; + component = old; + continue; + } + builder.append(c2); + } + if (builder.length() > 0) { + component.setText(builder.toString()); + components.add(component); + } + if (components.isEmpty()) { + components.add(new TextComponent("")); + } + return components.toArray(new BaseComponent[components.size()]); + } + + public TextComponent(TextComponent textComponent) { + super(textComponent); + this.setText(textComponent.getText()); + } + + public /* varargs */ TextComponent(BaseComponent ... extras) { + this.setText(""); + this.setExtra(new ArrayList(Arrays.asList(extras))); + } + + @Override + public BaseComponent duplicate() { + return new TextComponent(this); + } + + @Override + protected void toPlainText(StringBuilder builder) { + builder.append(this.text); + super.toPlainText(builder); + } + + @Override + protected void toLegacyText(StringBuilder builder) { + builder.append((Object)this.getColor()); + if (this.isBold()) { + builder.append((Object)ChatColor.BOLD); + } + if (this.isItalic()) { + builder.append((Object)ChatColor.ITALIC); + } + if (this.isUnderlined()) { + builder.append((Object)ChatColor.UNDERLINE); + } + if (this.isStrikethrough()) { + builder.append((Object)ChatColor.STRIKETHROUGH); + } + if (this.isObfuscated()) { + builder.append((Object)ChatColor.MAGIC); + } + builder.append(this.text); + super.toLegacyText(builder); + } + + @Override + public String toString() { + return String.format("TextComponent{text=%s, %s}", this.text, super.toString()); + } + + public String getText() { + return this.text; + } + + public void setText(String text) { + this.text = text; + } + + @ConstructorProperties(value={"text"}) + public TextComponent(String text) { + this.text = text; + } + + public TextComponent() { + } + +} + diff --git a/src/net/md_5/bungee/api/chat/TranslatableComponent.java b/src/net/md_5/bungee/api/chat/TranslatableComponent.java new file mode 100644 index 0000000..8e69917 --- /dev/null +++ b/src/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -0,0 +1,196 @@ +/* + * Decompiled with CFR 0_114. + */ +package net.md_5.bungee.api.chat; + +import java.util.ArrayList; +import java.util.List; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.TextComponent; + +public class TranslatableComponent +extends BaseComponent { + private final ResourceBundle locales = ResourceBundle.getBundle("mojang-translations/en_US"); + private final Pattern format = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)"); + private String translate; + private List with; + + public TranslatableComponent(TranslatableComponent original) { + super(original); + this.setTranslate(original.getTranslate()); + if (original.getWith() != null) { + ArrayList temp = new ArrayList(); + for (BaseComponent baseComponent : original.getWith()) { + temp.add(baseComponent.duplicate()); + } + this.setWith(temp); + } + } + + public /* varargs */ TranslatableComponent(String translate, Object ... with) { + this.setTranslate(translate); + ArrayList temp = new ArrayList(); + for (Object w : with) { + if (w instanceof String) { + temp.add(new TextComponent((String)w)); + continue; + } + temp.add((BaseComponent)w); + } + this.setWith(temp); + } + + @Override + public BaseComponent duplicate() { + return new TranslatableComponent(this); + } + + public void setWith(List components) { + for (BaseComponent component : components) { + component.parent = this; + } + this.with = components; + } + + public void addWith(String text) { + this.addWith(new TextComponent(text)); + } + + public void addWith(BaseComponent component) { + if (this.with == null) { + this.with = new ArrayList(); + } + component.parent = this; + this.with.add(component); + } + + @Override + protected void toPlainText(StringBuilder builder) { + String trans; + try { + trans = this.locales.getString(this.translate); + } + catch (MissingResourceException ex) { + trans = this.translate; + } + Matcher matcher = this.format.matcher(trans); + int position = 0; + int i = 0; + while (matcher.find(position)) { + int pos = matcher.start(); + if (pos != position) { + builder.append(trans.substring(position, pos)); + } + position = matcher.end(); + String formatCode = matcher.group(2); + switch (formatCode.charAt(0)) { + case 'd': + case 's': { + String withIndex = matcher.group(1); + this.with.get(withIndex != null ? Integer.parseInt(withIndex) - 1 : i++).toPlainText(builder); + break; + } + case '%': { + builder.append('%'); + } + } + } + if (trans.length() != position) { + builder.append(trans.substring(position, trans.length())); + } + super.toPlainText(builder); + } + + @Override + protected void toLegacyText(StringBuilder builder) { + String trans; + try { + trans = this.locales.getString(this.translate); + } + catch (MissingResourceException e) { + trans = this.translate; + } + Matcher matcher = this.format.matcher(trans); + int position = 0; + int i = 0; + while (matcher.find(position)) { + int pos = matcher.start(); + if (pos != position) { + this.addFormat(builder); + builder.append(trans.substring(position, pos)); + } + position = matcher.end(); + String formatCode = matcher.group(2); + switch (formatCode.charAt(0)) { + case 'd': + case 's': { + String withIndex = matcher.group(1); + this.with.get(withIndex != null ? Integer.parseInt(withIndex) - 1 : i++).toLegacyText(builder); + break; + } + case '%': { + this.addFormat(builder); + builder.append('%'); + } + } + } + if (trans.length() != position) { + this.addFormat(builder); + builder.append(trans.substring(position, trans.length())); + } + super.toLegacyText(builder); + } + + private void addFormat(StringBuilder builder) { + builder.append((Object)this.getColor()); + if (this.isBold()) { + builder.append((Object)ChatColor.BOLD); + } + if (this.isItalic()) { + builder.append((Object)ChatColor.ITALIC); + } + if (this.isUnderlined()) { + builder.append((Object)ChatColor.UNDERLINE); + } + if (this.isStrikethrough()) { + builder.append((Object)ChatColor.STRIKETHROUGH); + } + if (this.isObfuscated()) { + builder.append((Object)ChatColor.MAGIC); + } + } + + public ResourceBundle getLocales() { + return this.locales; + } + + public Pattern getFormat() { + return this.format; + } + + public String getTranslate() { + return this.translate; + } + + public List getWith() { + return this.with; + } + + public void setTranslate(String translate) { + this.translate = translate; + } + + @Override + public String toString() { + return "TranslatableComponent(locales=" + this.getLocales() + ", format=" + this.getFormat() + ", translate=" + this.getTranslate() + ", with=" + this.getWith() + ")"; + } + + public TranslatableComponent() { + } +} +