2016-06-09 72 views
0

这已被一些一直困扰我一段时间......Bukkit /插口HeroChat 5:在格式字符串自定义更换

HeroChat在其config.yml文件中,有一个叫段format:下,你会找到一个默认格式字符串。这在每个通道的配置中都有回应。这是一个什么样矿山看起来像一个例子...

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

{color}代表在该通道的配置定义的颜色
{nick}表示通道的“昵称”
{title}是一个自定义格式字符串
{groupprefix}是前缀分配给玩家的保险柜组。
{sender}是发送消息的玩家的显示名称(或昵称)。 {msg}是他们在完成内置审查后键入控制台的消息。

那么,你如何得到{title}或你的自定义字符串将改变什么?正如我上面所说,这是我很长一段时间无法弄清楚的事情。但是,通过一些研究,我发现它并没有那么困难。我在此将其作为Java开发人员遇到同一问题的资源。

+0

: //www.spigotmc.org/threads/bukkit-spigot-herochat-5-custom-replacement-in-format-string.154256/#post-1638156)。 –

回答

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); 
} 

下面是这已经张贴到[这个插口论坛主题(HTTPS完整的(虽然未注释)版本

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); 
    } 
}