Solved setBlockFast won't work?

Discussion in 'Plugin Development' started by blackwolf12333, Oct 20, 2012.

Thread Status:
Not open for further replies.
  1. I am trying to let my rollback work a bit faster, so i went searching and found bergerkiller's method to change blocks faster.
    I implemented that correctly, also updating the client with the changed chunks.
    But what just won't happen is that the blocks get set.
    What i basically do is loop through a list of Blocks, packed in a nice wrapper so it has some more functions, and call one of these functions to start the rollback of that block.

    loop:
    Code:
    public void run() {
      for(int i = 0; i < result.size(); i++) {
          rollback(result.get(i);
      }
    }
     
    public void rollback(BaseData line) {
        Events event = Events.getEvent(line.getEvent());
        if(event != null) {
             if(event.getCanRollback()) {
                  line.rollback(this);
             }
         }
    }
    The loop is inside a class called Rollback.

    line.rollback(Rollback rollback):
    Code:
    public void rollback(Rollback rollback) {
        Location loc = new Location(Bukkit.getWorld(worldName), blockX, blockY, blockZ);
        setBlockFast(loc, Material.getMaterial(blockType).getId(), blockData);
        rollback.chunks.add(loc.getChunk());
    }
    I can guarantee that all the values used in this code that i did not give the declaration for are all not null.
    If you need more code, just ask:)
    The run method was because i first had the rollback in a thread, because that didn't work with this code i tried without thread too, but that didn't work either.
    bergerkiller maybe you know what i am doing wrong?

    Thanks in advance, blackwolf12333
     
  2. Offline

    bergerkiller

    blackwolf12333
    I think I need to know the actual code or snippet of some logic to see what is wrong. I do see from your post you are adding chunks dynamically. Did you make sure that these chunks don't unload? Are all blocks/areas being rolled back adding the chunks correctly?
     
  3. Good point, i'll make sure they don't unload until the rollback is really finished.
    Also, i will update the github repository and if you want the specific files i use, i'll just put them on pastebin for you.
     
  4. Offline

    bergerkiller

  5. bergerkiller Github is updated, code is a bit messy because i was trying to figure out what was going wrong...
    also doesn't include checking if the chunks don't unload yet, adding that now. DONE:)
     
  6. Offline

    bergerkiller

    blackwolf12333
    I looked through it. A few things you have to improve first...

    You are using Location objects to store blocks. This is extremely inefficient. Just store the x/y/z and a World somewhere. You could make your own Position class or smth to do it, too. Or store a Block.

    Code:
        public void setBlockFast(Location loc, int typeID, byte data) {
    setBlockFast((int)loc.getX(), (int)loc.getY(), (int)loc.getZ(), loc.getWorld().getName().trim(), typeID, data);
    }
    Use loc.getBlockX(), the method you use is most likely invalid if the location is not a valid decimal x/y/z.

    Code:
        public void setBlockFast(int x, int y, int z, String world, int typeID, byte data) {
    Chunk c = Bukkit.getWorld(world).getChunkAt(x, z);
    net.minecraft.server.Chunk chunk = ((CraftChunk) c).getHandle();
    chunk.a(x & 15, y, z & 15, typeID, data);
    }
    world.getChunkAt uses chunk coordinates, not block coordinates. Change that to:
    Code:
    Chunk c = Bukkit.getWorld(world).getChunkAt(x >> 4, z >> 4);
    Or use a getChunkAtWorldCoordinates...if it exists. Maybe use world.getChunkAt(block).
     
  7. Ok, thanks:)
    That i used loc.getX() was because i was just trying if that would work:p
    And i didn't know that getChunkAt(x, z) uses chunk coordinates, that was probably the problem, so it is solved now:D
     
  8. bergerkiller I still have one problem left, it was said that this would give me the possibility to change a lot of blocks very fast without lag, which part did i not understand? because this is still causing a lot of lag, server and client side.
     
  9. Offline

    bergerkiller

    blackwolf12333
    Chunk load lag can not be prevented. Obviously chunks have to load if you want to change blocks in them, and since you are restoring blocks all over the place, it is pretty much certain that a lot of chunk loads occur. You can listen for the chunk load event to verify this yourself. If this is not the problem, you can add timing functions everywhere in your logic to see what takes long to do. Timing function:
    Code:
    long start = System.nanoTime();
    // Do you logic here
    System.out.println(((System.nanoTime() - start) / 1E6) + " ms");
     
  10. Hmm, ok. I think i will have to life with that than.
     
Thread Status:
Not open for further replies.

Share This Page