我正在尝试制作一个没有发誓的插件,它没有错误,但它是区分大小写的,即使我使用的是equalsIgnoreCase。我希望它用单词" curse"替换所有诅咒单词(如配置中所定义)。如果邮件收件人超过10个。为什么这段代码不起作用?
主要插件代码:
public class main extends JavaPlugin implements Listener {
public static Bukkit plugin;
public void onEnable()
{
Bukkit.getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
saveConfig();
this.reloadConfig();
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "Enabled!");
}
public void onDisable()
{
getLogger();
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "Disabled!");
}
@EventHandler
public void playerChat(AsyncPlayerChatEvent e){
Set r = e.getRecipients();
if (r.size() > 10 ) {
List g = this.getConfig().getList("Swears");
for (int i = 0; i < g.size(); i++) {
if(g.get(i).tostring().equalsIgnoreCase(e.getMessage())) {
String message = e.getMessage().replaceAll(g.get(i).toString(), "curse");
e.setMessage(message);
}
}
}
}
}
config.yml:
#Default Config
Swears:
plugin.yml:
name: NoSwear
main: me.mrpoopy345.bukkitplugin.main
version: 1.0
author: mrpoopy345
description: NoSwear
commands:
答案 0 :(得分:0)
以下是我改变事物的方式:
//By convention you always capitalize Class names. So I changed the name from "main" to "Main".
public class Main extends JavaPlugin implements Listener {
// I am not sure why you had a static Bukkit variable, so I removed it.
// This variable is transient, so it can be safely accessed from other threads
public transient List<String> swares;
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
saveConfig();
this.reloadConfig();
// I get the swears out of the config here, so I won't have to
// access the config while in the AsyncPlayerChatEvent
swares = getConfig().getStringList("Swears");
// A more common way to log something is through the logger, here is an example:
getLogger().log(Level.INFO, "Enabled!");
// Also, a good thing to remember is the the console will not see chat colors as their code
// So instead of displaying a message as red it would display '§4message'
}
public void onDisable() {
//Here is a different way to use the logger:
getLogger().info("Disabled!");
}
@EventHandler
public void playerChat(AsyncPlayerChatEvent e) {
// I combined this to one line, and reversed it so there are less curly braces.
if (e.getRecipients().size() > 10)
return;
// I replaced what you did with this, it's much simpler!
for (String sware : swares) {
// adding (?i) tells it to ignore case
e.setMessage(e.getMessage().replaceAll("(?i)" + sware, "curse"));
}
}
}
因为我更改了插件类名称,所以您还需要将plugin.yml main更改为me.mrpoopy345.bukkitplugin.Main
干杯!
(我不认为这会将聊天事件同步到主线程,但如果有人编辑我的帖子并修复它)