From f19cc7fe4fb915d085783d8b29c6818577ad7b06 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Mon, 8 Sep 2014 21:15:14 +0200 Subject: [PATCH] Add chat position API. --- .../bungee/api/connection/ProxiedPlayer.java | 17 +++++++++ .../net/md_5/bungee/api/ChatMessageType.java | 11 ++++++ .../java/net/md_5/bungee/UserConnection.java | 36 +++++++++++++++++-- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 chat/src/main/java/net/md_5/bungee/api/ChatMessageType.java diff --git a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java index f39c3473..4304b1e9 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java @@ -4,6 +4,7 @@ import java.util.Locale; import java.util.Map; import java.util.UUID; import net.md_5.bungee.api.Callback; +import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.Title; import net.md_5.bungee.api.chat.BaseComponent; @@ -31,6 +32,22 @@ public interface ProxiedPlayer extends Connection, CommandSender */ void setDisplayName(String name); + /** + * Send a message to the specified screen position of this player. + * + * @param position the screen position + * @param message the message to send + */ + public void sendMessage(ChatMessageType position, BaseComponent... message); + + /** + * Send a message to the specified screen position of this player. + * + * @param position the screen position + * @param message the message to send + */ + public void sendMessage(ChatMessageType position, BaseComponent message); + /** * Connects / transfers this user to the specified connection, gracefully * closing the current one. Depending on the implementation, this method diff --git a/chat/src/main/java/net/md_5/bungee/api/ChatMessageType.java b/chat/src/main/java/net/md_5/bungee/api/ChatMessageType.java new file mode 100644 index 00000000..1d126a91 --- /dev/null +++ b/chat/src/main/java/net/md_5/bungee/api/ChatMessageType.java @@ -0,0 +1,11 @@ +package net.md_5.bungee.api; + +/** + * Represents the position on the screen where a message will appear. + */ +public enum ChatMessageType +{ + CHAT, + SYSTEM, + ACTION_BAR +} diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 6a89b52a..e69544d1 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -24,6 +24,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; import net.md_5.bungee.api.Callback; +import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.Title; import net.md_5.bungee.api.chat.BaseComponent; @@ -370,13 +371,44 @@ public final class UserConnection implements ProxiedPlayer @Override public void sendMessage(BaseComponent... message) { - unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) ); + sendMessage( ChatMessageType.CHAT, message ); } @Override public void sendMessage(BaseComponent message) { - unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) ); + sendMessage( ChatMessageType.CHAT, message ); + } + + private void sendMessage(ChatMessageType position, String message) + { + unsafe().sendPacket( new Chat( message, (byte) position.ordinal() ) ); + } + + @Override + public void sendMessage(ChatMessageType position, BaseComponent... message) + { + // Action bar doesn't display the new JSON formattings, legacy works - send it using this for now + if ( position == ChatMessageType.ACTION_BAR && pendingConnection.getVersion() >= ProtocolConstants.MINECRAFT_SNAPSHOT ) + { + sendMessage( position, ComponentSerializer.toString( new TextComponent( TextComponent.toLegacyText( message ) ) ) ); + } else + { + sendMessage( position, ComponentSerializer.toString( message ) ); + } + } + + @Override + public void sendMessage(ChatMessageType position, BaseComponent message) + { + // Action bar doesn't display the new JSON formattings, legacy works - send it using this for now + if ( position == ChatMessageType.ACTION_BAR && pendingConnection.getVersion() >= ProtocolConstants.MINECRAFT_SNAPSHOT ) + { + sendMessage( position, ComponentSerializer.toString( new TextComponent( TextComponent.toLegacyText( message ) ) ) ); + } else + { + sendMessage( position, ComponentSerializer.toString( message ) ); + } } @Override