/*
 * This file is part of SpongeAPI, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.api.event.world.chunk;

import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
import org.spongepowered.api.world.chunk.Chunk;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.vector.Vector3i;

public interface ChunkEvent extends Event {

    /**
     * Gets the position of the {@link Chunk}, in chunk co-ordinates.
     *
     * @return the position
     */
    Vector3i chunkPosition();

    /**
     * Represents an event that has knowledge about the {@link ServerWorld} that
     * is being operated upon.
     */
    @NoFactoryMethod
    interface WorldScoped extends ChunkEvent {

        /**
         * Gets the {@link ResourceKey} of the {@link WorldScoped} that the
         * {@link WorldChunk} resides in.
         *
         * <p>Be careful when retrieving the {@link ServerWorld} within this
         * event as some events may not fire on the main thread and such world
         * access is not thread safe.</p>
         *
         * @return The world's {@link ResourceKey}
         */
        ResourceKey worldKey();

    }

    /**
     * Called when a {@link WorldChunk} was unloaded.
     */
    interface Unload extends WorldScoped {

    }

    /**
     * Called when a {@link WorldChunk} is performing a save.
     */
    interface Save extends WorldScoped {

        /**
         * Called before the {@link WorldChunk} is saved. Cancelling this will prevent any of
         * the chunk's data being written to it's storage container.
         */
        interface Pre extends Save, Cancellable {

            /**
             * Gets the {@link WorldChunk} being changed.
             *
             * @return The Chunk
             */
            WorldChunk targetChunk();

        }

        /**
         * Called after the {@link WorldChunk} is saved. Guaranteed to exist in the chunk's
         * storage container.
         *
         * Depending on the implementation, this event may be called off-thread.
         */
        interface Post extends Save {}
    }

    /**
     * Called when a new {@link WorldChunk} was generated.
     */
    interface Generated extends WorldScoped {

    }

    /**
     * Called when a {@link WorldChunk} is done loading.
     */
    interface Load extends WorldScoped {

        /**
         * Gets the {@link WorldChunk} being changed.
         *
         * @return The Chunk
         */
        WorldChunk targetChunk();
    }
}

@@ -1798,16 +1798,22 @@
 nk Chunk
+ chunk
 %7D, in ch
@@ -1986,16 +1986,22 @@
 verWorld
+ world
 %7D that%0A 
@@ -2157,16 +2157,20 @@
 ourceKey
+ key
 %7D of the
@@ -2227,16 +2227,22 @@
 rldChunk
+ chunk
 %7D reside
@@ -2321,16 +2321,22 @@
 verWorld
+ world
 %7D within
@@ -2522,16 +2522,20 @@
 ourceKey
+ key
 %7D%0A      
@@ -2568,25 +2568,24 @@
 worldKey();%0A
-%0A
     %7D%0A%0A    /
@@ -2621,24 +2621,30 @@
 k WorldChunk
+ chunk
 %7D was unload
@@ -2691,32 +2691,491 @@
  WorldScoped %7B%0A%0A
+        /**%0A         * Called before the %7B@link WorldChunk chunk%7D is unloaded.%0A         */%0A        interface Pre extends Unload %7B%0A%0A            /**%0A             * Gets the %7B@link WorldChunk chunk%7D being changed.%0A             *%0A             * @return The chunk%0A             */%0A            WorldChunk chunk();%0A        %7D%0A%0A        /**%0A         * Called after the %7B@link WorldChunk chunk%7D is unloaded.%0A         */%0A        interface Post extends Unload %7B%0A%0A        %7D%0A
     %7D%0A%0A    /**%0A 
@@ -3207,24 +3207,30 @@
 k WorldChunk
+ chunk
 %7D is perform
@@ -3341,32 +3341,38 @@
 @link WorldChunk
+ chunk
 %7D is saved. Canc
@@ -3584,32 +3584,38 @@
 @link WorldChunk
+ chunk
 %7D being changed.
@@ -3649,33 +3649,33 @@
   * @return The 
-C
+c
 hunk%0A           
@@ -3694,39 +3694,33 @@
      WorldChunk 
-targetC
+c
 hunk();%0A%0A       
@@ -3776,24 +3776,30 @@
 k WorldChunk
+ chunk
 %7D is saved. 
@@ -4059,24 +4059,30 @@
 k WorldChunk
+ chunk
 %7D was genera
@@ -4190,24 +4190,30 @@
 k WorldChunk
+ chunk
 %7D is done lo
@@ -4314,24 +4314,30 @@
 k WorldChunk
+ chunk
 %7D being chan
@@ -4375,17 +4375,17 @@
 urn The 
-C
+c
 hunk%0A   
@@ -4416,15 +4416,9 @@
 unk 
-targetC
+c
 hunk

Add ChunkEvent.Unload.Pre and Post