Bukkit/Spigot HeroChat 5:格式字符串中的自定义替换
Bukkit/Spigot HeroChat 5: Custom Replacement in format string
这件事让我困惑了一段时间...
HeroChat 在其 config.yml
文件中有一个名为 format:
的部分,您可以在该部分下找到默认格式字符串。这在每个通道的配置中都有体现。这是我的示例...
format:
default: '{color}[{nick}]{title} {groupprefix}&f{sender}: {color}{msg}'
{color}
表示该通道配置中定义的颜色
{nick}
代表频道的"nickname"
{title}
是自定义格式字符串
{groupprefix}
是分配给玩家的 Vault 组的前缀。
{sender}
是发送消息的玩家的显示名称(或昵称)。
{msg}
是他们在通过内置审查后输入控制台的消息。
那么,您如何获得 {title}
或您要更改的任何自定义字符串?正如我上面所说,这是我很长一段时间都想不通的事情。但是,做了一些研究,我已经弄明白了,实际上并没有那么难。我把它留在这里作为 Java 开发人员 运行 进入同一问题的资源。
我将在下面留下分步解决方案。本教程假设您熟悉您选择的 IDE 并且有办法检索特定于玩家或特定于组的替换字符串...
- 将 HeroChat.jar 加载到您的环境中。
- 创建并注册一个新的 PlayerListener。
- 添加以下导入:
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;
- 添加以下事件:
@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);
}
}
这件事让我困惑了一段时间...
HeroChat 在其 config.yml
文件中有一个名为 format:
的部分,您可以在该部分下找到默认格式字符串。这在每个通道的配置中都有体现。这是我的示例...
format:
default: '{color}[{nick}]{title} {groupprefix}&f{sender}: {color}{msg}'
{color}
表示该通道配置中定义的颜色
{nick}
代表频道的"nickname"
{title}
是自定义格式字符串
{groupprefix}
是分配给玩家的 Vault 组的前缀。
{sender}
是发送消息的玩家的显示名称(或昵称)。
{msg}
是他们在通过内置审查后输入控制台的消息。
那么,您如何获得 {title}
或您要更改的任何自定义字符串?正如我上面所说,这是我很长一段时间都想不通的事情。但是,做了一些研究,我已经弄明白了,实际上并没有那么难。我把它留在这里作为 Java 开发人员 运行 进入同一问题的资源。
我将在下面留下分步解决方案。本教程假设您熟悉您选择的 IDE 并且有办法检索特定于玩家或特定于组的替换字符串...
- 将 HeroChat.jar 加载到您的环境中。
- 创建并注册一个新的 PlayerListener。
- 添加以下导入:
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;
- 添加以下事件:
@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);
}
}