Copying Chunks?

Discussion in 'Spigot Plugin Development' started by xXVevzZXx, Apr 18, 2015.

  1. I want to copy a Chunk to another Empty Chunk. At the moment I can copy Chunks with the following code but the Problem is that if I copy the Chunk the Chunks are hooked into each others so if you break something in the first Chunk it will break in the second Chunk to and otherwise. I'm pretty sure it's because the ChunkSections are the same as from the second Chunk, but I have no clue how to copy the ChunkSections that they are independent from each other.
    Could anyone please Help me?

    Code (Text):
    public static void copyChunk(World world, Location from, Location to)
            org.bukkit.Chunk fromBukkitChunk = from.getWorld().getChunkAt(from);
            org.bukkit.Chunk toBukkitChunk = to.getWorld().getChunkAt(to);
            CraftChunk fromCC = (CraftChunk) fromBukkitChunk;
            CraftChunk toCC = (CraftChunk) toBukkitChunk;
            Chunk fromChunk = fromCC.getHandle();
            Chunk toChunk = toCC.getHandle();
            ChunkSection[] tsec = fromChunk.getSections();
           ChunkSection[] afterSec = new ChunkSection[tsec.length];
           for (int n = 0; n < tsec.length; n++) {
              afterSec[n] = tsec[n];
            world.getWorld().refreshChunk(toChunk.bukkitChunk.getX(), toChunk.bukkitChunk.getZ());
    Please dont say just male a 3 layer for loop to copy all blocks because this is no option since it causes lag.
  2. Im not really sure how to fix the problem you are having but I have to say. Thats has some interesting potential :p
  3. If you can't find anything worldedit's api could be an option maybe?
  4. Worldedit does not copy chunks.
  5. So you meant to say you need to clone it instead of copying.
    #6 007agentjames, Apr 19, 2015
    Last edited: Apr 19, 2015
  6. yeah that's maybe be a bit more accurate.
    I just want a clone that is completely independent from the original chunk.
  7. I think something like the following should go inside your for loop:

    Code (Text):
    ChunkSection source = tsec[n];
    ChunkSection clone = new ChunkSection(source.getYPosition(), false);

    clone.b(new NibbleArray(source.getSkyLightArray().a().clone());
    clone.a(new NibbleArray(source.getEmittedLightArray().a().clone());

    afterSec[n] = clone;
    You'll probably have to confirm the method names with the version of Spigot you're using. Looking at the source its pretty straight forward since the fields have decent names.

    Also I should mention I did not test this code.
  8. It worked thank you so much :)
    I doesn't copy the correct data ids but i think that shouldn't be that hard ^^

  9. Glad I could help. :)
  10. Still on 1.7 ?
  11. Yeah, why?
  12. Nothing,but 1.8 is the current opt version for servers