0

これは、しばらく私を逃してきたものでした...

HeroChat のconfig.ymlファイルには というセクションがあり、そのformat:下にデフォルトのフォーマット文字列があります。これは、各チャネルの構成に反映されます。これは私がどのように見えるかの例です...

format:
  default: '{color}[{nick}]{title} {groupprefix}&f{sender}: {color}{msg}'

{color}はチャンネルの設定で定義された色を
{nick}表します チャンネルの「ニックネーム」を表します は
{title}カスタムのフォーマット文字列
{groupprefix}です はプレーヤーの Vault グループに割り当てられたプレフィックスです。
{sender}メッセージを送信するプレイヤーの表示名 (またはニックネーム) です。 {msg}組み込みの検閲を通過した後、コンソールに入力されたメッセージです。

では、{title}カスタム文字列を変更するにはどうすればよいのでしょうか。上で述べたように、これは私が長い間理解できなかったことです。でも、いろいろ調べてみると、実はそんなに難しいことではないことがわかりました。これは、同じ問題に直面している Java 開発者向けのリソースとしてここに残しておきます。

4

1 に答える 1

0

以下に段階的な解決策を残します。このチュートリアルでは、選択した IDE に精通しており、プレーヤー固有またはグループ固有の置換文字列を取得する方法があることを前提としています...

  1. 環境に HeroChat.jar をロードします。
  2. 新しい PlayerListener を作成して登録します。
  3. 次のインポートを追加します。
    • import org.bukkit.entity.Player;
    • import org.bukkit.event.EventHandler;
    • import org.bukkit.event.Listener;
    • import org.bukkit.event.player.PlayerJoinEvent;
    • import com.dthielke.herochat.ChannelChatEvent;
    • import com.dthielke.herochat.ChannelManager;
    • import com.dthielke.herochat.Herochat;
  4. 次のイベントを追加します。
@EventHandler
public void onPlayerChat(ChannelChatEvent event) {
    ChannelManager cm = Herochat.getChannelManager();
    String newFormat;
    Player player = event.getSender().getPlayer();
    String chatReplacement = plugin.classWithReplacer.replacer(player);

    // Simple replacement here. If it is equal to "", let it replace as
    // normal. If it actually has a value, surround it with brackets.
    if (!chatTitle.equalsIgnoreCase("")) {
        chatTitle = "[" + chatTitle + "]";
    }

    // When the channel being spoken in uses the default format,
    // asking it for the format returns "{format}"
    //
    // We do not need to escape the brackets because
    // String.equalsIgnoreCase does not use regex.
    if (event.getFormat().equalsIgnoreCase("{default}")) {
        // cm.getStandardFormat() returns the format provided in config.yml
        newFormat = cm.getStandardFormat();

        // IMPORTANT!! You MUST escape the curly brackets or your plugin WILL throw regex errors!
        // We escape with two backslashes because the java parser takes one away, resulting in \{ and \} respectively.
        // Then, the regex parser comes in and converts \{ into a safe {, and \} into a safe }
        // "your_replacement_here" should be whatever your custom tag is within the config.yml or the channel's config file.
        // In my case, this was {title}.
        // Note: the square brackets can be added to your config file, but I chose to add them here to avoid empty brackets
        // when the player did not have a title.
        newFormat = newFormat.replaceAll("\\{your_replacement_here\\}", chatReplacement);
    } else {
        // event.getFormat() returns the current channel's format or "{default}"
        newFormat = event.getFormat();
        newFormat = newFormat.replaceAll("\\{your_replacement_here\\}", chatReplacement);
    }

    // This method performs a "one-time" change to the default format.
    // Because you are providing the same format as the original, only
    // contextually modified for the player or player's group, the chat
    // output will still be determined by the global or channel config
    event.setFormat(newFormat);
}

そして、ここに完全な(コメントはありませんが)バージョンがあります

package your.package.here;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import com.dthielke.herochat.ChannelChatEvent;
import com.dthielke.herochat.ChannelManager;
import com.dthielke.herochat.Herochat;
import your.package.here.MyPlugin;

public class MyPluginPlayerListener implements Listener {

    private MyPlugin plugin;

    public MyPluginPlayerListener(MyPlugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerChat(ChannelChatEvent event) {
        ChannelManager cm = Herochat.getChannelManager();
        String newFormat;
        Player player = event.getSender().getPlayer();
        String chatTitle = plugin.titlesConfig.getTitle(player);

        if (!chatTitle.equalsIgnoreCase("")) {
            chatTitle = "[" + chatTitle + "]";
        }
        if (event.getFormat().equalsIgnoreCase("{default}")) {
            newFormat = cm.getStandardFormat();
            newFormat = newFormat.replaceAll("\\{title\\}", chatTitle");
        } else {
            newFormat = event.getFormat();
            newFormat = newFormat.replaceAll("\\{title\\", chatTitle");
        }

        event.setFormat(newFormat);
    }
}
于 2016-06-09T14:46:01.997 に答える