【问题标题】:Bukkit trim not workingBukkit 装饰不工作
【发布时间】:2015-10-02 00:13:41
【问题描述】:

由于某些奇怪的原因,当我尝试修剪“替换:”时,它不会因为某些奇怪的原因而被修剪。就像它的一部分会根据列入黑名单的单词有多少字符而被修剪,但总体而言它没有按预期工作。

它的假设是将“replace:”替换为“”,但这并不想工作。

这是我的代码:

@EventHandler 公共无效BlackListWords(AsyncPlayerChatEvent e){ 字符串标签=“替换:”;

for (String s : p.file.getFile().getStringList(p.file.path + ".BlacklistedWords")) {
    String[] parts = labels.split(" ");

        String replace = parts[0];


        Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Words:\n" + s.replace("replace:", ""));
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "StringList:\n" + s.split(" ")[0]);

        if (e.getMessage().equalsIgnoreCase(s.split(" ")[0])) {
            e.setMessage(s.substring(replace.length()).replaceAll("//replace", "").split(" ")[0].replace("_", " "));
    }
}


}

【问题讨论】:

  • 您的代码对您的意图有点混乱,但我假设您正在尝试获取 .BlacklistedWords 文件中的条目并从 e.getMessage() 中删除它们的所有实例?您只是想将它们从消息本身中删除,还是尝试替换替换? .BladklistedWords 文件中条目的格式是什么,是“替换:坏词”吗?
  • @Michael,我正在尝试替换替换。当我尝试这样做时,替换:是否存在
  • 在下面查看我的答案,它涉及删除“替换:”您上面的代码也是使用 s 作为参数调用 set message,其中 s 是您从黑名单中读取的行,而不是消息本身。在下面的示例中,incomingMessage 将是您的 e.getMessage() 而 modifiedMessage 将是您将用作 e.setMessage() 中的值

标签: java bukkit


【解决方案1】:

这是一个可能有帮助的例子。阅读您的代码,您的文件中的字符串列表似乎可能包含以“替换:”作为前缀的行,然后您正在做一些多余的事情来尝试删除该前缀,然后最终使用错误的字符串作为您的消息.看看这是否有助于澄清,但它是基于我假设你正在尝试做的......

import java.util.ArrayList;
import org.junit.Test;

public class StackOverflow_32878663 {

    @Test
    public void replaceBlacklistedWords()
    {

        // mimicking your list of strings from the file
        ArrayList<String> wordList = new ArrayList<String>();
        wordList.add("replace: blue");
        wordList.add("replace: green");
        wordList.add("replace: red");
        wordList.add("replace: white");
        wordList.add("replace: yellow");

        String incomingMessage = "I am RED, white, bLuE, and green all over.  What am I?";
        String modifiedMessage = incomingMessage;

        for (String s : wordList)
        {
            // one way of many to remove "replace:" if that is important
            String justTheWord = s.split(" ", 2)[1];  

            // the (?i) tells the regex to perform in a case insensitive manner
            modifiedMessage = modifiedMessage.replaceAll("(?i)" + justTheWord, " ");  
        }

        System.out.println("Original message: " + incomingMessage);
        System.out.println("Modified message: " + modifiedMessage);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2020-07-11
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多