简体   繁体   English

向玩家发送消息时出现问题

[英]Problems sending a message to a player

I have a problem with my bukkit plugin. 我的bukkit插件有问题。 What I try to do is search through a file, and read it line by line (that works), then if the line has some text in it, it has to return that line, but it also has to return all the other lines in the file which also have that specific text in it. 我试图做的是搜索文件,并逐行读取(有效),然后如果该行中包含一些文本,则必须返回该行,但还必须返回其中的所有其他行该文件中还包含该特定文本。 And when i have these lines, i have to send these lines in a message to the Player, that is not the problem, but when i send the lines i get now, the "\\n" doesn't work, here is the code i use now: 当我有这些行时,我必须在一条消息中将这些行发送给播放器,这不是问题,但是当我现在发送这些行时,“ \\ n”不起作用,这是代码我现在使用:

  public String searchText(String text, String file, Player p)
    {
        String data = null;

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = null;

            while((line = br.readLine()) != null)
            {
                if(line.indexOf(text) >= 0)
                {
                    data += System.getProperty("line.separator") + line + System.getProperty("line.separator");
                }
                p.sendMessage("+++++++++++GriefLog+++++++++++");
                p.sendMessage(data);
                p.sendMessage("++++++++++GriefLogEnd+++++++++");
            }

            br.close();

        } catch (Exception e) {
            e.printStackTrace();            
        }

        return "";
    }

The return is meant to be empty, because the info is returned to the player a bit higher:P The problem now is, how do i add an "\\n" to the data variable, because when i use this function in the rest of my code, it gives a lot of lines, but without the "\\n", so how do i put that in? 返回值是空的,因为信息返回给播放器的位置更高:P现在的问题是,如何在数据变量中添加“ \\ n”,因为当我在其余部分使用此功能时我的代码中有很多行,但是没有“ \\ n”,所以我该怎么放呢?

Since your method isn't supposed to return anything, remove your return statement and set the return type to void. 由于您的方法不应返回任何内容,因此请删除return语句并将返回类型设置为void。 It looks like your code would output the data string once for each line your search term occurs, try: 看起来您的代码会为搜索词出现的每一行输出一次数据字符串,请尝试:

data = "";
while((line = br.readLine()) != null)
{
    if(line.indexOf(text) >= 0)
    {
        //remove the first System.getProperty("line.separator") if
        //you don't want a leading empty line
        data += System.getProperty("line.separator") + line + 
            System.getProperty("line.separator");
    }
}
if (data.length() > 0) {
    p.sendMessage("+++++++++++GriefLog+++++++++++");
    p.sendMessage(data);
    p.sendMessage("++++++++++GriefLogEnd+++++++++");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM