[SOLVED]How to rollback block break events?

Discussion in 'Plugin Development' started by blackwolf12333, May 17, 2012.

Thread Status:
Not open for further replies.
  1. So, what i want to do is rolling back a few block break events i logged in a file, the function i use for the rollback looks like this:
    Code:
    public boolean rollback(String file, Player p)
        {
            boolean ret = false;
            if(file.equalsIgnoreCase("config.yml"))    {/* do nothing here*/}
            else
            {
                int count = 0;
                while(count != fu.count(new File(plugin.getDataFolder() + File.separator + file)))
                {
                    String data = fu.readLine(new File(plugin.getDataFolder() + File.separator + file), count);
                    p.sendMessage(data);
                    String[] split = data.split(",");
                    if(split.length == 4)
                    {
                        int x = Integer.parseInt(split[0]);
                        int y = Integer.parseInt(split[1]);
                        int z = Integer.parseInt(split[2]);
                        int id = Integer.parseInt(split[3]);
                       
                        World world = p.getWorld();
                        Location loc = new Location(world, x, y, z);
                        world.getBlockAt(loc).setTypeId(id);
                        count++;
                    }
                }
                ret = true;
            }
            return ret;
        }
    The readFile function you see in there:
    Code:
    public String readLine(File file, int line)
        {
            int currentline;
            String ret = "0";
           
            if(!file.exists())
            {
                AutoGriefBan.log.info("Failed to open file!");
                return "0";
            }
            try {
                BufferedReader in = new BufferedReader(new FileReader(file));
               
                for(currentline = 0; currentline != count(file); currentline++)
                {
                    if(currentline == line)
                    {
                        ret = in.readLine();
                    }
                    else
                    {
                        in.readLine();
                    }
                }
               
                in.close();
            } catch(Exception e) {
                AutoGriefBan.log.warning(e.getMessage());
            }
            return ret;
        }
    The problem that occurs is that when i use this rollback function it rolls all the events back except for the last; so the last line of the file, now is the question, how do i also rollback that last event?

    thanks in advance, blackwolf12333
     
  2. for the actual reading you should use this loop instead of the for loop:
    Code:
    String currentLine = "";
    while((currentLine = in.readLine()) != null)
    {
       // Do whatever you want with the line
    }
     
  3. Yeah, i know, but that does almost the same as this for loop:p
    But thanks:)
     
  4. But you'll notice it'll read the last line as well :)
    But what does the count(File) method actually do?
     
  5. Count how many lines a file has:)
     
  6. Then did you tried using the '<=' operator instead of '!=' ?
    I'm not entirely sure, just guessing :p
     
  7. I tried almost everything possible:p so yeah, i tried that one too, i am now trying the while loop again, just testing.
    greetz blackwolf12333

    EDIT: so i tried the while loop, didn't happen anything, not even one block got rolled back:p
     
  8. It's starting to get really strange... I'm gonna test both things...
     
  9. Yeah, indeed:p
     
  10. Using the while loop it works totally fine. Read 7 of 7 lines from my file. BTW: wouldn't it be better if you read the whole file once and then just get the lines from the output rather than reading it over and over again?
     
  11. How did you do it, because it has to read the line you give as parameter in the function, i think you didn't do that:p
    and yes, it would be better, but if yours works and i can use it too, i'll just use that:p
    greetz blackwolf12333
     
  12. Code:
    String[] lines = new String[count(file)];
    String currentLine = "";
    for(int i = 0; (currentLine = in.readLine()) != null; i++)
    {
        lines[i] = currentLine;
    }
    Now you have all lines and can just iterate through them without the need of reading the file over and over again.
     


  13. Thank you so much:D, it works finally, great job:p
    greetz blackwolf12333[/CODE][/quote][/CODE][/FONT][/quote]

    geez this post just won't change...[/CODE][/FONT][/quote]
     
Thread Status:
Not open for further replies.

Share This Page